1 /*
2   This libary has been modified for use by the MySQL Archive Engine.
3      -Brian Aker
4 */
5 
6 /* zlib.h -- interface of the 'zlib' general purpose compression library
7   version 1.2.3, July 18th, 2005
8 
9   Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
10 
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.
14 
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:
18 
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.
26 
27   Jean-loup Gailly        Mark Adler
28   jloup@gzip.org          madler@alumni.caltech.edu
29 
30 
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).
34 */
35 
36 #include "../../mysys/mysys_priv.h"
37 #include <my_dir.h>
38 #include <zlib.h>
39 
40 #ifdef  __cplusplus
41 extern "C" {
42 #endif
43 /* Start of MySQL Specific Information */
44 
45 /*
46   ulonglong + ulonglong + ulonglong + ulonglong + uchar
47 */
48 #define AZMETA_BUFFER_SIZE sizeof(unsigned long long) \
49   + sizeof(unsigned long long) + sizeof(unsigned long long) + sizeof(unsigned long long) \
50   + sizeof(unsigned int) + sizeof(unsigned int) \
51   + sizeof(unsigned int) + sizeof(unsigned int) \
52   + sizeof(unsigned char)
53 
54 #define AZHEADER_SIZE 29
55 
56 #define AZ_MAGIC_POS 0
57 #define AZ_VERSION_POS 1
58 #define AZ_MINOR_VERSION_POS 2
59 #define AZ_BLOCK_POS 3
60 #define AZ_STRATEGY_POS 4
61 #define AZ_FRM_POS 5
62 #define AZ_FRM_LENGTH_POS 9
63 #define AZ_META_POS 13
64 #define AZ_META_LENGTH_POS 17
65 #define AZ_START_POS 21
66 #define AZ_ROW_POS 29
67 #define AZ_FLUSH_POS 37
68 #define AZ_CHECK_POS 45
69 #define AZ_AUTOINCREMENT_POS 53
70 #define AZ_LONGEST_POS 61
71 #define AZ_SHORTEST_POS 65
72 #define AZ_COMMENT_POS 69
73 #define AZ_COMMENT_LENGTH_POS 73
74 #define AZ_DIRTY_POS 77
75 
76 
77 /*
78   Flags for state
79 */
80 #define AZ_STATE_CLEAN 0
81 #define AZ_STATE_DIRTY 1
82 #define AZ_STATE_SAVED 2
83 #define AZ_STATE_CRASHED 3
84 
85 /*
86      The 'zlib' compression library provides in-memory compression and
87   decompression functions, including integrity checks of the uncompressed
88   data.  This version of the library supports only one compression method
89   (deflation) but other algorithms will be added later and will have the same
90   stream interface.
91 
92      Compression can be done in a single step if the buffers are large
93   enough (for example if an input file is mmap'ed), or can be done by
94   repeated calls of the compression function.  In the latter case, the
95   application must provide more input and/or consume the output
96   (providing more output space) before each call.
97 
98      The compressed data format used by default by the in-memory functions is
99   the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
100   around a deflate stream, which is itself documented in RFC 1951.
101 
102      The library also supports reading and writing files in gzip (.gz) format
103   with an interface similar to that of stdio using the functions that start
104   with "gz".  The gzip format is different from the zlib format.  gzip is a
105   gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
106 
107      This library can optionally read and write gzip streams in memory as well.
108 
109      The zlib format was designed to be compact and fast for use in memory
110   and on communications channels.  The gzip format was designed for single-
111   file compression on file systems, has a larger header than zlib to maintain
112   directory information, and uses a different, slower check method than zlib.
113 
114      The library does not install any signal handler. The decoder checks
115   the consistency of the compressed data, so the library should never
116   crash even in case of corrupted input.
117 */
118 
119 
120 /*
121    The application must update next_in and avail_in when avail_in has
122    dropped to zero. It must update next_out and avail_out when avail_out
123    has dropped to zero. The application must initialize zalloc, zfree and
124    opaque before calling the init function. All other fields are set by the
125    compression library and must not be updated by the application.
126 
127    The opaque value provided by the application will be passed as the first
128    parameter for calls of zalloc and zfree. This can be useful for custom
129    memory management. The compression library attaches no meaning to the
130    opaque value.
131 
132    zalloc must return Z_NULL if there is not enough memory for the object.
133    If zlib is used in a multi-threaded application, zalloc and zfree must be
134    thread safe.
135 
136    On 16-bit systems, the functions zalloc and zfree must be able to allocate
137    exactly 65536 bytes, but will not be required to allocate more than this
138    if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
139    pointers returned by zalloc for objects of exactly 65536 bytes *must*
140    have their offset normalized to zero. The default allocation function
141    provided by this library ensures this (see zutil.c). To reduce memory
142    requirements and avoid any allocation of 64K objects, at the expense of
143    compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
144 
145    The fields total_in and total_out can be used for statistics or
146    progress reports. After compression, total_in holds the total size of
147    the uncompressed data and may be saved for use in the decompressor
148    (particularly if the decompressor wants to decompress everything in
149    a single step).
150 */
151 
152                         /* constants */
153 
154 #define Z_NO_FLUSH      0
155 #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
156 #define Z_SYNC_FLUSH    2
157 #define Z_FULL_FLUSH    3
158 #define Z_FINISH        4
159 #define Z_BLOCK         5
160 /* Allowed flush values; see deflate() and inflate() below for details */
161 
162 #define Z_OK            0
163 #define Z_STREAM_END    1
164 #define Z_NEED_DICT     2
165 #define Z_ERRNO        (-1)
166 #define Z_STREAM_ERROR (-2)
167 #define Z_DATA_ERROR   (-3)
168 #define Z_MEM_ERROR    (-4)
169 #define Z_BUF_ERROR    (-5)
170 #define Z_VERSION_ERROR (-6)
171 /* Return codes for the compression/decompression functions. Negative
172  * values are errors, positive values are used for special but normal events.
173  */
174 
175 #define Z_NO_COMPRESSION         0
176 #define Z_BEST_SPEED             1
177 #define Z_BEST_COMPRESSION       9
178 #define Z_DEFAULT_COMPRESSION  (-1)
179 /* compression levels */
180 
181 #define Z_FILTERED            1
182 #define Z_HUFFMAN_ONLY        2
183 #define Z_RLE                 3
184 #define Z_FIXED               4
185 #define Z_DEFAULT_STRATEGY    0
186 /* compression strategy; see deflateInit2() below for details */
187 
188 #define Z_BINARY   0
189 #define Z_TEXT     1
190 #define Z_ASCII    Z_TEXT   /* for compatibility with 1.2.2 and earlier */
191 #define Z_UNKNOWN  2
192 /* Possible values of the data_type field (though see inflate()) */
193 
194 #define Z_DEFLATED   8
195 /* The deflate compression method (the only one supported in this version) */
196 
197 #define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
198 #define AZ_BUFSIZE_READ 32768
199 #define AZ_BUFSIZE_WRITE 16384
200 
201 #define AZ_FRMVER_LEN 16 /* same as MY_UUID_SIZE in 10.0.2 */
202 
203 typedef struct azio_stream {
204   z_stream 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 char frmver[AZ_FRMVER_LEN];
232   unsigned int frmver_length;
233   unsigned int comment_start_pos;   /* Position for start of comment */
234   unsigned int comment_length;   /* Position for start of comment */
235 } azio_stream;
236 
237                         /* basic functions */
238 
239 extern int azopen(azio_stream *s, const char *path, int Flags);
240 /*
241      Opens a gzip (.gz) file for reading or writing. The mode parameter
242    is as in fopen ("rb" or "wb") but can also include a compression level
243    ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
244    Huffman only compression as in "wb1h", or 'R' for run-length encoding
245    as in "wb1R". (See the description of deflateInit2 for more information
246    about the strategy parameter.)
247 
248      azopen can be used to read a file which is not in gzip format; in this
249    case gzread will directly read from the file without decompression.
250 
251      azopen returns NULL if the file could not be opened or if there was
252    insufficient memory to allocate the (de)compression state; errno
253    can be checked to distinguish the two cases (if errno is zero, the
254    zlib error is Z_MEM_ERROR).  */
255 
256 int azdopen(azio_stream *s,File fd, int Flags);
257 /*
258      azdopen() associates a azio_stream with the file descriptor fd.  File
259    descriptors are obtained from calls like open, dup, creat, pipe or
260    fileno (in the file has been previously opened with fopen).
261    The mode parameter is as in azopen.
262      The next call of gzclose on the returned azio_stream will also close the
263    file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
264    descriptor fd. If you want to keep fd open, use azdopen(dup(fd), mode).
265      azdopen returns NULL if there was insufficient memory to allocate
266    the (de)compression state.
267 */
268 
269 
270 extern unsigned int azread ( azio_stream *s, voidp buf, size_t len, int *error);
271 /*
272      Reads the given number of uncompressed bytes from the compressed file.
273    If the input file was not in gzip format, gzread copies the given number
274    of bytes into the buffer.
275      gzread returns the number of uncompressed bytes actually read (0 for
276    end of file, -1 for error). */
277 
278 extern unsigned int azwrite (azio_stream *s, const voidp buf, unsigned int len);
279 /*
280      Writes the given number of uncompressed bytes into the compressed file.
281    azwrite returns the number of uncompressed bytes actually written
282    (0 in case of error).
283 */
284 
285 
286 extern int azflush(azio_stream *file, int flush);
287 /*
288      Flushes all pending output into the compressed file. The parameter
289    flush is as in the deflate() function. The return value is the zlib
290    error number (see function gzerror below). gzflush returns Z_OK if
291    the flush parameter is Z_FINISH and all output could be flushed.
292      gzflush should be called only when strictly necessary because it can
293    degrade compression.
294 */
295 
296 extern my_off_t azseek (azio_stream *file,
297                                       my_off_t offset, int whence);
298 /*
299       Sets the starting position for the next gzread or gzwrite on the
300    given compressed file. The offset represents a number of bytes in the
301    uncompressed data stream. The whence parameter is defined as in lseek(2);
302    the value SEEK_END is not supported.
303      If the file is opened for reading, this function is emulated but can be
304    extremely slow. If the file is opened for writing, only forward seeks are
305    supported; gzseek then compresses a sequence of zeroes up to the new
306    starting position.
307 
308       gzseek returns the resulting offset location as measured in bytes from
309    the beginning of the uncompressed stream, or -1 in case of error, in
310    particular if the file is opened for writing and the new starting position
311    would be before the current position.
312 */
313 
314 extern int azrewind(azio_stream *file);
315 /*
316      Rewinds the given file. This function is supported only for reading.
317 
318    gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
319 */
320 
321 extern my_off_t aztell(azio_stream *file);
322 /*
323      Returns the starting position for the next gzread or gzwrite on the
324    given compressed file. This position represents a number of bytes in the
325    uncompressed data stream.
326 
327    gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
328 */
329 
330 extern int azclose(azio_stream *file);
331 /*
332      Flushes all pending output if necessary, closes the compressed file
333    and deallocates all the (de)compression state. The return value is the zlib
334    error number (see function gzerror below).
335 */
336 
337 extern int azwrite_frm (azio_stream *s, const uchar *blob,size_t length);
338 extern int azread_frm (azio_stream *s, uchar *blob);
339 extern int azwrite_comment (azio_stream *s, const char *blob,
340                             size_t length);
341 extern int azread_comment (azio_stream *s, char *blob);
342 
343 #ifdef	__cplusplus
344 }
345 #endif
346