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 "core/fpdfapi/parser/fpdf_parser_decode.h"
8 
9 #include <limits.h>
10 
11 #include <algorithm>
12 #include <sstream>
13 #include <utility>
14 #include <vector>
15 
16 #include "constants/stream_dict_common.h"
17 #include "core/fpdfapi/parser/cpdf_array.h"
18 #include "core/fpdfapi/parser/cpdf_dictionary.h"
19 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
20 #include "core/fxcodec/fax/faxmodule.h"
21 #include "core/fxcodec/flate/flatemodule.h"
22 #include "core/fxcodec/fx_codec.h"
23 #include "core/fxcodec/scanlinedecoder.h"
24 #include "core/fxcrt/fx_extension.h"
25 #include "core/fxcrt/fx_safe_types.h"
26 #include "third_party/base/numerics/safe_math.h"
27 #include "third_party/base/stl_util.h"
28 
29 namespace {
30 
31 const uint32_t kMaxStreamSize = 20 * 1024 * 1024;
32 
GetUnicodeFromBigEndianBytes(const uint8_t * bytes)33 uint16_t GetUnicodeFromBigEndianBytes(const uint8_t* bytes) {
34   return bytes[0] << 8 | bytes[1];
35 }
36 
GetUnicodeFromLittleEndianBytes(const uint8_t * bytes)37 uint16_t GetUnicodeFromLittleEndianBytes(const uint8_t* bytes) {
38   return bytes[1] << 8 | bytes[0];
39 }
40 
CheckFlateDecodeParams(int Colors,int BitsPerComponent,int Columns)41 bool CheckFlateDecodeParams(int Colors, int BitsPerComponent, int Columns) {
42   if (Colors < 0 || BitsPerComponent < 0 || Columns < 0)
43     return false;
44 
45   pdfium::base::CheckedNumeric<int> check = Columns;
46   check *= Colors;
47   check *= BitsPerComponent;
48   if (!check.IsValid())
49     return false;
50 
51   return check.ValueOrDie() <= INT_MAX - 7;
52 }
53 
GetA85Result(uint32_t res,size_t i)54 uint8_t GetA85Result(uint32_t res, size_t i) {
55   return static_cast<uint8_t>(res >> (3 - i) * 8);
56 }
57 
58 }  // namespace
59 
60 const uint16_t PDFDocEncoding[256] = {
61     0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008,
62     0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011,
63     0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x02d8, 0x02c7, 0x02c6,
64     0x02d9, 0x02dd, 0x02db, 0x02da, 0x02dc, 0x0020, 0x0021, 0x0022, 0x0023,
65     0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c,
66     0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
67     0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e,
68     0x003f, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
69     0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, 0x0050,
70     0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059,
71     0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, 0x0060, 0x0061, 0x0062,
72     0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b,
73     0x006c, 0x006d, 0x006e, 0x006f, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074,
74     0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d,
75     0x007e, 0x0000, 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x0192,
76     0x2044, 0x2039, 0x203a, 0x2212, 0x2030, 0x201e, 0x201c, 0x201d, 0x2018,
77     0x2019, 0x201a, 0x2122, 0xfb01, 0xfb02, 0x0141, 0x0152, 0x0160, 0x0178,
78     0x017d, 0x0131, 0x0142, 0x0153, 0x0161, 0x017e, 0x0000, 0x20ac, 0x00a1,
79     0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, 0x00a8, 0x00a9, 0x00aa,
80     0x00ab, 0x00ac, 0x0000, 0x00ae, 0x00af, 0x00b0, 0x00b1, 0x00b2, 0x00b3,
81     0x00b4, 0x00b5, 0x00b6, 0x00b7, 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc,
82     0x00bd, 0x00be, 0x00bf, 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5,
83     0x00c6, 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce,
84     0x00cf, 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
85     0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, 0x00e0,
86     0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, 0x00e8, 0x00e9,
87     0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, 0x00f0, 0x00f1, 0x00f2,
88     0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x00fa, 0x00fb,
89     0x00fc, 0x00fd, 0x00fe, 0x00ff};
90 
ValidateDecoderPipeline(const CPDF_Array * pDecoders)91 bool ValidateDecoderPipeline(const CPDF_Array* pDecoders) {
92   size_t count = pDecoders->size();
93   if (count == 0)
94     return true;
95 
96   for (size_t i = 0; i < count; ++i) {
97     if (!pDecoders->GetObjectAt(i)->IsName())
98       return false;
99   }
100 
101   if (count == 1)
102     return true;
103 
104   // TODO(thestig): Consolidate all the places that use these filter names.
105   static const char kValidDecoders[][16] = {
106       "FlateDecode",    "Fl",  "LZWDecode",       "LZW", "ASCII85Decode", "A85",
107       "ASCIIHexDecode", "AHx", "RunLengthDecode", "RL"};
108   for (size_t i = 0; i < count - 1; ++i) {
109     if (!pdfium::ContainsValue(kValidDecoders, pDecoders->GetStringAt(i)))
110       return false;
111   }
112   return true;
113 }
114 
A85Decode(pdfium::span<const uint8_t> src_span,std::unique_ptr<uint8_t,FxFreeDeleter> * dest_buf,uint32_t * dest_size)115 uint32_t A85Decode(pdfium::span<const uint8_t> src_span,
116                    std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
117                    uint32_t* dest_size) {
118   *dest_size = 0;
119   if (src_span.empty()) {
120     dest_buf->reset();
121     return 0;
122   }
123 
124   // Count legal characters and zeros.
125   uint32_t zcount = 0;
126   uint32_t pos = 0;
127   while (pos < src_span.size()) {
128     uint8_t ch = src_span[pos];
129     if (ch == 'z') {
130       zcount++;
131     } else if ((ch < '!' || ch > 'u') && !PDFCharIsLineEnding(ch) &&
132                ch != ' ' && ch != '\t') {
133       break;
134     }
135     pos++;
136   }
137   // No content to decode.
138   if (pos == 0)
139     return 0;
140 
141   // Count the space needed to contain non-zero characters. The encoding ratio
142   // of Ascii85 is 4:5.
143   uint32_t space_for_non_zeroes = (pos - zcount) / 5 * 4 + 4;
144   FX_SAFE_UINT32 size = zcount;
145   size *= 4;
146   size += space_for_non_zeroes;
147   if (!size.IsValid())
148     return FX_INVALID_OFFSET;
149 
150   dest_buf->reset(FX_Alloc(uint8_t, size.ValueOrDie()));
151   uint8_t* dest_buf_ptr = dest_buf->get();
152   size_t state = 0;
153   uint32_t res = 0;
154   pos = 0;
155   while (pos < src_span.size()) {
156     uint8_t ch = src_span[pos++];
157     if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t')
158       continue;
159 
160     if (ch == 'z') {
161       memset(dest_buf_ptr + *dest_size, 0, 4);
162       state = 0;
163       res = 0;
164       *dest_size += 4;
165       continue;
166     }
167 
168     // Check for the end or illegal character.
169     if (ch < '!' || ch > 'u')
170       break;
171 
172     res = res * 85 + ch - 33;
173     if (state < 4) {
174       ++state;
175       continue;
176     }
177 
178     for (size_t i = 0; i < 4; ++i) {
179       dest_buf_ptr[(*dest_size)++] = GetA85Result(res, i);
180     }
181     state = 0;
182     res = 0;
183   }
184   // Handle partial group.
185   if (state) {
186     for (size_t i = state; i < 5; ++i)
187       res = res * 85 + 84;
188     for (size_t i = 0; i < state - 1; ++i)
189       dest_buf_ptr[(*dest_size)++] = GetA85Result(res, i);
190   }
191   if (pos < src_span.size() && src_span[pos] == '>')
192     ++pos;
193   return pos;
194 }
195 
HexDecode(pdfium::span<const uint8_t> src_span,std::unique_ptr<uint8_t,FxFreeDeleter> * dest_buf,uint32_t * dest_size)196 uint32_t HexDecode(pdfium::span<const uint8_t> src_span,
197                    std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
198                    uint32_t* dest_size) {
199   *dest_size = 0;
200   if (src_span.empty()) {
201     dest_buf->reset();
202     return 0;
203   }
204 
205   uint32_t i = 0;
206   // Find the end of data.
207   while (i < src_span.size() && src_span[i] != '>')
208     ++i;
209 
210   dest_buf->reset(FX_Alloc(uint8_t, i / 2 + 1));
211   uint8_t* dest_buf_ptr = dest_buf->get();
212   bool bFirst = true;
213   for (i = 0; i < src_span.size(); ++i) {
214     uint8_t ch = src_span[i];
215     if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t')
216       continue;
217 
218     if (ch == '>') {
219       ++i;
220       break;
221     }
222     if (!std::isxdigit(ch))
223       continue;
224 
225     int digit = FXSYS_HexCharToInt(ch);
226     if (bFirst)
227       dest_buf_ptr[*dest_size] = digit * 16;
228     else
229       dest_buf_ptr[(*dest_size)++] += digit;
230     bFirst = !bFirst;
231   }
232   if (!bFirst)
233     ++(*dest_size);
234   return i;
235 }
236 
RunLengthDecode(pdfium::span<const uint8_t> src_span,std::unique_ptr<uint8_t,FxFreeDeleter> * dest_buf,uint32_t * dest_size)237 uint32_t RunLengthDecode(pdfium::span<const uint8_t> src_span,
238                          std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
239                          uint32_t* dest_size) {
240   size_t i = 0;
241   *dest_size = 0;
242   while (i < src_span.size()) {
243     if (src_span[i] == 128)
244       break;
245 
246     uint32_t old = *dest_size;
247     if (src_span[i] < 128) {
248       *dest_size += src_span[i] + 1;
249       if (*dest_size < old)
250         return FX_INVALID_OFFSET;
251       i += src_span[i] + 2;
252     } else {
253       *dest_size += 257 - src_span[i];
254       if (*dest_size < old)
255         return FX_INVALID_OFFSET;
256       i += 2;
257     }
258   }
259   if (*dest_size >= kMaxStreamSize)
260     return FX_INVALID_OFFSET;
261 
262   dest_buf->reset(FX_Alloc(uint8_t, *dest_size));
263   pdfium::span<uint8_t> dest_span(dest_buf->get(), *dest_size);
264   i = 0;
265   int dest_count = 0;
266   while (i < src_span.size()) {
267     if (src_span[i] == 128)
268       break;
269 
270     if (src_span[i] < 128) {
271       uint32_t copy_len = src_span[i] + 1;
272       uint32_t buf_left = src_span.size() - i - 1;
273       if (buf_left < copy_len) {
274         uint32_t delta = copy_len - buf_left;
275         copy_len = buf_left;
276         memset(&dest_span[dest_count + copy_len], '\0', delta);
277       }
278       auto copy_span = src_span.subspan(i + 1, copy_len);
279       memcpy(&dest_span[dest_count], copy_span.data(), copy_span.size());
280       dest_count += src_span[i] + 1;
281       i += src_span[i] + 2;
282     } else {
283       int fill = 0;
284       if (i < src_span.size() - 1)
285         fill = src_span[i + 1];
286       memset(&dest_span[dest_count], fill, 257 - src_span[i]);
287       dest_count += 257 - src_span[i];
288       i += 2;
289     }
290   }
291   return std::min(i + 1, src_span.size());
292 }
293 
CreateFaxDecoder(pdfium::span<const uint8_t> src_span,int width,int height,const CPDF_Dictionary * pParams)294 std::unique_ptr<ScanlineDecoder> CreateFaxDecoder(
295     pdfium::span<const uint8_t> src_span,
296     int width,
297     int height,
298     const CPDF_Dictionary* pParams) {
299   int K = 0;
300   bool EndOfLine = false;
301   bool ByteAlign = false;
302   bool BlackIs1 = false;
303   int Columns = 1728;
304   int Rows = 0;
305   if (pParams) {
306     K = pParams->GetIntegerFor("K");
307     EndOfLine = !!pParams->GetIntegerFor("EndOfLine");
308     ByteAlign = !!pParams->GetIntegerFor("EncodedByteAlign");
309     BlackIs1 = !!pParams->GetIntegerFor("BlackIs1");
310     Columns = pParams->GetIntegerFor("Columns", 1728);
311     Rows = pParams->GetIntegerFor("Rows");
312     if (Rows > USHRT_MAX)
313       Rows = 0;
314   }
315   return FaxModule::CreateDecoder(src_span, width, height, K, EndOfLine,
316                                   ByteAlign, BlackIs1, Columns, Rows);
317 }
318 
CreateFlateDecoder(pdfium::span<const uint8_t> src_span,int width,int height,int nComps,int bpc,const CPDF_Dictionary * pParams)319 std::unique_ptr<ScanlineDecoder> CreateFlateDecoder(
320     pdfium::span<const uint8_t> src_span,
321     int width,
322     int height,
323     int nComps,
324     int bpc,
325     const CPDF_Dictionary* pParams) {
326   int predictor = 0;
327   int Colors = 0;
328   int BitsPerComponent = 0;
329   int Columns = 0;
330   if (pParams) {
331     predictor = pParams->GetIntegerFor("Predictor");
332     Colors = pParams->GetIntegerFor("Colors", 1);
333     BitsPerComponent = pParams->GetIntegerFor("BitsPerComponent", 8);
334     Columns = pParams->GetIntegerFor("Columns", 1);
335     if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns))
336       return nullptr;
337   }
338   return FlateModule::CreateDecoder(src_span, width, height, nComps, bpc,
339                                     predictor, Colors, BitsPerComponent,
340                                     Columns);
341 }
342 
FlateOrLZWDecode(bool bLZW,pdfium::span<const uint8_t> src_span,const CPDF_Dictionary * pParams,uint32_t estimated_size,std::unique_ptr<uint8_t,FxFreeDeleter> * dest_buf,uint32_t * dest_size)343 uint32_t FlateOrLZWDecode(bool bLZW,
344                           pdfium::span<const uint8_t> src_span,
345                           const CPDF_Dictionary* pParams,
346                           uint32_t estimated_size,
347                           std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
348                           uint32_t* dest_size) {
349   int predictor = 0;
350   int Colors = 0;
351   int BitsPerComponent = 0;
352   int Columns = 0;
353   bool bEarlyChange = true;
354   if (pParams) {
355     predictor = pParams->GetIntegerFor("Predictor");
356     bEarlyChange = !!pParams->GetIntegerFor("EarlyChange", 1);
357     Colors = pParams->GetIntegerFor("Colors", 1);
358     BitsPerComponent = pParams->GetIntegerFor("BitsPerComponent", 8);
359     Columns = pParams->GetIntegerFor("Columns", 1);
360     if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns))
361       return FX_INVALID_OFFSET;
362   }
363   return FlateModule::FlateOrLZWDecode(bLZW, src_span, bEarlyChange, predictor,
364                                        Colors, BitsPerComponent, Columns,
365                                        estimated_size, dest_buf, dest_size);
366 }
367 
368 Optional<std::vector<std::pair<ByteString, const CPDF_Object*>>>
GetDecoderArray(const CPDF_Dictionary * pDict)369 GetDecoderArray(const CPDF_Dictionary* pDict) {
370   const CPDF_Object* pDecoder = pDict->GetDirectObjectFor("Filter");
371   if (!pDecoder || (!pDecoder->IsArray() && !pDecoder->IsName()))
372     return {};
373 
374   const CPDF_Object* pParams =
375       pDict->GetDirectObjectFor(pdfium::stream::kDecodeParms);
376 
377   std::vector<std::pair<ByteString, const CPDF_Object*>> decoder_array;
378   if (const CPDF_Array* pDecoders = pDecoder->AsArray()) {
379     if (!ValidateDecoderPipeline(pDecoders))
380       return {};
381 
382     const CPDF_Array* pParamsArray = ToArray(pParams);
383     for (size_t i = 0; i < pDecoders->size(); ++i) {
384       decoder_array.push_back(
385           {pDecoders->GetStringAt(i),
386            pParamsArray ? pParamsArray->GetDictAt(i) : nullptr});
387     }
388   } else {
389     decoder_array.push_back(
390         {pDecoder->GetString(), pParams ? pParams->GetDict() : nullptr});
391   }
392 
393   return decoder_array;
394 }
395 
PDF_DataDecode(pdfium::span<const uint8_t> src_span,uint32_t last_estimated_size,bool bImageAcc,const std::vector<std::pair<ByteString,const CPDF_Object * >> & decoder_array,std::unique_ptr<uint8_t,FxFreeDeleter> * dest_buf,uint32_t * dest_size,ByteString * ImageEncoding,RetainPtr<const CPDF_Dictionary> * pImageParams)396 bool PDF_DataDecode(
397     pdfium::span<const uint8_t> src_span,
398     uint32_t last_estimated_size,
399     bool bImageAcc,
400     const std::vector<std::pair<ByteString, const CPDF_Object*>>& decoder_array,
401     std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
402     uint32_t* dest_size,
403     ByteString* ImageEncoding,
404     RetainPtr<const CPDF_Dictionary>* pImageParams) {
405   std::unique_ptr<uint8_t, FxFreeDeleter> result;
406   // May be changed to point to |result| in the for-loop below. So put it below
407   // |result| and let it get destroyed first.
408   pdfium::span<const uint8_t> last_span = src_span;
409   size_t nSize = decoder_array.size();
410   for (size_t i = 0; i < nSize; ++i) {
411     int estimated_size = i == nSize - 1 ? last_estimated_size : 0;
412     ByteString decoder = decoder_array[i].first;
413     const CPDF_Dictionary* pParam = ToDictionary(decoder_array[i].second);
414     std::unique_ptr<uint8_t, FxFreeDeleter> new_buf;
415     uint32_t new_size = 0xFFFFFFFF;
416     uint32_t offset = FX_INVALID_OFFSET;
417     if (decoder == "Crypt")
418       continue;
419     if (decoder == "FlateDecode" || decoder == "Fl") {
420       if (bImageAcc && i == nSize - 1) {
421         *ImageEncoding = "FlateDecode";
422         *dest_buf = std::move(result);
423         *dest_size = last_span.size();
424         pImageParams->Reset(pParam);
425         return true;
426       }
427       offset = FlateOrLZWDecode(false, last_span, pParam, estimated_size,
428                                 &new_buf, &new_size);
429     } else if (decoder == "LZWDecode" || decoder == "LZW") {
430       offset = FlateOrLZWDecode(true, last_span, pParam, estimated_size,
431                                 &new_buf, &new_size);
432     } else if (decoder == "ASCII85Decode" || decoder == "A85") {
433       offset = A85Decode(last_span, &new_buf, &new_size);
434     } else if (decoder == "ASCIIHexDecode" || decoder == "AHx") {
435       offset = HexDecode(last_span, &new_buf, &new_size);
436     } else if (decoder == "RunLengthDecode" || decoder == "RL") {
437       if (bImageAcc && i == nSize - 1) {
438         *ImageEncoding = "RunLengthDecode";
439         *dest_buf = std::move(result);
440         *dest_size = last_span.size();
441         pImageParams->Reset(pParam);
442         return true;
443       }
444       offset = RunLengthDecode(last_span, &new_buf, &new_size);
445     } else {
446       // If we get here, assume it's an image decoder.
447       if (decoder == "DCT")
448         decoder = "DCTDecode";
449       else if (decoder == "CCF")
450         decoder = "CCITTFaxDecode";
451       *ImageEncoding = std::move(decoder);
452       pImageParams->Reset(pParam);
453       *dest_buf = std::move(result);
454       *dest_size = last_span.size();
455       return true;
456     }
457     if (offset == FX_INVALID_OFFSET)
458       return false;
459 
460     last_span = {new_buf.get(), new_size};
461     result = std::move(new_buf);
462   }
463   ImageEncoding->clear();
464   *pImageParams = nullptr;
465   *dest_buf = std::move(result);
466   *dest_size = last_span.size();
467   return true;
468 }
469 
PDF_DecodeText(pdfium::span<const uint8_t> span)470 WideString PDF_DecodeText(pdfium::span<const uint8_t> span) {
471   int dest_pos = 0;
472   WideString result;
473   if (span.size() >= 2 && ((span[0] == 0xfe && span[1] == 0xff) ||
474                            (span[0] == 0xff && span[1] == 0xfe))) {
475     size_t max_chars = (span.size() - 2) / 2;
476     if (!max_chars)
477       return result;
478 
479     pdfium::span<wchar_t> dest_buf = result.GetBuffer(max_chars);
480     uint16_t (*GetUnicodeFromBytes)(const uint8_t*) =
481         span[0] == 0xfe ? GetUnicodeFromBigEndianBytes
482                         : GetUnicodeFromLittleEndianBytes;
483     const uint8_t* unicode_str = &span[2];
484     for (size_t i = 0; i < max_chars * 2; i += 2) {
485       uint16_t unicode = GetUnicodeFromBytes(unicode_str + i);
486 
487       // 0x001B is a begin/end marker for language metadata region that
488       // should not be in the decoded text.
489       if (unicode == 0x001B) {
490         i += 2;
491         for (; i < max_chars * 2; i += 2) {
492           unicode = GetUnicodeFromBytes(unicode_str + i);
493           if (unicode == 0x001B) {
494             i += 2;
495             if (i < max_chars * 2)
496               unicode = GetUnicodeFromBytes(unicode_str + i);
497             break;
498           }
499         }
500         if (i >= max_chars * 2)
501           break;
502       }
503 
504       dest_buf[dest_pos++] = unicode;
505     }
506   } else {
507     pdfium::span<wchar_t> dest_buf = result.GetBuffer(span.size());
508     for (size_t i = 0; i < span.size(); ++i)
509       dest_buf[i] = PDFDocEncoding[span[i]];
510     dest_pos = span.size();
511   }
512   result.ReleaseBuffer(dest_pos);
513   return result;
514 }
515 
PDF_EncodeText(const WideString & str)516 ByteString PDF_EncodeText(const WideString& str) {
517   size_t i = 0;
518   size_t len = str.GetLength();
519   ByteString result;
520   {
521     pdfium::span<char> dest_buf = result.GetBuffer(len);
522     for (i = 0; i < len; ++i) {
523       int code;
524       for (code = 0; code < 256; ++code) {
525         if (PDFDocEncoding[code] == str[i])
526           break;
527       }
528       if (code == 256)
529         break;
530 
531       dest_buf[i] = code;
532     }
533   }
534   result.ReleaseBuffer(i);
535   if (i == len)
536     return result;
537 
538   if (len > INT_MAX / 2 - 1) {
539     result.ReleaseBuffer(0);
540     return result;
541   }
542 
543   size_t dest_index = 0;
544   size_t encLen = len * 2 + 2;
545   {
546     pdfium::span<uint8_t> dest_buf =
547         pdfium::as_writable_bytes(result.GetBuffer(encLen));
548     dest_buf[dest_index++] = 0xfe;
549     dest_buf[dest_index++] = 0xff;
550     for (size_t j = 0; j < len; ++j) {
551       dest_buf[dest_index++] = str[j] >> 8;
552       dest_buf[dest_index++] = static_cast<uint8_t>(str[j]);
553     }
554   }
555   result.ReleaseBuffer(encLen);
556   return result;
557 }
558 
PDF_EncodeString(const ByteString & src,bool bHex)559 ByteString PDF_EncodeString(const ByteString& src, bool bHex) {
560   std::ostringstream result;
561   int srclen = src.GetLength();
562   if (bHex) {
563     result << '<';
564     for (int i = 0; i < srclen; ++i) {
565       char buf[2];
566       FXSYS_IntToTwoHexChars(src[i], buf);
567       result << buf[0];
568       result << buf[1];
569     }
570     result << '>';
571     return ByteString(result);
572   }
573   result << '(';
574   for (int i = 0; i < srclen; ++i) {
575     uint8_t ch = src[i];
576     if (ch == 0x0a) {
577       result << "\\n";
578       continue;
579     }
580     if (ch == 0x0d) {
581       result << "\\r";
582       continue;
583     }
584     if (ch == ')' || ch == '\\' || ch == '(')
585       result << '\\';
586     result << static_cast<char>(ch);
587   }
588   result << ')';
589   return ByteString(result);
590 }
591 
FlateEncode(pdfium::span<const uint8_t> src_span,std::unique_ptr<uint8_t,FxFreeDeleter> * dest_buf,uint32_t * dest_size)592 bool FlateEncode(pdfium::span<const uint8_t> src_span,
593                  std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
594                  uint32_t* dest_size) {
595   return FlateModule::Encode(src_span.data(), src_span.size(), dest_buf,
596                              dest_size);
597 }
598 
FlateDecode(pdfium::span<const uint8_t> src_span,std::unique_ptr<uint8_t,FxFreeDeleter> * dest_buf,uint32_t * dest_size)599 uint32_t FlateDecode(pdfium::span<const uint8_t> src_span,
600                      std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
601                      uint32_t* dest_size) {
602   return FlateModule::FlateOrLZWDecode(false, src_span, false, 0, 0, 0, 0, 0,
603                                        dest_buf, dest_size);
604 }
605