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