Rizin
unix-like reverse engineering framework and cli tools
mszip.h
Go to the documentation of this file.
1 /* This file is part of libmspack.
2  * (C) 2003-2004 Stuart Caie.
3  *
4  * The deflate method was created by Phil Katz. MSZIP is equivalent to the
5  * deflate method.
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_MSZIP_H
14 #define MSPACK_MSZIP_H 1
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /* MSZIP (deflate) compression / (inflate) decompression definitions */
21 
22 #define MSZIP_FRAME_SIZE (32768) /* size of LZ history window */
23 #define MSZIP_LITERAL_MAXSYMBOLS (288) /* literal/length huffman tree */
24 #define MSZIP_LITERAL_TABLEBITS (9)
25 #define MSZIP_DISTANCE_MAXSYMBOLS (32) /* distance huffman tree */
26 #define MSZIP_DISTANCE_TABLEBITS (6)
27 
28 /* if there are less direct lookup entries than symbols, the longer
29  * code pointers will be <= maxsymbols. This must not happen, or we
30  * will decode entries badly */
31 #if (1 << MSZIP_LITERAL_TABLEBITS) < (MSZIP_LITERAL_MAXSYMBOLS * 2)
32 # define MSZIP_LITERAL_TABLESIZE (MSZIP_LITERAL_MAXSYMBOLS * 4)
33 #else
34 # define MSZIP_LITERAL_TABLESIZE ((1 << MSZIP_LITERAL_TABLEBITS) + \
35  (MSZIP_LITERAL_MAXSYMBOLS * 2))
36 #endif
37 
38 #if (1 << MSZIP_DISTANCE_TABLEBITS) < (MSZIP_DISTANCE_MAXSYMBOLS * 2)
39 # define MSZIP_DISTANCE_TABLESIZE (MSZIP_DISTANCE_MAXSYMBOLS * 4)
40 #else
41 # define MSZIP_DISTANCE_TABLESIZE ((1 << MSZIP_DISTANCE_TABLEBITS) + \
42  (MSZIP_DISTANCE_MAXSYMBOLS * 2))
43 #endif
44 
45 struct mszipd_stream {
46  struct mspack_system *sys; /* I/O routines */
47  struct mspack_file *input; /* input file handle */
48  struct mspack_file *output; /* output file handle */
49  unsigned int window_posn; /* offset within window */
50 
51  /* inflate() will call this whenever the window should be emptied. */
52  int (*flush_window)(struct mszipd_stream *, unsigned int);
53 
55 
56  /* I/O buffering */
57  unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end, input_end;
59 
60 
61  /* huffman code lengths */
64 
65  /* huffman decoding tables */
68 
69  /* 32kb history window */
70  unsigned char window[MSZIP_FRAME_SIZE];
71 };
72 
73 /* allocates MS-ZIP decompression stream for decoding the given stream.
74  *
75  * - uses system->alloc() to allocate memory
76  *
77  * - returns NULL if not enough memory
78  *
79  * - input_buffer_size is how many bytes to use as an input bitstream buffer
80  *
81  * - if repair_mode is non-zero, errors in decompression will be skipped
82  * and 'holes' left will be filled with zero bytes. This allows at least
83  * a partial recovery of erroneous data.
84  */
85 extern struct mszipd_stream *mszipd_init(struct mspack_system *system,
86  struct mspack_file *input,
87  struct mspack_file *output,
88  int input_buffer_size,
89  int repair_mode);
90 
91 /* decompresses, or decompresses more of, an MS-ZIP stream.
92  *
93  * - out_bytes of data will be decompressed and the function will return
94  * with an MSPACK_ERR_OK return code.
95  *
96  * - decompressing will stop as soon as out_bytes is reached. if the true
97  * amount of bytes decoded spills over that amount, they will be kept for
98  * a later invocation of mszipd_decompress().
99  *
100  * - the output bytes will be passed to the system->write() function given in
101  * mszipd_init(), using the output file handle given in mszipd_init(). More
102  * than one call may be made to system->write()
103  *
104  * - MS-ZIP will read input bytes as necessary using the system->read()
105  * function given in mszipd_init(), using the input file handle given in
106  * mszipd_init(). This will continue until system->read() returns 0 bytes,
107  * or an error.
108  */
109 extern int mszipd_decompress(struct mszipd_stream *zip, off_t out_bytes);
110 
111 /* decompresses an entire MS-ZIP stream in a KWAJ file. Acts very much
112  * like mszipd_decompress(), but doesn't take an out_bytes parameter
113  */
114 extern int mszipd_decompress_kwaj(struct mszipd_stream *zip);
115 
116 /* frees all stream associated with an MS-ZIP data stream
117  *
118  * - calls system->free() using the system pointer given in mszipd_init()
119  */
120 void mszipd_free(struct mszipd_stream *zip);
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif
#define MSZIP_FRAME_SIZE
Definition: mszip.h:22
void mszipd_free(struct mszipd_stream *zip)
Definition: mszipd.c:508
struct mszipd_stream * mszipd_init(struct mspack_system *system, struct mspack_file *input, struct mspack_file *output, int input_buffer_size, int repair_mode)
Definition: mszipd.c:342
#define MSZIP_DISTANCE_MAXSYMBOLS
Definition: mszip.h:25
#define MSZIP_DISTANCE_TABLESIZE
Definition: mszip.h:41
int mszipd_decompress_kwaj(struct mszipd_stream *zip)
Definition: mszipd.c:469
#define MSZIP_LITERAL_TABLESIZE
Definition: mszip.h:32
#define MSZIP_LITERAL_MAXSYMBOLS
Definition: mszip.h:23
int mszipd_decompress(struct mszipd_stream *zip, off_t out_bytes)
Definition: mszipd.c:384
static int
Definition: sfsocketcall.h:114
int off_t
Definition: sftypes.h:41
int error
Definition: mszip.h:54
struct mspack_system * sys
Definition: mszip.h:46
unsigned char LITERAL_len[MSZIP_LITERAL_MAXSYMBOLS]
Definition: mszip.h:62
int bytes_output
Definition: mszip.h:54
unsigned int inbuf_size
Definition: mszip.h:58
struct mspack_file * input
Definition: mszip.h:47
unsigned int bits_left
Definition: mszip.h:58
unsigned char * i_ptr
Definition: mszip.h:57
unsigned char * o_ptr
Definition: mszip.h:57
unsigned char input_end
Definition: mszip.h:57
unsigned char * o_end
Definition: mszip.h:57
unsigned int window_posn
Definition: mszip.h:49
unsigned char * inbuf
Definition: mszip.h:57
int(* flush_window)(struct mszipd_stream *, unsigned int)
Definition: mszip.h:52
unsigned short DISTANCE_table[MSZIP_DISTANCE_TABLESIZE]
Definition: mszip.h:67
int repair_mode
Definition: mszip.h:54
unsigned char * i_end
Definition: mszip.h:57
unsigned char DISTANCE_len[MSZIP_DISTANCE_MAXSYMBOLS]
Definition: mszip.h:63
unsigned int bit_buffer
Definition: mszip.h:58
struct mspack_file * output
Definition: mszip.h:48
unsigned short LITERAL_table[MSZIP_LITERAL_TABLESIZE]
Definition: mszip.h:66
Definition: zipint.h:278
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237