2
This libary has been modified for use by the MySQL Archive Engine.
6
/* zlib.h -- interface of the 'zlib' general purpose compression library
7
version 1.2.3, July 18th, 2005
9
Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
11
This software is provided 'as-is', without any express or implied
12
warranty. In no event will the authors be held liable for any damages
13
arising from the use of this software.
15
Permission is granted to anyone to use this software for any purpose,
16
including commercial applications, and to alter it and redistribute it
17
freely, subject to the following restrictions:
19
1. The origin of this software must not be misrepresented; you must not
20
claim that you wrote the original software. If you use this software
21
in a product, an acknowledgment in the product documentation would be
22
appreciated but is not required.
23
2. Altered source versions must be plainly marked as such, and must not be
24
misrepresented as being the original software.
25
3. This notice may not be removed or altered from any source distribution.
27
Jean-loup Gailly Mark Adler
28
jloup@gzip.org madler@alumni.caltech.edu
31
The data format used by the zlib library is described by RFCs (Request for
32
Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
33
(zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
38
#include "../../mysys/mysys_priv.h"
44
/* Start of MySQL Specific Information */
47
ulonglong + ulonglong + ulonglong + ulonglong + uchar
49
#define AZMETA_BUFFER_SIZE sizeof(unsigned long long) \
50
+ sizeof(unsigned long long) + sizeof(unsigned long long) + sizeof(unsigned long long) \
51
+ sizeof(unsigned int) + sizeof(unsigned int) \
52
+ sizeof(unsigned int) + sizeof(unsigned int) \
53
+ sizeof(unsigned char)
55
#define AZHEADER_SIZE 29
57
#define AZ_MAGIC_POS 0
58
#define AZ_VERSION_POS 1
59
#define AZ_MINOR_VERSION_POS 2
60
#define AZ_BLOCK_POS 3
61
#define AZ_STRATEGY_POS 4
63
#define AZ_FRM_LENGTH_POS 9
64
#define AZ_META_POS 13
65
#define AZ_META_LENGTH_POS 17
66
#define AZ_START_POS 21
68
#define AZ_FLUSH_POS 37
69
#define AZ_CHECK_POS 45
70
#define AZ_AUTOINCREMENT_POS 53
71
#define AZ_LONGEST_POS 61
72
#define AZ_SHORTEST_POS 65
73
#define AZ_COMMENT_POS 69
74
#define AZ_COMMENT_LENGTH_POS 73
75
#define AZ_DIRTY_POS 77
81
#define AZ_STATE_CLEAN 0
82
#define AZ_STATE_DIRTY 1
83
#define AZ_STATE_SAVED 2
84
#define AZ_STATE_CRASHED 3
87
The 'zlib' compression library provides in-memory compression and
88
decompression functions, including integrity checks of the uncompressed
89
data. This version of the library supports only one compression method
90
(deflation) but other algorithms will be added later and will have the same
93
Compression can be done in a single step if the buffers are large
94
enough (for example if an input file is mmap'ed), or can be done by
95
repeated calls of the compression function. In the latter case, the
96
application must provide more input and/or consume the output
97
(providing more output space) before each call.
99
The compressed data format used by default by the in-memory functions is
100
the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
101
around a deflate stream, which is itself documented in RFC 1951.
103
The library also supports reading and writing files in gzip (.gz) format
104
with an interface similar to that of stdio using the functions that start
105
with "gz". The gzip format is different from the zlib format. gzip is a
106
gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
108
This library can optionally read and write gzip streams in memory as well.
110
The zlib format was designed to be compact and fast for use in memory
111
and on communications channels. The gzip format was designed for single-
112
file compression on file systems, has a larger header than zlib to maintain
113
directory information, and uses a different, slower check method than zlib.
115
The library does not install any signal handler. The decoder checks
116
the consistency of the compressed data, so the library should never
117
crash even in case of corrupted input.
122
The application must update next_in and avail_in when avail_in has
123
dropped to zero. It must update next_out and avail_out when avail_out
124
has dropped to zero. The application must initialize zalloc, zfree and
125
opaque before calling the init function. All other fields are set by the
126
compression library and must not be updated by the application.
128
The opaque value provided by the application will be passed as the first
129
parameter for calls of zalloc and zfree. This can be useful for custom
130
memory management. The compression library attaches no meaning to the
133
zalloc must return Z_NULL if there is not enough memory for the object.
134
If zlib is used in a multi-threaded application, zalloc and zfree must be
137
On 16-bit systems, the functions zalloc and zfree must be able to allocate
138
exactly 65536 bytes, but will not be required to allocate more than this
139
if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
140
pointers returned by zalloc for objects of exactly 65536 bytes *must*
141
have their offset normalized to zero. The default allocation function
142
provided by this library ensures this (see zutil.c). To reduce memory
143
requirements and avoid any allocation of 64K objects, at the expense of
144
compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
146
The fields total_in and total_out can be used for statistics or
147
progress reports. After compression, total_in holds the total size of
148
the uncompressed data and may be saved for use in the decompressor
149
(particularly if the decompressor wants to decompress everything in
156
#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
157
#define Z_SYNC_FLUSH 2
158
#define Z_FULL_FLUSH 3
161
/* Allowed flush values; see deflate() and inflate() below for details */
164
#define Z_STREAM_END 1
165
#define Z_NEED_DICT 2
167
#define Z_STREAM_ERROR (-2)
168
#define Z_DATA_ERROR (-3)
169
#define Z_MEM_ERROR (-4)
170
#define Z_BUF_ERROR (-5)
171
#define Z_VERSION_ERROR (-6)
172
/* Return codes for the compression/decompression functions. Negative
173
* values are errors, positive values are used for special but normal events.
176
#define Z_NO_COMPRESSION 0
177
#define Z_BEST_SPEED 1
178
#define Z_BEST_COMPRESSION 9
179
#define Z_DEFAULT_COMPRESSION (-1)
180
/* compression levels */
183
#define Z_HUFFMAN_ONLY 2
186
#define Z_DEFAULT_STRATEGY 0
187
/* compression strategy; see deflateInit2() below for details */
191
#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
193
/* Possible values of the data_type field (though see inflate()) */
196
/* The deflate compression method (the only one supported in this version) */
198
#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
199
#define AZ_BUFSIZE_READ 32768
200
#define AZ_BUFSIZE_WRITE 16384
203
typedef struct azio_stream {
205
int z_err; /* error code for last stream operation */
206
int z_eof; /* set if end of input file */
207
File file; /* .gz file */
208
Byte inbuf[AZ_BUFSIZE_READ]; /* input buffer */
209
Byte outbuf[AZ_BUFSIZE_WRITE]; /* output buffer */
210
uLong crc; /* crc32 of uncompressed data */
211
char *msg; /* error message */
212
int transparent; /* 1 if input file is not a .gz file */
213
char mode; /* 'w' or 'r' */
214
my_off_t start; /* start of compressed data in file (header skipped) */
215
my_off_t in; /* bytes into deflate or inflate */
216
my_off_t out; /* bytes out of deflate or inflate */
217
int back; /* one character push-back */
218
int last; /* true if push-back is last character */
219
unsigned char version; /* Version */
220
unsigned char minor_version; /* Version */
221
unsigned int block_size; /* Block Size */
222
unsigned long long check_point; /* Last position we checked */
223
unsigned long long forced_flushes; /* Forced Flushes */
224
unsigned long long rows; /* rows */
225
unsigned long long auto_increment; /* auto increment field */
226
unsigned int longest_row; /* Longest row */
227
unsigned int shortest_row; /* Shortest row */
228
unsigned char dirty; /* State of file */
229
unsigned int frm_start_pos; /* Position for start of FRM */
230
unsigned int frm_length; /* Position for start of FRM */
231
unsigned int comment_start_pos; /* Position for start of comment */
232
unsigned int comment_length; /* Position for start of comment */
235
/* basic functions */
237
extern int azopen(azio_stream *s, const char *path, int Flags);
239
Opens a gzip (.gz) file for reading or writing. The mode parameter
240
is as in fopen ("rb" or "wb") but can also include a compression level
241
("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
242
Huffman only compression as in "wb1h", or 'R' for run-length encoding
243
as in "wb1R". (See the description of deflateInit2 for more information
244
about the strategy parameter.)
246
azopen can be used to read a file which is not in gzip format; in this
247
case gzread will directly read from the file without decompression.
249
azopen returns NULL if the file could not be opened or if there was
250
insufficient memory to allocate the (de)compression state; errno
251
can be checked to distinguish the two cases (if errno is zero, the
252
zlib error is Z_MEM_ERROR). */
254
int azdopen(azio_stream *s,File fd, int Flags);
256
azdopen() associates a azio_stream with the file descriptor fd. File
257
descriptors are obtained from calls like open, dup, creat, pipe or
258
fileno (in the file has been previously opened with fopen).
259
The mode parameter is as in azopen.
260
The next call of gzclose on the returned azio_stream will also close the
261
file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
262
descriptor fd. If you want to keep fd open, use azdopen(dup(fd), mode).
263
azdopen returns NULL if there was insufficient memory to allocate
264
the (de)compression state.
268
extern unsigned int azread ( azio_stream *s, voidp buf, size_t len, int *error);
270
Reads the given number of uncompressed bytes from the compressed file.
271
If the input file was not in gzip format, gzread copies the given number
272
of bytes into the buffer.
273
gzread returns the number of uncompressed bytes actually read (0 for
274
end of file, -1 for error). */
276
extern unsigned int azwrite (azio_stream *s, const voidp buf, unsigned int len);
278
Writes the given number of uncompressed bytes into the compressed file.
279
azwrite returns the number of uncompressed bytes actually written
280
(0 in case of error).
284
extern int azflush(azio_stream *file, int flush);
286
Flushes all pending output into the compressed file. The parameter
287
flush is as in the deflate() function. The return value is the zlib
288
error number (see function gzerror below). gzflush returns Z_OK if
289
the flush parameter is Z_FINISH and all output could be flushed.
290
gzflush should be called only when strictly necessary because it can
294
extern my_off_t azseek (azio_stream *file,
295
my_off_t offset, int whence);
297
Sets the starting position for the next gzread or gzwrite on the
298
given compressed file. The offset represents a number of bytes in the
299
uncompressed data stream. The whence parameter is defined as in lseek(2);
300
the value SEEK_END is not supported.
301
If the file is opened for reading, this function is emulated but can be
302
extremely slow. If the file is opened for writing, only forward seeks are
303
supported; gzseek then compresses a sequence of zeroes up to the new
306
gzseek returns the resulting offset location as measured in bytes from
307
the beginning of the uncompressed stream, or -1 in case of error, in
308
particular if the file is opened for writing and the new starting position
309
would be before the current position.
312
extern int azrewind(azio_stream *file);
314
Rewinds the given file. This function is supported only for reading.
316
gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
319
extern my_off_t aztell(azio_stream *file);
321
Returns the starting position for the next gzread or gzwrite on the
322
given compressed file. This position represents a number of bytes in the
323
uncompressed data stream.
325
gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
328
extern int azclose(azio_stream *file);
330
Flushes all pending output if necessary, closes the compressed file
331
and deallocates all the (de)compression state. The return value is the zlib
332
error number (see function gzerror below).
335
extern int azwrite_frm (azio_stream *s, char *blob, unsigned int length);
336
extern int azread_frm (azio_stream *s, char *blob);
337
extern int azwrite_comment (azio_stream *s, char *blob, unsigned int length);
338
extern int azread_comment (azio_stream *s, char *blob);