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).
36
#ifdef HAVE_MYSQL_CONFIG_H
37
#include "../../include/config.h"
43
/* We currently allow this on all platforms */
47
#include "my_global.h"
55
/* Start of MySQL Specific Information */
57
/* Some personal debugging functions */
58
#define WATCHPOINT fprintf(stderr, "\nWATCHPOINT %s:%d (%s)\n", __FILE__, __LINE__,__func__);fflush(stderr);
59
#define WATCHPOINT_STRING(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %s\n", __FILE__, __LINE__,__func__,A);fflush(stderr);
60
#define WATCHPOINT_NUMBER(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %d\n", __FILE__, __LINE__,__func__,(int)(A));fflush(stderr);
61
#define WATCHPOINT_ERRNO(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %s\n", __FILE__, __LINE__,__func__, strerror(A));A= 0;fflush(stderr);
64
ulonglong + ulonglong + ulonglong + ulonglong + uchar
66
#define AZMETA_BUFFER_SIZE sizeof(unsigned long long) \
67
+ sizeof(unsigned long long) + sizeof(unsigned long long) + sizeof(unsigned long long) \
68
+ sizeof(unsigned int) + sizeof(unsigned int) \
69
+ sizeof(unsigned int) + sizeof(unsigned int) \
70
+ sizeof(unsigned char)
72
#define AZHEADER_SIZE 29
74
#define AZ_MAGIC_POS 0
75
#define AZ_VERSION_POS 1
76
#define AZ_MINOR_VERSION_POS 2
77
#define AZ_BLOCK_POS 3
78
#define AZ_STRATEGY_POS 4
80
#define AZ_FRM_LENGTH_POS 9
81
#define AZ_META_POS 13
82
#define AZ_META_LENGTH_POS 17
83
#define AZ_START_POS 21
85
#define AZ_FLUSH_POS 37
86
#define AZ_CHECK_POS 45
87
#define AZ_AUTOINCREMENT_POS 53
88
#define AZ_LONGEST_POS 61
89
#define AZ_SHORTEST_POS 65
90
#define AZ_COMMENT_POS 69
91
#define AZ_COMMENT_LENGTH_POS 73
92
#define AZ_DIRTY_POS 77
98
#define AZ_STATE_CLEAN 0
99
#define AZ_STATE_DIRTY 1
100
#define AZ_STATE_SAVED 2
101
#define AZ_STATE_CRASHED 3
104
The 'zlib' compression library provides in-memory compression and
105
decompression functions, including integrity checks of the uncompressed
106
data. This version of the library supports only one compression method
107
(deflation) but other algorithms will be added later and will have the same
110
Compression can be done in a single step if the buffers are large
111
enough (for example if an input file is mmap'ed), or can be done by
112
repeated calls of the compression function. In the latter case, the
113
application must provide more input and/or consume the output
114
(providing more output space) before each call.
116
The compressed data format used by default by the in-memory functions is
117
the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
118
around a deflate stream, which is itself documented in RFC 1951.
120
The library also supports reading and writing files in gzip (.gz) format
121
with an interface similar to that of stdio using the functions that start
122
with "gz". The gzip format is different from the zlib format. gzip is a
123
gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
125
This library can optionally read and write gzip streams in memory as well.
127
The zlib format was designed to be compact and fast for use in memory
128
and on communications channels. The gzip format was designed for single-
129
file compression on file systems, has a larger header than zlib to maintain
130
directory information, and uses a different, slower check method than zlib.
132
The library does not install any signal handler. The decoder checks
133
the consistency of the compressed data, so the library should never
134
crash even in case of corrupted input.
139
The application must update next_in and avail_in when avail_in has
140
dropped to zero. It must update next_out and avail_out when avail_out
141
has dropped to zero. The application must initialize zalloc, zfree and
142
opaque before calling the init function. All other fields are set by the
143
compression library and must not be updated by the application.
145
The opaque value provided by the application will be passed as the first
146
parameter for calls of zalloc and zfree. This can be useful for custom
147
memory management. The compression library attaches no meaning to the
150
zalloc must return Z_NULL if there is not enough memory for the object.
151
If zlib is used in a multi-threaded application, zalloc and zfree must be
154
On 16-bit systems, the functions zalloc and zfree must be able to allocate
155
exactly 65536 bytes, but will not be required to allocate more than this
156
if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
157
pointers returned by zalloc for objects of exactly 65536 bytes *must*
158
have their offset normalized to zero. The default allocation function
159
provided by this library ensures this (see zutil.c). To reduce memory
160
requirements and avoid any allocation of 64K objects, at the expense of
161
compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
163
The fields total_in and total_out can be used for statistics or
164
progress reports. After compression, total_in holds the total size of
165
the uncompressed data and may be saved for use in the decompressor
166
(particularly if the decompressor wants to decompress everything in
173
#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
174
#define Z_SYNC_FLUSH 2
175
#define Z_FULL_FLUSH 3
178
/* Allowed flush values; see deflate() and inflate() below for details */
181
#define Z_STREAM_END 1
182
#define Z_NEED_DICT 2
184
#define Z_STREAM_ERROR (-2)
185
#define Z_DATA_ERROR (-3)
186
#define Z_MEM_ERROR (-4)
187
#define Z_BUF_ERROR (-5)
188
#define Z_VERSION_ERROR (-6)
189
/* Return codes for the compression/decompression functions. Negative
190
* values are errors, positive values are used for special but normal events.
193
#define Z_NO_COMPRESSION 0
194
#define Z_BEST_SPEED 1
195
#define Z_BEST_COMPRESSION 9
196
#define Z_DEFAULT_COMPRESSION (-1)
197
/* compression levels */
200
#define Z_HUFFMAN_ONLY 2
203
#define Z_DEFAULT_STRATEGY 0
204
/* compression strategy; see deflateInit2() below for details */
208
#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
210
/* Possible values of the data_type field (though see inflate()) */
213
/* The deflate compression method (the only one supported in this version) */
215
#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
216
#define AZ_BUFSIZE_READ 32768
217
#define AZ_BUFSIZE_WRITE 16384
231
typedef struct azio_container_st azio_container_st;
233
struct azio_container_st {
235
az_thread_type ready;
239
pthread_mutex_t thresh_mutex;
240
pthread_cond_t threshhold;
241
pthread_t mainthread; /* Thread descriptor */
245
typedef struct azio_stream {
247
int z_err; /* error code for last stream operation */
248
int z_eof; /* set if end of input file */
249
File file; /* .gz file */
250
Byte *inbuf; /* input buffer */
251
Byte buffer1[AZ_BUFSIZE_READ]; /* input buffer */
252
Byte buffer2[AZ_BUFSIZE_READ]; /* input buffer */
253
Byte outbuf[AZ_BUFSIZE_WRITE]; /* output buffer */
254
int aio_inited; /* Are we good to go */
255
uLong crc; /* crc32 of uncompressed data */
256
char *msg; /* error message */
257
char mode; /* 'w' or 'r' */
258
size_t start; /* start of compressed data in file (header skipped) */
259
size_t in; /* bytes into deflate or inflate */
260
size_t out; /* bytes out of deflate or inflate */
261
size_t pos; /* bytes out of deflate or inflate */
262
int back; /* one character push-back */
263
int last; /* true if push-back is last character */
264
unsigned char version; /* Version */
265
unsigned char minor_version; /* Version */
266
unsigned int block_size; /* Block Size */
267
unsigned long long check_point; /* Last position we checked */
268
unsigned long long forced_flushes; /* Forced Flushes */
269
unsigned long long rows; /* rows */
270
unsigned long long auto_increment; /* auto increment field */
271
unsigned int longest_row; /* Longest row */
272
unsigned int shortest_row; /* Shortest row */
273
unsigned char dirty; /* State of file */
274
unsigned int frm_start_pos; /* Position for start of FRM */
275
unsigned int frm_length; /* Position for start of FRM */
276
unsigned int comment_start_pos; /* Position for start of comment */
277
unsigned int comment_length; /* Position for start of comment */
279
azio_container_st container;
285
/* basic functions */
287
int azopen(azio_stream *s, const char *path, int Flags, az_method method);
289
Opens a gzip (.gz) file for reading or writing. The mode parameter
290
is as in fopen ("rb" or "wb") but can also include a compression level
291
("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
292
Huffman only compression as in "wb1h", or 'R' for run-length encoding
293
as in "wb1R". (See the description of deflateInit2 for more information
294
about the strategy parameter.)
296
azopen can be used to read a file which is not in gzip format; in this
297
case gzread will directly read from the file without decompression.
299
azopen returns NULL if the file could not be opened or if there was
300
insufficient memory to allocate the (de)compression state; errno
301
can be checked to distinguish the two cases (if errno is zero, the
302
zlib error is Z_MEM_ERROR). */
304
int azdopen(azio_stream *s,File fd, int Flags);
306
azdopen() associates a azio_stream with the file descriptor fd. File
307
descriptors are obtained from calls like open, dup, creat, pipe or
308
fileno (in the file has been previously opened with fopen).
309
The mode parameter is as in azopen.
310
The next call of gzclose on the returned azio_stream will also close the
311
file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
312
descriptor fd. If you want to keep fd open, use azdopen(dup(fd), mode).
313
azdopen returns NULL if there was insufficient memory to allocate
314
the (de)compression state.
318
unsigned int azread_internal( azio_stream *s, voidp buf, unsigned int len, int *error);
320
This function is legacy, do not use.
322
Reads the given number of uncompressed bytes from the compressed file.
323
If the input file was not in gzip format, gzread copies the given number
324
of bytes into the buffer.
325
gzread returns the number of uncompressed bytes actually read (0 for
326
end of file, -1 for error). */
328
extern int azflush(azio_stream *file, int flush);
330
Flushes all pending output into the compressed file. The parameter
331
flush is as in the deflate() function. The return value is the zlib
332
error number (see function gzerror below). gzflush returns Z_OK if
333
the flush parameter is Z_FINISH and all output could be flushed.
334
gzflush should be called only when strictly necessary because it can
338
extern size_t azseek (azio_stream *file,
339
size_t offset, int whence);
341
Sets the starting position for the next gzread or gzwrite on the
342
given compressed file. The offset represents a number of bytes in the
343
uncompressed data stream. The whence parameter is defined as in lseek(2);
344
the value SEEK_END is not supported.
345
If the file is opened for reading, this function is emulated but can be
346
extremely slow. If the file is opened for writing, only forward seeks are
347
supported; gzseek then compresses a sequence of zeroes up to the new
350
gzseek returns the resulting offset location as measured in bytes from
351
the beginning of the uncompressed stream, or -1 in case of error, in
352
particular if the file is opened for writing and the new starting position
353
would be before the current position.
356
extern size_t aztell(azio_stream *file);
358
Returns the starting position for the next gzread or gzwrite on the
359
given compressed file. This position represents a number of bytes in the
360
uncompressed data stream.
362
gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
365
extern int azclose(azio_stream *file);
367
Flushes all pending output if necessary, closes the compressed file
368
and deallocates all the (de)compression state. The return value is the zlib
369
error number (see function gzerror below).
372
int azread_init(azio_stream *s);
373
size_t azwrite_row(azio_stream *s, void *buf, unsigned int len);
374
size_t azread_row(azio_stream *s, int *error);
376
extern int azwrite_frm (azio_stream *s, char *blob, unsigned int length);
377
extern int azread_frm (azio_stream *s, char *blob);
378
extern int azwrite_comment (azio_stream *s, char *blob, unsigned int length);
379
extern int azread_comment (azio_stream *s, char *blob);