| 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 "codec_int.h" | 7 #include "codec_int.h" |
| 8 |
| 9 #include <memory> |
| 10 |
| 8 #include "core/include/fxcodec/fx_codec.h" | 11 #include "core/include/fxcodec/fx_codec.h" |
| 9 #include "core/include/fxcodec/fx_codec_flate.h" | 12 #include "core/include/fxcodec/fx_codec_flate.h" |
| 10 #include "third_party/base/nonstd_unique_ptr.h" | |
| 11 #include "third_party/zlib_v128/zlib.h" | 13 #include "third_party/zlib_v128/zlib.h" |
| 12 | 14 |
| 13 extern "C" { | 15 extern "C" { |
| 14 static void* my_alloc_func(void* opaque, | 16 static void* my_alloc_func(void* opaque, |
| 15 unsigned int items, | 17 unsigned int items, |
| 16 unsigned int size) { | 18 unsigned int size) { |
| 17 return FX_Alloc2D(uint8_t, items, size); | 19 return FX_Alloc2D(uint8_t, items, size); |
| 18 } | 20 } |
| 19 static void my_free_func(void* opaque, void* address) { | 21 static void my_free_func(void* opaque, void* address) { |
| 20 FX_Free(address); | 22 FX_Free(address); |
| (...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 } | 634 } |
| 633 FX_DWORD buf_size = guess_size; | 635 FX_DWORD buf_size = guess_size; |
| 634 FX_DWORD last_buf_size = buf_size; | 636 FX_DWORD last_buf_size = buf_size; |
| 635 | 637 |
| 636 dest_buf = nullptr; | 638 dest_buf = nullptr; |
| 637 dest_size = 0; | 639 dest_size = 0; |
| 638 void* context = FPDFAPI_FlateInit(my_alloc_func, my_free_func); | 640 void* context = FPDFAPI_FlateInit(my_alloc_func, my_free_func); |
| 639 if (!context) | 641 if (!context) |
| 640 return; | 642 return; |
| 641 | 643 |
| 642 nonstd::unique_ptr<uint8_t, FxFreeDeleter> guess_buf( | 644 std::unique_ptr<uint8_t, FxFreeDeleter> guess_buf( |
| 643 FX_Alloc(uint8_t, guess_size + 1)); | 645 FX_Alloc(uint8_t, guess_size + 1)); |
| 644 guess_buf.get()[guess_size] = '\0'; | 646 guess_buf.get()[guess_size] = '\0'; |
| 645 | 647 |
| 646 FPDFAPI_FlateInput(context, src_buf, src_size); | 648 FPDFAPI_FlateInput(context, src_buf, src_size); |
| 647 | 649 |
| 648 if (src_size < kStepSize) { | 650 if (src_size < kStepSize) { |
| 649 // This is the old implementation. | 651 // This is the old implementation. |
| 650 uint8_t* cur_buf = guess_buf.get(); | 652 uint8_t* cur_buf = guess_buf.get(); |
| 651 while (1) { | 653 while (1) { |
| 652 int32_t ret = FPDFAPI_FlateOutput(context, cur_buf, buf_size); | 654 int32_t ret = FPDFAPI_FlateOutput(context, cur_buf, buf_size); |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 926 int predictor_type = 0; | 928 int predictor_type = 0; |
| 927 if (predictor) { | 929 if (predictor) { |
| 928 if (predictor >= 10) { | 930 if (predictor >= 10) { |
| 929 predictor_type = 2; | 931 predictor_type = 2; |
| 930 } else if (predictor == 2) { | 932 } else if (predictor == 2) { |
| 931 predictor_type = 1; | 933 predictor_type = 1; |
| 932 } | 934 } |
| 933 } | 935 } |
| 934 if (bLZW) { | 936 if (bLZW) { |
| 935 { | 937 { |
| 936 nonstd::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); | 938 std::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); |
| 937 dest_size = (FX_DWORD)-1; | 939 dest_size = (FX_DWORD)-1; |
| 938 offset = src_size; | 940 offset = src_size; |
| 939 int err = decoder->Decode(NULL, dest_size, src_buf, offset, bEarlyChange); | 941 int err = decoder->Decode(NULL, dest_size, src_buf, offset, bEarlyChange); |
| 940 if (err || dest_size == 0 || dest_size + 1 < dest_size) { | 942 if (err || dest_size == 0 || dest_size + 1 < dest_size) { |
| 941 return -1; | 943 return -1; |
| 942 } | 944 } |
| 943 } | 945 } |
| 944 { | 946 { |
| 945 nonstd::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); | 947 std::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); |
| 946 dest_buf = FX_Alloc(uint8_t, dest_size + 1); | 948 dest_buf = FX_Alloc(uint8_t, dest_size + 1); |
| 947 dest_buf[dest_size] = '\0'; | 949 dest_buf[dest_size] = '\0'; |
| 948 decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); | 950 decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); |
| 949 } | 951 } |
| 950 } else { | 952 } else { |
| 951 FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size, | 953 FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size, |
| 952 offset); | 954 offset); |
| 953 } | 955 } |
| 954 if (predictor_type == 0) { | 956 if (predictor_type == 0) { |
| 955 return offset; | 957 return offset; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 994 FX_DWORD src_size, | 996 FX_DWORD src_size, |
| 995 uint8_t*& dest_buf, | 997 uint8_t*& dest_buf, |
| 996 FX_DWORD& dest_size) { | 998 FX_DWORD& dest_size) { |
| 997 dest_size = src_size + src_size / 1000 + 12; | 999 dest_size = src_size + src_size / 1000 + 12; |
| 998 dest_buf = FX_Alloc(uint8_t, dest_size); | 1000 dest_buf = FX_Alloc(uint8_t, dest_size); |
| 999 unsigned long temp_size = dest_size; | 1001 unsigned long temp_size = dest_size; |
| 1000 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); | 1002 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); |
| 1001 dest_size = (FX_DWORD)temp_size; | 1003 dest_size = (FX_DWORD)temp_size; |
| 1002 return TRUE; | 1004 return TRUE; |
| 1003 } | 1005 } |
| OLD | NEW |