| 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 "../../../../third_party/base/nonstd_unique_ptr.h" | 7 #include "../../../../third_party/base/nonstd_unique_ptr.h" |
| 8 #include "../../../../third_party/zlib_v128/zlib.h" | 8 #include "../../../../third_party/zlib_v128/zlib.h" |
| 9 #include "../../../include/fxcodec/fx_codec.h" | 9 #include "../../../include/fxcodec/fx_codec.h" |
| 10 #include "../../../include/fxcodec/fx_codec_flate.h" |
| 10 #include "codec_int.h" | 11 #include "codec_int.h" |
| 11 | 12 |
| 12 extern "C" | 13 extern "C" |
| 13 { | 14 { |
| 14 static void* my_alloc_func (void* opaque, unsigned int items, unsigned int s
ize) | 15 static void* my_alloc_func(void* opaque, unsigned int items, unsigned int si
ze) |
| 15 { | 16 { |
| 16 return FX_Alloc2D(uint8_t, items, size); | 17 return FX_Alloc2D(uint8_t, items, size); |
| 17 } | 18 } |
| 18 static void my_free_func (void* opaque, void* address) | 19 static void my_free_func(void* opaque, void* address) |
| 19 { | 20 { |
| 20 FX_Free(address); | 21 FX_Free(address); |
| 21 } | 22 } |
| 23 static int FPDFAPI_FlateGetTotalOut(void* context) |
| 24 { |
| 25 return ((z_stream*)context)->total_out; |
| 26 } |
| 27 static int FPDFAPI_FlateGetTotalIn(void* context) |
| 28 { |
| 29 return ((z_stream*)context)->total_in; |
| 30 } |
| 31 static void FPDFAPI_FlateCompress(unsigned char* dest_buf, |
| 32 unsigned long* dest_size, |
| 33 const unsigned char* src_buf, |
| 34 unsigned long src_size) |
| 35 { |
| 36 compress(dest_buf, dest_size, src_buf, src_size); |
| 37 } |
| 22 void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned in
t), | 38 void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned in
t), |
| 23 void (*free_func)(void*, void*)) | 39 void (*free_func)(void*, void*)) |
| 24 { | 40 { |
| 25 z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream)); | 41 z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream)); |
| 26 if (p == NULL) { | 42 if (p == NULL) { |
| 27 return NULL; | 43 return NULL; |
| 28 } | 44 } |
| 29 FXSYS_memset(p, 0, sizeof(z_stream)); | 45 FXSYS_memset(p, 0, sizeof(z_stream)); |
| 30 p->zalloc = alloc_func; | 46 p->zalloc = alloc_func; |
| 31 p->zfree = free_func; | 47 p->zfree = free_func; |
| 32 inflateInit(p); | 48 inflateInit(p); |
| 33 return p; | 49 return p; |
| 34 } | 50 } |
| 35 void FPDFAPI_FlateInput(void* context, const unsigned char* src_buf, unsigne
d int src_size) | 51 void FPDFAPI_FlateInput(void* context, const unsigned char* src_buf, unsigne
d int src_size) |
| 36 { | 52 { |
| 37 ((z_stream*)context)->next_in = (unsigned char*)src_buf; | 53 ((z_stream*)context)->next_in = (unsigned char*)src_buf; |
| 38 ((z_stream*)context)->avail_in = src_size; | 54 ((z_stream*)context)->avail_in = src_size; |
| 39 } | 55 } |
| 40 int FPDFAPI_FlateGetTotalOut(void* context) | |
| 41 { | |
| 42 return ((z_stream*)context)->total_out; | |
| 43 } | |
| 44 int FPDFAPI_FlateOutput(void* context, unsigned char* dest_buf, unsigned int
dest_size) | 56 int FPDFAPI_FlateOutput(void* context, unsigned char* dest_buf, unsigned int
dest_size) |
| 45 { | 57 { |
| 46 ((z_stream*)context)->next_out = dest_buf; | 58 ((z_stream*)context)->next_out = dest_buf; |
| 47 ((z_stream*)context)->avail_out = dest_size; | 59 ((z_stream*)context)->avail_out = dest_size; |
| 48 unsigned int pre_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context); | 60 unsigned int pre_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context); |
| 49 int ret = inflate((z_stream*)context, Z_SYNC_FLUSH); | 61 int ret = inflate((z_stream*)context, Z_SYNC_FLUSH); |
| 50 unsigned int post_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context); | 62 unsigned int post_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context); |
| 51 unsigned int written = post_pos - pre_pos; | 63 unsigned int written = post_pos - pre_pos; |
| 52 if (written < dest_size) { | 64 if (written < dest_size) { |
| 53 FXSYS_memset(dest_buf + written, '\0', dest_size - written); | 65 FXSYS_memset(dest_buf + written, '\0', dest_size - written); |
| 54 } | 66 } |
| 55 return ret; | 67 return ret; |
| 56 } | 68 } |
| 57 int FPDFAPI_FlateGetTotalIn(void* context) | 69 int FPDFAPI_FlateGetAvailIn(void* context) |
| 58 { | 70 { |
| 59 return ((z_stream*)context)->total_in; | 71 return ((z_stream*)context)->avail_in; |
| 60 } | 72 } |
| 61 int FPDFAPI_FlateGetAvailOut(void* context) | 73 int FPDFAPI_FlateGetAvailOut(void* context) |
| 62 { | 74 { |
| 63 return ((z_stream*)context)->avail_out; | 75 return ((z_stream*)context)->avail_out; |
| 64 } | 76 } |
| 65 int FPDFAPI_FlateGetAvailIn(void* context) | |
| 66 { | |
| 67 return ((z_stream*)context)->avail_in; | |
| 68 } | |
| 69 void FPDFAPI_FlateEnd(void* context) | 77 void FPDFAPI_FlateEnd(void* context) |
| 70 { | 78 { |
| 71 inflateEnd((z_stream*)context); | 79 inflateEnd((z_stream*)context); |
| 72 ((z_stream*)context)->zfree(0, context); | 80 ((z_stream*)context)->zfree(0, context); |
| 73 } | 81 } |
| 74 void FPDFAPI_FlateCompress(unsigned char* dest_buf, unsigned long* dest_size
, const unsigned char* src_buf, unsigned long src_size) | 82 } // extern "C" |
| 75 { | 83 |
| 76 compress(dest_buf, dest_size, src_buf, src_size); | |
| 77 } | |
| 78 } | |
| 79 class CLZWDecoder | 84 class CLZWDecoder |
| 80 { | 85 { |
| 81 public: | 86 public: |
| 82 FX_BOOL Decode(uint8_t* output, FX_DWORD& outlen, const uint8_t* input, FX_D
WORD& size, FX_BOOL bEarlyChange); | 87 FX_BOOL Decode(uint8_t* output, FX_DWORD& outlen, const uint8_t* input, FX_D
WORD& size, FX_BOOL bEarlyChange); |
| 83 private: | 88 private: |
| 84 FX_DWORD m_InPos; | 89 FX_DWORD m_InPos; |
| 85 FX_DWORD m_OutPos; | 90 FX_DWORD m_OutPos; |
| 86 uint8_t* m_pOutput; | 91 uint8_t* m_pOutput; |
| 87 const uint8_t* m_pInput; | 92 const uint8_t* m_pInput; |
| 88 FX_BOOL m_Early; | 93 FX_BOOL m_Early; |
| (...skipping 815 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 904 } | 909 } |
| 905 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, FX_DWORD src_size, ui
nt8_t*& dest_buf, FX_DWORD& dest_size) | 910 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, FX_DWORD src_size, ui
nt8_t*& dest_buf, FX_DWORD& dest_size) |
| 906 { | 911 { |
| 907 dest_size = src_size + src_size / 1000 + 12; | 912 dest_size = src_size + src_size / 1000 + 12; |
| 908 dest_buf = FX_Alloc( uint8_t, dest_size); | 913 dest_buf = FX_Alloc( uint8_t, dest_size); |
| 909 unsigned long temp_size = dest_size; | 914 unsigned long temp_size = dest_size; |
| 910 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); | 915 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); |
| 911 dest_size = (FX_DWORD)temp_size; | 916 dest_size = (FX_DWORD)temp_size; |
| 912 return TRUE; | 917 return TRUE; |
| 913 } | 918 } |
| OLD | NEW |