UltraScan III
us_gzip.h
Go to the documentation of this file.
1 #ifndef US_GZIP_H
2 #define US_GZIP_H
3 
4 #include <qstring.h>
5 #include <sys/types.h>
6 #include "us_extern.h"
7 
8 // Error codes
9 
10 #define GZIP_OK 0
11 #define GZIP_NOEXIST 1
12 #define GZIP_NOTFILE 2
13 #define GZIP_NOREAD 3
14 #define GZIP_NOTGZ 4
15 #define GZIP_OPTIONNOTSUPPORTED 5
16 #define GZIP_OUTFILEEXISTS 6
17 #define GZIP_CRCERROR 7
18 #define GZIP_READERROR 8
19 #define GZIP_WRITEERROR 9
20 #define GZIP_LENGTHERROR 10
21 #define GZIP_FILENAMEERROR 11
22 #define GZIP_INTERNAL 12
23 
24 
25 typedef unsigned char uch;
26 typedef unsigned short ush;
27 typedef unsigned long ulg;
28 
29 
35 {
36  public:
37  US_Gzip();
38 
42  int gzip ( const QString& );
43 
44 
48  int gunzip ( const QString& );
49 
53  QString explain( const int );
54 
56  typedef struct config
57  {
62  } config;
63 
64  private:
65  off_t bytes_in; /* number of input bytes */
66  off_t bytes_out; /* number of output bytes */
67 
68  ulg bb; /* bit buffer */
69  unsigned bk; /* bits in bit buffer */
70  unsigned outcnt; /* bytes in output buffer */
71 
72  unsigned inptr;
73  unsigned hufts; /* track memory usage */
74  unsigned insize;
75 
76  unsigned crc; /* current crc value */
77 
78  int ifd; /* input file descriptor */
79  int ofd; /* output file descriptor */
80 
81 #define INBUFSIZ 0x8000 /* input buffer size */
82 #define INBUF_EXTRA 64 /* required by unlzw() */
83 #define OUTBUFSIZ 16384 /* output buffer size */
84 #define OUTBUF_EXTRA 2048 /* required by unlzw() */
85 
86 #define WSIZE 0x8000 /* window size--must be a power of two, and */
87  /* at least 32K for zip's deflate method */
88 
97  struct huft
98  {
99  uch e;
100  uch b;
101  union
102  {
103  ush n;
104  struct huft* t;
105  } v;
106  };
107 
108  uch inbuf [ INBUFSIZ + INBUF_EXTRA ];
110  uch window[ 2L * WSIZE ];
111 
112  int treat_file ( const QString&, bool );
113  QString make_ofname ( const QString&, bool );
114  int huft_build ( unsigned*, unsigned, unsigned, ush*, ush*,
115  struct huft**, int* );
116  int huft_free ( struct huft * );
117  int inflate_codes ( struct huft*, struct huft*, int, int );
118  int inflate_stored ( void );
119  int inflate_fixed ( void );
120  int inflate_dynamic( void );
121  int inflate_block ( int* );
122  int inflate ( void );
123 
124  int fill_inbuf ( int );
125  void flush_window ( void );
126  ulg updcrc ( uch*, unsigned );
127  void write_buf ( int, void*, unsigned );
128  char* base_name ( char* );
129  void flush_outbuf ( void );
130 
131  // deflate variables and defines
132 
133  typedef ush Pos;
134  typedef unsigned IPos;
135  /* A Pos is an index in the character window. We use short instead of int to
136  * save space in the various tables. IPos is used only for parameter passing. */
137 
139  /* window position at the beginning of the current output block. Gets
140  * negative when the window is moved backwards. */
141 
142  unsigned ins_h; /* hash index of string to be inserted */
143  unsigned lookahead; /* number of valid bytes ahead in window */
144  int eofile; /* flag set at end of input file */
145  unsigned int max_lazy_match;
146  /* Attempt to find a better match only when the current match is strictly
147  * smaller than this value. This mechanism is used only for compression
148  * levels >= 4. */
149 
151  /* To speed up deflation, hash chains are never searched beyond this length.
152  * A higher limit improves compression ratio but degrades the speed. */
153 
154  unsigned int prev_length;
155  /* Length of the best match at previous step. Matches not greater than this
156  * are discarded. This is used in the lazy match evaluation. */
157 
158  unsigned strstart; /* start of string to insert */
159  unsigned match_start; /* start of matching string */
160 
161 #define max_insert_length max_lazy_match
162  /* Insert new strings in the hash table only if the match length
163  * is not greater than this length. This saves time but degrades compression.
164  * max_insert_length is used only for compression levels <= 3. */
165 
166  unsigned good_match;
167  /* Use a faster search when the previous match is longer than this */
168 
169  int nice_match; /* Stop searching when current match exceeds this */
170 
171 #define tab_prefix prev /* hash link (see deflate.c) */
172 #define head ( prev + WSIZE ) /* hash head (see deflate.c) */
173 
174 #define BITS 16
175  ush tab_prefix[ 1L << BITS ];
176 
177  unsigned short bi_buf;
178  /* Output buffer. bits are inserted starting at the bottom (least significant
179  * bits). */
180 
181 #define Buf_size ( 8 * 2 * sizeof( char ) )
182  /* Number of bits used within bi_buf. (bi_buf might be implemented on
183  * more than 16 bits on some systems.) */
184 
185  int bi_valid;
186  /* Number of valid bits in bi_buf. All bits above the last valid bit
187  * are always zero. */
188 
189  unsigned last_lit; /* running index in inbuf */
190 
191 #define MAX_BITS 15
192  /* All codes must not exceed MAX_BITS bits */
193 
194 #define MAX_BL_BITS 7
195  /* Bit length codes must not exceed MAX_BL_BITS bits */
196 
197 #define LENGTH_CODES 29
198  /* number of length codes, not counting the special END_BLOCK code */
199 
200 #define LITERALS 256
201  /* number of literal bytes 0..255 */
202 
203 #define END_BLOCK 256
204  /* end of block literal code */
205 
206 #define D_CODES 30
207  /* number of distance codes */
208 
209 #define BL_CODES 19
210  /* number of codes used to transfer the bit lengths */
211 
212 #define L_CODES ( LITERALS + 1 + LENGTH_CODES )
213  /* number of Literal or Length codes, including the END_BLOCK code */
214 
215 #define Freq fc.freq
216 #define Code fc.code
217 #define Dad dl.dad
218 #define Len dl.len
219 
220 #define HEAP_SIZE ( 2 * L_CODES + 1 )
221 
222 /* Data structure describing a single value and its code string. */
223  typedef struct ct_data
224  {
225  union
226  {
227  ush freq; /* frequency count */
228  ush code; /* bit string */
229  } fc;
230  union
231  {
232  ush dad; /* father node in Huffman tree */
233  ush len; /* length of bit string */
234 
235  } dl;
236  } ct_data;
237 
238  ct_data dyn_ltree[ HEAP_SIZE ]; /* literal and length tree */
239  ct_data dyn_dtree[ 2 * D_CODES + 1 ]; /* distance tree */
240 
241  ct_data static_ltree[ L_CODES + 2 ];
242  /* The static literal tree. Since the bit lengths are imposed, there is no
243  * need for the L_CODES extra codes used during heap construction. However
244  * The codes 286 and 287 are needed to build a canonical tree (see ct_init
245  * below). */
246 
247  ct_data static_dtree[ D_CODES ];
248  /* The static distance tree. (Actually a trivial tree since all codes use
249  * 5 bits.) */
250 
251  ct_data bl_tree[ 2 * BL_CODES + 1 ];
252  /* Huffman tree for the bit lengths */
253 
254 #define MIN_MATCH 3
255 #define MAX_MATCH 258
256  /* The minimum and maximum match lengths */
257 
258  uch length_code[ MAX_MATCH - MIN_MATCH + 1 ];
259  /* length code for each normalized match length (0 == MIN_MATCH) */
260 
261  uch dist_code[ 512 ];
262  /* distance codes. The first 256 values correspond to the distances
263  * 3 .. 258, the last 256 values correspond to the top 8 bits of
264  * the 15 bit distances. */
265 
266  unsigned last_dist; /* running index in d_buf */
267  unsigned last_flags; /* running index in flag_buf */
268  uch flags; /* current flags not yet saved in flag_buf */
269  uch flag_bit; /* current bit used in flags */
270  /* bits are filled in flags starting at bit 0 (least significant).
271  * Note: these flags are overkill in the current code since we don't
272  * take advantage of DIST_BUFSIZE == LIT_BUFSIZE. */
273 
274 #define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
275  ush d_buf[ DIST_BUFSIZE ];
276 
277 #ifndef LIT_BUFSIZE
278 # ifdef SMALL_MEM
279 # define LIT_BUFSIZE 0x2000
280 # else
281 # ifdef MEDIUM_MEM
282 # define LIT_BUFSIZE 0x4000
283 # else
284 # define LIT_BUFSIZE 0x8000
285 # endif
286 # endif
287 #endif
288 
289  uch flag_buf[ LIT_BUFSIZE / 8 ];
290 
291  typedef struct tree_desc
292  {
293  ct_data* dyn_tree; /* the dynamic tree */
294  ct_data* static_tree; /* corresponding static tree or NULL */
295  int* extra_bits; /* extra bits for each code or NULL */
296  int extra_base; /* base index for extra_bits */
297  int elems; /* max number of elements in the tree */
298  int max_length; /* max bit length for the codes */
299  int max_code; /* largest code with non zero frequency */
300  } tree_desc;
301 
305 
306  ulg opt_len; /* bit length of current block with optimal trees */
307  ulg static_len; /* bit length of current block with static trees */
308  off_t compressed_len; /* total bit length of compressed file */
309  int* file_method; /* pointer to DEFLATE or STORE */
310 
311  int base_length[ LENGTH_CODES ];
312  int base_dist[ D_CODES ];
313 
314  int heap[ 2 * L_CODES + 1 ]; /* heap used to build the Huffman trees */
315  int heap_len; /* number of elements in the heap */
316  int heap_max; /* element of largest frequency */
317  /* The sons of heap[n] are heap[ 2 * n ] and heap[ 2 * n + 1 ]. heap[ 0 ]
318  * is not used. The same heap array is used to build all trees. */
319 
320  uch depth[ 2 * L_CODES + 1 ];
321  /* Depth of each subtree used as tie breaker for trees of equal frequency */
322 
323  ush bl_count[ MAX_BITS + 1 ];
324  /* Number of codes at each bit length for an optimal tree */
325 
326  // Deflate methods
327  off_t deflate ( void );
328  void lm_init ( void );
329  int file_read ( char*, unsigned int );
330  void fill_window ( void );
331  int longest_match ( IPos );
332  void ct_init ( void );
333  int ct_tally ( int, int );
334  off_t flush_block ( char*, ulg, int );
335  void bi_init ( void );
336  void build_tree ( tree_desc* );
337  int build_bl_tree ( void );
338  void copy_block ( char*, unsigned, int );
339  void send_bits ( int, int );
340  void compress_block ( ct_data*, ct_data* );
341  void send_all_trees ( int, int, int );
342  void send_tree ( ct_data*, int );
343  void init_block ( void );
344  void bi_windup ( void );
345  unsigned bi_reverse ( unsigned, int );
346  void gen_bitlen ( tree_desc* );
347  void gen_codes ( ct_data*, int );
348  void pqdownheap ( ct_data*, int );
349  void scan_tree ( ct_data*, int );
350 
351 };
352 #endif
353