Logo Search packages:      
Sourcecode: libpodofo version File versions  Download package

void PoDoFo::PdfString::SetHexData ( const char *  pszHex,
pdf_long  lLen = -1,
PdfEncrypt pEncrypt = NULL 
)

Set hex encoded data as the strings data.

Parameters:
pszHexmust be hex encoded data.
lLenlength of the hex encoded data. if lLen == -1 then strlen( pszHex ) will be used as length of the hex data. pszHex has to be zero terminated in this case.
pEncryptif !NULL assume the hex data is encrypted and should be decrypted after hexdecoding

Definition at line 219 of file PdfString.cpp.

References PoDoFo::PdfDataType::AssertMutable(), PoDoFo::PdfEncrypt::Encrypt(), PoDoFo::ePdfError_InvalidHandle, PoDoFo::PdfRefCountedBuffer::GetBuffer(), PoDoFo::PdfTokenizer::GetHexValue(), PoDoFo::PdfRefCountedBuffer::GetSize(), PoDoFo::PdfTokenizer::IsWhitespace(), m_bHex, m_buffer, m_bUnicode, and PODOFO_RAISE_ERROR.

Referenced by PoDoFo::PdfTokenizer::ReadHexString().

{
    AssertMutable();

    if( !pszHex ) 
    {
        PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
    }

    if( lLen == -1 )
        lLen = strlen( pszHex );

    // Allocate a buffer large enough for the hex decoded data
    // and the 2 terminating zeros
    m_buffer = PdfRefCountedBuffer( lLen % 2 ? ((lLen + 1) >> 1) + 2 : (lLen >> 1) + 2 );
    m_bHex   = true;
    char* pBuffer = m_buffer.GetBuffer();
    char val;
    char cDecodedByte = 0;
    bool bLow = true;

    while( lLen-- ) 
    {
        if( PdfTokenizer::IsWhitespace( *pszHex ) )
        {
            ++pszHex;
            continue;
        }

        val = PdfTokenizer::GetHexValue( *pszHex );
        if( bLow ) 
        {
            cDecodedByte = (val & 0x0F);
            bLow         = false;
        }
        else
        {
            cDecodedByte = ((cDecodedByte << 4) | val);
            bLow         = true;

            *pBuffer++ = cDecodedByte;
        }

        ++pszHex;
    }

    if( !bLow ) 
    {
        // an odd number of bytes was read,
        // so the last byte is 0
        *pBuffer++ = cDecodedByte;
    }

    *pBuffer++ = '\0';
    *pBuffer++ = '\0';

    // If the allocated internal buffer is to big (e.g. because of whitespaces in the data)
    // copy to a smaller buffer so that PdfString::GetLength() will be correct
    lLen = pBuffer - m_buffer.GetBuffer();
    if( static_cast<size_t>(lLen) != m_buffer.GetSize() )
    {
        PdfRefCountedBuffer temp( lLen );
        memcpy( temp.GetBuffer(), m_buffer.GetBuffer(), lLen );
        m_buffer = temp;
    }

    if( pEncrypt )
        pEncrypt->Encrypt( reinterpret_cast<unsigned char*>(m_buffer.GetBuffer()), 
                           static_cast<unsigned int>(m_buffer.GetSize()-2) );

    // Now check for the first two bytes, to see if we got a unicode string
    if( m_buffer.GetSize()-2 > 2 ) 
    {
            m_bUnicode = (m_buffer.GetBuffer()[0] == static_cast<char>(0xFE) && m_buffer.GetBuffer()[1] == static_cast<char>(0xFF));
            
            if( m_bUnicode ) 
        {
            PdfRefCountedBuffer temp( m_buffer.GetSize() - 2 );
            memcpy( temp.GetBuffer(), m_buffer.GetBuffer() + 2, m_buffer.GetSize() - 2 );
            m_buffer = temp;           
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index