• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <limits.h>
8 
9 #include "core/include/fpdfapi/fpdf_module.h"
10 #include "core/include/fpdfapi/fpdf_parser.h"
11 #include "core/include/fxcodec/fx_codec.h"
12 #include "core/include/fxcrt/fx_ext.h"
13 
14 #define _STREAM_MAX_SIZE_ 20 * 1024 * 1024
15 
16 const FX_WORD PDFDocEncoding[256] = {
17     0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008,
18     0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011,
19     0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x02d8, 0x02c7, 0x02c6,
20     0x02d9, 0x02dd, 0x02db, 0x02da, 0x02dc, 0x0020, 0x0021, 0x0022, 0x0023,
21     0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c,
22     0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
23     0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e,
24     0x003f, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
25     0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, 0x0050,
26     0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059,
27     0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, 0x0060, 0x0061, 0x0062,
28     0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b,
29     0x006c, 0x006d, 0x006e, 0x006f, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074,
30     0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d,
31     0x007e, 0x0000, 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x0192,
32     0x2044, 0x2039, 0x203a, 0x2212, 0x2030, 0x201e, 0x201c, 0x201d, 0x2018,
33     0x2019, 0x201a, 0x2122, 0xfb01, 0xfb02, 0x0141, 0x0152, 0x0160, 0x0178,
34     0x017d, 0x0131, 0x0142, 0x0153, 0x0161, 0x017e, 0x0000, 0x20ac, 0x00a1,
35     0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, 0x00a8, 0x00a9, 0x00aa,
36     0x00ab, 0x00ac, 0x0000, 0x00ae, 0x00af, 0x00b0, 0x00b1, 0x00b2, 0x00b3,
37     0x00b4, 0x00b5, 0x00b6, 0x00b7, 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc,
38     0x00bd, 0x00be, 0x00bf, 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5,
39     0x00c6, 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce,
40     0x00cf, 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
41     0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, 0x00e0,
42     0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, 0x00e8, 0x00e9,
43     0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, 0x00f0, 0x00f1, 0x00f2,
44     0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x00fa, 0x00fb,
45     0x00fc, 0x00fd, 0x00fe, 0x00ff};
46 
A85Decode(const uint8_t * src_buf,FX_DWORD src_size,uint8_t * & dest_buf,FX_DWORD & dest_size)47 FX_DWORD A85Decode(const uint8_t* src_buf,
48                    FX_DWORD src_size,
49                    uint8_t*& dest_buf,
50                    FX_DWORD& dest_size) {
51   dest_size = 0;
52   dest_buf = NULL;
53   if (src_size == 0) {
54     return 0;
55   }
56   FX_DWORD zcount = 0;
57   FX_DWORD pos = 0;
58   while (pos < src_size) {
59     uint8_t ch = src_buf[pos];
60     if (ch < '!' && ch != '\n' && ch != '\r' && ch != ' ' && ch != '\t') {
61       break;
62     }
63     if (ch == 'z') {
64       zcount++;
65     } else if (ch > 'u') {
66       break;
67     }
68     pos++;
69   }
70   if (pos == 0) {
71     return 0;
72   }
73   if (zcount > UINT_MAX / 4) {
74     return (FX_DWORD)-1;
75   }
76   if (zcount * 4 > UINT_MAX - (pos - zcount)) {
77     return (FX_DWORD)-1;
78   }
79   dest_buf = FX_Alloc(uint8_t, zcount * 4 + (pos - zcount));
80   int state = 0;
81   uint32_t res = 0;
82   pos = dest_size = 0;
83   while (pos < src_size) {
84     uint8_t ch = src_buf[pos++];
85     if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t')
86       continue;
87 
88     if (ch == 'z') {
89       FXSYS_memset(dest_buf + dest_size, 0, 4);
90       state = 0;
91       res = 0;
92       dest_size += 4;
93     } else {
94       if (ch < '!' || ch > 'u') {
95         break;
96       }
97       res = res * 85 + ch - 33;
98       state++;
99       if (state == 5) {
100         for (int i = 0; i < 4; i++) {
101           dest_buf[dest_size++] = (uint8_t)(res >> (3 - i) * 8);
102         }
103         state = 0;
104         res = 0;
105       }
106     }
107   }
108   if (state) {
109     int i;
110     for (i = state; i < 5; i++) {
111       res = res * 85 + 84;
112     }
113     for (i = 0; i < state - 1; i++) {
114       dest_buf[dest_size++] = (uint8_t)(res >> (3 - i) * 8);
115     }
116   }
117   if (pos < src_size && src_buf[pos] == '>') {
118     pos++;
119   }
120   return pos;
121 }
HexDecode(const uint8_t * src_buf,FX_DWORD src_size,uint8_t * & dest_buf,FX_DWORD & dest_size)122 FX_DWORD HexDecode(const uint8_t* src_buf,
123                    FX_DWORD src_size,
124                    uint8_t*& dest_buf,
125                    FX_DWORD& dest_size) {
126   FX_DWORD i;
127   for (i = 0; i < src_size; i++)
128     if (src_buf[i] == '>') {
129       break;
130     }
131   dest_buf = FX_Alloc(uint8_t, i / 2 + 1);
132   dest_size = 0;
133   bool bFirst = true;
134   for (i = 0; i < src_size; i++) {
135     uint8_t ch = src_buf[i];
136     if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t')
137       continue;
138 
139     if (ch == '>') {
140       ++i;
141       break;
142     }
143     if (!std::isxdigit(ch))
144       continue;
145 
146     int digit = FXSYS_toHexDigit(ch);
147     if (bFirst)
148       dest_buf[dest_size] = digit * 16;
149     else
150       dest_buf[dest_size++] += digit;
151 
152     bFirst = !bFirst;
153   }
154   if (!bFirst)
155     dest_size++;
156   return i;
157 }
158 
RunLengthDecode(const uint8_t * src_buf,FX_DWORD src_size,uint8_t * & dest_buf,FX_DWORD & dest_size)159 FX_DWORD RunLengthDecode(const uint8_t* src_buf,
160                          FX_DWORD src_size,
161                          uint8_t*& dest_buf,
162                          FX_DWORD& dest_size) {
163   FX_DWORD i = 0;
164   FX_DWORD old;
165   dest_size = 0;
166   while (i < src_size) {
167     if (src_buf[i] < 128) {
168       old = dest_size;
169       dest_size += src_buf[i] + 1;
170       if (dest_size < old) {
171         return (FX_DWORD)-1;
172       }
173       i += src_buf[i] + 2;
174     } else if (src_buf[i] > 128) {
175       old = dest_size;
176       dest_size += 257 - src_buf[i];
177       if (dest_size < old) {
178         return (FX_DWORD)-1;
179       }
180       i += 2;
181     } else {
182       break;
183     }
184   }
185   if (dest_size >= _STREAM_MAX_SIZE_) {
186     return -1;
187   }
188   dest_buf = FX_Alloc(uint8_t, dest_size);
189   i = 0;
190   int dest_count = 0;
191   while (i < src_size) {
192     if (src_buf[i] < 128) {
193       FX_DWORD copy_len = src_buf[i] + 1;
194       FX_DWORD buf_left = src_size - i - 1;
195       if (buf_left < copy_len) {
196         FX_DWORD delta = copy_len - buf_left;
197         copy_len = buf_left;
198         FXSYS_memset(dest_buf + dest_count + copy_len, '\0', delta);
199       }
200       FXSYS_memcpy(dest_buf + dest_count, src_buf + i + 1, copy_len);
201       dest_count += src_buf[i] + 1;
202       i += src_buf[i] + 2;
203     } else if (src_buf[i] > 128) {
204       int fill = 0;
205       if (i < src_size - 1) {
206         fill = src_buf[i + 1];
207       }
208       FXSYS_memset(dest_buf + dest_count, fill, 257 - src_buf[i]);
209       dest_count += 257 - src_buf[i];
210       i += 2;
211     } else {
212       break;
213     }
214   }
215   FX_DWORD ret = i + 1;
216   if (ret > src_size) {
217     ret = src_size;
218   }
219   return ret;
220 }
FPDFAPI_CreateFaxDecoder(const uint8_t * src_buf,FX_DWORD src_size,int width,int height,const CPDF_Dictionary * pParams)221 ICodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder(
222     const uint8_t* src_buf,
223     FX_DWORD src_size,
224     int width,
225     int height,
226     const CPDF_Dictionary* pParams) {
227   int K = 0;
228   FX_BOOL EndOfLine = FALSE;
229   FX_BOOL ByteAlign = FALSE;
230   FX_BOOL BlackIs1 = FALSE;
231   int Columns = 1728;
232   int Rows = 0;
233   if (pParams) {
234     K = pParams->GetInteger("K");
235     EndOfLine = pParams->GetInteger("EndOfLine");
236     ByteAlign = pParams->GetInteger("EncodedByteAlign");
237     BlackIs1 = pParams->GetInteger("BlackIs1");
238     Columns = pParams->GetInteger("Columns", 1728);
239     Rows = pParams->GetInteger("Rows");
240     if (Rows > USHRT_MAX) {
241       Rows = 0;
242     }
243     if (Columns <= 0 || Rows < 0 || Columns > USHRT_MAX || Rows > USHRT_MAX) {
244       return NULL;
245     }
246   }
247   return CPDF_ModuleMgr::Get()->GetFaxModule()->CreateDecoder(
248       src_buf, src_size, width, height, K, EndOfLine, ByteAlign, BlackIs1,
249       Columns, Rows);
250 }
CheckFlateDecodeParams(int Colors,int BitsPerComponent,int Columns)251 static FX_BOOL CheckFlateDecodeParams(int Colors,
252                                       int BitsPerComponent,
253                                       int Columns) {
254   if (Columns < 0) {
255     return FALSE;
256   }
257   int check = Columns;
258   if (Colors < 0 || (check > 0 && Colors > INT_MAX / check)) {
259     return FALSE;
260   }
261   check *= Colors;
262   if (BitsPerComponent < 0 ||
263       (check > 0 && BitsPerComponent > INT_MAX / check)) {
264     return FALSE;
265   }
266   check *= BitsPerComponent;
267   if (check > INT_MAX - 7) {
268     return FALSE;
269   }
270   return TRUE;
271 }
FPDFAPI_CreateFlateDecoder(const uint8_t * src_buf,FX_DWORD src_size,int width,int height,int nComps,int bpc,const CPDF_Dictionary * pParams)272 ICodec_ScanlineDecoder* FPDFAPI_CreateFlateDecoder(
273     const uint8_t* src_buf,
274     FX_DWORD src_size,
275     int width,
276     int height,
277     int nComps,
278     int bpc,
279     const CPDF_Dictionary* pParams) {
280   int predictor = 0;
281   int Colors = 0, BitsPerComponent = 0, Columns = 0;
282   if (pParams) {
283     predictor = pParams->GetInteger("Predictor");
284     Colors = pParams->GetInteger("Colors", 1);
285     BitsPerComponent = pParams->GetInteger("BitsPerComponent", 8);
286     Columns = pParams->GetInteger("Columns", 1);
287     if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) {
288       return NULL;
289     }
290   }
291   return CPDF_ModuleMgr::Get()->GetFlateModule()->CreateDecoder(
292       src_buf, src_size, width, height, nComps, bpc, predictor, Colors,
293       BitsPerComponent, Columns);
294 }
FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW,const uint8_t * src_buf,FX_DWORD src_size,CPDF_Dictionary * pParams,FX_DWORD estimated_size,uint8_t * & dest_buf,FX_DWORD & dest_size)295 FX_DWORD FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW,
296                                   const uint8_t* src_buf,
297                                   FX_DWORD src_size,
298                                   CPDF_Dictionary* pParams,
299                                   FX_DWORD estimated_size,
300                                   uint8_t*& dest_buf,
301                                   FX_DWORD& dest_size) {
302   int predictor = 0;
303   FX_BOOL bEarlyChange = TRUE;
304   int Colors = 0, BitsPerComponent = 0, Columns = 0;
305   if (pParams) {
306     predictor = pParams->GetInteger("Predictor");
307     bEarlyChange = pParams->GetInteger("EarlyChange", 1);
308     Colors = pParams->GetInteger("Colors", 1);
309     BitsPerComponent = pParams->GetInteger("BitsPerComponent", 8);
310     Columns = pParams->GetInteger("Columns", 1);
311     if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) {
312       return (FX_DWORD)-1;
313     }
314   }
315   return CPDF_ModuleMgr::Get()->GetFlateModule()->FlateOrLZWDecode(
316       bLZW, src_buf, src_size, bEarlyChange, predictor, Colors,
317       BitsPerComponent, Columns, estimated_size, dest_buf, dest_size);
318 }
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 last_estimated_size,FX_BOOL bImageAcc)319 FX_BOOL PDF_DataDecode(const uint8_t* src_buf,
320                        FX_DWORD src_size,
321                        const CPDF_Dictionary* pDict,
322                        uint8_t*& dest_buf,
323                        FX_DWORD& dest_size,
324                        CFX_ByteString& ImageEncoding,
325                        CPDF_Dictionary*& pImageParms,
326                        FX_DWORD last_estimated_size,
327                        FX_BOOL bImageAcc) {
328   CPDF_Object* pDecoder = pDict ? pDict->GetElementValue("Filter") : nullptr;
329   if (!pDecoder || (!pDecoder->IsArray() && !pDecoder->IsName()))
330     return FALSE;
331 
332   CPDF_Object* pParams =
333       pDict ? pDict->GetElementValue("DecodeParms") : nullptr;
334   CFX_ByteStringArray DecoderList;
335   CFX_ArrayTemplate<CPDF_Object*> ParamList;
336   if (CPDF_Array* pDecoders = pDecoder->AsArray()) {
337     CPDF_Array* pParamsArray = ToArray(pParams);
338     if (!pParamsArray)
339       pParams = nullptr;
340 
341     for (FX_DWORD i = 0; i < pDecoders->GetCount(); i++) {
342       CFX_ByteStringC str = pDecoders->GetConstString(i);
343       DecoderList.Add(str);
344       ParamList.Add(pParams ? pParamsArray->GetDict(i) : nullptr);
345     }
346   } else {
347     DecoderList.Add(pDecoder->GetConstString());
348     ParamList.Add(pParams ? pParams->GetDict() : nullptr);
349   }
350   uint8_t* last_buf = (uint8_t*)src_buf;
351   FX_DWORD last_size = src_size;
352   for (int i = 0; i < DecoderList.GetSize(); i++) {
353     int estimated_size =
354         i == DecoderList.GetSize() - 1 ? last_estimated_size : 0;
355     CFX_ByteString decoder = DecoderList[i];
356     // Use ToDictionary here because we can push NULL into the ParamList.
357     CPDF_Dictionary* pParam = ToDictionary(ParamList[i]);
358     uint8_t* new_buf = nullptr;
359     FX_DWORD new_size = (FX_DWORD)-1;
360     int offset = -1;
361     if (decoder == "FlateDecode" || decoder == "Fl") {
362       if (bImageAcc && i == DecoderList.GetSize() - 1) {
363         ImageEncoding = "FlateDecode";
364         dest_buf = (uint8_t*)last_buf;
365         dest_size = last_size;
366         pImageParms = pParam;
367         return TRUE;
368       }
369       offset = FPDFAPI_FlateOrLZWDecode(FALSE, last_buf, last_size, pParam,
370                                         estimated_size, new_buf, new_size);
371     } else if (decoder == "LZWDecode" || decoder == "LZW") {
372       offset = FPDFAPI_FlateOrLZWDecode(TRUE, last_buf, last_size, pParam,
373                                         estimated_size, new_buf, new_size);
374     } else if (decoder == "ASCII85Decode" || decoder == "A85") {
375       offset = A85Decode(last_buf, last_size, new_buf, new_size);
376     } else if (decoder == "ASCIIHexDecode" || decoder == "AHx") {
377       offset = HexDecode(last_buf, last_size, new_buf, new_size);
378     } else if (decoder == "RunLengthDecode" || decoder == "RL") {
379       if (bImageAcc && i == DecoderList.GetSize() - 1) {
380         ImageEncoding = "RunLengthDecode";
381         dest_buf = (uint8_t*)last_buf;
382         dest_size = last_size;
383         pImageParms = pParam;
384         return TRUE;
385       }
386       offset = RunLengthDecode(last_buf, last_size, new_buf, new_size);
387     } else if (decoder == "Crypt") {
388       continue;
389     } else {
390       // If we get here, assume it's an image decoder.
391       if (decoder == "DCT") {
392         decoder = "DCTDecode";
393       } else if (decoder == "CCF") {
394         decoder = "CCITTFaxDecode";
395       }
396       ImageEncoding = decoder;
397       pImageParms = pParam;
398       dest_buf = (uint8_t*)last_buf;
399       dest_size = last_size;
400       if (CPDF_Array* pDecoders = pDecoder->AsArray())
401         pDecoders->RemoveAt(i + 1, pDecoders->GetCount() - i - 1);
402       return TRUE;
403     }
404     if (last_buf != src_buf) {
405       FX_Free(last_buf);
406     }
407     if (offset == -1) {
408       FX_Free(new_buf);
409       return FALSE;
410     }
411     last_buf = new_buf;
412     last_size = new_size;
413   }
414   ImageEncoding = "";
415   pImageParms = nullptr;
416   dest_buf = last_buf;
417   dest_size = last_size;
418   return TRUE;
419 }
PDF_DecodeText(const uint8_t * src_data,FX_DWORD src_len,CFX_CharMap * pCharMap)420 CFX_WideString PDF_DecodeText(const uint8_t* src_data,
421                               FX_DWORD src_len,
422                               CFX_CharMap* pCharMap) {
423   CFX_WideString result;
424   if (src_len >= 2 && ((src_data[0] == 0xfe && src_data[1] == 0xff) ||
425                        (src_data[0] == 0xff && src_data[1] == 0xfe))) {
426     FX_BOOL bBE = src_data[0] == 0xfe;
427     FX_DWORD max_chars = (src_len - 2) / 2;
428     if (!max_chars) {
429       return result;
430     }
431     if (src_data[0] == 0xff) {
432       bBE = !src_data[2];
433     }
434     FX_WCHAR* dest_buf = result.GetBuffer(max_chars);
435     const uint8_t* uni_str = src_data + 2;
436     int dest_pos = 0;
437     for (FX_DWORD i = 0; i < max_chars * 2; i += 2) {
438       FX_WORD unicode = bBE ? (uni_str[i] << 8 | uni_str[i + 1])
439                             : (uni_str[i + 1] << 8 | uni_str[i]);
440       if (unicode == 0x1b) {
441         i += 2;
442         while (i < max_chars * 2) {
443           FX_WORD unicode = bBE ? (uni_str[i] << 8 | uni_str[i + 1])
444                                 : (uni_str[i + 1] << 8 | uni_str[i]);
445           i += 2;
446           if (unicode == 0x1b) {
447             break;
448           }
449         }
450       } else {
451         dest_buf[dest_pos++] = unicode;
452       }
453     }
454     result.ReleaseBuffer(dest_pos);
455   } else if (!pCharMap) {
456     FX_WCHAR* dest_buf = result.GetBuffer(src_len);
457     for (FX_DWORD i = 0; i < src_len; i++) {
458       dest_buf[i] = PDFDocEncoding[src_data[i]];
459     }
460     result.ReleaseBuffer(src_len);
461   } else {
462     return (*pCharMap->m_GetWideString)(
463         pCharMap, CFX_ByteString((const FX_CHAR*)src_data, src_len));
464   }
465   return result;
466 }
PDF_EncodeText(const FX_WCHAR * pString,int len,CFX_CharMap * pCharMap)467 CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString,
468                               int len,
469                               CFX_CharMap* pCharMap) {
470   if (len == -1) {
471     len = FXSYS_wcslen(pString);
472   }
473   CFX_ByteString result;
474   if (!pCharMap) {
475     FX_CHAR* dest_buf1 = result.GetBuffer(len);
476     int i;
477     for (i = 0; i < len; i++) {
478       int code;
479       for (code = 0; code < 256; code++)
480         if (PDFDocEncoding[code] == pString[i]) {
481           break;
482         }
483       if (code == 256) {
484         break;
485       }
486       dest_buf1[i] = code;
487     }
488     result.ReleaseBuffer(i);
489     if (i == len) {
490       return result;
491     }
492   }
493 
494   if (len > INT_MAX / 2 - 1) {
495     result.ReleaseBuffer(0);
496     return result;
497   }
498 
499   int encLen = len * 2 + 2;
500 
501   uint8_t* dest_buf2 = (uint8_t*)result.GetBuffer(encLen);
502   dest_buf2[0] = 0xfe;
503   dest_buf2[1] = 0xff;
504   dest_buf2 += 2;
505   for (int i = 0; i < len; i++) {
506     *dest_buf2++ = pString[i] >> 8;
507     *dest_buf2++ = (uint8_t)pString[i];
508   }
509   result.ReleaseBuffer(encLen);
510   return result;
511 }
PDF_EncodeString(const CFX_ByteString & src,FX_BOOL bHex)512 CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex) {
513   CFX_ByteTextBuf result;
514   int srclen = src.GetLength();
515   if (bHex) {
516     result.AppendChar('<');
517     for (int i = 0; i < srclen; i++) {
518       result.AppendChar("0123456789ABCDEF"[src[i] / 16]);
519       result.AppendChar("0123456789ABCDEF"[src[i] % 16]);
520     }
521     result.AppendChar('>');
522     return result.GetByteString();
523   }
524   result.AppendChar('(');
525   for (int i = 0; i < srclen; i++) {
526     uint8_t ch = src[i];
527     if (ch == ')' || ch == '\\' || ch == '(') {
528       result.AppendChar('\\');
529     } else if (ch == 0x0a) {
530       result << "\\n";
531       continue;
532     } else if (ch == 0x0d) {
533       result << "\\r";
534       continue;
535     }
536     result.AppendChar(ch);
537   }
538   result.AppendChar(')');
539   return result.GetByteString();
540 }
FlateEncode(const uint8_t * src_buf,FX_DWORD src_size,uint8_t * & dest_buf,FX_DWORD & dest_size)541 void FlateEncode(const uint8_t* src_buf,
542                  FX_DWORD src_size,
543                  uint8_t*& dest_buf,
544                  FX_DWORD& dest_size) {
545   CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule();
546   if (pEncoders) {
547     pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf, dest_size);
548   }
549 }
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)550 void FlateEncode(const uint8_t* src_buf,
551                  FX_DWORD src_size,
552                  int predictor,
553                  int Colors,
554                  int BitsPerComponent,
555                  int Columns,
556                  uint8_t*& dest_buf,
557                  FX_DWORD& dest_size) {
558   CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule();
559   if (pEncoders) {
560     pEncoders->GetFlateModule()->Encode(src_buf, src_size, predictor, Colors,
561                                         BitsPerComponent, Columns, dest_buf,
562                                         dest_size);
563   }
564 }
FlateDecode(const uint8_t * src_buf,FX_DWORD src_size,uint8_t * & dest_buf,FX_DWORD & dest_size)565 FX_DWORD FlateDecode(const uint8_t* src_buf,
566                      FX_DWORD src_size,
567                      uint8_t*& dest_buf,
568                      FX_DWORD& dest_size) {
569   CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule();
570   if (pEncoders) {
571     return pEncoders->GetFlateModule()->FlateOrLZWDecode(
572         FALSE, src_buf, src_size, FALSE, 0, 0, 0, 0, 0, dest_buf, dest_size);
573   }
574   return 0;
575 }
576