diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/include/fpdfapi/fpdf_module.h | 1 | ||||
-rw-r--r-- | core/include/fpdfapi/fpdf_parser.h | 84 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 52 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp | 4 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp | 31 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp | 81 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 165 |
7 files changed, 130 insertions, 288 deletions
diff --git a/core/include/fpdfapi/fpdf_module.h b/core/include/fpdfapi/fpdf_module.h index f68511cc77..6a17a489c2 100644 --- a/core/include/fpdfapi/fpdf_module.h +++ b/core/include/fpdfapi/fpdf_module.h @@ -28,7 +28,6 @@ class CPDF_PageObjectList; class CPDF_PageRenderCache; class CPDF_RenderConfig; class CPDF_RenderOptions; -class CPDF_SecurityHandler; class CPDF_Stream; class ICodec_FaxModule; class ICodec_FlateModule; diff --git a/core/include/fpdfapi/fpdf_parser.h b/core/include/fpdfapi/fpdf_parser.h index 294c0c5f4a..62c2a8b1a1 100644 --- a/core/include/fpdfapi/fpdf_parser.h +++ b/core/include/fpdfapi/fpdf_parser.h @@ -27,9 +27,9 @@ class CPDF_Image; class CPDF_Object; class CPDF_Parser; class CPDF_Pattern; -class CPDF_SecurityHandler; class CPDF_StandardSecurityHandler; class IFX_FileRead; +class IPDF_SecurityHandler; #define FPDFPERM_PRINT 0x0004 #define FPDFPERM_MODIFY 0x0008 @@ -104,8 +104,6 @@ class CPDF_Document : public CFX_PrivateData, public CPDF_IndirectObjectHolder { FX_DWORD GetUserPermissions(FX_BOOL bCheckRevision = FALSE) const; - FX_BOOL IsOwner() const; - CPDF_DocPageData* GetPageData() { return GetValidatePageData(); } void ClearPageData(); @@ -248,7 +246,7 @@ class CPDF_SyntaxParser { void InitParser(IFX_FileRead* pFileAccess, FX_DWORD HeaderOffset); - FX_FILESIZE SavePos() { return m_Pos; } + FX_FILESIZE SavePos() const { return m_Pos; } void RestorePos(FX_FILESIZE pos) { m_Pos = pos; } @@ -263,14 +261,8 @@ class CPDF_SyntaxParser { int GetDirectNum(); - CFX_ByteString GetString(FX_DWORD objnum, FX_DWORD gennum); - - CFX_ByteString GetName(); - CFX_ByteString GetKeyword(); - void GetBinary(uint8_t* buffer, FX_DWORD size); - void ToNextLine(); void ToNextWord(); @@ -286,11 +278,7 @@ class CPDF_SyntaxParser { FX_FILESIZE FindTag(const CFX_ByteStringC& tag, FX_FILESIZE limit); - void SetEncrypt(CPDF_CryptoHandler* pCryptoHandler) { - m_pCryptoHandler.reset(pCryptoHandler); - } - - FX_BOOL IsEncrypted() { return m_pCryptoHandler != NULL; } + void SetEncrypt(std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler); FX_BOOL GetCharAt(FX_FILESIZE pos, uint8_t& ch); @@ -353,13 +341,8 @@ class CPDF_Parser { CPDF_Parser(); ~CPDF_Parser(); - Error StartParse(IFX_FileRead* pFile, - FX_BOOL bReParse = FALSE, - FX_BOOL bOwnFileRead = TRUE); - void CloseParser(FX_BOOL bReParse = FALSE); - + Error StartParse(IFX_FileRead* pFile); FX_DWORD GetPermissions(FX_BOOL bCheckRevision = FALSE); - FX_BOOL IsOwner(); void SetPassword(const FX_CHAR* password) { m_Password = password; } CFX_ByteString GetPassword() { return m_Password; } @@ -368,9 +351,6 @@ class CPDF_Parser { return m_Syntax.m_pCryptoHandler.get(); } - void SetSecurityHandler(CPDF_SecurityHandler* pSecurityHandler, - FX_BOOL bForced = FALSE); - CPDF_Dictionary* GetTrailer() const { return m_pTrailer; } FX_FILESIZE GetLastXRefOffset() const { return m_LastXRefOffset; } CPDF_Document* GetDocument() const { return m_pDocument; } @@ -386,6 +366,11 @@ class CPDF_Parser { FX_DWORD GetLastObjNum() const; bool IsValidObjectNumber(FX_DWORD objnum) const; + FX_FILESIZE GetObjectPositionOrZero(FX_DWORD objnum) const; + uint8_t GetObjectType(FX_DWORD objnum) const; + uint16_t GetObjectGenNum(FX_DWORD objnum) const; + bool IsVersionUpdated() const { return m_bVersionUpdated; } + bool IsObjectFreeOrNull(FX_DWORD objnum) const; FX_BOOL IsFormStream(FX_DWORD objnum, FX_BOOL& bForm); FX_FILESIZE GetObjectOffset(FX_DWORD objnum) const; @@ -406,13 +391,12 @@ class CPDF_Parser { FX_DWORD objnum, FX_FILESIZE* pResultPos); - Error StartAsynParse(IFX_FileRead* pFile, - FX_BOOL bReParse = FALSE, - FX_BOOL bOwnFileRead = TRUE); + Error StartAsyncParse(IFX_FileRead* pFile); FX_DWORD GetFirstPageNo() const { return m_dwFirstPageNo; } protected: + void CloseParser(); CPDF_Object* ParseDirect(CPDF_Object* pObj); FX_BOOL LoadAllCrossRefV4(FX_FILESIZE pos); FX_BOOL LoadAllCrossRefV5(FX_FILESIZE pos); @@ -430,19 +414,17 @@ class CPDF_Parser { FX_BOOL IsLinearizedFile(IFX_FileRead* pFileAccess, FX_DWORD offset); bool FindPosInOffsets(FX_FILESIZE pos) const; void SetEncryptDictionary(CPDF_Dictionary* pDict); - FX_FILESIZE GetObjectPositionOrZero(FX_DWORD objnum) const; void ShrinkObjectMap(FX_DWORD size); CPDF_Document* m_pDocument; CPDF_SyntaxParser m_Syntax; - FX_BOOL m_bOwnFileRead; + bool m_bOwnFileRead; int m_FileVersion; CPDF_Dictionary* m_pTrailer; CPDF_Dictionary* m_pEncryptDict; FX_FILESIZE m_LastXRefOffset; FX_BOOL m_bXRefStream; - std::unique_ptr<CPDF_SecurityHandler> m_pSecurityHandler; - FX_BOOL m_bForceUseSecurityHandler; + std::unique_ptr<IPDF_SecurityHandler> m_pSecurityHandler; CFX_ByteString m_bsRecipient; CFX_ByteString m_FilePath; CFX_ByteString m_Password; @@ -482,7 +464,6 @@ class CPDF_Parser { // All indirect object numbers that are being parsed. std::set<FX_DWORD> m_ParsingObjNums; - friend class CPDF_Creator; friend class CPDF_DataAvail; }; @@ -490,43 +471,42 @@ class CPDF_Parser { #define FXCIPHER_RC4 1 #define FXCIPHER_AES 2 #define FXCIPHER_AES2 3 -class CPDF_SecurityHandler { + +class IPDF_SecurityHandler { public: - virtual ~CPDF_SecurityHandler() {} + virtual ~IPDF_SecurityHandler() {} virtual FX_BOOL OnInit(CPDF_Parser* pParser, CPDF_Dictionary* pEncryptDict) = 0; virtual FX_DWORD GetPermissions() = 0; - virtual FX_BOOL IsOwner() = 0; - virtual FX_BOOL GetCryptInfo(int& cipher, const uint8_t*& buffer, int& keylen) = 0; - virtual FX_BOOL IsMetadataEncrypted() { return TRUE; } + virtual FX_BOOL IsMetadataEncrypted() = 0; virtual CPDF_CryptoHandler* CreateCryptoHandler() = 0; virtual CPDF_StandardSecurityHandler* GetStandardHandler() { return NULL; } }; + #define PDF_ENCRYPT_CONTENT 0 -class CPDF_StandardSecurityHandler : public CPDF_SecurityHandler { + +class CPDF_StandardSecurityHandler : public IPDF_SecurityHandler { public: CPDF_StandardSecurityHandler(); ~CPDF_StandardSecurityHandler() override; - // CPDF_SecurityHandler + // IPDF_SecurityHandler: FX_BOOL OnInit(CPDF_Parser* pParser, CPDF_Dictionary* pEncryptDict) override; FX_DWORD GetPermissions() override; - FX_BOOL IsOwner() override { return m_bOwner; } FX_BOOL GetCryptInfo(int& cipher, const uint8_t*& buffer, int& keylen) override; FX_BOOL IsMetadataEncrypted() override; CPDF_CryptoHandler* CreateCryptoHandler() override; - CPDF_StandardSecurityHandler* GetStandardHandler() override { return this; } void OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_Array* pIdArray, @@ -542,17 +522,9 @@ class CPDF_StandardSecurityHandler : public CPDF_SecurityHandler { FX_DWORD user_size, FX_DWORD type = PDF_ENCRYPT_CONTENT); - CFX_ByteString GetUserPassword(const uint8_t* owner_pass, FX_DWORD pass_size); CFX_ByteString GetUserPassword(const uint8_t* owner_pass, FX_DWORD pass_size, int32_t key_len); - int GetVersion() { return m_Version; } - int GetRevision() { return m_Revision; } - - int CheckPassword(const uint8_t* password, - FX_DWORD pass_size, - FX_BOOL bOwner, - uint8_t* key); int CheckPassword(const uint8_t* password, FX_DWORD pass_size, FX_BOOL bOwner, @@ -607,8 +579,6 @@ class CPDF_StandardSecurityHandler : public CPDF_SecurityHandler { FX_DWORD type); FX_BOOL CheckSecurity(int32_t key_len); - FX_BOOL m_bOwner; - FX_DWORD m_Permissions; int m_Cipher; @@ -618,14 +588,14 @@ class CPDF_StandardSecurityHandler : public CPDF_SecurityHandler { int m_KeyLen; }; -CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler(); +IPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler(); class CPDF_CryptoHandler { public: virtual ~CPDF_CryptoHandler() {} virtual FX_BOOL Init(CPDF_Dictionary* pEncryptDict, - CPDF_SecurityHandler* pSecurityHandler) = 0; + IPDF_SecurityHandler* pSecurityHandler) = 0; virtual FX_DWORD DecryptGetSize(FX_DWORD src_size) = 0; @@ -659,7 +629,7 @@ class CPDF_StandardCryptoHandler : public CPDF_CryptoHandler { // CPDF_CryptoHandler FX_BOOL Init(CPDF_Dictionary* pEncryptDict, - CPDF_SecurityHandler* pSecurityHandler) override; + IPDF_SecurityHandler* pSecurityHandler) override; FX_DWORD DecryptGetSize(FX_DWORD src_size) override; void* DecryptStart(FX_DWORD objnum, FX_DWORD gennum) override; FX_BOOL DecryptStream(void* context, @@ -753,8 +723,6 @@ class CFDF_Document : public CPDF_IndirectObjectHolder { CPDF_Dictionary* GetRoot() const { return m_pRootDict; } - CFX_WideString GetWin32Path() const; - protected: CFDF_Document(); void ParseStream(IFX_FileRead* pFile, FX_BOOL bOwnFile); @@ -763,10 +731,6 @@ class CFDF_Document : public CPDF_IndirectObjectHolder { FX_BOOL m_bOwnFile; }; -CFX_WideString FPDF_FileSpec_GetWin32Path(const CPDF_Object* pFileSpec); -void FPDF_FileSpec_SetWin32Path(CPDF_Object* pFileSpec, - const CFX_WideString& fullpath); - void FlateEncode(const uint8_t* src_buf, FX_DWORD src_size, uint8_t*& dest_buf, diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index 50644706b6..95d23ba501 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -954,10 +954,8 @@ int32_t CPDF_Creator::WriteIndirectObjectToStream(const CPDF_Object* pObj) { return 1; FX_DWORD objnum = pObj->GetObjNum(); - if (m_pParser && m_pParser->m_ObjVersion.GetSize() > (int32_t)objnum && - m_pParser->m_ObjVersion[objnum] > 0) { + if (m_pParser && m_pParser->GetObjectGenNum(objnum) > 0) return 1; - } if (pObj->IsNumber()) return 1; @@ -1280,15 +1278,15 @@ int32_t CPDF_Creator::WriteDirectObj(FX_DWORD objnum, return 1; } int32_t CPDF_Creator::WriteOldIndirectObject(FX_DWORD objnum) { - if (m_pParser->m_V5Type[objnum] == 0 || m_pParser->m_V5Type[objnum] == 255) { + if (m_pParser->IsObjectFreeOrNull(objnum)) return 0; - } + m_ObjectOffset[objnum] = m_Offset; FX_BOOL bExistInMap = pdfium::ContainsKey(m_pDocument->m_IndirectObjs, objnum); - FX_BOOL bObjStm = - (m_pParser->m_V5Type[objnum] == 2) && m_pEncryptDict && !m_pXRefStream; - if (m_pParser->m_bVersionUpdated || m_bSecurityChanged || bExistInMap || + const uint8_t object_type = m_pParser->GetObjectType(objnum); + FX_BOOL bObjStm = (object_type == 2) && m_pEncryptDict && !m_pXRefStream; + if (m_pParser->IsVersionUpdated() || m_bSecurityChanged || bExistInMap || bObjStm) { CPDF_Object* pObj = m_pDocument->GetIndirectObject(objnum); if (!pObj) { @@ -1308,7 +1306,7 @@ int32_t CPDF_Creator::WriteOldIndirectObject(FX_DWORD objnum) { if (!pBuffer) { return 0; } - if (m_pParser->m_V5Type[objnum] == 2) { + if (object_type == 2) { if (m_pXRefStream) { if (WriteIndirectObjectToStream(objnum, pBuffer, size) < 0) { FX_Free(pBuffer); @@ -1396,18 +1394,16 @@ void CPDF_Creator::InitOldObjNumOffsets() { FX_DWORD dwStart = 0; FX_DWORD dwEnd = m_pParser->GetLastObjNum(); while (dwStart <= dwEnd) { - while (dwStart <= dwEnd && (m_pParser->m_V5Type[dwStart] == 0 || - m_pParser->m_V5Type[dwStart] == 255)) { + while (dwStart <= dwEnd && m_pParser->IsObjectFreeOrNull(dwStart)) dwStart++; - } - if (dwStart > dwEnd) { + + if (dwStart > dwEnd) break; - } + j = dwStart; - while (j <= dwEnd && m_pParser->m_V5Type[j] != 0 && - m_pParser->m_V5Type[j] != 255) { + while (j <= dwEnd && !m_pParser->IsObjectFreeOrNull(j)) j++; - } + m_ObjectOffset.Add(dwStart, j - dwStart); dwStart = j; } @@ -1416,17 +1412,20 @@ void CPDF_Creator::InitNewObjNumOffsets() { FX_BOOL bIncremental = (m_dwFlags & FPDFCREATE_INCREMENTAL) != 0; FX_BOOL bNoOriginal = (m_dwFlags & FPDFCREATE_NO_ORIGINAL) != 0; for (const auto& pair : m_pDocument->m_IndirectObjs) { - if (pair.second->GetObjNum() == -1) + const FX_DWORD objnum = pair.first; + const CPDF_Object* pObj = pair.second; + if (pObj->GetObjNum() == -1) continue; if (bIncremental) { - if (!pair.second->IsModified()) + if (!pObj->IsModified()) continue; - } else if (m_pParser && m_pParser->IsValidObjectNumber(pair.first) && - m_pParser->m_V5Type[pair.first]) { + } else if (m_pParser && m_pParser->IsValidObjectNumber(objnum) && + m_pParser->GetObjectType(objnum)) { continue; } - AppendNewObjNum(pair.first); + AppendNewObjNum(objnum); } + int32_t iCount = m_NewObjNumArray.GetSize(); if (iCount == 0) { return; @@ -1565,11 +1564,10 @@ int32_t CPDF_Creator::WriteDoc_Stage1(IFX_Pause* pPause) { FX_DWORD dwEnd = m_pParser->GetLastObjNum(); FX_BOOL bObjStm = (m_dwFlags & FPDFCREATE_OBJECTSTREAM) != 0; for (FX_DWORD objnum = 0; objnum <= dwEnd; objnum++) { - if (m_pParser->m_V5Type[objnum] == 0 || - m_pParser->m_V5Type[objnum] == 255) { + if (m_pParser->IsObjectFreeOrNull(objnum)) continue; - } - m_ObjectOffset[objnum] = m_pParser->m_ObjectInfo[objnum].pos; + + m_ObjectOffset[objnum] = m_pParser->GetObjectPositionOrZero(objnum); if (bObjStm) { m_pXRefStream->AddObjectNumberToIndexArray(objnum); } @@ -1763,7 +1761,7 @@ int32_t CPDF_Creator::WriteDoc_Stage4(IFX_Pause* pPause) { } } if (m_pParser) { - CPDF_Dictionary* p = m_pParser->m_pTrailer; + CPDF_Dictionary* p = m_pParser->GetTrailer(); for (const auto& it : *p) { const CFX_ByteString& key = it.first; CPDF_Object* pValue = it.second; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp index be55a56d0f..a63b0ed336 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp @@ -313,10 +313,6 @@ FX_DWORD CPDF_Document::GetUserPermissions(FX_BOOL bCheckRevision) const { return m_pParser->GetPermissions(bCheckRevision); } -FX_BOOL CPDF_Document::IsOwner() const { - return !m_pParser || m_pParser->IsOwner(); -} - FX_BOOL CPDF_Document::IsFormStream(FX_DWORD objnum, FX_BOOL& bForm) const { auto it = m_IndirectObjs.find(objnum); if (it != m_IndirectObjs.end()) { diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp index 595e4d6da6..61becaee6a 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp @@ -59,22 +59,24 @@ void CalcEncryptKey(CPDF_Dictionary* pEncrypt, CPDF_CryptoHandler* CPDF_StandardSecurityHandler::CreateCryptoHandler() { return new CPDF_StandardCryptoHandler; } + typedef struct _PDF_CRYPTOITEM { int32_t m_Cipher; int32_t m_KeyLen; FX_BOOL m_bChecked; CPDF_StandardCryptoHandler* m_pCryptoHandler; } PDF_CRYPTOITEM; + CPDF_StandardSecurityHandler::CPDF_StandardSecurityHandler() { m_Version = 0; m_Revision = 0; m_pParser = NULL; m_pEncryptDict = NULL; - m_bOwner = FALSE; m_Permissions = 0; m_Cipher = FXCIPHER_NONE; m_KeyLen = 0; } + CPDF_StandardSecurityHandler::~CPDF_StandardSecurityHandler() {} FX_BOOL CPDF_StandardSecurityHandler::OnInit(CPDF_Parser* pParser, CPDF_Dictionary* pEncryptDict) { @@ -97,7 +99,6 @@ FX_BOOL CPDF_StandardSecurityHandler::CheckSecurity(int32_t key_len) { return FALSE; } } - m_bOwner = TRUE; return TRUE; } return CheckPassword(password, password.GetLength(), FALSE, m_EncryptKey, @@ -151,9 +152,9 @@ static FX_BOOL _LoadCryptInfo(CPDF_Dictionary* pEncryptDict, } return TRUE; } + FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) { m_pEncryptDict = pEncryptDict; - m_bOwner = FALSE; m_Version = pEncryptDict->GetIntegerBy("V"); m_Revision = pEncryptDict->GetIntegerBy("R"); m_Permissions = pEncryptDict->GetIntegerBy("P", -1); @@ -170,12 +171,12 @@ FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) { } return TRUE; } + FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, FX_DWORD type, int& cipher, int& key_len) { m_pEncryptDict = pEncryptDict; - m_bOwner = FALSE; m_Version = pEncryptDict->GetIntegerBy("V"); m_Revision = pEncryptDict->GetIntegerBy("R"); m_Permissions = pEncryptDict->GetIntegerBy("P", -1); @@ -193,8 +194,8 @@ FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, m_Cipher = cipher; m_KeyLen = key_len; return TRUE; - return TRUE; } + FX_BOOL CPDF_StandardSecurityHandler::GetCryptInfo(int& cipher, const uint8_t*& buffer, int& keylen) { @@ -384,12 +385,7 @@ FX_BOOL CPDF_StandardSecurityHandler::AES256_CheckPassword( } return TRUE; } -int CPDF_StandardSecurityHandler::CheckPassword(const uint8_t* password, - FX_DWORD pass_size, - FX_BOOL bOwner, - uint8_t* key) { - return CheckPassword(password, pass_size, bOwner, key, m_KeyLen); -} + int CPDF_StandardSecurityHandler::CheckPassword(const uint8_t* password, FX_DWORD size, FX_BOOL bOwner, @@ -458,11 +454,6 @@ FX_BOOL CPDF_StandardSecurityHandler::CheckUserPassword( } CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword( const uint8_t* owner_pass, - FX_DWORD pass_size) { - return GetUserPassword(owner_pass, pass_size, m_KeyLen); -} -CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword( - const uint8_t* owner_pass, FX_DWORD pass_size, int32_t key_len) { CFX_ByteString okey = m_pEncryptDict->GetStringBy("O"); @@ -526,9 +517,11 @@ FX_BOOL CPDF_StandardSecurityHandler::CheckOwnerPassword( FX_BOOL CPDF_StandardSecurityHandler::IsMetadataEncrypted() { return m_pEncryptDict->GetBooleanBy("EncryptMetadata", TRUE); } -CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler() { + +IPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler() { return new CPDF_StandardSecurityHandler; } + void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_Array* pIdArray, const uint8_t* user_pass, @@ -940,9 +933,10 @@ void* CPDF_StandardCryptoHandler::DecryptStart(FX_DWORD objnum, FX_DWORD CPDF_StandardCryptoHandler::DecryptGetSize(FX_DWORD src_size) { return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size; } + FX_BOOL CPDF_StandardCryptoHandler::Init( CPDF_Dictionary* pEncryptDict, - CPDF_SecurityHandler* pSecurityHandler) { + IPDF_SecurityHandler* pSecurityHandler) { const uint8_t* key; if (!pSecurityHandler->GetCryptInfo(m_Cipher, key, m_KeyLen)) { return FALSE; @@ -958,6 +952,7 @@ FX_BOOL CPDF_StandardCryptoHandler::Init( } return TRUE; } + FX_BOOL CPDF_StandardCryptoHandler::Init(int cipher, const uint8_t* key, int keylen) { diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp index f5701fefff..b80568b63c 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp @@ -94,84 +94,3 @@ FX_BOOL CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const { << " 0 R>>\r\n%%EOF\r\n"; return TRUE; } -CFX_WideString CFDF_Document::GetWin32Path() const { - CPDF_Dictionary* pDict = m_pRootDict ? m_pRootDict->GetDictBy("FDF") : NULL; - CPDF_Object* pFileSpec = pDict ? pDict->GetElementValue("F") : NULL; - if (!pFileSpec) - return CFX_WideString(); - if (pFileSpec->IsString()) - return FPDF_FileSpec_GetWin32Path(m_pRootDict->GetDictBy("FDF")); - return FPDF_FileSpec_GetWin32Path(pFileSpec); -} -static CFX_WideString ChangeSlash(const FX_WCHAR* str) { - CFX_WideString result; - while (*str) { - if (*str == '\\') { - result += '/'; - } else if (*str == '/') { - result += '\\'; - } else { - result += *str; - } - str++; - } - return result; -} -void FPDF_FileSpec_SetWin32Path(CPDF_Object* pFileSpec, - const CFX_WideString& filepath) { - CFX_WideString result; - if (filepath.GetLength() > 1 && filepath[1] == ':') { - result = L"/"; - result += filepath[0]; - if (filepath[2] != '\\') { - result += '/'; - } - result += ChangeSlash(filepath.c_str() + 2); - } else if (filepath.GetLength() > 1 && filepath[0] == '\\' && - filepath[1] == '\\') { - result = ChangeSlash(filepath.c_str() + 1); - } else { - result = ChangeSlash(filepath.c_str()); - } - - if (pFileSpec->IsString()) { - pFileSpec->SetString(CFX_ByteString::FromUnicode(result)); - } else if (CPDF_Dictionary* pFileDict = pFileSpec->AsDictionary()) { - pFileDict->SetAtString("F", CFX_ByteString::FromUnicode(result)); - pFileDict->SetAtString("UF", PDF_EncodeText(result)); - pFileDict->RemoveAt("FS"); - } -} -CFX_WideString FPDF_FileSpec_GetWin32Path(const CPDF_Object* pFileSpec) { - CFX_WideString wsFileName; - if (!pFileSpec) { - wsFileName = CFX_WideString(); - } else if (const CPDF_Dictionary* pDict = pFileSpec->AsDictionary()) { - wsFileName = pDict->GetUnicodeTextBy("UF"); - if (wsFileName.IsEmpty()) { - wsFileName = CFX_WideString::FromLocal(pDict->GetStringBy("F")); - } - if (pDict->GetStringBy("FS") == "URL") { - return wsFileName; - } - if (wsFileName.IsEmpty() && pDict->KeyExist("DOS")) { - wsFileName = CFX_WideString::FromLocal(pDict->GetStringBy("DOS")); - } - } else { - wsFileName = CFX_WideString::FromLocal(pFileSpec->GetString()); - } - if (wsFileName[0] != '/') { - return ChangeSlash(wsFileName.c_str()); - } - if (wsFileName[2] == '/') { - CFX_WideString result; - result += wsFileName[1]; - result += ':'; - result += ChangeSlash(wsFileName.c_str() + 2); - return result; - } - CFX_WideString result; - result += '\\'; - result += ChangeSlash(wsFileName.c_str()); - return result; -} diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index 010554335d..653ce2888e 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -97,19 +97,18 @@ bool IsSignatureDict(const CPDF_Dictionary* pDict) { return pType && pType->GetString() == "Sig"; } -CPDF_Parser::CPDF_Parser() { - m_pDocument = NULL; - m_pTrailer = NULL; - m_pEncryptDict = NULL; - m_pLinearized = NULL; - m_dwFirstPageNo = 0; - m_dwXrefStartObjNum = 0; - m_bOwnFileRead = TRUE; - m_FileVersion = 0; - m_bForceUseSecurityHandler = FALSE; -} +CPDF_Parser::CPDF_Parser() + : m_pDocument(nullptr), + m_bOwnFileRead(true), + m_FileVersion(0), + m_pTrailer(nullptr), + m_pEncryptDict(nullptr), + m_pLinearized(nullptr), + m_dwFirstPageNo(0), + m_dwXrefStartObjNum(0) {} + CPDF_Parser::~CPDF_Parser() { - CloseParser(FALSE); + CloseParser(); } FX_DWORD CPDF_Parser::GetLastObjNum() const { @@ -120,15 +119,28 @@ bool CPDF_Parser::IsValidObjectNumber(FX_DWORD objnum) const { return !m_ObjectInfo.empty() && objnum <= m_ObjectInfo.rbegin()->first; } -void CPDF_Parser::SetEncryptDictionary(CPDF_Dictionary* pDict) { - m_pEncryptDict = pDict; -} - FX_FILESIZE CPDF_Parser::GetObjectPositionOrZero(FX_DWORD objnum) const { auto it = m_ObjectInfo.find(objnum); return it != m_ObjectInfo.end() ? it->second.pos : 0; } +uint8_t CPDF_Parser::GetObjectType(FX_DWORD objnum) const { + return m_V5Type[objnum]; +} + +uint16_t CPDF_Parser::GetObjectGenNum(FX_DWORD objnum) const { + return m_ObjVersion[objnum]; +} + +bool CPDF_Parser::IsObjectFreeOrNull(FX_DWORD objnum) const { + uint8_t type = GetObjectType(objnum); + return type == 0 || type == 255; +} + +void CPDF_Parser::SetEncryptDictionary(CPDF_Dictionary* pDict) { + m_pEncryptDict = pDict; +} + void CPDF_Parser::ShrinkObjectMap(FX_DWORD objnum) { if (objnum == 0) { m_ObjectInfo.clear(); @@ -145,21 +157,20 @@ void CPDF_Parser::ShrinkObjectMap(FX_DWORD objnum) { m_ObjectInfo[objnum - 1].pos = 0; } -void CPDF_Parser::CloseParser(FX_BOOL bReParse) { +void CPDF_Parser::CloseParser() { m_bVersionUpdated = FALSE; - if (!bReParse) { - delete m_pDocument; - m_pDocument = NULL; - } + delete m_pDocument; + m_pDocument = nullptr; + if (m_pTrailer) { m_pTrailer->Release(); - m_pTrailer = NULL; + m_pTrailer = nullptr; } ReleaseEncryptHandler(); - SetEncryptDictionary(NULL); + SetEncryptDictionary(nullptr); if (m_bOwnFileRead && m_Syntax.m_pFileAccess) { m_Syntax.m_pFileAccess->Release(); - m_Syntax.m_pFileAccess = NULL; + m_Syntax.m_pFileAccess = nullptr; } m_ObjectStreamMap.clear(); m_ObjCache.clear(); @@ -176,21 +187,19 @@ void CPDF_Parser::CloseParser(FX_BOOL bReParse) { m_Trailers.RemoveAll(); if (m_pLinearized) { m_pLinearized->Release(); - m_pLinearized = NULL; + m_pLinearized = nullptr; } } -CPDF_Parser::Error CPDF_Parser::StartParse(IFX_FileRead* pFileAccess, - FX_BOOL bReParse, - FX_BOOL bOwnFileRead) { - CloseParser(bReParse); +CPDF_Parser::Error CPDF_Parser::StartParse(IFX_FileRead* pFileAccess) { + CloseParser(); m_bXRefStream = FALSE; m_LastXRefOffset = 0; - m_bOwnFileRead = bOwnFileRead; + m_bOwnFileRead = true; int32_t offset = GetHeaderOffset(pFileAccess); if (offset == -1) { - if (bOwnFileRead && pFileAccess) + if (pFileAccess) pFileAccess->Release(); return FORMAT_ERROR; } @@ -211,8 +220,7 @@ CPDF_Parser::Error CPDF_Parser::StartParse(IFX_FileRead* pFileAccess, return FORMAT_ERROR; m_Syntax.RestorePos(m_Syntax.m_FileLen - m_Syntax.m_HeaderOffset - 9); - if (!bReParse) - m_pDocument = new CPDF_Document(this); + m_pDocument = new CPDF_Document(this); FX_BOOL bXRefRebuilt = FALSE; if (m_Syntax.SearchWord("startxref", TRUE, FALSE, 4096)) { @@ -300,25 +308,12 @@ CPDF_Parser::Error CPDF_Parser::SetEncryptHandler() { SetEncryptDictionary(pEncryptObj->GetDict()); } } - if (m_bForceUseSecurityHandler) { - if (!m_pSecurityHandler) { - return HANDLER_ERROR; - } - if (!m_pSecurityHandler->OnInit(this, m_pEncryptDict)) { - return HANDLER_ERROR; - } - std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler( - m_pSecurityHandler->CreateCryptoHandler()); - if (!pCryptoHandler->Init(m_pEncryptDict, m_pSecurityHandler.get())) { - return HANDLER_ERROR; - } - m_Syntax.SetEncrypt(pCryptoHandler.release()); - } else if (m_pEncryptDict) { + if (m_pEncryptDict) { CFX_ByteString filter = m_pEncryptDict->GetStringBy("Filter"); - std::unique_ptr<CPDF_SecurityHandler> pSecurityHandler; + std::unique_ptr<IPDF_SecurityHandler> pSecurityHandler; Error err = HANDLER_ERROR; if (filter == "Standard") { - pSecurityHandler.reset(FPDF_CreateStandardSecurityHandler()); + pSecurityHandler.reset(new CPDF_StandardSecurityHandler); err = PASSWORD_ERROR; } if (!pSecurityHandler) { @@ -330,18 +325,16 @@ CPDF_Parser::Error CPDF_Parser::SetEncryptHandler() { m_pSecurityHandler = std::move(pSecurityHandler); std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler( m_pSecurityHandler->CreateCryptoHandler()); - if (!pCryptoHandler->Init(m_pEncryptDict, m_pSecurityHandler.get())) { + if (!pCryptoHandler->Init(m_pEncryptDict, m_pSecurityHandler.get())) return HANDLER_ERROR; - } - m_Syntax.SetEncrypt(pCryptoHandler.release()); + m_Syntax.SetEncrypt(std::move(pCryptoHandler)); } return SUCCESS; } + void CPDF_Parser::ReleaseEncryptHandler() { m_Syntax.m_pCryptoHandler.reset(); - if (!m_bForceUseSecurityHandler) { - m_pSecurityHandler.reset(); - } + m_pSecurityHandler.reset(); } FX_FILESIZE CPDF_Parser::GetObjectOffset(FX_DWORD objnum) const { @@ -1496,19 +1489,7 @@ FX_DWORD CPDF_Parser::GetPermissions(FX_BOOL bCheckRevision) { } return dwPermission; } -FX_BOOL CPDF_Parser::IsOwner() { - return !m_pSecurityHandler || m_pSecurityHandler->IsOwner(); -} -void CPDF_Parser::SetSecurityHandler(CPDF_SecurityHandler* pSecurityHandler, - FX_BOOL bForced) { - m_bForceUseSecurityHandler = bForced; - m_pSecurityHandler.reset(pSecurityHandler); - if (m_bForceUseSecurityHandler) { - return; - } - m_Syntax.m_pCryptoHandler.reset(pSecurityHandler->CreateCryptoHandler()); - m_Syntax.m_pCryptoHandler->Init(NULL, pSecurityHandler); -} + FX_BOOL CPDF_Parser::IsLinearizedFile(IFX_FileRead* pFileAccess, FX_DWORD offset) { m_Syntax.InitParser(pFileAccess, offset); @@ -1560,24 +1541,20 @@ FX_BOOL CPDF_Parser::IsLinearizedFile(IFX_FileRead* pFileAccess, m_pLinearized = NULL; return FALSE; } -CPDF_Parser::Error CPDF_Parser::StartAsynParse(IFX_FileRead* pFileAccess, - FX_BOOL bReParse, - FX_BOOL bOwnFileRead) { - CloseParser(bReParse); +CPDF_Parser::Error CPDF_Parser::StartAsyncParse(IFX_FileRead* pFileAccess) { + CloseParser(); m_bXRefStream = FALSE; m_LastXRefOffset = 0; - m_bOwnFileRead = bOwnFileRead; + m_bOwnFileRead = true; int32_t offset = GetHeaderOffset(pFileAccess); if (offset == -1) { return FORMAT_ERROR; } if (!IsLinearizedFile(pFileAccess, offset)) { - m_Syntax.m_pFileAccess = NULL; - return StartParse(pFileAccess, bReParse, bOwnFileRead); - } - if (!bReParse) { - m_pDocument = new CPDF_Document(this); + m_Syntax.m_pFileAccess = nullptr; + return StartParse(pFileAccess); } + m_pDocument = new CPDF_Document(this); FX_FILESIZE dwFirstXRefOffset = m_Syntax.SavePos(); FX_BOOL bXRefRebuilt = FALSE; FX_BOOL bLoadV4 = FALSE; @@ -1642,6 +1619,7 @@ CPDF_Parser::Error CPDF_Parser::StartAsynParse(IFX_FileRead* pFileAccess, } return SUCCESS; } + FX_BOOL CPDF_Parser::LoadLinearizedAllCrossRefV5(FX_FILESIZE xrefpos) { if (!LoadCrossRefV5(&xrefpos, FALSE)) { return FALSE; @@ -2590,18 +2568,10 @@ FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag, } return -1; } -void CPDF_SyntaxParser::GetBinary(uint8_t* buffer, FX_DWORD size) { - FX_DWORD offset = 0; - uint8_t ch; - while (1) { - if (!GetNextChar(ch)) { - return; - } - buffer[offset++] = ch; - if (offset == size) { - break; - } - } + +void CPDF_SyntaxParser::SetEncrypt( + std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler) { + m_pCryptoHandler = std::move(pCryptoHandler); } class CPDF_DataAvail final : public IPDF_DataAvail { @@ -2868,7 +2838,7 @@ CPDF_DataAvail::CPDF_DataAvail(IFX_FileAvail* pFileAvail, m_pPageDict = NULL; m_pPageResource = NULL; m_docStatus = PDF_DATAAVAIL_HEADER; - m_parser.m_bOwnFileRead = FALSE; + m_parser.m_bOwnFileRead = false; m_bTotalLoadPageTree = FALSE; m_bCurPageDictLoadOK = FALSE; m_bLinearedDataOK = FALSE; @@ -2894,15 +2864,16 @@ void CPDF_DataAvail::SetDocument(CPDF_Document* pDoc) { m_pDocument = pDoc; } FX_DWORD CPDF_DataAvail::GetObjectSize(FX_DWORD objnum, FX_FILESIZE& offset) { - CPDF_Parser* pParser = (CPDF_Parser*)(m_pDocument->GetParser()); + CPDF_Parser* pParser = m_pDocument->GetParser(); if (!pParser || !pParser->IsValidObjectNumber(objnum)) return 0; - if (pParser->m_V5Type[objnum] == 2) - objnum = pParser->m_ObjectInfo[objnum].pos; + if (pParser->GetObjectType(objnum) == 2) + objnum = pParser->GetObjectPositionOrZero(objnum); - if (pParser->m_V5Type[objnum] == 1 || pParser->m_V5Type[objnum] == 255) { - offset = pParser->m_ObjectInfo[objnum].pos; + if (pParser->GetObjectType(objnum) == 1 || + pParser->GetObjectType(objnum) == 255) { + offset = pParser->GetObjectPositionOrZero(objnum); if (offset == 0) { return 0; } @@ -3136,7 +3107,7 @@ FX_BOOL CPDF_DataAvail::LoadAllFile(IFX_DownloadHints* pHints) { } FX_BOOL CPDF_DataAvail::LoadAllXref(IFX_DownloadHints* pHints) { m_parser.m_Syntax.InitParser(m_pFileRead, (FX_DWORD)m_dwHeaderOffset); - m_parser.m_bOwnFileRead = FALSE; + m_parser.m_bOwnFileRead = false; if (!m_parser.LoadAllCrossRefV4(m_dwLastXRefOffset) && !m_parser.LoadAllCrossRefV5(m_dwLastXRefOffset)) { m_docStatus = PDF_DATAAVAIL_LOADALLFILE; |