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

PdfEncrypt * PoDoFo::PdfEncrypt::CreatePdfEncrypt ( const PdfObject pObject ) [static, inherited]

Initialize a PdfEncrypt object from an encryption dictionary in a PDF file.

This is required for encrypting a PDF file, but handled internally in PdfParser for you.

Will use only encrypting algorithms that are enabled.

Parameters:
pObjecta PDF encryption dictionary
See also:
GetEnabledEncryptionAlgorithms

Definition at line 506 of file PdfEncrypt.cpp.

References PoDoFo::PdfError::AddToCallstack(), PoDoFo::PdfEncrypt::ePdfEncryptAlgorithm_AESV2, PoDoFo::PdfEncrypt::ePdfEncryptAlgorithm_RC4V1, PoDoFo::PdfEncrypt::ePdfEncryptAlgorithm_RC4V2, PoDoFo::ePdfError_UnsupportedFilter, PoDoFo::PdfVariant::GetDictionary(), PoDoFo::PdfDictionary::GetKey(), PoDoFo::PdfName::GetName(), PoDoFo::PdfVariant::GetName(), PoDoFo::PdfVariant::GetNumber(), PoDoFo::PdfVariant::GetString(), PoDoFo::PdfDictionary::HasKey(), PoDoFo::PdfEncrypt::IsEncryptionEnabled(), and PODOFO_RAISE_ERROR_INFO.

{
    PdfEncrypt* pdfEncrypt = NULL;
    if( !pObject->GetDictionary().HasKey( PdfName("Filter") ) ||
        pObject->GetDictionary().GetKey( PdfName("Filter" ) )->GetName() != PdfName("Standard") )
    {
        std::ostringstream oss;
        if( pObject->GetDictionary().HasKey( PdfName("Filter") ) )
        {
            oss << "Unsupported encryption filter: " << pObject->GetDictionary().GetKey( PdfName("Filter" ) )->GetName().GetName();
        }
        else
        {
            oss << "Encryption dictionary does not have a key /Filter.";
        }

        PODOFO_RAISE_ERROR_INFO( ePdfError_UnsupportedFilter, oss.str().c_str() );
    }
        
    long lV;
    long long lLength;
    int rValue;
    int pValue;
    PdfString oValue;
    PdfString uValue;
    
    try {
        PdfString sTmp;
        
        lV           = static_cast<long>(pObject->GetDictionary().GetKey( PdfName("V") )->GetNumber());
        rValue       = static_cast<int>(pObject->GetDictionary().GetKey( PdfName("R") )->GetNumber());

        pValue       = static_cast<int>(pObject->GetDictionary().GetKey( PdfName("P") )->GetNumber());
        oValue       = pObject->GetDictionary().GetKey( PdfName("O") )->GetString();
             
        uValue       = pObject->GetDictionary().GetKey( PdfName("U") )->GetString();
            
        if( pObject->GetDictionary().HasKey( PdfName("Length") ) )
        {
            lLength = pObject->GetDictionary().GetKey( PdfName("Length") )->GetNumber();
        }
        else
        {
            lLength = 0;
        }

    } catch( PdfError & e ) {
        e.AddToCallstack( __FILE__, __LINE__, "Invalid key in encryption dictionary" );
        throw e;
    }

    if( (lV == 1L) && (rValue == 2L)
        && PdfEncrypt::IsEncryptionEnabled( ePdfEncryptAlgorithm_RC4V1 ) ) 
    {
        pdfEncrypt = new PdfEncryptRC4(oValue, uValue, pValue, rValue, ePdfEncryptAlgorithm_RC4V1, 40);
    }
    else if( (lV == 2L) && (rValue == 3L)
             && PdfEncrypt::IsEncryptionEnabled( ePdfEncryptAlgorithm_RC4V2 ) ) 
    {
                  // [Alexey] - lLength is long long. Please make changes in encryption algorithms
        pdfEncrypt = new PdfEncryptRC4(oValue, uValue, pValue, rValue, ePdfEncryptAlgorithm_RC4V2, static_cast<int>(lLength));
    }
    else if( (lV == 4L) && (rValue == 4L) 
         && PdfEncrypt::IsEncryptionEnabled( ePdfEncryptAlgorithm_AESV2 ) ) 
    {
        pdfEncrypt = new PdfEncryptAES(oValue, uValue, pValue);      
    }
    else
    {
        std::ostringstream oss;
        oss << "Unsupported encryption method Version=" << lV << " Revision=" << rValue;
        PODOFO_RAISE_ERROR_INFO( ePdfError_UnsupportedFilter, oss.str().c_str() );
    }
    return pdfEncrypt;
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index