Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(248)

Side by Side Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp

Issue 1666663004: Add unit tests for ascii85 and hex decoders. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include <limits.h> 7 #include <limits.h>
8 8
9 #include "core/include/fpdfapi/fpdf_module.h" 9 #include "core/include/fpdfapi/fpdf_module.h"
10 #include "core/include/fpdfapi/fpdf_parser.h" 10 #include "core/include/fpdfapi/fpdf_parser.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, 0x00e8, 0x00e9, 42 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, 0x00e8, 0x00e9,
43 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, 0x00f0, 0x00f1, 0x00f2, 43 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, 0x00f0, 0x00f1, 0x00f2,
44 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x00fa, 0x00fb, 44 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x00fa, 0x00fb,
45 0x00fc, 0x00fd, 0x00fe, 0x00ff}; 45 0x00fc, 0x00fd, 0x00fe, 0x00ff};
46 46
47 FX_DWORD A85Decode(const uint8_t* src_buf, 47 FX_DWORD A85Decode(const uint8_t* src_buf,
48 FX_DWORD src_size, 48 FX_DWORD src_size,
49 uint8_t*& dest_buf, 49 uint8_t*& dest_buf,
50 FX_DWORD& dest_size) { 50 FX_DWORD& dest_size) {
51 dest_size = 0; 51 dest_size = 0;
52 dest_buf = NULL; 52 dest_buf = nullptr;
53 if (src_size == 0) { 53 if (src_size == 0)
54 return 0; 54 return 0;
55 } 55
56 // Count legal characters and zeros.
56 FX_DWORD zcount = 0; 57 FX_DWORD zcount = 0;
57 FX_DWORD pos = 0; 58 FX_DWORD pos = 0;
58 while (pos < src_size) { 59 while (pos < src_size) {
59 uint8_t ch = src_buf[pos]; 60 uint8_t ch = src_buf[pos];
60 if (ch < '!' && ch != '\n' && ch != '\r' && ch != ' ' && ch != '\t') {
61 break;
62 }
63 if (ch == 'z') { 61 if (ch == 'z') {
64 zcount++; 62 zcount++;
65 } else if (ch > 'u') { 63 } else if ((ch < '!' || ch > 'u') && !PDFCharIsLineEnding(ch) &&
64 ch != ' ' && ch != '\t') {
66 break; 65 break;
67 } 66 }
68 pos++; 67 pos++;
69 } 68 }
70 if (pos == 0) { 69 // No content to decode.
70 if (pos == 0)
71 return 0; 71 return 0;
72 } 72
73 if (zcount > UINT_MAX / 4) {
Lei Zhang 2016/02/05 01:25:33 Why remove this?
Wei Li 2016/02/06 00:11:44 This one is redundant as the next statement should
Lei Zhang 2016/02/06 00:17:04 Sorry, it's not. If |zcount| is very large, zcount
Wei Li 2016/02/06 01:16:26 In that case, this should fix it.
74 return (FX_DWORD)-1;
75 }
76 if (zcount * 4 > UINT_MAX - (pos - zcount)) { 73 if (zcount * 4 > UINT_MAX - (pos - zcount)) {
77 return (FX_DWORD)-1; 74 return (FX_DWORD)-1;
78 } 75 }
79 dest_buf = FX_Alloc(uint8_t, zcount * 4 + (pos - zcount)); 76 dest_buf = FX_Alloc(uint8_t, zcount * 4 + (pos - zcount));
80 int state = 0; 77 size_t state = 0;
81 uint32_t res = 0; 78 uint32_t res = 0;
82 pos = dest_size = 0; 79 pos = dest_size = 0;
83 while (pos < src_size) { 80 while (pos < src_size) {
84 uint8_t ch = src_buf[pos++]; 81 uint8_t ch = src_buf[pos++];
85 if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t') 82 if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t')
86 continue; 83 continue;
87 84
88 if (ch == 'z') { 85 if (ch == 'z') {
89 FXSYS_memset(dest_buf + dest_size, 0, 4); 86 FXSYS_memset(dest_buf + dest_size, 0, 4);
90 state = 0; 87 state = 0;
91 res = 0; 88 res = 0;
92 dest_size += 4; 89 dest_size += 4;
93 } else { 90 } else if (ch >= '!' && ch <= 'u') {
94 if (ch < '!' || ch > 'u') {
95 break;
96 }
97 res = res * 85 + ch - 33; 91 res = res * 85 + ch - 33;
98 state++; 92 state++;
99 if (state == 5) { 93 if (state == 5) {
100 for (int i = 0; i < 4; i++) { 94 for (size_t i = 0; i < 4; i++) {
101 dest_buf[dest_size++] = (uint8_t)(res >> (3 - i) * 8); 95 dest_buf[dest_size++] = (uint8_t)(res >> (3 - i) * 8);
102 } 96 }
103 state = 0; 97 state = 0;
104 res = 0; 98 res = 0;
105 } 99 }
100 } else {
101 // The end or illegal character.
102 break;
106 } 103 }
107 } 104 }
105 // Handle partial group.
108 if (state) { 106 if (state) {
109 int i; 107 for (size_t i = state; i < 5; i++)
110 for (i = state; i < 5; i++) {
111 res = res * 85 + 84; 108 res = res * 85 + 84;
112 } 109 for (size_t i = 0; i < state - 1; i++)
113 for (i = 0; i < state - 1; i++) {
114 dest_buf[dest_size++] = (uint8_t)(res >> (3 - i) * 8); 110 dest_buf[dest_size++] = (uint8_t)(res >> (3 - i) * 8);
115 }
116 } 111 }
117 if (pos < src_size && src_buf[pos] == '>') { 112 if (pos < src_size && src_buf[pos] == '>')
118 pos++; 113 pos++;
119 }
120 return pos; 114 return pos;
121 } 115 }
116
122 FX_DWORD HexDecode(const uint8_t* src_buf, 117 FX_DWORD HexDecode(const uint8_t* src_buf,
123 FX_DWORD src_size, 118 FX_DWORD src_size,
124 uint8_t*& dest_buf, 119 uint8_t*& dest_buf,
125 FX_DWORD& dest_size) { 120 FX_DWORD& dest_size) {
126 FX_DWORD i; 121 dest_size = 0;
127 for (i = 0; i < src_size; i++) 122 dest_buf = nullptr;
Lei Zhang 2016/02/05 01:25:33 Move this line into the 0 src_size handler?
Wei Li 2016/02/06 00:11:44 Done.
128 if (src_buf[i] == '>') { 123 if (src_size == 0)
129 break; 124 return 0;
130 } 125
126 FX_DWORD i = 0;
127 // Find the end of data.
128 while (i < src_size && src_buf[i++] != '>') {
Lei Zhang 2016/02/05 01:25:33 Is |i| going to be incremented by 1 more in the ne
Wei Li 2016/02/06 00:11:44 Done.
129 }
130
131 dest_buf = FX_Alloc(uint8_t, i / 2 + 1); 131 dest_buf = FX_Alloc(uint8_t, i / 2 + 1);
132 dest_size = 0;
133 bool bFirst = true; 132 bool bFirst = true;
134 for (i = 0; i < src_size; i++) { 133 for (i = 0; i < src_size; i++) {
135 uint8_t ch = src_buf[i]; 134 uint8_t ch = src_buf[i];
136 if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t') 135 if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t')
137 continue; 136 continue;
138 137
139 if (ch == '>') { 138 if (ch == '>') {
140 ++i; 139 ++i;
141 break; 140 break;
142 } 141 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 } else { 210 } else {
212 break; 211 break;
213 } 212 }
214 } 213 }
215 FX_DWORD ret = i + 1; 214 FX_DWORD ret = i + 1;
216 if (ret > src_size) { 215 if (ret > src_size) {
217 ret = src_size; 216 ret = src_size;
218 } 217 }
219 return ret; 218 return ret;
220 } 219 }
220
221 ICodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder( 221 ICodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder(
222 const uint8_t* src_buf, 222 const uint8_t* src_buf,
223 FX_DWORD src_size, 223 FX_DWORD src_size,
224 int width, 224 int width,
225 int height, 225 int height,
226 const CPDF_Dictionary* pParams) { 226 const CPDF_Dictionary* pParams) {
227 int K = 0; 227 int K = 0;
228 FX_BOOL EndOfLine = FALSE; 228 FX_BOOL EndOfLine = FALSE;
229 FX_BOOL ByteAlign = FALSE; 229 FX_BOOL ByteAlign = FALSE;
230 FX_BOOL BlackIs1 = FALSE; 230 FX_BOOL BlackIs1 = FALSE;
231 int Columns = 1728; 231 int Columns = 1728;
232 int Rows = 0; 232 int Rows = 0;
233 if (pParams) { 233 if (pParams) {
234 K = pParams->GetIntegerBy("K"); 234 K = pParams->GetIntegerBy("K");
235 EndOfLine = pParams->GetIntegerBy("EndOfLine"); 235 EndOfLine = pParams->GetIntegerBy("EndOfLine");
236 ByteAlign = pParams->GetIntegerBy("EncodedByteAlign"); 236 ByteAlign = pParams->GetIntegerBy("EncodedByteAlign");
237 BlackIs1 = pParams->GetIntegerBy("BlackIs1"); 237 BlackIs1 = pParams->GetIntegerBy("BlackIs1");
238 Columns = pParams->GetIntegerBy("Columns", 1728); 238 Columns = pParams->GetIntegerBy("Columns", 1728);
239 Rows = pParams->GetIntegerBy("Rows"); 239 Rows = pParams->GetIntegerBy("Rows");
240 if (Rows > USHRT_MAX) { 240 if (Rows > USHRT_MAX) {
241 Rows = 0; 241 Rows = 0;
242 } 242 }
243 if (Columns <= 0 || Rows < 0 || Columns > USHRT_MAX || Rows > USHRT_MAX) { 243 if (Columns <= 0 || Rows < 0 || Columns > USHRT_MAX || Rows > USHRT_MAX) {
244 return NULL; 244 return nullptr;
245 } 245 }
246 } 246 }
247 return CPDF_ModuleMgr::Get()->GetFaxModule()->CreateDecoder( 247 return CPDF_ModuleMgr::Get()->GetFaxModule()->CreateDecoder(
248 src_buf, src_size, width, height, K, EndOfLine, ByteAlign, BlackIs1, 248 src_buf, src_size, width, height, K, EndOfLine, ByteAlign, BlackIs1,
249 Columns, Rows); 249 Columns, Rows);
250 } 250 }
251
251 static FX_BOOL CheckFlateDecodeParams(int Colors, 252 static FX_BOOL CheckFlateDecodeParams(int Colors,
252 int BitsPerComponent, 253 int BitsPerComponent,
253 int Columns) { 254 int Columns) {
254 if (Columns < 0) { 255 if (Columns < 0) {
255 return FALSE; 256 return FALSE;
256 } 257 }
257 int check = Columns; 258 int check = Columns;
258 if (Colors < 0 || (check > 0 && Colors > INT_MAX / check)) { 259 if (Colors < 0 || (check > 0 && Colors > INT_MAX / check)) {
259 return FALSE; 260 return FALSE;
260 } 261 }
261 check *= Colors; 262 check *= Colors;
262 if (BitsPerComponent < 0 || 263 if (BitsPerComponent < 0 ||
263 (check > 0 && BitsPerComponent > INT_MAX / check)) { 264 (check > 0 && BitsPerComponent > INT_MAX / check)) {
264 return FALSE; 265 return FALSE;
265 } 266 }
266 check *= BitsPerComponent; 267 check *= BitsPerComponent;
267 if (check > INT_MAX - 7) { 268 if (check > INT_MAX - 7) {
268 return FALSE; 269 return FALSE;
269 } 270 }
270 return TRUE; 271 return TRUE;
271 } 272 }
273
272 ICodec_ScanlineDecoder* FPDFAPI_CreateFlateDecoder( 274 ICodec_ScanlineDecoder* FPDFAPI_CreateFlateDecoder(
273 const uint8_t* src_buf, 275 const uint8_t* src_buf,
274 FX_DWORD src_size, 276 FX_DWORD src_size,
275 int width, 277 int width,
276 int height, 278 int height,
277 int nComps, 279 int nComps,
278 int bpc, 280 int bpc,
279 const CPDF_Dictionary* pParams) { 281 const CPDF_Dictionary* pParams) {
280 int predictor = 0; 282 int predictor = 0;
281 int Colors = 0, BitsPerComponent = 0, Columns = 0; 283 int Colors = 0, BitsPerComponent = 0, Columns = 0;
282 if (pParams) { 284 if (pParams) {
283 predictor = pParams->GetIntegerBy("Predictor"); 285 predictor = pParams->GetIntegerBy("Predictor");
284 Colors = pParams->GetIntegerBy("Colors", 1); 286 Colors = pParams->GetIntegerBy("Colors", 1);
285 BitsPerComponent = pParams->GetIntegerBy("BitsPerComponent", 8); 287 BitsPerComponent = pParams->GetIntegerBy("BitsPerComponent", 8);
286 Columns = pParams->GetIntegerBy("Columns", 1); 288 Columns = pParams->GetIntegerBy("Columns", 1);
287 if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) { 289 if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) {
288 return NULL; 290 return nullptr;
289 } 291 }
290 } 292 }
291 return CPDF_ModuleMgr::Get()->GetFlateModule()->CreateDecoder( 293 return CPDF_ModuleMgr::Get()->GetFlateModule()->CreateDecoder(
292 src_buf, src_size, width, height, nComps, bpc, predictor, Colors, 294 src_buf, src_size, width, height, nComps, bpc, predictor, Colors,
293 BitsPerComponent, Columns); 295 BitsPerComponent, Columns);
294 } 296 }
297
295 FX_DWORD FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW, 298 FX_DWORD FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW,
296 const uint8_t* src_buf, 299 const uint8_t* src_buf,
297 FX_DWORD src_size, 300 FX_DWORD src_size,
298 CPDF_Dictionary* pParams, 301 CPDF_Dictionary* pParams,
299 FX_DWORD estimated_size, 302 FX_DWORD estimated_size,
300 uint8_t*& dest_buf, 303 uint8_t*& dest_buf,
301 FX_DWORD& dest_size) { 304 FX_DWORD& dest_size) {
302 int predictor = 0; 305 int predictor = 0;
303 FX_BOOL bEarlyChange = TRUE; 306 FX_BOOL bEarlyChange = TRUE;
304 int Colors = 0, BitsPerComponent = 0, Columns = 0; 307 int Colors = 0, BitsPerComponent = 0, Columns = 0;
305 if (pParams) { 308 if (pParams) {
306 predictor = pParams->GetIntegerBy("Predictor"); 309 predictor = pParams->GetIntegerBy("Predictor");
307 bEarlyChange = pParams->GetIntegerBy("EarlyChange", 1); 310 bEarlyChange = pParams->GetIntegerBy("EarlyChange", 1);
308 Colors = pParams->GetIntegerBy("Colors", 1); 311 Colors = pParams->GetIntegerBy("Colors", 1);
309 BitsPerComponent = pParams->GetIntegerBy("BitsPerComponent", 8); 312 BitsPerComponent = pParams->GetIntegerBy("BitsPerComponent", 8);
310 Columns = pParams->GetIntegerBy("Columns", 1); 313 Columns = pParams->GetIntegerBy("Columns", 1);
311 if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) { 314 if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) {
312 return (FX_DWORD)-1; 315 return (FX_DWORD)-1;
313 } 316 }
314 } 317 }
315 return CPDF_ModuleMgr::Get()->GetFlateModule()->FlateOrLZWDecode( 318 return CPDF_ModuleMgr::Get()->GetFlateModule()->FlateOrLZWDecode(
316 bLZW, src_buf, src_size, bEarlyChange, predictor, Colors, 319 bLZW, src_buf, src_size, bEarlyChange, predictor, Colors,
317 BitsPerComponent, Columns, estimated_size, dest_buf, dest_size); 320 BitsPerComponent, Columns, estimated_size, dest_buf, dest_size);
318 } 321 }
322
319 FX_BOOL PDF_DataDecode(const uint8_t* src_buf, 323 FX_BOOL PDF_DataDecode(const uint8_t* src_buf,
320 FX_DWORD src_size, 324 FX_DWORD src_size,
321 const CPDF_Dictionary* pDict, 325 const CPDF_Dictionary* pDict,
322 uint8_t*& dest_buf, 326 uint8_t*& dest_buf,
323 FX_DWORD& dest_size, 327 FX_DWORD& dest_size,
324 CFX_ByteString& ImageEncoding, 328 CFX_ByteString& ImageEncoding,
325 CPDF_Dictionary*& pImageParms, 329 CPDF_Dictionary*& pImageParms,
326 FX_DWORD last_estimated_size, 330 FX_DWORD last_estimated_size,
327 FX_BOOL bImageAcc) { 331 FX_BOOL bImageAcc) {
328 CPDF_Object* pDecoder = pDict ? pDict->GetElementValue("Filter") : nullptr; 332 CPDF_Object* pDecoder = pDict ? pDict->GetElementValue("Filter") : nullptr;
(...skipping 17 matching lines...) Expand all
346 } else { 350 } else {
347 DecoderList.Add(pDecoder->GetConstString()); 351 DecoderList.Add(pDecoder->GetConstString());
348 ParamList.Add(pParams ? pParams->GetDict() : nullptr); 352 ParamList.Add(pParams ? pParams->GetDict() : nullptr);
349 } 353 }
350 uint8_t* last_buf = (uint8_t*)src_buf; 354 uint8_t* last_buf = (uint8_t*)src_buf;
351 FX_DWORD last_size = src_size; 355 FX_DWORD last_size = src_size;
352 for (int i = 0; i < DecoderList.GetSize(); i++) { 356 for (int i = 0; i < DecoderList.GetSize(); i++) {
353 int estimated_size = 357 int estimated_size =
354 i == DecoderList.GetSize() - 1 ? last_estimated_size : 0; 358 i == DecoderList.GetSize() - 1 ? last_estimated_size : 0;
355 CFX_ByteString decoder = DecoderList[i]; 359 CFX_ByteString decoder = DecoderList[i];
356 // Use ToDictionary here because we can push NULL into the ParamList. 360 // Use ToDictionary here because we can push nullptr into the ParamList.
357 CPDF_Dictionary* pParam = ToDictionary(ParamList[i]); 361 CPDF_Dictionary* pParam = ToDictionary(ParamList[i]);
358 uint8_t* new_buf = nullptr; 362 uint8_t* new_buf = nullptr;
359 FX_DWORD new_size = (FX_DWORD)-1; 363 FX_DWORD new_size = (FX_DWORD)-1;
360 int offset = -1; 364 int offset = -1;
361 if (decoder == "FlateDecode" || decoder == "Fl") { 365 if (decoder == "FlateDecode" || decoder == "Fl") {
362 if (bImageAcc && i == DecoderList.GetSize() - 1) { 366 if (bImageAcc && i == DecoderList.GetSize() - 1) {
363 ImageEncoding = "FlateDecode"; 367 ImageEncoding = "FlateDecode";
364 dest_buf = (uint8_t*)last_buf; 368 dest_buf = (uint8_t*)last_buf;
365 dest_size = last_size; 369 dest_size = last_size;
366 pImageParms = pParam; 370 pImageParms = pParam;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 } 414 }
411 last_buf = new_buf; 415 last_buf = new_buf;
412 last_size = new_size; 416 last_size = new_size;
413 } 417 }
414 ImageEncoding = ""; 418 ImageEncoding = "";
415 pImageParms = nullptr; 419 pImageParms = nullptr;
416 dest_buf = last_buf; 420 dest_buf = last_buf;
417 dest_size = last_size; 421 dest_size = last_size;
418 return TRUE; 422 return TRUE;
419 } 423 }
424
420 CFX_WideString PDF_DecodeText(const uint8_t* src_data, 425 CFX_WideString PDF_DecodeText(const uint8_t* src_data,
421 FX_DWORD src_len, 426 FX_DWORD src_len,
422 CFX_CharMap* pCharMap) { 427 CFX_CharMap* pCharMap) {
423 CFX_WideString result; 428 CFX_WideString result;
424 if (src_len >= 2 && ((src_data[0] == 0xfe && src_data[1] == 0xff) || 429 if (src_len >= 2 && ((src_data[0] == 0xfe && src_data[1] == 0xff) ||
425 (src_data[0] == 0xff && src_data[1] == 0xfe))) { 430 (src_data[0] == 0xff && src_data[1] == 0xfe))) {
426 FX_BOOL bBE = src_data[0] == 0xfe; 431 FX_BOOL bBE = src_data[0] == 0xfe;
427 FX_DWORD max_chars = (src_len - 2) / 2; 432 FX_DWORD max_chars = (src_len - 2) / 2;
428 if (!max_chars) { 433 if (!max_chars) {
429 return result; 434 return result;
(...skipping 27 matching lines...) Expand all
457 for (FX_DWORD i = 0; i < src_len; i++) { 462 for (FX_DWORD i = 0; i < src_len; i++) {
458 dest_buf[i] = PDFDocEncoding[src_data[i]]; 463 dest_buf[i] = PDFDocEncoding[src_data[i]];
459 } 464 }
460 result.ReleaseBuffer(src_len); 465 result.ReleaseBuffer(src_len);
461 } else { 466 } else {
462 return (*pCharMap->m_GetWideString)( 467 return (*pCharMap->m_GetWideString)(
463 pCharMap, CFX_ByteString((const FX_CHAR*)src_data, src_len)); 468 pCharMap, CFX_ByteString((const FX_CHAR*)src_data, src_len));
464 } 469 }
465 return result; 470 return result;
466 } 471 }
472
467 CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString, 473 CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString,
468 int len, 474 int len,
469 CFX_CharMap* pCharMap) { 475 CFX_CharMap* pCharMap) {
470 if (len == -1) { 476 if (len == -1) {
471 len = FXSYS_wcslen(pString); 477 len = FXSYS_wcslen(pString);
472 } 478 }
473 CFX_ByteString result; 479 CFX_ByteString result;
474 if (!pCharMap) { 480 if (!pCharMap) {
475 FX_CHAR* dest_buf1 = result.GetBuffer(len); 481 FX_CHAR* dest_buf1 = result.GetBuffer(len);
476 int i; 482 int i;
(...skipping 25 matching lines...) Expand all
502 dest_buf2[0] = 0xfe; 508 dest_buf2[0] = 0xfe;
503 dest_buf2[1] = 0xff; 509 dest_buf2[1] = 0xff;
504 dest_buf2 += 2; 510 dest_buf2 += 2;
505 for (int i = 0; i < len; i++) { 511 for (int i = 0; i < len; i++) {
506 *dest_buf2++ = pString[i] >> 8; 512 *dest_buf2++ = pString[i] >> 8;
507 *dest_buf2++ = (uint8_t)pString[i]; 513 *dest_buf2++ = (uint8_t)pString[i];
508 } 514 }
509 result.ReleaseBuffer(encLen); 515 result.ReleaseBuffer(encLen);
510 return result; 516 return result;
511 } 517 }
518
512 CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex) { 519 CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex) {
513 CFX_ByteTextBuf result; 520 CFX_ByteTextBuf result;
514 int srclen = src.GetLength(); 521 int srclen = src.GetLength();
515 if (bHex) { 522 if (bHex) {
516 result.AppendChar('<'); 523 result.AppendChar('<');
517 for (int i = 0; i < srclen; i++) { 524 for (int i = 0; i < srclen; i++) {
518 result.AppendChar("0123456789ABCDEF"[src[i] / 16]); 525 result.AppendChar("0123456789ABCDEF"[src[i] / 16]);
519 result.AppendChar("0123456789ABCDEF"[src[i] % 16]); 526 result.AppendChar("0123456789ABCDEF"[src[i] % 16]);
520 } 527 }
521 result.AppendChar('>'); 528 result.AppendChar('>');
522 return result.GetByteString(); 529 return result.GetByteString();
523 } 530 }
524 result.AppendChar('('); 531 result.AppendChar('(');
525 for (int i = 0; i < srclen; i++) { 532 for (int i = 0; i < srclen; i++) {
526 uint8_t ch = src[i]; 533 uint8_t ch = src[i];
527 if (ch == ')' || ch == '\\' || ch == '(') { 534 if (ch == ')' || ch == '\\' || ch == '(') {
528 result.AppendChar('\\'); 535 result.AppendChar('\\');
529 } else if (ch == 0x0a) { 536 } else if (ch == 0x0a) {
530 result << "\\n"; 537 result << "\\n";
531 continue; 538 continue;
532 } else if (ch == 0x0d) { 539 } else if (ch == 0x0d) {
533 result << "\\r"; 540 result << "\\r";
534 continue; 541 continue;
535 } 542 }
536 result.AppendChar(ch); 543 result.AppendChar(ch);
537 } 544 }
538 result.AppendChar(')'); 545 result.AppendChar(')');
539 return result.GetByteString(); 546 return result.GetByteString();
540 } 547 }
548
541 void FlateEncode(const uint8_t* src_buf, 549 void FlateEncode(const uint8_t* src_buf,
542 FX_DWORD src_size, 550 FX_DWORD src_size,
543 uint8_t*& dest_buf, 551 uint8_t*& dest_buf,
544 FX_DWORD& dest_size) { 552 FX_DWORD& dest_size) {
545 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); 553 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule();
546 if (pEncoders) { 554 if (pEncoders) {
547 pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf, dest_size); 555 pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf, dest_size);
548 } 556 }
549 } 557 }
558
550 void FlateEncode(const uint8_t* src_buf, 559 void FlateEncode(const uint8_t* src_buf,
551 FX_DWORD src_size, 560 FX_DWORD src_size,
552 int predictor, 561 int predictor,
553 int Colors, 562 int Colors,
554 int BitsPerComponent, 563 int BitsPerComponent,
555 int Columns, 564 int Columns,
556 uint8_t*& dest_buf, 565 uint8_t*& dest_buf,
557 FX_DWORD& dest_size) { 566 FX_DWORD& dest_size) {
558 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); 567 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule();
559 if (pEncoders) { 568 if (pEncoders) {
560 pEncoders->GetFlateModule()->Encode(src_buf, src_size, predictor, Colors, 569 pEncoders->GetFlateModule()->Encode(src_buf, src_size, predictor, Colors,
561 BitsPerComponent, Columns, dest_buf, 570 BitsPerComponent, Columns, dest_buf,
562 dest_size); 571 dest_size);
563 } 572 }
564 } 573 }
574
565 FX_DWORD FlateDecode(const uint8_t* src_buf, 575 FX_DWORD FlateDecode(const uint8_t* src_buf,
566 FX_DWORD src_size, 576 FX_DWORD src_size,
567 uint8_t*& dest_buf, 577 uint8_t*& dest_buf,
568 FX_DWORD& dest_size) { 578 FX_DWORD& dest_size) {
569 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); 579 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule();
570 if (pEncoders) { 580 if (pEncoders) {
571 return pEncoders->GetFlateModule()->FlateOrLZWDecode( 581 return pEncoders->GetFlateModule()->FlateOrLZWDecode(
572 FALSE, src_buf, src_size, FALSE, 0, 0, 0, 0, 0, dest_buf, dest_size); 582 FALSE, src_buf, src_size, FALSE, 0, 0, 0, 0, 0, dest_buf, dest_size);
573 } 583 }
574 return 0; 584 return 0;
575 } 585 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698