9dd8d4ddaacd856414a85ca4150f3102363d79b6
[firmware_extractor.git] / cms_lzw.h
1 /*
2  * LZW decoder
3  * Copyright (c) 2003 Fabrice Bellard.
4  * Copyright (c) 2006 Konstantin Shishkov.
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #ifndef __CMS_LZW_H__
24 #define __CMS_LZW_H__
25
26 /*!\file cms_lzw.h
27  * \brief Header file for LZW Encoder/Decoder functions.
28  * 
29  * These functions were taken from the ffmpeg library.
30  */
31
32
33 #define LZW_MAXBITS        12
34 #define LZW_SIZTABLE       (1<<LZW_MAXBITS)
35 #define LZW_HASH_SIZE      16411
36 #define LZW_HASH_SHIFT     6
37
38 #define LZW_PREFIX_EMPTY   -1
39 #define LZW_PREFIX_FREE    -2
40
41
42 /** fixed length header in front of compressed config file */
43 #define COMPRESSED_CONFIG_HEADER_LENGTH 40
44
45 /** start of compressed config file */
46 #define COMPRESSED_CONFIG_HEADER        "<compressed alg=lzw len="
47
48
49 /** One code in hash table */
50 typedef struct Code{
51     /// Hash code of prefix, LZW_PREFIX_EMPTY if empty prefix, or LZW_PREFIX_FREE if no code
52     int hash_prefix;
53     int code;               ///< LZW code
54     uint8_t suffix;         ///< Last character in code block
55 }Code;
56
57
58 typedef struct PutBitContext {
59     uint32_t bit_buf;
60     int bit_left;
61     uint8_t *buf, *buf_ptr, *buf_end;
62 } PutBitContext;
63
64
65 /** LZW encode state */
66 typedef struct {
67     int clear_code;          ///< Value of clear code
68     int end_code;            ///< Value of end code
69     Code tab[LZW_HASH_SIZE]; ///< Hash table
70     int tabsize;             ///< Number of values in hash table
71     int bits;                ///< Actual bits code
72     int bufsize;             ///< Size of output buffer
73     PutBitContext pb;        ///< Put bit context for output
74     int maxbits;             ///< Max bits code
75     int maxcode;             ///< Max value of code
76     int output_bytes;        ///< Number of written bytes
77     int last_code;           ///< Value of last output code or LZW_PREFIX_EMPTY
78 }LZWEncoderState;
79
80
81 /** LZW decoder state structure */
82 typedef struct {
83     uint8_t *pbuf, *ebuf;
84     int bbits;
85     unsigned int bbuf;
86
87     int mode;                   ///< Decoder mode
88     int cursize;                ///< The current code size
89     int curmask;
90     int codesize;
91     int clear_code;
92     int end_code;
93     int newcodes;               ///< First available code
94     int top_slot;               ///< Highest code for current size
95     int extra_slot;
96     int slot;                   ///< Last read code
97     int fc, oc;
98     uint8_t *sp;
99     uint8_t stack[LZW_SIZTABLE];
100     uint8_t suffix[LZW_SIZTABLE];
101     uint16_t prefix[LZW_SIZTABLE];
102     int bs;                     ///< current buffer size for GIF
103 } LZWDecoderState;
104
105
106 /** Create and Initialize a LZW encoder.
107  *
108  * @param s       (IN/OUT) LZW encoder state structure allocated by this function.
109  * @param outbuf  (IN)  Output buffer, caller is responsible for allocating the buffer.
110  * @param outsize (IN)  Size of output buffer
111  *
112  * @return CmsRet enum
113  */
114 CmsRet cmsLzw_initEncoder(LZWEncoderState **s, UINT8 *outbuf, UINT32 outsize);
115
116
117 /** LZW main encode/compress function
118  *
119  * @param s      (IN) LZW encoder state
120  * @param inbuf  (IN) Input buffer, contains data to be compressed
121  * @param insize (IN) Size of input buffer
122  * @return Number of bytes written or -1 on error
123  */
124 SINT32 cmsLzw_encode(LZWEncoderState *s, const UINT8 *inbuf, UINT32 insize);
125
126
127 /** Write end code and flush bitstream
128  *
129  * @param s (IN) LZW encoder state
130  * @return Number of bytes written or -1 on error.  Note the count here should
131  *         be added to the count returned by cmsLzw_encode for the total number
132  *         of encoded/compressed bytes.
133  */
134 SINT32 cmsLzw_flushEncoder(LZWEncoderState *s);
135
136
137 /** Destroy the LZW encoder.
138  *  Note the output buffer that was passed into the cmsLzw_initEncoder
139  *  is not freed.  That buffer is the responsibility of the caller.
140  *
141  * @param s      (IN) LZW encoder state
142  */
143 void cmsLzw_cleanupEncoder(LZWEncoderState **s);
144
145
146 /** Create and Initialize a LZW decoder
147  *
148  * @param s          (IN/OUT) LZW decoder context
149  * @param inbuf      (IN) input compressed data
150  * @param inbuf_size (IN) input data size
151  *
152  * @return CmsRet enum
153  */
154 CmsRet cmsLzw_initDecoder(LZWDecoderState **s, UINT8 *inbuf, UINT32 inbuf_size);
155
156
157 /** Decode given number of bytes
158  *
159  * @param s       (IN) LZW decoder context
160  * @param outbuf  (IN) output buffer, caller is responsible for allocating this buffer
161  * @param outlen  (IN) number of bytes to decode, or the length of the output buffer.
162  *                  The decompressor/decoder will stop if the end of the encoded
163  *                  data is reached or if the end of the output buffer is reached.
164  *
165  * @return number of bytes decoded, or -1 on error.
166  */
167 SINT32 cmsLzw_decode(LZWDecoderState *s, UINT8 *outbuf, UINT32 outlen);
168
169
170 /** Destroy the LZW Decoder.
171  *
172  * Note the input buffer that was passed into cmsLzw_initDecoder is not freed.
173  * That buffer is the responsibility of the caller.
174  */
175 void cmsLzw_cleanupDecoder(LZWDecoderState **s);
176
177
178
179 #endif /* __CMS_LZW_H__ */