1 /* 2 Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2.0, 6 as published by the Free Software Foundation. 7 8 This program is also distributed with certain software (including 9 but not limited to OpenSSL) that is licensed under separate terms, 10 as designated in a particular file or component or in included license 11 documentation. The authors of MySQL hereby grant you an additional 12 permission to link the program and your derivative works with the 13 separately licensed software that they have included with MySQL. 14 15 This program is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License, version 2.0, for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this program; if not, write to the Free Software 22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 */ 24 25 /* 26 This library has been brought into the 21st century. 27 - Magnus Blåudd 28 */ 29 30 /* 31 This libary has been modified for use by the MySQL Archive Engine. 32 -Brian Aker 33 */ 34 35 /* zlib.h -- interface of the 'zlib' general purpose compression library 36 version 1.2.3, July 18th, 2005 37 38 Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler 39 40 This software is provided 'as-is', without any express or implied 41 warranty. In no event will the authors be held liable for any damages 42 arising from the use of this software. 43 44 Permission is granted to anyone to use this software for any purpose, 45 including commercial applications, and to alter it and redistribute it 46 freely, subject to the following restrictions: 47 48 1. The origin of this software must not be misrepresented; you must not 49 claim that you wrote the original software. If you use this software 50 in a product, an acknowledgment in the product documentation would be 51 appreciated but is not required. 52 2. Altered source versions must be plainly marked as such, and must not be 53 misrepresented as being the original software. 54 3. This notice may not be removed or altered from any source distribution. 55 56 Jean-loup Gailly Mark Adler 57 jloup@gzip.org madler@alumni.caltech.edu 58 59 60 The data format used by the zlib library is described by RFCs (Request for 61 Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt 62 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). 63 */ 64 65 #include <zlib.h> 66 67 #ifdef __cplusplus 68 extern "C" { 69 #endif 70 71 size_t ndbz_inflate_mem_size(); 72 size_t ndbz_deflate_mem_size(); 73 struct ndbz_alloc_rec { 74 size_t size; 75 size_t mfree; 76 char *mem; 77 }; 78 79 typedef struct ndbzio_stream { 80 z_stream stream; 81 int z_err; /* error code for last stream operation */ 82 int z_eof; /* set if end of input file */ 83 File file; /* .gz file */ 84 Byte *inbuf; /* input buffer */ 85 Byte *outbuf; /* output buffer */ 86 uLong crc; /* crc32 of uncompressed data */ 87 char *msg; /* error message */ 88 int transparent; /* 1 if input file is not a .gz file */ 89 char mode; /* 'w' or 'r' */ 90 char bufalloced; /* true if ndbzio allocated buffers */ 91 my_off_t start; /* start of compressed data in file (header skipped) */ 92 my_off_t in; /* bytes into deflate or inflate */ 93 my_off_t out; /* bytes out of deflate or inflate */ 94 int back; /* one character push-back */ 95 int last; /* true if push-back is last character */ 96 unsigned char version; /* Version */ 97 unsigned char minor_version; /* Version */ 98 unsigned int block_size; /* Block Size */ 99 unsigned long long check_point; /* Last position we checked */ 100 unsigned long long forced_flushes; /* Forced Flushes */ 101 unsigned long long rows; /* rows */ 102 unsigned long long auto_increment; /* auto increment field */ 103 unsigned int longest_row; /* Longest row */ 104 unsigned int shortest_row; /* Shortest row */ 105 unsigned char dirty; /* State of file */ 106 unsigned int frm_start_pos; /* Position for start of FRM */ 107 unsigned int frm_length; /* Position for start of FRM */ 108 unsigned int comment_start_pos; /* Position for start of comment */ 109 unsigned int comment_length; /* Position for start of comment */ 110 } ndbzio_stream; 111 112 /* Return the size in bytes used for reading */ 113 size_t ndbz_bufsize_read(void); 114 115 /* Return the size in bytes used for writing */ 116 size_t ndbz_bufsize_write(void); 117 118 /* basic functions */ 119 extern int ndbzopen(ndbzio_stream *s, const char *path, int Flags); 120 /* 121 Opens a gzip (.gz) file for reading or writing. The mode parameter 122 is as in fopen ("rb" or "wb") but can also include a compression level 123 ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for 124 Huffman only compression as in "wb1h", or 'R' for run-length encoding 125 as in "wb1R". (See the description of deflateInit2 for more information 126 about the strategy parameter.) 127 128 ndbzopen can be used to read a file which is not in gzip format; in this 129 case gzread will directly read from the file without decompression. 130 131 ndbzopen returns NULL if the file could not be opened or if there was 132 insufficient memory to allocate the (de)compression state; errno 133 can be checked to distinguish the two cases (if errno is zero, the 134 zlib error is Z_MEM_ERROR). */ 135 136 int ndbzdopen(ndbzio_stream *s, File fd, int Flags); 137 /* 138 ndbzdopen() associates a ndbzio_stream with the file descriptor fd. File 139 descriptors are obtained from calls like open, dup, creat, pipe or 140 fileno (in the file has been previously opened with fopen). 141 The mode parameter is as in ndbzopen. 142 The next call of gzclose on the returned ndbzio_stream will also close the 143 file descriptor fd, just like fclose(fdopen(fd), mode) closes the file 144 descriptor fd. If you want to keep fd open, use ndbzdopen(dup(fd), mode). 145 ndbzdopen returns NULL if there was insufficient memory to allocate 146 the (de)compression state. 147 */ 148 149 150 extern unsigned int ndbzread (ndbzio_stream *s, voidp buf, 151 unsigned int len, int *error); 152 /* 153 Reads the given number of uncompressed bytes from the compressed file. 154 If the input file was not in gzip format, gzread copies the given number 155 of bytes into the buffer. 156 gzread returns the number of uncompressed bytes actually read (0 for 157 end of file, -1 for error). */ 158 159 extern unsigned int ndbzwrite (ndbzio_stream *s, const void* buf, 160 unsigned int len); 161 /* 162 Writes the given number of uncompressed bytes into the compressed file. 163 ndbzwrite returns the number of uncompressed bytes actually written 164 (0 in case of error). 165 */ 166 167 168 extern int ndbzflush(ndbzio_stream *file, int flush); 169 /* 170 Flushes all pending output into the compressed file. The parameter 171 flush is as in the deflate() function. The return value is the zlib 172 error number (see function gzerror below). gzflush returns Z_OK if 173 the flush parameter is Z_FINISH and all output could be flushed. 174 gzflush should be called only when strictly necessary because it can 175 degrade compression. 176 */ 177 178 extern my_off_t ndbzseek (ndbzio_stream *file, 179 my_off_t offset, int whence); 180 /* 181 Sets the starting position for the next gzread or gzwrite on the 182 given compressed file. The offset represents a number of bytes in the 183 uncompressed data stream. The whence parameter is defined as in lseek(2); 184 the value SEEK_END is not supported. 185 If the file is opened for reading, this function is emulated but can be 186 extremely slow. If the file is opened for writing, only forward seeks are 187 supported; gzseek then compresses a sequence of zeroes up to the new 188 starting position. 189 190 gzseek returns the resulting offset location as measured in bytes from 191 the beginning of the uncompressed stream, or -1 in case of error, in 192 particular if the file is opened for writing and the new starting position 193 would be before the current position. 194 */ 195 196 extern int ndbzrewind(ndbzio_stream *file); 197 /* 198 Rewinds the given file. This function is supported only for reading. 199 200 gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) 201 */ 202 203 extern my_off_t ndbztell(ndbzio_stream *file); 204 /* 205 Returns the starting position for the next gzread or gzwrite on the 206 given compressed file. This position represents a number of bytes in the 207 uncompressed data stream. 208 209 gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) 210 */ 211 212 extern int ndbzclose(ndbzio_stream *file); 213 /* 214 Flushes all pending output if necessary, closes the compressed file 215 and deallocates all the (de)compression state. The return value is the zlib 216 error number (see function gzerror below). 217 */ 218 219 /* 220 Return file size of the open ndbzio_stream 221 */ 222 extern int ndbz_file_size(ndbzio_stream *file, size_t *size); 223 224 #ifdef __cplusplus 225 } 226 #endif 227