Rizin
unix-like reverse engineering framework and cli tools
lzx.h
Go to the documentation of this file.
1 /* This file is part of libmspack.
2  * (C) 2003-2013 Stuart Caie.
3  *
4  * The LZX method was created by Jonathan Forbes and Tomi Poutanen, adapted
5  * by Microsoft Corporation.
6  *
7  * libmspack is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Lesser General Public License (LGPL) version 2.1
9  *
10  * For further details, see the file COPYING.LIB distributed with libmspack
11  */
12 
13 #ifndef MSPACK_LZX_H
14 #define MSPACK_LZX_H 1
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /* LZX compression / decompression definitions */
21 
22 /* some constants defined by the LZX specification */
23 #define LZX_MIN_MATCH (2)
24 #define LZX_MAX_MATCH (257)
25 #define LZX_NUM_CHARS (256)
26 #define LZX_BLOCKTYPE_INVALID (0) /* also blocktypes 4-7 invalid */
27 #define LZX_BLOCKTYPE_VERBATIM (1)
28 #define LZX_BLOCKTYPE_ALIGNED (2)
29 #define LZX_BLOCKTYPE_UNCOMPRESSED (3)
30 #define LZX_PRETREE_NUM_ELEMENTS (20)
31 #define LZX_ALIGNED_NUM_ELEMENTS (8) /* aligned offset tree #elements */
32 #define LZX_NUM_PRIMARY_LENGTHS (7) /* this one missing from spec! */
33 #define LZX_NUM_SECONDARY_LENGTHS (249) /* length tree #elements */
34 
35 /* LZX huffman defines: tweak tablebits as desired */
36 #define LZX_PRETREE_MAXSYMBOLS (LZX_PRETREE_NUM_ELEMENTS)
37 #define LZX_PRETREE_TABLEBITS (6)
38 #define LZX_MAINTREE_MAXSYMBOLS (LZX_NUM_CHARS + 290*8)
39 #define LZX_MAINTREE_TABLEBITS (12)
40 #define LZX_LENGTH_MAXSYMBOLS (LZX_NUM_SECONDARY_LENGTHS+1)
41 #define LZX_LENGTH_TABLEBITS (12)
42 #define LZX_ALIGNED_MAXSYMBOLS (LZX_ALIGNED_NUM_ELEMENTS)
43 #define LZX_ALIGNED_TABLEBITS (7)
44 #define LZX_LENTABLE_SAFETY (64) /* table decoding overruns are allowed */
45 
46 #define LZX_FRAME_SIZE (32768) /* the size of a frame in LZX */
47 
48 struct lzxd_stream {
49  struct mspack_system *sys; /* I/O routines */
50  struct mspack_file *input; /* input file handle */
51  struct mspack_file *output; /* output file handle */
52 
53  off_t offset; /* number of bytes actually output */
54  off_t length; /* overall decompressed length of stream */
55 
56  unsigned char *window; /* decoding window */
57  unsigned int window_size; /* window size */
58  unsigned int ref_data_size; /* LZX DELTA reference data size */
59  unsigned int num_offsets; /* number of match_offset entries in table */
60  unsigned int window_posn; /* decompression offset within window */
61  unsigned int frame_posn; /* current frame offset within in window */
62  unsigned int frame; /* the number of 32kb frames processed */
63  unsigned int reset_interval; /* which frame do we reset the compressor? */
64 
65  unsigned int R0, R1, R2; /* for the LRU offset system */
66  unsigned int block_length; /* uncompressed length of this LZX block */
67  unsigned int block_remaining; /* uncompressed bytes still left to decode */
68 
69  signed int intel_filesize; /* magic header value used for transform */
70 
71  unsigned char intel_started; /* has intel E8 decoding started? */
72  unsigned char block_type; /* type of the current block */
73  unsigned char header_read; /* have we started decoding at all yet? */
74  unsigned char input_end; /* have we reached the end of input? */
75  unsigned char is_delta; /* does stream follow LZX DELTA spec? */
76 
77  int error;
78 
79  /* I/O buffering */
80  unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
81  unsigned int bit_buffer, bits_left, inbuf_size;
82 
83  /* huffman code lengths */
88 
89  /* huffman decoding tables */
90  unsigned short PRETREE_table [(1 << LZX_PRETREE_TABLEBITS) +
92  unsigned short MAINTREE_table[(1 << LZX_MAINTREE_TABLEBITS) +
94  unsigned short LENGTH_table [(1 << LZX_LENGTH_TABLEBITS) +
95  (LZX_LENGTH_MAXSYMBOLS * 2)];
96  unsigned short ALIGNED_table [(1 << LZX_ALIGNED_TABLEBITS) +
98  unsigned char LENGTH_empty;
99 
100  /* this is used purely for doing the intel E8 transform */
101  unsigned char e8_buf[LZX_FRAME_SIZE];
102 };
103 
146 extern struct lzxd_stream *lzxd_init(struct mspack_system *system,
147  struct mspack_file *input,
148  struct mspack_file *output,
149  int window_bits,
150  int reset_interval,
151  int input_buffer_size,
152  off_t output_length,
153  char is_delta);
154 
155 /* see description of output_length in lzxd_init() */
156 extern void lzxd_set_output_length(struct lzxd_stream *lzx,
157  off_t output_length);
158 
174 extern int lzxd_set_reference_data(struct lzxd_stream *lzx,
175  struct mspack_system *system,
176  struct mspack_file *input,
177  unsigned int length);
178 
206 extern int lzxd_decompress(struct lzxd_stream *lzx, off_t out_bytes);
207 
214 void lzxd_free(struct lzxd_stream *lzx);
215 
216 #ifdef __cplusplus
217 }
218 #endif
219 
220 #endif
int lzxd_set_reference_data(struct lzxd_stream *lzx, struct mspack_system *system, struct mspack_file *input, unsigned int length)
Definition: lzxd.c:353
struct lzxd_stream * lzxd_init(struct mspack_system *system, struct mspack_file *input, struct mspack_file *output, int window_bits, int reset_interval, int input_buffer_size, off_t output_length, char is_delta)
Definition: lzxd.c:279
void lzxd_free(struct lzxd_stream *lzx)
Definition: lzxd.c:800
int lzxd_decompress(struct lzxd_stream *lzx, off_t out_bytes)
Definition: lzxd.c:393
void lzxd_set_output_length(struct lzxd_stream *lzx, off_t output_length)
Definition: lzxd.c:389
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
#define LZX_LENGTH_TABLEBITS
Definition: lzx.h:41
#define LZX_MAINTREE_MAXSYMBOLS
Definition: lzx.h:38
#define LZX_PRETREE_MAXSYMBOLS
Definition: lzx.h:36
#define LZX_LENGTH_MAXSYMBOLS
Definition: lzx.h:40
#define LZX_MAINTREE_TABLEBITS
Definition: lzx.h:39
#define LZX_ALIGNED_TABLEBITS
Definition: lzx.h:43
#define LZX_ALIGNED_MAXSYMBOLS
Definition: lzx.h:42
#define LZX_FRAME_SIZE
Definition: lzx.h:46
#define LZX_PRETREE_TABLEBITS
Definition: lzx.h:37
#define LZX_LENTABLE_SAFETY
Definition: lzx.h:44
int off_t
Definition: sftypes.h:41
unsigned short LENGTH_table[(1<< LZX_LENGTH_TABLEBITS)+(LZX_LENGTH_MAXSYMBOLS *2)]
Definition: lzx.h:95
off_t length
Definition: lzx.h:54
unsigned int R1
Definition: lzx.h:65
unsigned char LENGTH_len[LZX_LENGTH_MAXSYMBOLS+LZX_LENTABLE_SAFETY]
Definition: lzx.h:86
unsigned int frame
Definition: lzx.h:62
off_t offset
Definition: lzx.h:53
struct mspack_system * sys
Definition: lzx.h:49
unsigned char PRETREE_len[LZX_PRETREE_MAXSYMBOLS+LZX_LENTABLE_SAFETY]
Definition: lzx.h:84
unsigned int bit_buffer
Definition: lzx.h:81
unsigned char intel_started
Definition: lzx.h:71
unsigned int ref_data_size
Definition: lzx.h:58
unsigned char * window
Definition: lzx.h:56
unsigned char * i_ptr
Definition: lzx.h:80
unsigned char input_end
Definition: lzx.h:74
unsigned int R2
Definition: lzx.h:65
struct mspack_file * output
Definition: lzx.h:51
unsigned char * o_end
Definition: lzx.h:80
struct mspack_file * input
Definition: lzx.h:50
unsigned int block_remaining
Definition: lzx.h:67
unsigned int window_size
Definition: lzx.h:57
unsigned char ALIGNED_len[LZX_ALIGNED_MAXSYMBOLS+LZX_LENTABLE_SAFETY]
Definition: lzx.h:87
unsigned int bits_left
Definition: lzx.h:81
int error
Definition: lzx.h:77
signed int intel_filesize
Definition: lzx.h:69
unsigned char e8_buf[LZX_FRAME_SIZE]
Definition: lzx.h:101
unsigned char * o_ptr
Definition: lzx.h:80
unsigned char MAINTREE_len[LZX_MAINTREE_MAXSYMBOLS+LZX_LENTABLE_SAFETY]
Definition: lzx.h:85
unsigned int reset_interval
Definition: lzx.h:63
unsigned short MAINTREE_table[(1<< LZX_MAINTREE_TABLEBITS)+(LZX_MAINTREE_MAXSYMBOLS *2)]
Definition: lzx.h:93
unsigned int block_length
Definition: lzx.h:66
unsigned int R0
Definition: lzx.h:65
unsigned int frame_posn
Definition: lzx.h:61
unsigned int num_offsets
Definition: lzx.h:59
unsigned int inbuf_size
Definition: lzx.h:81
unsigned int window_posn
Definition: lzx.h:60
unsigned char LENGTH_empty
Definition: lzx.h:98
unsigned char block_type
Definition: lzx.h:72
unsigned char * i_end
Definition: lzx.h:80
unsigned short ALIGNED_table[(1<< LZX_ALIGNED_TABLEBITS)+(LZX_ALIGNED_MAXSYMBOLS *2)]
Definition: lzx.h:97
unsigned char is_delta
Definition: lzx.h:75
unsigned char * inbuf
Definition: lzx.h:80
unsigned char header_read
Definition: lzx.h:73
unsigned short PRETREE_table[(1<< LZX_PRETREE_TABLEBITS)+(LZX_PRETREE_MAXSYMBOLS *2)]
Definition: lzx.h:91
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237