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 202 typedef struct azio_stream { 203 z_stream stream; 204 int z_err; /* error code for last stream operation */ 205 int z_eof; /* set if end of input file */ 206 File file; /* .gz file */ 207 Byte inbuf[AZ_BUFSIZE_READ]; /* input buffer */ 208 Byte outbuf[AZ_BUFSIZE_WRITE]; /* output buffer */ 209 uLong crc; /* crc32 of uncompressed data */ 210 char *msg; /* error message */ 211 int transparent; /* 1 if input file is not a .gz file */ 212 char mode; /* 'w' or 'r' */ 213 my_off_t start; /* start of compressed data in file (header skipped) */ 214 my_off_t in; /* bytes into deflate or inflate */ 215 my_off_t out; /* bytes out of deflate or inflate */ 216 int back; /* one character push-back */ 217 int last; /* true if push-back is last character */ 218 unsigned char version; /* Version */ 219 unsigned char minor_version; /* Version */ 220 unsigned int block_size; /* Block Size */ 221 unsigned long long check_point; /* Last position we checked */ 222 unsigned long long forced_flushes; /* Forced Flushes */ 223 unsigned long long rows; /* rows */ 224 unsigned long long auto_increment; /* auto increment field */ 225 unsigned int longest_row; /* Longest row */ 226 unsigned int shortest_row; /* Shortest row */ 227 unsigned char dirty; /* State of file */ 228 unsigned int frm_start_pos; /* Position for start of FRM */ 229 unsigned int frm_length; /* Position for start of FRM */ 230 unsigned int comment_start_pos; /* Position for start of comment */ 231 unsigned int comment_length; /* Position for start of comment */ 232 } azio_stream; 233 234 /* basic functions */ 235 236 extern int azopen(azio_stream *s, const char *path, int Flags); 237 /* 238 Opens a gzip (.gz) file for reading or writing. The mode parameter 239 is as in fopen ("rb" or "wb") but can also include a compression level 240 ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for 241 Huffman only compression as in "wb1h", or 'R' for run-length encoding 242 as in "wb1R". (See the description of deflateInit2 for more information 243 about the strategy parameter.) 244 245 azopen can be used to read a file which is not in gzip format; in this 246 case gzread will directly read from the file without decompression. 247 248 azopen returns NULL if the file could not be opened or if there was 249 insufficient memory to allocate the (de)compression state; errno 250 can be checked to distinguish the two cases (if errno is zero, the 251 zlib error is Z_MEM_ERROR). */ 252 253 int azdopen(azio_stream *s,File fd, int Flags); 254 /* 255 azdopen() associates a azio_stream with the file descriptor fd. File 256 descriptors are obtained from calls like open, dup, creat, pipe or 257 fileno (in the file has been previously opened with fopen). 258 The mode parameter is as in azopen. 259 The next call of gzclose on the returned azio_stream will also close the 260 file descriptor fd, just like fclose(fdopen(fd), mode) closes the file 261 descriptor fd. If you want to keep fd open, use azdopen(dup(fd), mode). 262 azdopen returns NULL if there was insufficient memory to allocate 263 the (de)compression state. 264 */ 265 266 267 extern unsigned int azread ( azio_stream *s, voidp buf, size_t len, int *error); 268 /* 269 Reads the given number of uncompressed bytes from the compressed file. 270 If the input file was not in gzip format, gzread copies the given number 271 of bytes into the buffer. 272 gzread returns the number of uncompressed bytes actually read (0 for 273 end of file, -1 for error). */ 274 275 extern unsigned int azwrite (azio_stream *s, const voidp buf, unsigned int len); 276 /* 277 Writes the given number of uncompressed bytes into the compressed file. 278 azwrite returns the number of uncompressed bytes actually written 279 (0 in case of error). 280 */ 281 282 283 extern int azflush(azio_stream *file, int flush); 284 /* 285 Flushes all pending output into the compressed file. The parameter 286 flush is as in the deflate() function. The return value is the zlib 287 error number (see function gzerror below). gzflush returns Z_OK if 288 the flush parameter is Z_FINISH and all output could be flushed. 289 gzflush should be called only when strictly necessary because it can 290 degrade compression. 291 */ 292 293 extern my_off_t azseek (azio_stream *file, 294 my_off_t offset, int whence); 295 /* 296 Sets the starting position for the next gzread or gzwrite on the 297 given compressed file. The offset represents a number of bytes in the 298 uncompressed data stream. The whence parameter is defined as in lseek(2); 299 the value SEEK_END is not supported. 300 If the file is opened for reading, this function is emulated but can be 301 extremely slow. If the file is opened for writing, only forward seeks are 302 supported; gzseek then compresses a sequence of zeroes up to the new 303 starting position. 304 305 gzseek returns the resulting offset location as measured in bytes from 306 the beginning of the uncompressed stream, or -1 in case of error, in 307 particular if the file is opened for writing and the new starting position 308 would be before the current position. 309 */ 310 311 extern int azrewind(azio_stream *file); 312 /* 313 Rewinds the given file. This function is supported only for reading. 314 315 gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) 316 */ 317 318 extern my_off_t aztell(azio_stream *file); 319 /* 320 Returns the starting position for the next gzread or gzwrite on the 321 given compressed file. This position represents a number of bytes in the 322 uncompressed data stream. 323 324 gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) 325 */ 326 327 extern int azclose(azio_stream *file); 328 /* 329 Flushes all pending output if necessary, closes the compressed file 330 and deallocates all the (de)compression state. The return value is the zlib 331 error number (see function gzerror below). 332 */ 333 334 extern int azwrite_frm (azio_stream *s, char *blob, unsigned int length); 335 extern int azread_frm (azio_stream *s, char *blob); 336 extern int azwrite_comment (azio_stream *s, char *blob, unsigned int length); 337 extern int azread_comment (azio_stream *s, char *blob); 338 339 #ifdef __cplusplus 340 } 341 #endif 342