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

void PoDoFo::PdfString::Write ( PdfOutputDevice pDevice,
EPdfWriteMode  eWriteMode,
const PdfEncrypt pEncrypt = NULL 
) const [virtual]

Write this PdfString in PDF format to a PdfOutputDevice

Parameters:
pDevicethe output device.
eWriteModeadditional options for writing this object
pEncryptan encryption object which is used to encrypt this object or NULL to not encrypt this object

Implements PoDoFo::PdfDataType.

Definition at line 303 of file PdfString.cpp.

References PoDoFo::PdfEncrypt::CalculateStreamLength(), PoDoFo::PdfEncrypt::CalculateStreamOffset(), PoDoFo::PdfEncrypt::Encrypt(), PoDoFo::PdfEncrypt::ePdfEncryptAlgorithm_AESV2, PoDoFo::PdfRefCountedBuffer::GetBuffer(), PoDoFo::PdfEncrypt::GetEncryptAlgorithm(), GetLength(), PoDoFo::PdfRefCountedBuffer::GetSize(), GetString(), m_bHex, m_buffer, m_bUnicode, m_escMap, PoDoFo::PdfOutputDevice::Print(), s_pszUnicodeMarker, s_pszUnicodeMarkerHex, PoDoFo::PdfOutputDevice::Write(), and Write().

Referenced by Write().

{
    // Strings in PDF documents may contain \0 especially if they are encrypted
    // this case has to be handled!

    // Peter Petrov: 17 May 2008
    // Added check - m_buffer.GetSize()
    // Now we are not encrypting the empty strings (was access violation)!
    if( pEncrypt && m_buffer.GetSize()) 
    {
        pdf_long nOffset = pEncrypt->CalculateStreamOffset();
        pdf_long nLen = this->GetLength();
        // Add two bytes for the s_pszUnicodeMarker
        pdf_long nOutputLen = pEncrypt->CalculateStreamLength( (m_bUnicode ? nLen + 2 : nLen) );
        
        char* pBuffer = new char [nOutputLen + 1];
        memcpy(&pBuffer[nOffset], this->GetString(), this->GetLength());
        
        std::string enc = std::string(pBuffer, nOutputLen);
        if( m_bUnicode )
        {
            std::string tmp( reinterpret_cast<const char*>(&PdfString::s_pszUnicodeMarker), 2 );

            if( pEncrypt->GetEncryptAlgorithm() == PdfEncrypt::ePdfEncryptAlgorithm_AESV2 )
            {
                // Insert after the initial vector
                enc.insert(16,tmp);
                enc.erase(nOutputLen,std::string::npos);  
            }
            else
            {
                enc.insert(0,tmp);
                enc.erase(nOutputLen,std::string::npos);  
            }

            // Add two bytes for the s_pszUnicodeMarker
            pEncrypt->Encrypt(enc, nLen + 2 );
        }
        else
        {
            pEncrypt->Encrypt(enc, nLen );
        }

        PdfString str( enc.c_str(), enc.length(), true );
        str.Write( pDevice, eWriteMode, NULL );

        delete[] pBuffer;
            
        return;
    }

    pDevice->Print( m_bHex ? "<" : "(" );
    if( m_buffer.GetSize() )
    {
        char* pBuf = m_buffer.GetBuffer();
        pdf_long  lLen = m_buffer.GetSize() - 2;

        if( m_bHex ) 
        {
            if( m_bUnicode )
                pDevice->Write( PdfString::s_pszUnicodeMarkerHex, 4 );

            char data[2];
            while( lLen-- )
            {
                data[0]  = (*pBuf & 0xF0) >> 4;
                data[0] += (data[0] > 9 ? 'A' - 10 : '0');
                
                data[1]  = (*pBuf & 0x0F);
                data[1] += (data[1] > 9 ? 'A' - 10 : '0');
                
                pDevice->Write( data, 2 );
                
                ++pBuf;
            }
        }
        else
        {
            if( m_bUnicode ) 
            {
                pDevice->Write( PdfString::s_pszUnicodeMarker, sizeof( PdfString::s_pszUnicodeMarker ) );
            }

            while( lLen-- ) 
            {
                const char & cEsc = m_escMap[static_cast<unsigned char>(*pBuf)];
                if( cEsc != 0 ) 
                {
                    pDevice->Write( "\\", 1 );
                    pDevice->Write( &cEsc, 1 );
                }
                else 
                {
                    pDevice->Write( &*pBuf, 1 );
                }

                ++pBuf;
            }
        }
    }

    pDevice->Print( m_bHex ? ">" : ")" );
}

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