Fix a heap buffer overflow issue in CPDF_CMap::GetNextChar
[pdfium.git] / core / src / fpdfapi / fpdf_font / font_int.h
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4  
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 typedef void* FXFT_Library;
8 class CPDF_CMapManager : public CFX_Object
9 {
10 public:
11     CPDF_CMapManager();
12     ~CPDF_CMapManager();
13     FX_LPVOID                           GetPackage(FX_BOOL bPrompt);
14     CPDF_CMap*                          GetPredefinedCMap(const CFX_ByteString& name, FX_BOOL bPrompt);
15     CPDF_CID2UnicodeMap*        GetCID2UnicodeMap(int charset, FX_BOOL bPrompt);
16     void                                        ReloadAll();
17 private:
18     CPDF_CMap*                          LoadPredefinedCMap(const CFX_ByteString& name,  FX_BOOL bPrompt);
19     CPDF_CID2UnicodeMap*        LoadCID2UnicodeMap(int charset, FX_BOOL bPrompt);
20     void                                        DropAll(FX_BOOL bReload);
21     FX_BOOL                                     m_bPrompted;
22     CFX_MapByteStringToPtr      m_CMaps;
23     CPDF_CID2UnicodeMap*        m_CID2UnicodeMaps[6];
24 };
25 class CPDF_FontGlobals : public CFX_Object
26 {
27 public:
28     CPDF_FontGlobals();
29     ~CPDF_FontGlobals();
30     void                                ClearAll();
31     void                                Clear(void* key);
32     CPDF_Font*                  Find(void* key, int index);
33     void                                Set(void* key, int index, CPDF_Font* pFont);
34     CFX_MapPtrToPtr             m_pStockMap;
35     CPDF_CMapManager    m_CMapManager;
36     struct {
37         const struct FXCMAP_CMap*       m_pMapList;
38         int                             m_Count;
39     } m_EmbeddedCharsets[NUMBER_OF_CIDSETS];
40     struct {
41         const FX_WORD*  m_pMap;
42         int                             m_Count;
43     } m_EmbeddedToUnicodes[NUMBER_OF_CIDSETS];
44     FX_LPBYTE                   m_pContrastRamps;
45 };
46 struct _CMap_CodeRange {
47     int                 m_CharSize;
48     FX_BYTE             m_Lower[4];
49     FX_BYTE             m_Upper[4];
50 };
51 class CPDF_CMapParser : public CFX_Object
52 {
53 public:
54     CPDF_CMapParser();
55     ~CPDF_CMapParser() {}
56     FX_BOOL     Initialize(CPDF_CMap*);
57     void        ParseWord(FX_BSTR str);
58     CFX_BinaryBuf       m_AddMaps;
59 private:
60     CPDF_CMap*  m_pCMap;
61     int                         m_Status;
62     int                         m_CodeSeq;
63     FX_DWORD            m_CodePoints[4];
64     CFX_ArrayTemplate<_CMap_CodeRange>  m_CodeRanges;
65     CFX_ByteString      m_Registry, m_Ordering, m_Supplement;
66     CFX_ByteString      m_LastWord;
67 };
68 #define CIDCODING_UNKNOWN       0
69 #define CIDCODING_GB            1
70 #define CIDCODING_BIG5          2
71 #define CIDCODING_JIS           3
72 #define CIDCODING_KOREA         4
73 #define CIDCODING_UCS2          5
74 #define CIDCODING_CID           6
75 #define CIDCODING_UTF16         7
76 class CPDF_CMap : public CFX_Object
77 {
78 public:
79     CPDF_CMap();
80     FX_BOOL                                     LoadPredefined(CPDF_CMapManager* pMgr, const FX_CHAR* name, FX_BOOL bPromptCJK);
81     FX_BOOL                                     LoadEmbedded(FX_LPCBYTE pData, FX_DWORD dwSize);
82     void                                        Release();
83     FX_BOOL                                     IsLoaded() const
84     {
85         return m_bLoaded;
86     }
87     int                                         GetCharset()
88     {
89         return m_Charset;
90     }
91     FX_BOOL                                     IsVertWriting() const
92     {
93         return m_bVertical;
94     }
95     FX_WORD                                     CIDFromCharCode(FX_DWORD charcode) const;
96     FX_DWORD                            CharCodeFromCID(FX_WORD CID) const;
97     int                                         GetCharSize(FX_DWORD charcode) const;
98     FX_DWORD                            GetNextChar(FX_LPCSTR pString, int nStrLen, int& offset) const;
99     int                                         CountChar(FX_LPCSTR pString, int size) const;
100     int                                         AppendChar(FX_LPSTR str, FX_DWORD charcode) const;
101     typedef enum {OneByte, TwoBytes, MixedTwoBytes, MixedFourBytes} CodingScheme;
102 protected:
103     ~CPDF_CMap();
104     friend class                        CPDF_CMapParser;
105     friend class                        CPDF_CMapManager;
106     friend class                        CPDF_CIDFont;
107 protected:
108     CFX_ByteString                      m_PredefinedCMap;
109     FX_BOOL                                     m_bVertical;
110     int                                         m_Charset, m_Coding;
111     CodingScheme                        m_CodingScheme;
112     int                                         m_nCodeRanges;
113     FX_BYTE*                            m_pLeadingBytes;
114     FX_WORD*                            m_pMapping;
115     FX_LPBYTE                           m_pAddMapping;
116     FX_BOOL                                     m_bLoaded;
117     const FXCMAP_CMap*          m_pEmbedMap;
118     CPDF_CMap*                          m_pUseMap;
119 };
120 class CPDF_PredefinedCMap
121 {
122 public:
123     FX_LPCSTR   m_pName;
124     int                 m_Charset;
125     int                 m_Coding;
126     CPDF_CMap::CodingScheme     m_CodingScheme;
127     FX_DWORD    m_LeadingSegCount;
128     FX_BYTE             m_LeadingSegs[4];
129 };
130 typedef struct _FileHeader {
131     FX_BYTE             btTag[4];
132     FX_BYTE             btVersion;
133     FX_BYTE             btFormat;
134     FX_BYTE             btReserved1[2];
135     FX_DWORD    dwStartIndex;
136     FX_DWORD    dwEndIndex;
137     FX_DWORD    dwDataSize;
138     FX_DWORD    dwDataOffset;
139     FX_DWORD    dwRecordSize;
140 } FXMP_FILEHEADER;
141 class CPDF_CID2UnicodeMap : public CFX_Object
142 {
143 public:
144     CPDF_CID2UnicodeMap();
145     ~CPDF_CID2UnicodeMap();
146     FX_BOOL             Initialize();
147     FX_BOOL             IsLoaded();
148     void                Load(CPDF_CMapManager* pMgr, int charset, FX_BOOL bPromptCJK);
149     FX_WCHAR    UnicodeFromCID(FX_WORD CID);
150 protected:
151     int                 m_Charset;
152     const FX_WORD*      m_pEmbeddedMap;
153     FX_DWORD    m_EmbeddedCount;
154 };
155 class CPDF_ToUnicodeMap : public CFX_Object
156 {
157 public:
158     void                                        Load(CPDF_Stream* pStream);
159     CFX_WideString                      Lookup(FX_DWORD charcode);
160     FX_DWORD                            ReverseLookup(FX_WCHAR unicode);
161 protected:
162     CFX_CMapDWordToDWord        m_Map;
163     CPDF_CID2UnicodeMap*        m_pBaseMap;
164     CFX_WideTextBuf                     m_MultiCharBuf;
165 };
166 class CPDF_FontCharMap : public CFX_CharMap, public CFX_Object
167 {
168 public:
169     CPDF_FontCharMap(CPDF_Font* pFont);
170     CPDF_Font*          m_pFont;
171 };