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