Cleanup: Do not check pointers before deleting them.
[pdfium.git] / core / src / fpdfapi / fpdf_page / pageint.h
index 52aded0..95e16cb 100644 (file)
@@ -1,29 +1,30 @@
 // Copyright 2014 PDFium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
+
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#ifndef _PAGEINT_H_
-#define _PAGEINT_H_
+#ifndef CORE_SRC_FPDFAPI_FPDF_PAGE_PAGEINT_H_
+#define CORE_SRC_FPDFAPI_FPDF_PAGE_PAGEINT_H_
+
+#include <map>
 
 #include "../../../include/fpdfapi/fpdf_pageobj.h"
+
 #define PARSE_STEP_LIMIT               100
-#define STREAM_PARSE_BUFSIZE   20480
-class CPDF_QuickFontCache;
-#ifndef _FPDFAPI_MINI_
-class CPDF_StreamParser : public CFX_Object
+
+class CPDF_StreamParser
 {
 public:
 
-    CPDF_StreamParser(const FX_BYTE* pData, FX_DWORD dwSize);
+    CPDF_StreamParser(const uint8_t* pData, FX_DWORD dwSize);
     ~CPDF_StreamParser();
 
     CPDF_Stream*               ReadInlineStream(CPDF_Document* pDoc, CPDF_Dictionary* pDict, CPDF_Object* pCSObj, FX_BOOL bDecode);
     typedef enum { EndOfData, Number, Keyword, Name, Others } SyntaxType;
 
     SyntaxType                 ParseNextElement();
-    FX_LPBYTE                  GetWordBuf()
+    uint8_t*                   GetWordBuf()
     {
         return m_WordBuffer;
     }
@@ -52,14 +53,13 @@ protected:
     void                               GetNextWord(FX_BOOL& bIsNumber);
     CFX_ByteString             ReadString();
     CFX_ByteString             ReadHexString();
-    const FX_BYTE*             m_pBuf;
+    const uint8_t*             m_pBuf;
     FX_DWORD                   m_Size;
     FX_DWORD                   m_Pos;
-    FX_BYTE                            m_WordBuffer[256];
+    uint8_t                            m_WordBuffer[256];
     FX_DWORD                   m_WordSize;
     CPDF_Object*               m_pLastObj;
 };
-#endif
 typedef enum {
     PDFOP_CloseFillStrokePath = 0, PDFOP_FillStrokePath,
     PDFOP_CloseEOFillStrokePath, PDFOP_EOFillStrokePath,
@@ -117,14 +117,10 @@ typedef struct {
         } m_Name;
     };
 } _ContentParam;
-#if defined(_FPDFAPI_MINI_)
-#define _FPDF_MAX_FORM_LEVEL_          17
-#else
 #define _FPDF_MAX_FORM_LEVEL_          30
-#endif
 #define _FPDF_MAX_TYPE3_FORM_LEVEL_    4
 #define _FPDF_MAX_OBJECT_STACK_SIZE_ 512
-class CPDF_StreamContentParser : public CFX_Object
+class CPDF_StreamContentParser
 {
 public:
     CPDF_StreamContentParser();
@@ -147,9 +143,9 @@ public:
     _ContentParam              m_ParamBuf1[PARAM_BUF_SIZE];
     FX_DWORD                   m_ParamStartPos;
     FX_DWORD                   m_ParamCount;
-    void                               AddNumberParam(FX_LPCSTR str, int len);
+    void                               AddNumberParam(const FX_CHAR* str, int len);
     void                               AddObjectParam(CPDF_Object* pObj);
-    void                               AddNameParam(FX_LPCSTR name, int size);
+    void                               AddNameParam(const FX_CHAR* name, int size);
     int                                        GetNextParamPos();
     void                               ClearAllParams();
     CPDF_Object*               GetObject(FX_DWORD index);
@@ -158,48 +154,16 @@ public:
     FX_FLOAT           GetNumber16(FX_DWORD index);
     int                                        GetInteger(FX_DWORD index)
     {
-        return (FX_INT32)(GetNumber(index));
+        return (int32_t)(GetNumber(index));
     }
-    FX_BOOL                            OnOperator(FX_LPCSTR op);
+    FX_BOOL                            OnOperator(const FX_CHAR* op);
     void                               BigCaseCaller(int index);
     FX_BOOL                            m_bAbort;
-#ifndef _FPDFAPI_MINI_
     CPDF_StreamParser* m_pSyntax;
     FX_DWORD                   GetParsePos()
     {
         return m_pSyntax->GetPos();
     }
-#else
-    int                                        m_WordState;
-    void                               InputData(FX_LPCBYTE src_buf, FX_DWORD src_size);
-    void                               Finish();
-    void                               StartArray();
-    void                               EndArray();
-    void                               StartDict();
-    void                               EndDict();
-    void                               EndName();
-    void                               EndNumber();
-    void                               EndKeyword();
-    void                               EndHexString();
-    void                               EndString();
-    void                               EndImageDict();
-    void                               EndInlineImage();
-    FX_LPBYTE                  m_pWordBuf;
-    FX_DWORD                   m_WordSize;
-    CFX_BinaryBuf              m_StringBuf;
-    int                                        m_StringLevel, m_StringState, m_EscCode;
-    void                               AddContainer(CPDF_Object* pObject);
-    FX_BOOL                            SetToCurObj(CPDF_Object* pObject);
-    FX_LPBYTE                  m_pDictName;
-    FX_BOOL                            m_bDictName;
-    CPDF_Object**              m_pObjectStack;
-    FX_BOOL*                   m_pObjectState;
-    FX_DWORD                   m_ObjectSize;
-    int                                        m_InlineImageState;
-    FX_BYTE                            m_InlineWhiteChar;
-    CFX_BinaryBuf              m_ImageSrcBuf;
-    FX_LPBYTE                  m_pStreamBuf;
-#endif
     CPDF_AllStates*            m_pCurStates;
     CPDF_ContentMark   m_CurContentMark;
     CFX_PtrArray               m_ClipTextList;
@@ -210,10 +174,8 @@ public:
     void                               ConvertUserSpace(FX_FLOAT& x, FX_FLOAT& y);
     void                               ConvertTextSpace(FX_FLOAT& x, FX_FLOAT& y);
     void                               OnChangeTextMatrix();
-#ifndef _FPDFAPI_MINI_
-    FX_DWORD                   Parse(FX_LPCBYTE pData, FX_DWORD dwSize, FX_DWORD max_cost);
+    FX_DWORD                   Parse(const uint8_t* pData, FX_DWORD dwSize, FX_DWORD max_cost);
     void                               ParsePathObject();
-#endif
     int                                        m_CompatCount;
     FX_PATHPOINT*              m_pPathPoints;
     int                                        m_PathPointCount;
@@ -244,7 +206,7 @@ public:
     CPDF_Font*                 FindFont(const CFX_ByteString& name);
     CPDF_ColorSpace*   FindColorSpace(const CFX_ByteString& name);
     CPDF_Pattern*              FindPattern(const CFX_ByteString& name, FX_BOOL bShading);
-    CPDF_Object*               FindResourceObj(FX_BSTR type, const CFX_ByteString& name);
+    CPDF_Object*               FindResourceObj(const CFX_ByteStringC& type, const CFX_ByteString& name);
     void Handle_CloseFillStrokePath();
     void Handle_FillStrokePath();
     void Handle_CloseEOFillStrokePath();
@@ -320,7 +282,7 @@ public:
     void Handle_NextLineShowText_Space();
     void Handle_Invalid();
 };
-class CPDF_ContentParser : public CFX_Object
+class CPDF_ContentParser
 {
 public:
     CPDF_ContentParser();
@@ -346,7 +308,7 @@ protected:
     CPDF_StreamAcc*            m_pSingleStream;
     CPDF_StreamAcc**   m_pStreamArray;
     FX_DWORD                   m_nStreams;
-    FX_LPBYTE                  m_pData;
+    uint8_t*                   m_pData;
     FX_DWORD                   m_Size;
     class CPDF_StreamContentParser*    m_pParser;
     FX_DWORD                   m_CurrentOffset;
@@ -364,82 +326,58 @@ public:
     FX_FLOAT                           m_TextX, m_TextY, m_TextLineX, m_TextLineY;
     FX_FLOAT                           m_TextLeading, m_TextRise, m_TextHorzScale;
 };
-template <class ObjClass> class CPDF_CountedObject : public CFX_Object
-{
-public:
-    ObjClass   m_Obj;
-    FX_DWORD   m_nCount;
-};
-typedef CFX_MapPtrTemplate<CPDF_Dictionary*, CPDF_CountedObject<CPDF_Font*>*>          CPDF_FontMap;
-typedef CFX_MapPtrTemplate<CPDF_Object*, CPDF_CountedObject<CPDF_ColorSpace*>*>                CPDF_ColorSpaceMap;
-typedef CFX_MapPtrTemplate<CPDF_Object*, CPDF_CountedObject<CPDF_Pattern*>*>           CPDF_PatternMap;
-typedef CFX_MapPtrTemplate<FX_DWORD, CPDF_CountedObject<CPDF_Image*>*>                         CPDF_ImageMap;
-typedef CFX_MapPtrTemplate<CPDF_Stream*, CPDF_CountedObject<CPDF_IccProfile*>*>                CPDF_IccProfileMap;
-typedef CFX_MapPtrTemplate<CPDF_Stream*, CPDF_CountedObject<CPDF_StreamAcc*>*>         CPDF_FontFileMap;
-template <class KeyType, class ValueType>
-KeyType PDF_DocPageData_FindValue(const CFX_MapPtrTemplate<KeyType, CPDF_CountedObject<ValueType>*> &map, ValueType findValue, CPDF_CountedObject<ValueType>*& findData)
-{
-    FX_POSITION pos = map.GetStartPosition();
-    while (pos) {
-        KeyType findKey;
-        map.GetNextAssoc(pos, findKey, findData);
-        if (findData->m_Obj == findValue) {
-            return findKey;
-        }
-    }
-    findData = NULL;
-    return (KeyType)(FX_UINTPTR)NULL;
-}
-template <class KeyType, class ValueType>
-FX_BOOL PDF_DocPageData_Release(CFX_MapPtrTemplate<KeyType, CPDF_CountedObject<ValueType>*> &map, KeyType findKey, ValueType findValue, FX_BOOL bForce = FALSE)
-{
-    if (!findKey && !findValue) {
-        return FALSE;
-    }
-    CPDF_CountedObject<ValueType>* findData = NULL;
-    if (!findKey) {
-        findKey = PDF_DocPageData_FindValue<KeyType, ValueType>(map, findValue, findData);
-    } else if (!map.Lookup(findKey, findData)) {
-        return FALSE;
-    }
-    if (findData && ((-- findData->m_nCount) == 0 || bForce)) {
-        delete findData->m_Obj;
-        delete findData;
-        map.RemoveKey(findKey);
-        return TRUE;
-    }
-    return FALSE;
-}
-class CPDF_DocPageData : public CFX_Object
+
+class CPDF_DocPageData
 {
-public:
-    CPDF_DocPageData(CPDF_Document *pPDFDoc);
+  public:
+    explicit CPDF_DocPageData(CPDF_Document *pPDFDoc);
     ~CPDF_DocPageData();
-    void                                       Clear(FX_BOOL bRelease = FALSE);
-    CPDF_Font*                         GetFont(CPDF_Dictionary* pFontDict, FX_BOOL findOnly);
-    CPDF_Font*                         GetStandardFont(FX_BSTR fontName, CPDF_FontEncoding* pEncoding);
-    void                                       ReleaseFont(CPDF_Dictionary* pFontDict);
-    CPDF_ColorSpace*           GetColorSpace(CPDF_Object* pCSObj, CPDF_Dictionary* pResources);
-    CPDF_ColorSpace*           GetCopiedColorSpace(CPDF_Object* pCSObj);
-    void                                       ReleaseColorSpace(CPDF_Object* pColorSpace);
-    CPDF_Pattern*                      GetPattern(CPDF_Object* pPatternObj, FX_BOOL bShading, const CFX_AffineMatrix* matrix);
-    void                                       ReleasePattern(CPDF_Object* pPatternObj);
-    CPDF_Image*                                GetImage(CPDF_Object* pImageStream);
-    void                                       ReleaseImage(CPDF_Object* pImageStream);
-    CPDF_IccProfile*           GetIccProfile(CPDF_Stream* pIccProfileStream, FX_INT32 nComponents);
-    void                                       ReleaseIccProfile(CPDF_Stream* pIccProfileStream, CPDF_IccProfile* pIccProfile);
-    CPDF_StreamAcc*                    GetFontFileStreamAcc(CPDF_Stream* pFontStream);
-    void                                       ReleaseFontFileStreamAcc(CPDF_Stream* pFontStream, FX_BOOL bForce = FALSE);
-    CPDF_Document*                     m_pPDFDoc;
-    CPDF_FontMap                       m_FontMap;
-    CPDF_ColorSpaceMap         m_ColorSpaceMap;
-    CPDF_PatternMap                    m_PatternMap;
-    CPDF_ImageMap                      m_ImageMap;
-    CPDF_IccProfileMap         m_IccProfileMap;
-    CFX_MapByteStringToPtr     m_HashProfileMap;
-    CPDF_FontFileMap           m_FontFileMap;
+
+    void                        Clear(FX_BOOL bRelease = FALSE);
+    CPDF_Font*                  GetFont(CPDF_Dictionary* pFontDict, FX_BOOL findOnly);
+    CPDF_Font*                  GetStandardFont(const CFX_ByteStringC& fontName, CPDF_FontEncoding* pEncoding);
+    void                        ReleaseFont(CPDF_Dictionary* pFontDict);
+    CPDF_ColorSpace*            GetColorSpace(CPDF_Object* pCSObj, CPDF_Dictionary* pResources);
+    CPDF_ColorSpace*            GetCopiedColorSpace(CPDF_Object* pCSObj);
+    void                        ReleaseColorSpace(CPDF_Object* pColorSpace);
+    CPDF_Pattern*               GetPattern(CPDF_Object* pPatternObj, FX_BOOL bShading, const CFX_AffineMatrix* matrix);
+    void                        ReleasePattern(CPDF_Object* pPatternObj);
+    CPDF_Image*                 GetImage(CPDF_Object* pImageStream);
+    void                        ReleaseImage(CPDF_Object* pImageStream);
+    CPDF_IccProfile*            GetIccProfile(CPDF_Stream* pIccProfileStream);
+    void                        ReleaseIccProfile(CPDF_IccProfile* pIccProfile);
+    CPDF_StreamAcc*             GetFontFileStreamAcc(CPDF_Stream* pFontStream);
+    void                        ReleaseFontFileStreamAcc(CPDF_Stream* pFontStream, FX_BOOL bForce = FALSE);
+    FX_BOOL                     IsForceClear() const {return m_bForceClear;}
+    CPDF_CountedColorSpace*     FindColorSpacePtr(CPDF_Object* pCSObj) const;
+    CPDF_CountedPattern*        FindPatternPtr(CPDF_Object* pPatternObj) const;
+
+  private:
+    using CPDF_CountedFont = CPDF_CountedObject<CPDF_Font>;
+    using CPDF_CountedIccProfile = CPDF_CountedObject<CPDF_IccProfile>;
+    using CPDF_CountedImage = CPDF_CountedObject<CPDF_Image>;
+    using CPDF_CountedStreamAcc = CPDF_CountedObject<CPDF_StreamAcc>;
+
+    using CPDF_ColorSpaceMap = std::map<CPDF_Object*, CPDF_CountedColorSpace*>;
+    using CPDF_FontFileMap = std::map<CPDF_Stream*, CPDF_CountedStreamAcc*>;
+    using CPDF_FontMap = std::map<CPDF_Dictionary*, CPDF_CountedFont*>;
+    using CPDF_IccProfileMap = std::map<CPDF_Stream*, CPDF_CountedIccProfile*>;
+    using CPDF_ImageMap = std::map<FX_DWORD, CPDF_CountedImage*>;
+    using CPDF_PatternMap = std::map<CPDF_Object*, CPDF_CountedPattern*>;
+
+    CPDF_Document* const m_pPDFDoc;
+    CFX_MapByteStringToPtr m_HashProfileMap;
+    FX_BOOL m_bForceClear;
+
+    CPDF_ColorSpaceMap m_ColorSpaceMap;
+    CPDF_FontFileMap m_FontFileMap;
+    CPDF_FontMap m_FontMap;
+    CPDF_IccProfileMap m_IccProfileMap;
+    CPDF_ImageMap m_ImageMap;
+    CPDF_PatternMap m_PatternMap;
 };
-class CPDF_Function : public CFX_Object
+
+class CPDF_Function
 {
 public:
     static CPDF_Function*      Load(CPDF_Object* pFuncObj);
@@ -462,49 +400,68 @@ protected:
     virtual FX_BOOL    v_Init(CPDF_Object* pObj) = 0;
     virtual FX_BOOL    v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const = 0;
 };
-class CPDF_IccProfile : public CFX_Object
+class CPDF_IccProfile
 {
 public:
-    CPDF_IccProfile(FX_LPCBYTE pData, FX_DWORD dwSize, int nComponents);
+    CPDF_IccProfile(const uint8_t* pData, FX_DWORD dwSize);
     ~CPDF_IccProfile();
+    int32_t GetComponents() const { return m_nSrcComponents; }
     FX_BOOL                                    m_bsRGB;
-    FX_LPVOID                          m_pTransform;
+    void*                              m_pTransform;
+private:
+    int32_t                m_nSrcComponents;
 };
+
 class CPDF_DeviceCS : public CPDF_ColorSpace
 {
 public:
-    CPDF_DeviceCS(int family);
-    virtual FX_BOOL    GetRGB(FX_FLOAT* pBuf, FX_FLOAT& R, FX_FLOAT& G, FX_FLOAT& B) const;
-    FX_BOOL    SetRGB(FX_FLOAT* pBuf, FX_FLOAT R, FX_FLOAT G, FX_FLOAT B) const;
-    FX_BOOL    v_GetCMYK(FX_FLOAT* pBuf, FX_FLOAT& c, FX_FLOAT& m, FX_FLOAT& y, FX_FLOAT& k) const;
-    FX_BOOL    v_SetCMYK(FX_FLOAT* pBuf, FX_FLOAT c, FX_FLOAT m, FX_FLOAT y, FX_FLOAT k) const;
-    virtual void       TranslateImageLine(FX_LPBYTE pDestBuf, FX_LPCBYTE pSrcBuf, int pixels, int image_width, int image_height, FX_BOOL bTransMask = FALSE) const;
+    CPDF_DeviceCS(CPDF_Document* pDoc, int family);
+
+    FX_BOOL GetRGB(FX_FLOAT* pBuf,
+                   FX_FLOAT& R,
+                   FX_FLOAT& G,
+                   FX_FLOAT& B) const override;
+    FX_BOOL SetRGB(FX_FLOAT* pBuf,
+                   FX_FLOAT R,
+                   FX_FLOAT G,
+                   FX_FLOAT B) const override;
+    FX_BOOL v_GetCMYK(FX_FLOAT* pBuf,
+                      FX_FLOAT& c,
+                      FX_FLOAT& m,
+                      FX_FLOAT& y,
+                      FX_FLOAT& k) const override;
+    FX_BOOL v_SetCMYK(FX_FLOAT* pBuf,
+                      FX_FLOAT c,
+                      FX_FLOAT m,
+                      FX_FLOAT y,
+                      FX_FLOAT k) const override;
+    void TranslateImageLine(uint8_t* pDestBuf,
+                            const uint8_t* pSrcBuf,
+                            int pixels,
+                            int image_width,
+                            int image_height,
+                            FX_BOOL bTransMask = FALSE) const override;
 };
+
 class CPDF_PatternCS : public CPDF_ColorSpace
 {
 public:
-    CPDF_PatternCS();
-    ~CPDF_PatternCS();
-    virtual FX_BOOL            v_Load(CPDF_Document* pDoc, CPDF_Array* pArray);
-    virtual FX_BOOL            GetRGB(FX_FLOAT* pBuf, FX_FLOAT& R, FX_FLOAT& G, FX_FLOAT& B) const;
-    virtual CPDF_ColorSpace*   GetBaseCS() const
-    {
-        return m_pBaseCS;
+    explicit CPDF_PatternCS(CPDF_Document* pDoc)
+        : CPDF_ColorSpace(pDoc, PDFCS_PATTERN, 1),
+          m_pBaseCS(nullptr),
+          m_pCountedBaseCS(nullptr) {
     }
-    CPDF_ColorSpace*   m_pBaseCS;
-};
-#define        MAX_PAGE_OBJECTS_UNIFY_NAMING                           4096
-class CPDF_ResourceNaming : public CFX_Object
-{
-public:
-    struct _NamingState : public CFX_Object {
-        CFX_ByteString m_Prefix;
-        int                            m_nIndex;
-    };
-    ~CPDF_ResourceNaming();
-    CFX_ByteString             GetName(const CPDF_Dictionary* pResList, FX_LPCSTR szType);
-protected:
-    CFX_MapByteStringToPtr     m_NamingCache;
+    ~CPDF_PatternCS() override;
+    FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override;
+    FX_BOOL GetRGB(FX_FLOAT* pBuf,
+                   FX_FLOAT& R,
+                   FX_FLOAT& G,
+                   FX_FLOAT& B) const override;
+    CPDF_ColorSpace* GetBaseCS() const override;
+
+private:
+    CPDF_ColorSpace* m_pBaseCS;
+    CPDF_CountedColorSpace* m_pCountedBaseCS;
 };
 
-#endif  // _PAGEINT_H_
+#endif  // CORE_SRC_FPDFAPI_FPDF_PAGE_PAGEINT_H_