1 // Copyright 2017 PDFium Authors. All rights reserved. 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 #ifndef CORE_FXCODEC_GIF_CFX_LZWDECOMPRESSOR_H_ 8 #define CORE_FXCODEC_GIF_CFX_LZWDECOMPRESSOR_H_ 9 10 #include <memory> 11 #include <vector> 12 13 #include "core/fxcodec/gif/cfx_gif.h" 14 15 class CFX_LZWDecompressor { 16 public: 17 struct CodeEntry { 18 uint16_t prefix; 19 uint8_t suffix; 20 }; 21 22 // Returns nullptr on error 23 static std::unique_ptr<CFX_LZWDecompressor> Create(uint8_t color_exp, 24 uint8_t code_exp); 25 ~CFX_LZWDecompressor(); 26 27 CFX_GifDecodeStatus Decode(const uint8_t* src_buf, 28 uint32_t src_size, 29 uint8_t* dest_buf, 30 uint32_t* dest_size); 31 32 // Used by unittests, should not be called in production code. ExtractDataForTest(uint8_t * dest_buf,uint32_t dest_size)33 uint32_t ExtractDataForTest(uint8_t* dest_buf, uint32_t dest_size) { 34 return ExtractData(dest_buf, dest_size); 35 } 36 DecompressedForTest()37 std::vector<uint8_t>* DecompressedForTest() { return &decompressed_; } DecompressedNextForTest()38 size_t* DecompressedNextForTest() { return &decompressed_next_; } 39 40 private: 41 CFX_LZWDecompressor(uint8_t color_exp, uint8_t code_exp); 42 void ClearTable(); 43 void AddCode(uint16_t prefix_code, uint8_t append_char); 44 bool DecodeString(uint16_t code); 45 uint32_t ExtractData(uint8_t* dest_buf, uint32_t dest_size); 46 47 uint8_t code_size_; 48 uint8_t code_size_cur_; 49 uint16_t code_color_end_; 50 uint16_t code_clear_; 51 uint16_t code_end_; 52 uint16_t code_next_; 53 uint8_t code_first_; 54 std::vector<uint8_t> decompressed_; 55 size_t decompressed_next_; 56 uint16_t code_old_; 57 const uint8_t* next_in_; 58 uint32_t avail_in_; 59 uint8_t bits_left_; 60 uint32_t code_store_; 61 CodeEntry code_table_[GIF_MAX_LZW_CODE]; 62 }; 63 64 #endif // CORE_FXCODEC_GIF_CFX_LZWDECOMPRESSOR_H_ 65