OLD | NEW |
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 "core/fxcodec/codec/codec_int.h" | 7 #include "core/fxcodec/codec/codec_int.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 inflateEnd((z_stream*)context); | 76 inflateEnd((z_stream*)context); |
77 ((z_stream*)context)->zfree(0, context); | 77 ((z_stream*)context)->zfree(0, context); |
78 } | 78 } |
79 } // extern "C" | 79 } // extern "C" |
80 | 80 |
81 namespace { | 81 namespace { |
82 | 82 |
83 class CLZWDecoder { | 83 class CLZWDecoder { |
84 public: | 84 public: |
85 int Decode(uint8_t* output, | 85 int Decode(uint8_t* output, |
86 FX_DWORD& outlen, | 86 uint32_t& outlen, |
87 const uint8_t* input, | 87 const uint8_t* input, |
88 FX_DWORD& size, | 88 uint32_t& size, |
89 FX_BOOL bEarlyChange); | 89 FX_BOOL bEarlyChange); |
90 | 90 |
91 private: | 91 private: |
92 void AddCode(FX_DWORD prefix_code, uint8_t append_char); | 92 void AddCode(uint32_t prefix_code, uint8_t append_char); |
93 void DecodeString(FX_DWORD code); | 93 void DecodeString(uint32_t code); |
94 | 94 |
95 FX_DWORD m_InPos; | 95 uint32_t m_InPos; |
96 FX_DWORD m_OutPos; | 96 uint32_t m_OutPos; |
97 uint8_t* m_pOutput; | 97 uint8_t* m_pOutput; |
98 const uint8_t* m_pInput; | 98 const uint8_t* m_pInput; |
99 FX_BOOL m_Early; | 99 FX_BOOL m_Early; |
100 FX_DWORD m_CodeArray[5021]; | 100 uint32_t m_CodeArray[5021]; |
101 FX_DWORD m_nCodes; | 101 uint32_t m_nCodes; |
102 uint8_t m_DecodeStack[4000]; | 102 uint8_t m_DecodeStack[4000]; |
103 FX_DWORD m_StackLen; | 103 uint32_t m_StackLen; |
104 int m_CodeLen; | 104 int m_CodeLen; |
105 }; | 105 }; |
106 void CLZWDecoder::AddCode(FX_DWORD prefix_code, uint8_t append_char) { | 106 void CLZWDecoder::AddCode(uint32_t prefix_code, uint8_t append_char) { |
107 if (m_nCodes + m_Early == 4094) { | 107 if (m_nCodes + m_Early == 4094) { |
108 return; | 108 return; |
109 } | 109 } |
110 m_CodeArray[m_nCodes++] = (prefix_code << 16) | append_char; | 110 m_CodeArray[m_nCodes++] = (prefix_code << 16) | append_char; |
111 if (m_nCodes + m_Early == 512 - 258) { | 111 if (m_nCodes + m_Early == 512 - 258) { |
112 m_CodeLen = 10; | 112 m_CodeLen = 10; |
113 } else if (m_nCodes + m_Early == 1024 - 258) { | 113 } else if (m_nCodes + m_Early == 1024 - 258) { |
114 m_CodeLen = 11; | 114 m_CodeLen = 11; |
115 } else if (m_nCodes + m_Early == 2048 - 258) { | 115 } else if (m_nCodes + m_Early == 2048 - 258) { |
116 m_CodeLen = 12; | 116 m_CodeLen = 12; |
117 } | 117 } |
118 } | 118 } |
119 void CLZWDecoder::DecodeString(FX_DWORD code) { | 119 void CLZWDecoder::DecodeString(uint32_t code) { |
120 while (1) { | 120 while (1) { |
121 int index = code - 258; | 121 int index = code - 258; |
122 if (index < 0 || index >= (int)m_nCodes) { | 122 if (index < 0 || index >= (int)m_nCodes) { |
123 break; | 123 break; |
124 } | 124 } |
125 FX_DWORD data = m_CodeArray[index]; | 125 uint32_t data = m_CodeArray[index]; |
126 if (m_StackLen >= sizeof(m_DecodeStack)) { | 126 if (m_StackLen >= sizeof(m_DecodeStack)) { |
127 return; | 127 return; |
128 } | 128 } |
129 m_DecodeStack[m_StackLen++] = (uint8_t)data; | 129 m_DecodeStack[m_StackLen++] = (uint8_t)data; |
130 code = data >> 16; | 130 code = data >> 16; |
131 } | 131 } |
132 if (m_StackLen >= sizeof(m_DecodeStack)) { | 132 if (m_StackLen >= sizeof(m_DecodeStack)) { |
133 return; | 133 return; |
134 } | 134 } |
135 m_DecodeStack[m_StackLen++] = (uint8_t)code; | 135 m_DecodeStack[m_StackLen++] = (uint8_t)code; |
136 } | 136 } |
137 int CLZWDecoder::Decode(uint8_t* dest_buf, | 137 int CLZWDecoder::Decode(uint8_t* dest_buf, |
138 FX_DWORD& dest_size, | 138 uint32_t& dest_size, |
139 const uint8_t* src_buf, | 139 const uint8_t* src_buf, |
140 FX_DWORD& src_size, | 140 uint32_t& src_size, |
141 FX_BOOL bEarlyChange) { | 141 FX_BOOL bEarlyChange) { |
142 m_CodeLen = 9; | 142 m_CodeLen = 9; |
143 m_InPos = 0; | 143 m_InPos = 0; |
144 m_OutPos = 0; | 144 m_OutPos = 0; |
145 m_pInput = src_buf; | 145 m_pInput = src_buf; |
146 m_pOutput = dest_buf; | 146 m_pOutput = dest_buf; |
147 m_Early = bEarlyChange ? 1 : 0; | 147 m_Early = bEarlyChange ? 1 : 0; |
148 m_nCodes = 0; | 148 m_nCodes = 0; |
149 FX_DWORD old_code = (FX_DWORD)-1; | 149 uint32_t old_code = (uint32_t)-1; |
150 uint8_t last_char = 0; | 150 uint8_t last_char = 0; |
151 while (1) { | 151 while (1) { |
152 if (m_InPos + m_CodeLen > src_size * 8) { | 152 if (m_InPos + m_CodeLen > src_size * 8) { |
153 break; | 153 break; |
154 } | 154 } |
155 int byte_pos = m_InPos / 8; | 155 int byte_pos = m_InPos / 8; |
156 int bit_pos = m_InPos % 8, bit_left = m_CodeLen; | 156 int bit_pos = m_InPos % 8, bit_left = m_CodeLen; |
157 FX_DWORD code = 0; | 157 uint32_t code = 0; |
158 if (bit_pos) { | 158 if (bit_pos) { |
159 bit_left -= 8 - bit_pos; | 159 bit_left -= 8 - bit_pos; |
160 code = (m_pInput[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left; | 160 code = (m_pInput[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left; |
161 } | 161 } |
162 if (bit_left < 8) { | 162 if (bit_left < 8) { |
163 code |= m_pInput[byte_pos] >> (8 - bit_left); | 163 code |= m_pInput[byte_pos] >> (8 - bit_left); |
164 } else { | 164 } else { |
165 bit_left -= 8; | 165 bit_left -= 8; |
166 code |= m_pInput[byte_pos++] << bit_left; | 166 code |= m_pInput[byte_pos++] << bit_left; |
167 if (bit_left) { | 167 if (bit_left) { |
168 code |= m_pInput[byte_pos] >> (8 - bit_left); | 168 code |= m_pInput[byte_pos] >> (8 - bit_left); |
169 } | 169 } |
170 } | 170 } |
171 m_InPos += m_CodeLen; | 171 m_InPos += m_CodeLen; |
172 if (code < 256) { | 172 if (code < 256) { |
173 if (m_OutPos == dest_size) { | 173 if (m_OutPos == dest_size) { |
174 return -5; | 174 return -5; |
175 } | 175 } |
176 if (m_pOutput) { | 176 if (m_pOutput) { |
177 m_pOutput[m_OutPos] = (uint8_t)code; | 177 m_pOutput[m_OutPos] = (uint8_t)code; |
178 } | 178 } |
179 m_OutPos++; | 179 m_OutPos++; |
180 last_char = (uint8_t)code; | 180 last_char = (uint8_t)code; |
181 if (old_code != (FX_DWORD)-1) { | 181 if (old_code != (uint32_t)-1) { |
182 AddCode(old_code, last_char); | 182 AddCode(old_code, last_char); |
183 } | 183 } |
184 old_code = code; | 184 old_code = code; |
185 } else if (code == 256) { | 185 } else if (code == 256) { |
186 m_CodeLen = 9; | 186 m_CodeLen = 9; |
187 m_nCodes = 0; | 187 m_nCodes = 0; |
188 old_code = (FX_DWORD)-1; | 188 old_code = (uint32_t)-1; |
189 } else if (code == 257) { | 189 } else if (code == 257) { |
190 break; | 190 break; |
191 } else { | 191 } else { |
192 if (old_code == (FX_DWORD)-1) { | 192 if (old_code == (uint32_t)-1) { |
193 return 2; | 193 return 2; |
194 } | 194 } |
195 m_StackLen = 0; | 195 m_StackLen = 0; |
196 if (code >= m_nCodes + 258) { | 196 if (code >= m_nCodes + 258) { |
197 if (m_StackLen < sizeof(m_DecodeStack)) { | 197 if (m_StackLen < sizeof(m_DecodeStack)) { |
198 m_DecodeStack[m_StackLen++] = last_char; | 198 m_DecodeStack[m_StackLen++] = last_char; |
199 } | 199 } |
200 DecodeString(old_code); | 200 DecodeString(old_code); |
201 } else { | 201 } else { |
202 DecodeString(code); | 202 DecodeString(code); |
203 } | 203 } |
204 if (m_OutPos + m_StackLen > dest_size) { | 204 if (m_OutPos + m_StackLen > dest_size) { |
205 return -5; | 205 return -5; |
206 } | 206 } |
207 if (m_pOutput) { | 207 if (m_pOutput) { |
208 for (FX_DWORD i = 0; i < m_StackLen; i++) { | 208 for (uint32_t i = 0; i < m_StackLen; i++) { |
209 m_pOutput[m_OutPos + i] = m_DecodeStack[m_StackLen - i - 1]; | 209 m_pOutput[m_OutPos + i] = m_DecodeStack[m_StackLen - i - 1]; |
210 } | 210 } |
211 } | 211 } |
212 m_OutPos += m_StackLen; | 212 m_OutPos += m_StackLen; |
213 last_char = m_DecodeStack[m_StackLen - 1]; | 213 last_char = m_DecodeStack[m_StackLen - 1]; |
214 if (old_code < 256) { | 214 if (old_code < 256) { |
215 AddCode(old_code, last_char); | 215 AddCode(old_code, last_char); |
216 } else if (old_code - 258 >= m_nCodes) { | 216 } else if (old_code - 258 >= m_nCodes) { |
217 dest_size = m_OutPos; | 217 dest_size = m_OutPos; |
218 src_size = (m_InPos + 7) / 8; | 218 src_size = (m_InPos + 7) / 8; |
(...skipping 17 matching lines...) Expand all Loading... |
236 if (pa <= pb && pa <= pc) { | 236 if (pa <= pb && pa <= pc) { |
237 return (uint8_t)a; | 237 return (uint8_t)a; |
238 } | 238 } |
239 if (pb <= pc) { | 239 if (pb <= pc) { |
240 return (uint8_t)b; | 240 return (uint8_t)b; |
241 } | 241 } |
242 return (uint8_t)c; | 242 return (uint8_t)c; |
243 } | 243 } |
244 | 244 |
245 FX_BOOL PNG_PredictorEncode(uint8_t*& data_buf, | 245 FX_BOOL PNG_PredictorEncode(uint8_t*& data_buf, |
246 FX_DWORD& data_size, | 246 uint32_t& data_size, |
247 int predictor, | 247 int predictor, |
248 int Colors, | 248 int Colors, |
249 int BitsPerComponent, | 249 int BitsPerComponent, |
250 int Columns) { | 250 int Columns) { |
251 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; | 251 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; |
252 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 252 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
253 if (row_size <= 0) | 253 if (row_size <= 0) |
254 return FALSE; | 254 return FALSE; |
255 const int row_count = (data_size + row_size - 1) / row_size; | 255 const int row_count = (data_size + row_size - 1) / row_size; |
256 const int last_row_size = data_size % row_size; | 256 const int last_row_size = data_size % row_size; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 break; | 393 break; |
394 } | 394 } |
395 default: | 395 default: |
396 pDestData[byte] = raw_byte; | 396 pDestData[byte] = raw_byte; |
397 break; | 397 break; |
398 } | 398 } |
399 } | 399 } |
400 } | 400 } |
401 | 401 |
402 FX_BOOL PNG_Predictor(uint8_t*& data_buf, | 402 FX_BOOL PNG_Predictor(uint8_t*& data_buf, |
403 FX_DWORD& data_size, | 403 uint32_t& data_size, |
404 int Colors, | 404 int Colors, |
405 int BitsPerComponent, | 405 int BitsPerComponent, |
406 int Columns) { | 406 int Columns) { |
407 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; | 407 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; |
408 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 408 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
409 if (row_size <= 0) | 409 if (row_size <= 0) |
410 return FALSE; | 410 return FALSE; |
411 const int row_count = (data_size + row_size) / (row_size + 1); | 411 const int row_count = (data_size + row_size) / (row_size + 1); |
412 if (row_count <= 0) | 412 if (row_count <= 0) |
413 return FALSE; | 413 return FALSE; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 uint16_t pixel = (dest_buf[i] << 8) | dest_buf[i + 1]; | 533 uint16_t pixel = (dest_buf[i] << 8) | dest_buf[i + 1]; |
534 pixel -= | 534 pixel -= |
535 (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1]; | 535 (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1]; |
536 dest_buf[i] = pixel >> 8; | 536 dest_buf[i] = pixel >> 8; |
537 dest_buf[i + 1] = (uint8_t)pixel; | 537 dest_buf[i + 1] = (uint8_t)pixel; |
538 } | 538 } |
539 } | 539 } |
540 } | 540 } |
541 | 541 |
542 FX_BOOL TIFF_PredictorEncode(uint8_t*& data_buf, | 542 FX_BOOL TIFF_PredictorEncode(uint8_t*& data_buf, |
543 FX_DWORD& data_size, | 543 uint32_t& data_size, |
544 int Colors, | 544 int Colors, |
545 int BitsPerComponent, | 545 int BitsPerComponent, |
546 int Columns) { | 546 int Columns) { |
547 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 547 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
548 if (row_size == 0) | 548 if (row_size == 0) |
549 return FALSE; | 549 return FALSE; |
550 const int row_count = (data_size + row_size - 1) / row_size; | 550 const int row_count = (data_size + row_size - 1) / row_size; |
551 const int last_row_size = data_size % row_size; | 551 const int last_row_size = data_size % row_size; |
552 for (int row = 0; row < row_count; row++) { | 552 for (int row = 0; row < row_count; row++) { |
553 uint8_t* scan_line = data_buf + row * row_size; | 553 uint8_t* scan_line = data_buf + row * row_size; |
554 if ((row + 1) * row_size > (int)data_size) { | 554 if ((row + 1) * row_size > (int)data_size) { |
555 row_size = last_row_size; | 555 row_size = last_row_size; |
556 } | 556 } |
557 TIFF_PredictorEncodeLine(scan_line, row_size, BitsPerComponent, Colors, | 557 TIFF_PredictorEncodeLine(scan_line, row_size, BitsPerComponent, Colors, |
558 Columns); | 558 Columns); |
559 } | 559 } |
560 return TRUE; | 560 return TRUE; |
561 } | 561 } |
562 | 562 |
563 void TIFF_PredictLine(uint8_t* dest_buf, | 563 void TIFF_PredictLine(uint8_t* dest_buf, |
564 FX_DWORD row_size, | 564 uint32_t row_size, |
565 int BitsPerComponent, | 565 int BitsPerComponent, |
566 int Colors, | 566 int Colors, |
567 int Columns) { | 567 int Columns) { |
568 if (BitsPerComponent == 1) { | 568 if (BitsPerComponent == 1) { |
569 int row_bits = std::min(BitsPerComponent * Colors * Columns, | 569 int row_bits = std::min(BitsPerComponent * Colors * Columns, |
570 pdfium::base::checked_cast<int>(row_size * 8)); | 570 pdfium::base::checked_cast<int>(row_size * 8)); |
571 int index_pre = 0; | 571 int index_pre = 0; |
572 int col_pre = 0; | 572 int col_pre = 0; |
573 for (int i = 1; i < row_bits; i++) { | 573 for (int i = 1; i < row_bits; i++) { |
574 int col = i % 8; | 574 int col = i % 8; |
575 int index = i / 8; | 575 int index = i / 8; |
576 if (((dest_buf[index] >> (7 - col)) & 1) ^ | 576 if (((dest_buf[index] >> (7 - col)) & 1) ^ |
577 ((dest_buf[index_pre] >> (7 - col_pre)) & 1)) { | 577 ((dest_buf[index_pre] >> (7 - col_pre)) & 1)) { |
578 dest_buf[index] |= 1 << (7 - col); | 578 dest_buf[index] |= 1 << (7 - col); |
579 } else { | 579 } else { |
580 dest_buf[index] &= ~(1 << (7 - col)); | 580 dest_buf[index] &= ~(1 << (7 - col)); |
581 } | 581 } |
582 index_pre = index; | 582 index_pre = index; |
583 col_pre = col; | 583 col_pre = col; |
584 } | 584 } |
585 return; | 585 return; |
586 } | 586 } |
587 int BytesPerPixel = BitsPerComponent * Colors / 8; | 587 int BytesPerPixel = BitsPerComponent * Colors / 8; |
588 if (BitsPerComponent == 16) { | 588 if (BitsPerComponent == 16) { |
589 for (FX_DWORD i = BytesPerPixel; i < row_size; i += 2) { | 589 for (uint32_t i = BytesPerPixel; i < row_size; i += 2) { |
590 uint16_t pixel = | 590 uint16_t pixel = |
591 (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1]; | 591 (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1]; |
592 pixel += (dest_buf[i] << 8) | dest_buf[i + 1]; | 592 pixel += (dest_buf[i] << 8) | dest_buf[i + 1]; |
593 dest_buf[i] = pixel >> 8; | 593 dest_buf[i] = pixel >> 8; |
594 dest_buf[i + 1] = (uint8_t)pixel; | 594 dest_buf[i + 1] = (uint8_t)pixel; |
595 } | 595 } |
596 } else { | 596 } else { |
597 for (FX_DWORD i = BytesPerPixel; i < row_size; i++) { | 597 for (uint32_t i = BytesPerPixel; i < row_size; i++) { |
598 dest_buf[i] += dest_buf[i - BytesPerPixel]; | 598 dest_buf[i] += dest_buf[i - BytesPerPixel]; |
599 } | 599 } |
600 } | 600 } |
601 } | 601 } |
602 | 602 |
603 FX_BOOL TIFF_Predictor(uint8_t*& data_buf, | 603 FX_BOOL TIFF_Predictor(uint8_t*& data_buf, |
604 FX_DWORD& data_size, | 604 uint32_t& data_size, |
605 int Colors, | 605 int Colors, |
606 int BitsPerComponent, | 606 int BitsPerComponent, |
607 int Columns) { | 607 int Columns) { |
608 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 608 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
609 if (row_size == 0) | 609 if (row_size == 0) |
610 return FALSE; | 610 return FALSE; |
611 const int row_count = (data_size + row_size - 1) / row_size; | 611 const int row_count = (data_size + row_size - 1) / row_size; |
612 const int last_row_size = data_size % row_size; | 612 const int last_row_size = data_size % row_size; |
613 for (int row = 0; row < row_count; row++) { | 613 for (int row = 0; row < row_count; row++) { |
614 uint8_t* scan_line = data_buf + row * row_size; | 614 uint8_t* scan_line = data_buf + row * row_size; |
615 if ((row + 1) * row_size > (int)data_size) { | 615 if ((row + 1) * row_size > (int)data_size) { |
616 row_size = last_row_size; | 616 row_size = last_row_size; |
617 } | 617 } |
618 TIFF_PredictLine(scan_line, row_size, BitsPerComponent, Colors, Columns); | 618 TIFF_PredictLine(scan_line, row_size, BitsPerComponent, Colors, Columns); |
619 } | 619 } |
620 return TRUE; | 620 return TRUE; |
621 } | 621 } |
622 | 622 |
623 void FlateUncompress(const uint8_t* src_buf, | 623 void FlateUncompress(const uint8_t* src_buf, |
624 FX_DWORD src_size, | 624 uint32_t src_size, |
625 FX_DWORD orig_size, | 625 uint32_t orig_size, |
626 uint8_t*& dest_buf, | 626 uint8_t*& dest_buf, |
627 FX_DWORD& dest_size, | 627 uint32_t& dest_size, |
628 FX_DWORD& offset) { | 628 uint32_t& offset) { |
629 FX_DWORD guess_size = orig_size ? orig_size : src_size * 2; | 629 uint32_t guess_size = orig_size ? orig_size : src_size * 2; |
630 const FX_DWORD kStepSize = 10240; | 630 const uint32_t kStepSize = 10240; |
631 FX_DWORD alloc_step = orig_size ? kStepSize : std::min(src_size, kStepSize); | 631 uint32_t alloc_step = orig_size ? kStepSize : std::min(src_size, kStepSize); |
632 static const FX_DWORD kMaxInitialAllocSize = 10000000; | 632 static const uint32_t kMaxInitialAllocSize = 10000000; |
633 if (guess_size > kMaxInitialAllocSize) { | 633 if (guess_size > kMaxInitialAllocSize) { |
634 guess_size = kMaxInitialAllocSize; | 634 guess_size = kMaxInitialAllocSize; |
635 alloc_step = kMaxInitialAllocSize; | 635 alloc_step = kMaxInitialAllocSize; |
636 } | 636 } |
637 FX_DWORD buf_size = guess_size; | 637 uint32_t buf_size = guess_size; |
638 FX_DWORD last_buf_size = buf_size; | 638 uint32_t last_buf_size = buf_size; |
639 | 639 |
640 dest_buf = nullptr; | 640 dest_buf = nullptr; |
641 dest_size = 0; | 641 dest_size = 0; |
642 void* context = FPDFAPI_FlateInit(my_alloc_func, my_free_func); | 642 void* context = FPDFAPI_FlateInit(my_alloc_func, my_free_func); |
643 if (!context) | 643 if (!context) |
644 return; | 644 return; |
645 | 645 |
646 std::unique_ptr<uint8_t, FxFreeDeleter> guess_buf( | 646 std::unique_ptr<uint8_t, FxFreeDeleter> guess_buf( |
647 FX_Alloc(uint8_t, guess_size + 1)); | 647 FX_Alloc(uint8_t, guess_size + 1)); |
648 guess_buf.get()[guess_size] = '\0'; | 648 guess_buf.get()[guess_size] = '\0'; |
649 | 649 |
650 FPDFAPI_FlateInput(context, src_buf, src_size); | 650 FPDFAPI_FlateInput(context, src_buf, src_size); |
651 | 651 |
652 if (src_size < kStepSize) { | 652 if (src_size < kStepSize) { |
653 // This is the old implementation. | 653 // This is the old implementation. |
654 uint8_t* cur_buf = guess_buf.get(); | 654 uint8_t* cur_buf = guess_buf.get(); |
655 while (1) { | 655 while (1) { |
656 int32_t ret = FPDFAPI_FlateOutput(context, cur_buf, buf_size); | 656 int32_t ret = FPDFAPI_FlateOutput(context, cur_buf, buf_size); |
657 if (ret != Z_OK) | 657 if (ret != Z_OK) |
658 break; | 658 break; |
659 int32_t avail_buf_size = FPDFAPI_FlateGetAvailOut(context); | 659 int32_t avail_buf_size = FPDFAPI_FlateGetAvailOut(context); |
660 if (avail_buf_size != 0) | 660 if (avail_buf_size != 0) |
661 break; | 661 break; |
662 | 662 |
663 FX_DWORD old_size = guess_size; | 663 uint32_t old_size = guess_size; |
664 guess_size += alloc_step; | 664 guess_size += alloc_step; |
665 if (guess_size < old_size || guess_size + 1 < guess_size) { | 665 if (guess_size < old_size || guess_size + 1 < guess_size) { |
666 FPDFAPI_FlateEnd(context); | 666 FPDFAPI_FlateEnd(context); |
667 return; | 667 return; |
668 } | 668 } |
669 | 669 |
670 { | 670 { |
671 uint8_t* new_buf = | 671 uint8_t* new_buf = |
672 FX_Realloc(uint8_t, guess_buf.release(), guess_size + 1); | 672 FX_Realloc(uint8_t, guess_buf.release(), guess_size + 1); |
673 guess_buf.reset(new_buf); | 673 guess_buf.reset(new_buf); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
708 result_tmp_bufs.Add(cur_buf); | 708 result_tmp_bufs.Add(cur_buf); |
709 cur_buf = FX_Alloc(uint8_t, buf_size + 1); | 709 cur_buf = FX_Alloc(uint8_t, buf_size + 1); |
710 cur_buf[buf_size] = '\0'; | 710 cur_buf[buf_size] = '\0'; |
711 } | 711 } |
712 dest_size = FPDFAPI_FlateGetTotalOut(context); | 712 dest_size = FPDFAPI_FlateGetTotalOut(context); |
713 offset = FPDFAPI_FlateGetTotalIn(context); | 713 offset = FPDFAPI_FlateGetTotalIn(context); |
714 if (result_tmp_bufs.GetSize() == 1) { | 714 if (result_tmp_bufs.GetSize() == 1) { |
715 dest_buf = result_tmp_bufs[0]; | 715 dest_buf = result_tmp_bufs[0]; |
716 } else { | 716 } else { |
717 uint8_t* result_buf = FX_Alloc(uint8_t, dest_size); | 717 uint8_t* result_buf = FX_Alloc(uint8_t, dest_size); |
718 FX_DWORD result_pos = 0; | 718 uint32_t result_pos = 0; |
719 for (int32_t i = 0; i < result_tmp_bufs.GetSize(); i++) { | 719 for (int32_t i = 0; i < result_tmp_bufs.GetSize(); i++) { |
720 uint8_t* tmp_buf = result_tmp_bufs[i]; | 720 uint8_t* tmp_buf = result_tmp_bufs[i]; |
721 FX_DWORD tmp_buf_size = buf_size; | 721 uint32_t tmp_buf_size = buf_size; |
722 if (i == result_tmp_bufs.GetSize() - 1) { | 722 if (i == result_tmp_bufs.GetSize() - 1) { |
723 tmp_buf_size = last_buf_size; | 723 tmp_buf_size = last_buf_size; |
724 } | 724 } |
725 FXSYS_memcpy(result_buf + result_pos, tmp_buf, tmp_buf_size); | 725 FXSYS_memcpy(result_buf + result_pos, tmp_buf, tmp_buf_size); |
726 result_pos += tmp_buf_size; | 726 result_pos += tmp_buf_size; |
727 FX_Free(result_tmp_bufs[i]); | 727 FX_Free(result_tmp_bufs[i]); |
728 } | 728 } |
729 dest_buf = result_buf; | 729 dest_buf = result_buf; |
730 } | 730 } |
731 } | 731 } |
732 FPDFAPI_FlateEnd(context); | 732 FPDFAPI_FlateEnd(context); |
733 } | 733 } |
734 | 734 |
735 } // namespace | 735 } // namespace |
736 | 736 |
737 class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder { | 737 class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder { |
738 public: | 738 public: |
739 CCodec_FlateScanlineDecoder(); | 739 CCodec_FlateScanlineDecoder(); |
740 ~CCodec_FlateScanlineDecoder() override; | 740 ~CCodec_FlateScanlineDecoder() override; |
741 | 741 |
742 void Create(const uint8_t* src_buf, | 742 void Create(const uint8_t* src_buf, |
743 FX_DWORD src_size, | 743 uint32_t src_size, |
744 int width, | 744 int width, |
745 int height, | 745 int height, |
746 int nComps, | 746 int nComps, |
747 int bpc, | 747 int bpc, |
748 int predictor, | 748 int predictor, |
749 int Colors, | 749 int Colors, |
750 int BitsPerComponent, | 750 int BitsPerComponent, |
751 int Columns); | 751 int Columns); |
752 void Destroy() { delete this; } | 752 void Destroy() { delete this; } |
753 | 753 |
754 // CCodec_ScanlineDecoder | 754 // CCodec_ScanlineDecoder |
755 void v_DownScale(int dest_width, int dest_height) override {} | 755 void v_DownScale(int dest_width, int dest_height) override {} |
756 FX_BOOL v_Rewind() override; | 756 FX_BOOL v_Rewind() override; |
757 uint8_t* v_GetNextLine() override; | 757 uint8_t* v_GetNextLine() override; |
758 FX_DWORD GetSrcOffset() override; | 758 uint32_t GetSrcOffset() override; |
759 | 759 |
760 void* m_pFlate; | 760 void* m_pFlate; |
761 const uint8_t* m_SrcBuf; | 761 const uint8_t* m_SrcBuf; |
762 FX_DWORD m_SrcSize; | 762 uint32_t m_SrcSize; |
763 uint8_t* m_pScanline; | 763 uint8_t* m_pScanline; |
764 uint8_t* m_pLastLine; | 764 uint8_t* m_pLastLine; |
765 uint8_t* m_pPredictBuffer; | 765 uint8_t* m_pPredictBuffer; |
766 uint8_t* m_pPredictRaw; | 766 uint8_t* m_pPredictRaw; |
767 int m_Predictor; | 767 int m_Predictor; |
768 int m_Colors; | 768 int m_Colors; |
769 int m_BitsPerComponent; | 769 int m_BitsPerComponent; |
770 int m_Columns; | 770 int m_Columns; |
771 FX_DWORD m_PredictPitch; | 771 uint32_t m_PredictPitch; |
772 size_t m_LeftOver; | 772 size_t m_LeftOver; |
773 }; | 773 }; |
774 | 774 |
775 CCodec_FlateScanlineDecoder::CCodec_FlateScanlineDecoder() { | 775 CCodec_FlateScanlineDecoder::CCodec_FlateScanlineDecoder() { |
776 m_pFlate = NULL; | 776 m_pFlate = NULL; |
777 m_pScanline = NULL; | 777 m_pScanline = NULL; |
778 m_pLastLine = NULL; | 778 m_pLastLine = NULL; |
779 m_pPredictBuffer = NULL; | 779 m_pPredictBuffer = NULL; |
780 m_pPredictRaw = NULL; | 780 m_pPredictRaw = NULL; |
781 m_LeftOver = 0; | 781 m_LeftOver = 0; |
782 } | 782 } |
783 CCodec_FlateScanlineDecoder::~CCodec_FlateScanlineDecoder() { | 783 CCodec_FlateScanlineDecoder::~CCodec_FlateScanlineDecoder() { |
784 FX_Free(m_pScanline); | 784 FX_Free(m_pScanline); |
785 FX_Free(m_pLastLine); | 785 FX_Free(m_pLastLine); |
786 FX_Free(m_pPredictBuffer); | 786 FX_Free(m_pPredictBuffer); |
787 FX_Free(m_pPredictRaw); | 787 FX_Free(m_pPredictRaw); |
788 if (m_pFlate) { | 788 if (m_pFlate) { |
789 FPDFAPI_FlateEnd(m_pFlate); | 789 FPDFAPI_FlateEnd(m_pFlate); |
790 } | 790 } |
791 } | 791 } |
792 void CCodec_FlateScanlineDecoder::Create(const uint8_t* src_buf, | 792 void CCodec_FlateScanlineDecoder::Create(const uint8_t* src_buf, |
793 FX_DWORD src_size, | 793 uint32_t src_size, |
794 int width, | 794 int width, |
795 int height, | 795 int height, |
796 int nComps, | 796 int nComps, |
797 int bpc, | 797 int bpc, |
798 int predictor, | 798 int predictor, |
799 int Colors, | 799 int Colors, |
800 int BitsPerComponent, | 800 int BitsPerComponent, |
801 int Columns) { | 801 int Columns) { |
802 m_SrcBuf = src_buf; | 802 m_SrcBuf = src_buf; |
803 m_SrcSize = src_size; | 803 m_SrcSize = src_size; |
804 m_OutputWidth = m_OrigWidth = width; | 804 m_OutputWidth = m_OrigWidth = width; |
805 m_OutputHeight = m_OrigHeight = height; | 805 m_OutputHeight = m_OrigHeight = height; |
806 m_nComps = nComps; | 806 m_nComps = nComps; |
807 m_bpc = bpc; | 807 m_bpc = bpc; |
808 m_bColorTransformed = FALSE; | 808 m_bColorTransformed = FALSE; |
809 m_Pitch = (static_cast<FX_DWORD>(width) * nComps * bpc + 7) / 8; | 809 m_Pitch = (static_cast<uint32_t>(width) * nComps * bpc + 7) / 8; |
810 m_pScanline = FX_Alloc(uint8_t, m_Pitch); | 810 m_pScanline = FX_Alloc(uint8_t, m_Pitch); |
811 m_Predictor = 0; | 811 m_Predictor = 0; |
812 if (predictor) { | 812 if (predictor) { |
813 if (predictor >= 10) { | 813 if (predictor >= 10) { |
814 m_Predictor = 2; | 814 m_Predictor = 2; |
815 } else if (predictor == 2) { | 815 } else if (predictor == 2) { |
816 m_Predictor = 1; | 816 m_Predictor = 1; |
817 } | 817 } |
818 if (m_Predictor) { | 818 if (m_Predictor) { |
819 if (BitsPerComponent * Colors * Columns == 0) { | 819 if (BitsPerComponent * Colors * Columns == 0) { |
820 BitsPerComponent = m_bpc; | 820 BitsPerComponent = m_bpc; |
821 Colors = m_nComps; | 821 Colors = m_nComps; |
822 Columns = m_OrigWidth; | 822 Columns = m_OrigWidth; |
823 } | 823 } |
824 m_Colors = Colors; | 824 m_Colors = Colors; |
825 m_BitsPerComponent = BitsPerComponent; | 825 m_BitsPerComponent = BitsPerComponent; |
826 m_Columns = Columns; | 826 m_Columns = Columns; |
827 m_PredictPitch = | 827 m_PredictPitch = |
828 (static_cast<FX_DWORD>(m_BitsPerComponent) * m_Colors * m_Columns + | 828 (static_cast<uint32_t>(m_BitsPerComponent) * m_Colors * m_Columns + |
829 7) / | 829 7) / |
830 8; | 830 8; |
831 m_pLastLine = FX_Alloc(uint8_t, m_PredictPitch); | 831 m_pLastLine = FX_Alloc(uint8_t, m_PredictPitch); |
832 m_pPredictRaw = FX_Alloc(uint8_t, m_PredictPitch + 1); | 832 m_pPredictRaw = FX_Alloc(uint8_t, m_PredictPitch + 1); |
833 m_pPredictBuffer = FX_Alloc(uint8_t, m_PredictPitch); | 833 m_pPredictBuffer = FX_Alloc(uint8_t, m_PredictPitch); |
834 } | 834 } |
835 } | 835 } |
836 } | 836 } |
837 FX_BOOL CCodec_FlateScanlineDecoder::v_Rewind() { | 837 FX_BOOL CCodec_FlateScanlineDecoder::v_Rewind() { |
838 if (m_pFlate) { | 838 if (m_pFlate) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 read_bytes); | 887 read_bytes); |
888 m_LeftOver += m_PredictPitch - read_bytes; | 888 m_LeftOver += m_PredictPitch - read_bytes; |
889 bytes_to_go -= read_bytes; | 889 bytes_to_go -= read_bytes; |
890 } | 890 } |
891 } | 891 } |
892 } else { | 892 } else { |
893 FPDFAPI_FlateOutput(m_pFlate, m_pScanline, m_Pitch); | 893 FPDFAPI_FlateOutput(m_pFlate, m_pScanline, m_Pitch); |
894 } | 894 } |
895 return m_pScanline; | 895 return m_pScanline; |
896 } | 896 } |
897 FX_DWORD CCodec_FlateScanlineDecoder::GetSrcOffset() { | 897 uint32_t CCodec_FlateScanlineDecoder::GetSrcOffset() { |
898 return FPDFAPI_FlateGetTotalIn(m_pFlate); | 898 return FPDFAPI_FlateGetTotalIn(m_pFlate); |
899 } | 899 } |
900 | 900 |
901 ICodec_ScanlineDecoder* CCodec_FlateModule::CreateDecoder( | 901 ICodec_ScanlineDecoder* CCodec_FlateModule::CreateDecoder( |
902 const uint8_t* src_buf, | 902 const uint8_t* src_buf, |
903 FX_DWORD src_size, | 903 uint32_t src_size, |
904 int width, | 904 int width, |
905 int height, | 905 int height, |
906 int nComps, | 906 int nComps, |
907 int bpc, | 907 int bpc, |
908 int predictor, | 908 int predictor, |
909 int Colors, | 909 int Colors, |
910 int BitsPerComponent, | 910 int BitsPerComponent, |
911 int Columns) { | 911 int Columns) { |
912 CCodec_FlateScanlineDecoder* pDecoder = new CCodec_FlateScanlineDecoder; | 912 CCodec_FlateScanlineDecoder* pDecoder = new CCodec_FlateScanlineDecoder; |
913 pDecoder->Create(src_buf, src_size, width, height, nComps, bpc, predictor, | 913 pDecoder->Create(src_buf, src_size, width, height, nComps, bpc, predictor, |
914 Colors, BitsPerComponent, Columns); | 914 Colors, BitsPerComponent, Columns); |
915 return pDecoder; | 915 return pDecoder; |
916 } | 916 } |
917 FX_DWORD CCodec_FlateModule::FlateOrLZWDecode(FX_BOOL bLZW, | 917 uint32_t CCodec_FlateModule::FlateOrLZWDecode(FX_BOOL bLZW, |
918 const uint8_t* src_buf, | 918 const uint8_t* src_buf, |
919 FX_DWORD src_size, | 919 uint32_t src_size, |
920 FX_BOOL bEarlyChange, | 920 FX_BOOL bEarlyChange, |
921 int predictor, | 921 int predictor, |
922 int Colors, | 922 int Colors, |
923 int BitsPerComponent, | 923 int BitsPerComponent, |
924 int Columns, | 924 int Columns, |
925 FX_DWORD estimated_size, | 925 uint32_t estimated_size, |
926 uint8_t*& dest_buf, | 926 uint8_t*& dest_buf, |
927 FX_DWORD& dest_size) { | 927 uint32_t& dest_size) { |
928 dest_buf = NULL; | 928 dest_buf = NULL; |
929 FX_DWORD offset = 0; | 929 uint32_t offset = 0; |
930 int predictor_type = 0; | 930 int predictor_type = 0; |
931 if (predictor) { | 931 if (predictor) { |
932 if (predictor >= 10) { | 932 if (predictor >= 10) { |
933 predictor_type = 2; | 933 predictor_type = 2; |
934 } else if (predictor == 2) { | 934 } else if (predictor == 2) { |
935 predictor_type = 1; | 935 predictor_type = 1; |
936 } | 936 } |
937 } | 937 } |
938 if (bLZW) { | 938 if (bLZW) { |
939 { | 939 { |
940 std::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); | 940 std::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); |
941 dest_size = (FX_DWORD)-1; | 941 dest_size = (uint32_t)-1; |
942 offset = src_size; | 942 offset = src_size; |
943 int err = decoder->Decode(NULL, dest_size, src_buf, offset, bEarlyChange); | 943 int err = decoder->Decode(NULL, dest_size, src_buf, offset, bEarlyChange); |
944 if (err || dest_size == 0 || dest_size + 1 < dest_size) { | 944 if (err || dest_size == 0 || dest_size + 1 < dest_size) { |
945 return static_cast<FX_DWORD>(-1); | 945 return static_cast<uint32_t>(-1); |
946 } | 946 } |
947 } | 947 } |
948 { | 948 { |
949 std::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); | 949 std::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); |
950 dest_buf = FX_Alloc(uint8_t, dest_size + 1); | 950 dest_buf = FX_Alloc(uint8_t, dest_size + 1); |
951 dest_buf[dest_size] = '\0'; | 951 dest_buf[dest_size] = '\0'; |
952 decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); | 952 decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); |
953 } | 953 } |
954 } else { | 954 } else { |
955 FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size, | 955 FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size, |
956 offset); | 956 offset); |
957 } | 957 } |
958 if (predictor_type == 0) { | 958 if (predictor_type == 0) { |
959 return offset; | 959 return offset; |
960 } | 960 } |
961 FX_BOOL ret = TRUE; | 961 FX_BOOL ret = TRUE; |
962 if (predictor_type == 2) { | 962 if (predictor_type == 2) { |
963 ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns); | 963 ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns); |
964 } else if (predictor_type == 1) { | 964 } else if (predictor_type == 1) { |
965 ret = | 965 ret = |
966 TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns); | 966 TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns); |
967 } | 967 } |
968 return ret ? offset : static_cast<FX_DWORD>(-1); | 968 return ret ? offset : static_cast<uint32_t>(-1); |
969 } | 969 } |
970 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, | 970 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, |
971 FX_DWORD src_size, | 971 uint32_t src_size, |
972 int predictor, | 972 int predictor, |
973 int Colors, | 973 int Colors, |
974 int BitsPerComponent, | 974 int BitsPerComponent, |
975 int Columns, | 975 int Columns, |
976 uint8_t*& dest_buf, | 976 uint8_t*& dest_buf, |
977 FX_DWORD& dest_size) { | 977 uint32_t& dest_size) { |
978 if (predictor != 2 && predictor < 10) { | 978 if (predictor != 2 && predictor < 10) { |
979 return Encode(src_buf, src_size, dest_buf, dest_size); | 979 return Encode(src_buf, src_size, dest_buf, dest_size); |
980 } | 980 } |
981 uint8_t* pSrcBuf = NULL; | 981 uint8_t* pSrcBuf = NULL; |
982 pSrcBuf = FX_Alloc(uint8_t, src_size); | 982 pSrcBuf = FX_Alloc(uint8_t, src_size); |
983 FXSYS_memcpy(pSrcBuf, src_buf, src_size); | 983 FXSYS_memcpy(pSrcBuf, src_buf, src_size); |
984 FX_BOOL ret = TRUE; | 984 FX_BOOL ret = TRUE; |
985 if (predictor == 2) { | 985 if (predictor == 2) { |
986 ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, | 986 ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, |
987 Columns); | 987 Columns); |
988 } else if (predictor >= 10) { | 988 } else if (predictor >= 10) { |
989 ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, | 989 ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, |
990 BitsPerComponent, Columns); | 990 BitsPerComponent, Columns); |
991 } | 991 } |
992 if (ret) | 992 if (ret) |
993 ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); | 993 ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); |
994 FX_Free(pSrcBuf); | 994 FX_Free(pSrcBuf); |
995 return ret; | 995 return ret; |
996 } | 996 } |
997 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, | 997 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, |
998 FX_DWORD src_size, | 998 uint32_t src_size, |
999 uint8_t*& dest_buf, | 999 uint8_t*& dest_buf, |
1000 FX_DWORD& dest_size) { | 1000 uint32_t& dest_size) { |
1001 dest_size = src_size + src_size / 1000 + 12; | 1001 dest_size = src_size + src_size / 1000 + 12; |
1002 dest_buf = FX_Alloc(uint8_t, dest_size); | 1002 dest_buf = FX_Alloc(uint8_t, dest_size); |
1003 unsigned long temp_size = dest_size; | 1003 unsigned long temp_size = dest_size; |
1004 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); | 1004 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); |
1005 dest_size = (FX_DWORD)temp_size; | 1005 dest_size = (uint32_t)temp_size; |
1006 return TRUE; | 1006 return TRUE; |
1007 } | 1007 } |
OLD | NEW |