Home | History | Annotate | Download | only in gif
      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   typedef struct {
     18     uint16_t prefix;
     19     uint8_t suffix;
     20   } CodeEntry;
     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(uint8_t* src_buf,
     28                              uint32_t src_size,
     29                              uint8_t* des_buf,
     30                              uint32_t* des_size);
     31 
     32   // Used by unittests, should not be called in production code.
     33   uint32_t ExtractDataForTest(uint8_t* des_buf, uint32_t des_size) {
     34     return ExtractData(des_buf, des_size);
     35   }
     36 
     37   std::vector<uint8_t>* DecompressedForTest() { return &decompressed_; }
     38 
     39   size_t* DecompressedNextForTest() { return &decompressed_next_; }
     40 
     41  private:
     42   CFX_LZWDecompressor(uint8_t color_exp, uint8_t code_exp);
     43   void ClearTable();
     44   void AddCode(uint16_t prefix_code, uint8_t append_char);
     45   bool DecodeString(uint16_t code);
     46   uint32_t ExtractData(uint8_t* des_buf, uint32_t des_size);
     47 
     48   uint8_t code_size_;
     49   uint8_t code_size_cur_;
     50   uint16_t code_color_end_;
     51   uint16_t code_clear_;
     52   uint16_t code_end_;
     53   uint16_t code_next_;
     54   uint8_t code_first_;
     55   std::vector<uint8_t> decompressed_;
     56   size_t decompressed_next_;
     57   CodeEntry code_table_[GIF_MAX_LZW_CODE];
     58   uint16_t code_old_;
     59   uint8_t* next_in_;
     60   uint32_t avail_in_;
     61   uint8_t bits_left_;
     62   uint32_t code_store_;
     63 };
     64 
     65 #endif  // CORE_FXCODEC_GIF_CFX_LZWDECOMPRESSOR_H_
     66