1 // minimized version of etc.c.zlib 2 module bio.core.utils.zlib; 3 4 import core.stdc.config; 5 6 extern (C) { 7 8 const char[] ZLIB_VERSION = "1.2.3"; 9 const ZLIB_VERNUM = 0x1230; 10 11 alias void* function (void* opaque, uint items, uint size) alloc_func; 12 alias void function (void* opaque, void* address) free_func; 13 14 struct z_stream 15 { 16 ubyte *next_in; /* next input byte */ 17 uint avail_in; /* number of bytes available at next_in */ 18 c_ulong total_in; /* total nb of input bytes read so far */ 19 20 ubyte *next_out; /* next output byte should be put there */ 21 uint avail_out; /* remaining free space at next_out */ 22 c_ulong total_out; /* total nb of bytes output so far */ 23 24 char *msg; /* last error message, NULL if no error */ 25 void* state; /* not visible by applications */ 26 27 alloc_func zalloc; /* used to allocate the internal state */ 28 free_func zfree; /* used to free the internal state */ 29 void* opaque; /* private data object passed to zalloc and zfree */ 30 31 int data_type; /* best guess about the data type: binary or text */ 32 c_ulong adler; /* adler32 value of the uncompressed data */ 33 c_ulong reserved; /* reserved for future use */ 34 } 35 36 alias z_stream* z_streamp; 37 38 /* 39 gzip header information passed to and from zlib routines. See RFC 1952 40 for more details on the meanings of these fields. 41 */ 42 struct gz_header { 43 int text; /* true if compressed data believed to be text */ 44 c_ulong time; /* modification time */ 45 int xflags; /* extra flags (not used when writing a gzip file) */ 46 int os; /* operating system */ 47 byte *extra; /* pointer to extra field or Z_NULL if none */ 48 uint extra_len; /* extra field length (valid if extra != Z_NULL) */ 49 uint extra_max; /* space at extra (only when reading header) */ 50 byte *name; /* pointer to zero-terminated file name or Z_NULL */ 51 uint name_max; /* space at name (only when reading header) */ 52 byte *comment; /* pointer to zero-terminated comment or Z_NULL */ 53 uint comm_max; /* space at comment (only when reading header) */ 54 int hcrc; /* true if there was or will be a header crc */ 55 int done; /* true when done reading gzip header (not used 56 when writing a gzip file) */ 57 } 58 59 alias gz_header* gz_headerp; 60 61 /* constants */ 62 63 enum 64 { 65 Z_NO_FLUSH = 0, 66 Z_PARTIAL_FLUSH = 1, /* will be removed, use Z_SYNC_FLUSH instead */ 67 Z_SYNC_FLUSH = 2, 68 Z_FULL_FLUSH = 3, 69 Z_FINISH = 4, 70 Z_BLOCK = 5, 71 Z_TREES = 6, 72 } 73 /* Allowed flush values; see deflate() and inflate() below for details */ 74 75 enum 76 { 77 Z_OK = 0, 78 Z_STREAM_END = 1, 79 Z_NEED_DICT = 2, 80 Z_ERRNO = -1, 81 Z_STREAM_ERROR = -2, 82 Z_DATA_ERROR = -3, 83 Z_MEM_ERROR = -4, 84 Z_BUF_ERROR = -5, 85 Z_VERSION_ERROR = -6, 86 } 87 /* Return codes for the compression/decompression functions. Negative 88 * values are errors, positive values are used for special but normal events. 89 */ 90 91 enum 92 { 93 Z_NO_COMPRESSION = 0, 94 Z_BEST_SPEED = 1, 95 Z_BEST_COMPRESSION = 9, 96 Z_DEFAULT_COMPRESSION = -1, 97 } 98 /* compression levels */ 99 100 enum 101 { 102 Z_FILTERED = 1, 103 Z_HUFFMAN_ONLY = 2, 104 Z_RLE = 3, 105 Z_FIXED = 4, 106 Z_DEFAULT_STRATEGY = 0, 107 } 108 /* compression strategy; see deflateInit2() below for details */ 109 110 enum 111 { 112 Z_BINARY = 0, 113 Z_TEXT = 1, 114 Z_UNKNOWN = 2, 115 116 Z_ASCII = Z_TEXT 117 } 118 /* Possible values of the data_type field (though see inflate()) */ 119 120 enum 121 { 122 Z_DEFLATED = 8, 123 } 124 /* The deflate compression method (the only one supported in this version) */ 125 126 const int Z_NULL = 0; /* for initializing zalloc, zfree, opaque */ 127 128 /* basic functions */ 129 130 char* zlibVersion(); 131 int deflateInit(z_streamp strm, int level) 132 { 133 return deflateInit_(strm, level, ZLIB_VERSION.ptr, z_stream.sizeof); 134 } 135 136 int deflate(z_streamp strm, int flush); 137 int deflateEnd(z_streamp strm); 138 139 int inflateInit(z_streamp strm) 140 { 141 return inflateInit_(strm, ZLIB_VERSION.ptr, z_stream.sizeof); 142 } 143 int inflate(z_streamp strm, int flush); 144 int inflateEnd(z_streamp strm); 145 146 int deflateInit2(z_streamp strm, 147 int level, 148 int method, 149 int windowBits, 150 int memLevel, 151 int strategy) 152 { 153 return deflateInit2_(strm, level, method, windowBits, memLevel, 154 strategy, ZLIB_VERSION.ptr, z_stream.sizeof); 155 } 156 157 158 int deflateBound(z_streamp strm, size_t sourceLen); 159 160 int inflateInit2(z_streamp strm, int windowBits) 161 { 162 return inflateInit2_(strm, windowBits, ZLIB_VERSION.ptr, z_stream.sizeof); 163 } 164 165 int compress(ubyte* dest, 166 size_t* destLen, 167 ubyte* source, 168 size_t sourceLen); 169 170 int compress2(ubyte* dest, 171 size_t* destLen, 172 ubyte* source, 173 size_t sourceLen, 174 int level); 175 176 size_t compressBound(size_t sourceLen); 177 178 int uncompress(ubyte* dest, 179 size_t* destLen, 180 ubyte* source, 181 size_t sourceLen); 182 183 alias void* gzFile; 184 alias int z_off_t; // file offset 185 186 gzFile gzopen(char* path, char* mode); 187 gzFile gzdopen(int fd, char* mode); 188 189 int gzsetparams(gzFile file, int level, int strategy); 190 int gzread(gzFile file, void* buf, uint len); 191 int gzwrite(gzFile file, void* buf, uint len); 192 int gzprintf(gzFile file, char* format, ...); 193 int gzputs(gzFile file, char* s); 194 char* gzgets(gzFile file, char* buf, int len); 195 int gzputc(gzFile file, int c); 196 int gzgetc(gzFile file); 197 int gzungetc(int c, gzFile file); 198 int gzflush(gzFile file, int flush); 199 z_off_t gzseek(gzFile file, z_off_t offset, int whence); 200 int gzrewind(gzFile file); 201 z_off_t gztell(gzFile file); 202 int gzeof(gzFile file); 203 int gzdirect(gzFile file); 204 int gzclose(gzFile file); 205 char* gzerror(gzFile file, int *errnum); 206 void gzclearerr (gzFile file); 207 uint adler32 (uint adler, ubyte *buf, uint len); 208 uint adler32_combine(uint adler1, uint adler2, z_off_t len2); 209 uint crc32(uint crc, ubyte *buf, uint len); 210 uint crc32_combine (uint crc1, uint crc2, z_off_t len2); 211 212 int deflateInit_(z_streamp strm, 213 int level, 214 const char* versionx, 215 int stream_size); 216 217 int inflateInit_(z_streamp strm, 218 const char* versionx, 219 int stream_size); 220 221 int deflateInit2_(z_streamp strm, 222 int level, 223 int method, 224 int windowBits, 225 int memLevel, 226 int strategy, 227 const char* versionx, 228 int stream_size); 229 230 int inflateBackInit_(z_stream* strm, 231 int windowBits, 232 ubyte* window, 233 const char* z_version, 234 int stream_size); 235 236 int inflateInit2_(z_streamp strm, 237 int windowBits, 238 const char* versionx, 239 int stream_size); 240 241 char* zError(int err); 242 int inflateSyncPoint(z_streamp z); 243 uint* get_crc_table(); 244 245 } 246 247 class ZlibException : Exception 248 { 249 this(int errnum) { 250 auto msg = "[zlib] " ~ messageFromErrnum(errnum); 251 super(msg); 252 } 253 254 this(string func, int errnum) { 255 auto msg = "[zlib/" ~ func ~ "] " ~ messageFromErrnum(errnum); 256 super(msg); 257 } 258 259 private string messageFromErrnum(int errnum) { 260 switch (errnum) 261 { 262 case Z_STREAM_END: msg = "stream end"; break; 263 case Z_NEED_DICT: msg = "need dict"; break; 264 case Z_ERRNO: msg = "errno"; break; 265 case Z_STREAM_ERROR: msg = "stream error"; break; 266 case Z_DATA_ERROR: msg = "data error"; break; 267 case Z_MEM_ERROR: msg = "mem error"; break; 268 case Z_BUF_ERROR: msg = "buf error"; break; 269 case Z_VERSION_ERROR: msg = "version error"; break; 270 default: msg = "unknown error"; break; 271 } 272 return msg; 273 } 274 } 275 276 uint crc32(uint crc, const(void)[] buf) 277 { 278 return crc32(crc, cast(ubyte *)buf.ptr, cast(uint)(buf.length)); 279 } 280