1
/* This file is part of libmspack.
2
* (C) 2003-2004 Stuart Caie.
4
* The deflate method was created by Phil Katz. MSZIP is equivalent to the
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
10
* For further details, see the file COPYING.LIB distributed with libmspack
13
#ifndef MSPACK_MSZIP_H
14
#define MSPACK_MSZIP_H 1
20
/* MSZIP (deflate) compression / (inflate) decompression definitions */
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)
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)
34
# define MSZIP_LITERAL_TABLESIZE ((1 << MSZIP_LITERAL_TABLEBITS) + \
35
(MSZIP_LITERAL_MAXSYMBOLS * 2))
38
#if (1 << MSZIP_DISTANCE_TABLEBITS) < (MSZIP_DISTANCE_MAXSYMBOLS * 2)
39
# define MSZIP_DISTANCE_TABLESIZE (MSZIP_DISTANCE_MAXSYMBOLS * 4)
41
# define MSZIP_DISTANCE_TABLESIZE ((1 << MSZIP_DISTANCE_TABLEBITS) + \
42
(MSZIP_DISTANCE_MAXSYMBOLS * 2))
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 */
51
/* inflate() will call this whenever the window should be emptied. */
52
int (*flush_window)(struct mszipd_stream *, unsigned int);
54
int error, repair_mode, bytes_output;
57
unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end, input_end;
58
unsigned int bit_buffer, bits_left, inbuf_size;
61
/* huffman code lengths */
62
unsigned char LITERAL_len[MSZIP_LITERAL_MAXSYMBOLS];
63
unsigned char DISTANCE_len[MSZIP_DISTANCE_MAXSYMBOLS];
65
/* huffman decoding tables */
66
unsigned short LITERAL_table [MSZIP_LITERAL_TABLESIZE];
67
unsigned short DISTANCE_table[MSZIP_DISTANCE_TABLESIZE];
69
/* 32kb history window */
70
unsigned char window[MSZIP_FRAME_SIZE];
73
/* allocates MS-ZIP decompression stream for decoding the given stream.
75
* - uses system->alloc() to allocate memory
77
* - returns NULL if not enough memory
79
* - input_buffer_size is how many bytes to use as an input bitstream buffer
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.
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,
91
/* decompresses, or decompresses more of, an MS-ZIP stream.
93
* - out_bytes of data will be decompressed and the function will return
94
* with an MSPACK_ERR_OK return code.
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().
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()
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,
109
extern int mszipd_decompress(struct mszipd_stream *zip, off_t out_bytes);
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
114
extern int mszipd_decompress_kwaj(struct mszipd_stream *zip);
116
/* frees all stream associated with an MS-ZIP data stream
118
* - calls system->free() using the system pointer given in mszipd_init()
120
void mszipd_free(struct mszipd_stream *zip);