Add PDFCharIsLineEnding helper
[pdfium.git] / core / include / fpdfapi / fpdf_parser.h
index f80bb24..32509eb 100644 (file)
@@ -7,6 +7,7 @@
 #ifndef CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_
 #define CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_
 
+#include "../../../third_party/base/nonstd_unique_ptr.h"
 #include "../fxcrt/fx_system.h"
 #include "fpdf_objects.h"
 
@@ -44,13 +45,29 @@ class CFX_PrivateData;
 #define FPDFPERM_PRINT_HIGH 0x0800
 #define FPDF_PAGE_MAX_NUM 0xFFFFF
 
-// Indexed by 8-bit character code, contains either:
-//   'W' - for whitespace: NUL, TAB, CR, LF, FF, 0x80, 0xff
-//   'N' - for numeric: 0123456789+-.
-//   'D' - for delimiter: %()/<>[]{}
-//   'R' - otherwise.
+// Use the accessors below instead of directly accessing PDF_CharType.
 extern const char PDF_CharType[256];
 
+inline bool PDFCharIsWhitespace(uint8_t c) {
+  return PDF_CharType[c] == 'W';
+}
+inline bool PDFCharIsNumeric(uint8_t c) {
+  return PDF_CharType[c] == 'N';
+}
+inline bool PDFCharIsDelimiter(uint8_t c) {
+  return PDF_CharType[c] == 'D';
+}
+inline bool PDFCharIsOther(uint8_t c) {
+  return PDF_CharType[c] == 'R';
+}
+
+inline bool PDFCharIsLineEnding(uint8_t c) {
+  return c == '\r' || c == '\n';
+}
+
+// Indexed by 8-bit char code, contains unicode code points.
+extern const FX_WORD PDFDocEncoding[256];
+
 class CPDF_Document : public CFX_PrivateData, public CPDF_IndirectObjects {
  public:
   CPDF_Document();
@@ -267,7 +284,7 @@ class CPDF_SyntaxParser {
   FX_FILESIZE FindTag(const CFX_ByteStringC& tag, FX_FILESIZE limit);
 
   void SetEncrypt(CPDF_CryptoHandler* pCryptoHandler) {
-    m_pCryptoHandler = pCryptoHandler;
+    m_pCryptoHandler.reset(pCryptoHandler);
   }
 
   FX_BOOL IsEncrypted() { return m_pCryptoHandler != NULL; }
@@ -291,12 +308,15 @@ class CPDF_SyntaxParser {
   FX_BOOL IsWholeWord(FX_FILESIZE startpos,
                       FX_FILESIZE limit,
                       const uint8_t* tag,
-                      FX_DWORD taglen);
+                      FX_DWORD taglen,
+                      FX_BOOL checkKeyword);
 
   CFX_ByteString ReadString();
 
   CFX_ByteString ReadHexString();
 
+  unsigned int ReadEOLMarkers(FX_FILESIZE pos);
+
   CPDF_Stream* ReadStream(CPDF_Dictionary* pDict,
                           PARSE_CONTEXT* pContext,
                           FX_DWORD objnum,
@@ -320,7 +340,7 @@ class CPDF_SyntaxParser {
 
   FX_FILESIZE m_BufOffset;
 
-  CPDF_CryptoHandler* m_pCryptoHandler;
+  nonstd::unique_ptr<CPDF_CryptoHandler> m_pCryptoHandler;
 
   uint8_t m_WordBuffer[257];
 
@@ -355,8 +375,6 @@ class CPDF_Parser {
   CPDF_Parser();
   ~CPDF_Parser();
 
-  FX_DWORD StartParse(const FX_CHAR* filename, FX_BOOL bReParse = FALSE);
-  FX_DWORD StartParse(const FX_WCHAR* filename, FX_BOOL bReParse = FALSE);
   FX_DWORD StartParse(IFX_FileRead* pFile,
                       FX_BOOL bReParse = FALSE,
                       FX_BOOL bOwnFileRead = TRUE);
@@ -371,9 +389,13 @@ class CPDF_Parser {
 
   CFX_ByteString GetPassword() { return m_Password; }
 
-  CPDF_SecurityHandler* GetSecurityHandler() { return m_pSecurityHandler; }
+  CPDF_SecurityHandler* GetSecurityHandler() {
+    return m_pSecurityHandler.get();
+  }
 
-  CPDF_CryptoHandler* GetCryptoHandler() { return m_Syntax.m_pCryptoHandler; }
+  CPDF_CryptoHandler* GetCryptoHandler() {
+    return m_Syntax.m_pCryptoHandler.get();
+  }
 
   void SetSecurityHandler(CPDF_SecurityHandler* pSecurityHandler,
                           FX_BOOL bForced = FALSE);
@@ -449,10 +471,10 @@ class CPDF_Parser {
 
   FX_BOOL LoadAllCrossRefV5(FX_FILESIZE pos);
 
-  FX_BOOL LoadCrossRefV4(FX_FILESIZE pos,
-                         FX_FILESIZE streampos,
-                         FX_BOOL bSkip,
-                         FX_BOOL bFirst);
+  bool LoadCrossRefV4(FX_FILESIZE pos,
+                      FX_FILESIZE streampos,
+                      FX_BOOL bSkip,
+                      FX_BOOL bFirst);
 
   FX_BOOL LoadCrossRefV5(FX_FILESIZE pos, FX_FILESIZE& prev, FX_BOOL bMainXRef);
 
@@ -478,6 +500,8 @@ class CPDF_Parser {
 
   FX_BOOL IsLinearizedFile(IFX_FileRead* pFileAccess, FX_DWORD offset);
 
+  bool FindPosInOffsets(FX_FILESIZE pos) const;
+
   int m_FileVersion;
 
   CPDF_Dictionary* m_pTrailer;
@@ -489,7 +513,7 @@ class CPDF_Parser {
 
   FX_BOOL m_bXRefStream;
 
-  CPDF_SecurityHandler* m_pSecurityHandler;
+  nonstd::unique_ptr<CPDF_SecurityHandler> m_pSecurityHandler;
 
   FX_BOOL m_bForceUseSecurityHandler;
 
@@ -547,17 +571,18 @@ class CPDF_SecurityHandler {
 class CPDF_StandardSecurityHandler : public CPDF_SecurityHandler {
  public:
   CPDF_StandardSecurityHandler();
-
-  virtual ~CPDF_StandardSecurityHandler();
-  virtual FX_BOOL OnInit(CPDF_Parser* pParser, CPDF_Dictionary* pEncryptDict);
-  virtual FX_DWORD GetPermissions();
-  virtual FX_BOOL IsOwner() { return m_bOwner; }
-  virtual FX_BOOL GetCryptInfo(int& cipher,
-                               const uint8_t*& buffer,
-                               int& keylen);
-  virtual FX_BOOL IsMetadataEncrypted();
-  virtual CPDF_CryptoHandler* CreateCryptoHandler();
-  virtual CPDF_StandardSecurityHandler* GetStandardHandler() { return this; }
+  ~CPDF_StandardSecurityHandler() override;
+
+  // CPDF_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,
@@ -683,29 +708,30 @@ class CPDF_CryptoHandler {
 class CPDF_StandardCryptoHandler : public CPDF_CryptoHandler {
  public:
   CPDF_StandardCryptoHandler();
-
-  virtual ~CPDF_StandardCryptoHandler();
+  ~CPDF_StandardCryptoHandler() override;
+
+  // CPDF_CryptoHandler
+  FX_BOOL Init(CPDF_Dictionary* pEncryptDict,
+               CPDF_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,
+                        const uint8_t* src_buf,
+                        FX_DWORD src_size,
+                        CFX_BinaryBuf& dest_buf) override;
+  FX_BOOL DecryptFinish(void* context, CFX_BinaryBuf& dest_buf) override;
+  FX_DWORD EncryptGetSize(FX_DWORD objnum,
+                          FX_DWORD version,
+                          const uint8_t* src_buf,
+                          FX_DWORD src_size) override;
+  FX_BOOL EncryptContent(FX_DWORD objnum,
+                         FX_DWORD version,
+                         const uint8_t* src_buf,
+                         FX_DWORD src_size,
+                         uint8_t* dest_buf,
+                         FX_DWORD& dest_size) override;
 
   FX_BOOL Init(int cipher, const uint8_t* key, int keylen);
-  virtual FX_BOOL Init(CPDF_Dictionary* pEncryptDict,
-                       CPDF_SecurityHandler* pSecurityHandler);
-  virtual FX_DWORD DecryptGetSize(FX_DWORD src_size);
-  virtual void* DecryptStart(FX_DWORD objnum, FX_DWORD gennum);
-  virtual FX_BOOL DecryptStream(void* context,
-                                const uint8_t* src_buf,
-                                FX_DWORD src_size,
-                                CFX_BinaryBuf& dest_buf);
-  virtual FX_BOOL DecryptFinish(void* context, CFX_BinaryBuf& dest_buf);
-  virtual FX_DWORD EncryptGetSize(FX_DWORD objnum,
-                                  FX_DWORD version,
-                                  const uint8_t* src_buf,
-                                  FX_DWORD src_size);
-  virtual FX_BOOL EncryptContent(FX_DWORD objnum,
-                                 FX_DWORD version,
-                                 const uint8_t* src_buf,
-                                 FX_DWORD src_size,
-                                 uint8_t* dest_buf,
-                                 FX_DWORD& dest_size);
 
  protected:
   virtual void CryptBlock(FX_BOOL bEncrypt,
@@ -799,6 +825,14 @@ void FlateEncode(const uint8_t* src_buf,
                  FX_DWORD src_size,
                  uint8_t*& dest_buf,
                  FX_DWORD& dest_size);
+void FlateEncode(const uint8_t* src_buf,
+                 FX_DWORD src_size,
+                 int predictor,
+                 int Colors,
+                 int BitsPerComponent,
+                 int Columns,
+                 uint8_t*& dest_buf,
+                 FX_DWORD& dest_size);
 FX_DWORD FlateDecode(const uint8_t* src_buf,
                      FX_DWORD src_size,
                      uint8_t*& dest_buf,
@@ -807,6 +841,8 @@ FX_DWORD RunLengthDecode(const uint8_t* src_buf,
                          FX_DWORD src_size,
                          uint8_t*& dest_buf,
                          FX_DWORD& dest_size);
+FX_BOOL IsSignatureDict(const CPDF_Dictionary* pDict);
+
 class CPDF_NumberTree {
  public:
   CPDF_NumberTree(CPDF_Dictionary* pRoot) { m_pRoot = pRoot; }
@@ -909,4 +945,14 @@ enum PDF_DATAAVAIL_STATUS {
   PDF_DATAAVAIL_TRAILER_APPEND
 };
 
+FX_BOOL PDF_DataDecode(const uint8_t* src_buf,
+                       FX_DWORD src_size,
+                       const CPDF_Dictionary* pDict,
+                       uint8_t*& dest_buf,
+                       FX_DWORD& dest_size,
+                       CFX_ByteString& ImageEncoding,
+                       CPDF_Dictionary*& pImageParms,
+                       FX_DWORD estimated_size,
+                       FX_BOOL bImageAcc);
+
 #endif  // CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_