1 /******************************************************************************* 2 3 D bindings to zlib compression library. 4 5 Needs -lz when linking. 6 7 Copyright: 8 Copyright (c) 2004-2009 Tango contributors. 9 Some parts copyright (c) 2009-2016 dunnhumby Germany GmbH. 10 All rights reserved. 11 12 License: 13 Tango Dual License: 3-Clause BSD License / Academic Free License v3.0. 14 See LICENSE_TANGO.txt for details. 15 16 Bear in mind this module provides bindings to an external library that 17 has its own license, which might be more restrictive. Please check the 18 external library license to see which conditions apply for linking. 19 20 *******************************************************************************/ 21 22 module ocean.util.compress.c.zlib; 23 24 import ocean.transition; 25 26 /// See original's library documentation for details. 27 static immutable ZLIB_VERSION = "1.2.3".ptr; 28 /// See original's library documentation for details. 29 static immutable uint ZLIB_VERNUM = 0x1230; 30 /// See original's library documentation for details. 31 static immutable void* Z_NULL = null; 32 33 extern (C): 34 private 35 { 36 import core.stdc.config : c_long, c_ulong; 37 38 import ocean.stdc.posix.sys.types : z_off_t = off_t; 39 40 alias ubyte Byte; 41 alias uint uInt; 42 alias c_ulong uLong; 43 44 alias Byte Bytef; 45 alias char charf; 46 alias int intf; 47 alias uInt uIntf; 48 alias uLong uLongf; 49 50 alias void* voidpc; // TODO: normally const 51 alias void* voidpf; 52 alias void* voidp; 53 54 alias voidpf function(voidpf opaque, uInt items, uInt size) alloc_func; 55 alias void function(voidpf opaque, voidpf address) free_func; 56 57 struct internal_state {} 58 } 59 60 struct z_stream 61 { 62 Bytef* next_in; 63 uInt avail_in; 64 uLong total_in; 65 66 Bytef* next_out; 67 uInt avail_out; 68 uLong total_out; 69 70 char* msg; 71 internal_state* state; 72 73 alloc_func zalloc; 74 free_func zfree; 75 voidpf opaque; 76 77 int data_type; 78 uLong adler; 79 uLong reserved; 80 } 81 82 /// See original's library documentation for details. 83 alias z_stream* z_streamp; 84 85 /// See original's library documentation for details. 86 struct gz_header 87 { 88 int text; 89 uLong time; 90 int xflags; 91 int os; 92 Bytef* extra; 93 uInt extra_len; 94 uInt extra_max; 95 Bytef* name; 96 uInt name_max; 97 Bytef* comment; 98 uInt comm_max; 99 int hcrc; 100 int done; 101 } 102 103 /// See original's library documentation for details. 104 alias gz_header* gz_headerp; 105 106 107 108 /// See original's library documentation for details. 109 enum 110 { 111 Z_NO_FLUSH = 0, 112 Z_PARTIAL_FLUSH = 1, 113 Z_SYNC_FLUSH = 2, 114 Z_FULL_FLUSH = 3, 115 Z_FINISH = 4, 116 Z_BLOCK = 5, 117 } 118 119 /// See original's library documentation for details. 120 enum 121 { 122 Z_OK = 0, 123 Z_STREAM_END = 1, 124 Z_NEED_DICT = 2, 125 Z_ERRNO = -1, 126 Z_STREAM_ERROR = -2, 127 Z_DATA_ERROR = -3, 128 Z_MEM_ERROR = -4, 129 Z_BUF_ERROR = -5, 130 Z_VERSION_ERROR = -6, 131 } 132 133 /// See original's library documentation for details. 134 enum 135 { 136 Z_NO_COMPRESSION = 0, 137 Z_BEST_SPEED = 1, 138 Z_BEST_COMPRESSION = 9, 139 Z_DEFAULT_COMPRESSION = -1, 140 } 141 142 /// See original's library documentation for details. 143 enum 144 { 145 Z_FILTERED = 1, 146 Z_HUFFMAN_ONLY = 2, 147 Z_RLE = 3, 148 Z_FIXED = 4, 149 Z_DEFAULT_STRATEGY = 0, 150 } 151 152 /// See original's library documentation for details. 153 enum 154 { 155 Z_BINARY = 0, 156 Z_TEXT = 1, 157 Z_ASCII = Z_TEXT, 158 Z_UNKNOWN = 2, 159 } 160 161 /// See original's library documentation for details. 162 enum 163 { 164 Z_DEFLATED = 8, 165 } 166 167 /// See original's library documentation for details. 168 alias zlibVersion zlib_version; 169 170 171 /// See original's library documentation for details. 172 char* zlibVersion(); 173 174 175 176 /// See original's library documentation for details. 177 int deflate(z_streamp strm, int flush); 178 179 180 /// See original's library documentation for details. 181 int deflateEnd(z_streamp strm); 182 183 184 185 186 /// See original's library documentation for details. 187 int inflate(z_streamp strm, int flush); 188 189 190 /// See original's library documentation for details. 191 int inflateEnd(z_streamp strm); 192 193 194 195 196 /// See original's library documentation for details. 197 int deflateSetDictionary(z_streamp strm, 198 Bytef* dictionary, 199 uInt dictLength); 200 201 /// See original's library documentation for details. 202 int deflateCopy(z_streamp dest, 203 z_streamp source); 204 205 /// See original's library documentation for details. 206 int deflateReset(z_streamp strm); 207 208 /// See original's library documentation for details. 209 int deflateParams(z_streamp strm, 210 int level, 211 int strategy); 212 213 /// See original's library documentation for details. 214 int deflateTune(z_streamp strm, 215 int good_length, 216 int max_lazy, 217 int nice_length, 218 int max_chain); 219 220 /// See original's library documentation for details. 221 uLong deflateBound(z_streamp strm, 222 uLong sourceLen); 223 224 /// See original's library documentation for details. 225 int deflatePrime(z_streamp strm, 226 int bits, 227 int value); 228 229 /// See original's library documentation for details. 230 int deflateSetHeader(z_streamp strm, 231 gz_headerp head); 232 233 234 /// See original's library documentation for details. 235 int inflateSetDictionary(z_streamp strm, 236 Bytef* dictionary, 237 uInt dictLength); 238 239 /// See original's library documentation for details. 240 int inflateSync(z_streamp strm); 241 242 /// See original's library documentation for details. 243 int inflateCopy(z_streamp dest, 244 z_streamp source); 245 246 /// See original's library documentation for details. 247 int inflateReset(z_streamp strm); 248 249 /// See original's library documentation for details. 250 int inflatePrime(z_streamp strm, 251 int bits, 252 int value); 253 254 /// See original's library documentation for details. 255 int inflateGetHeader(z_streamp strm, 256 gz_headerp head); 257 258 259 alias uint function(void*, ubyte**) in_func; 260 alias int function(void*, ubyte*, uint) out_func; 261 262 /// See original's library documentation for details. 263 int inflateBack(z_streamp strm, 264 scope in_func in_fn, 265 void* in_desc, 266 scope out_func out_fn, 267 void* out_desc); 268 269 /// See original's library documentation for details. 270 int inflateBackEnd(z_streamp strm); 271 272 /// See original's library documentation for details. 273 uLong zlibCompileFlags(); 274 275 276 277 278 /// See original's library documentation for details. 279 int compress(Bytef* dest, 280 uLongf* destLen, 281 Bytef* source, 282 uLong sourceLen); 283 284 /// See original's library documentation for details. 285 int compress2(Bytef* dest, 286 uLongf* destLen, 287 Bytef* source, 288 uLong sourceLen, 289 int level); 290 291 /// See original's library documentation for details. 292 uLong compressBound(uLong sourceLen); 293 294 /// See original's library documentation for details. 295 int uncompress(Bytef* dest, 296 uLongf* destLen, 297 Bytef* source, 298 uLong sourceLen); 299 300 301 /// See original's library documentation for details. 302 mixin(Typedef!(voidp, "gzFile")); 303 304 /// See original's library documentation for details. 305 gzFile gzopen(char* path, char* mode); 306 307 /// See original's library documentation for details. 308 gzFile gzdopen(int fd, char* mode); 309 310 /// See original's library documentation for details. 311 int gzsetparams(gzFile file, int level, int strategy); 312 313 /// See original's library documentation for details. 314 int gzread(gzFile file, voidp buf, uint len); 315 316 /// See original's library documentation for details. 317 int gzwrite(gzFile file, voidpc buf, uint len); 318 319 /// See original's library documentation for details. 320 int gzprintf (gzFile file, char* format, ...); 321 322 /// See original's library documentation for details. 323 int gzputs(gzFile file, char* s); 324 325 /// See original's library documentation for details. 326 char* gzgets(gzFile file, char* buf, int len); 327 328 /// See original's library documentation for details. 329 int gzputc(gzFile file, int c); 330 331 /// See original's library documentation for details. 332 int gzgetc (gzFile file); 333 334 /// See original's library documentation for details. 335 int gzungetc(int c, gzFile file); 336 337 /// See original's library documentation for details. 338 int gzflush(gzFile file, int flush); 339 340 /// See original's library documentation for details. 341 z_off_t gzseek (gzFile file, z_off_t offset, int whence); 342 343 /// See original's library documentation for details. 344 int gzrewind(gzFile file); 345 346 /// See original's library documentation for details. 347 z_off_t gztell (gzFile file); 348 349 /// See original's library documentation for details. 350 int gzeof(gzFile file); 351 352 /// See original's library documentation for details. 353 int gzdirect(gzFile file); 354 355 /// See original's library documentation for details. 356 int gzclose(gzFile file); 357 358 /// See original's library documentation for details. 359 char* gzerror(gzFile file, int* errnum); 360 361 /// See original's library documentation for details. 362 void gzclearerr(gzFile file); 363 364 /* checksum functions */ 365 366 367 /// See original's library documentation for details. 368 uLong adler32(uLong adler, Bytef* buf, uInt len); 369 370 /// See original's library documentation for details. 371 uLong adler32_combine(uLong adler1, uLong adler2, z_off_t len2); 372 373 /// See original's library documentation for details. 374 uLong crc32(uLong crc, Bytef* buf, uInt len); 375 376 /// See original's library documentation for details. 377 uLong crc32_combine(uLong crc1, uLong crc2, z_off_t len2); 378 379 380 381 /* various hacks, don't look :) */ 382 383 /// See original's library documentation for details. 384 int deflateInit_(z_streamp strm, 385 int level, 386 Const!(char)* ver, 387 int stream_size); 388 /// See original's library documentation for details. 389 int inflateInit_(z_streamp strm, 390 Const!(char)* ver, 391 int stream_size); 392 /// See original's library documentation for details. 393 int deflateInit2_(z_streamp strm, 394 int level, 395 int method, 396 int windowBits, 397 int memLevel, 398 int strategy, 399 Const!(char)* ver, 400 int stream_size); 401 /// See original's library documentation for details. 402 int inflateInit2_(z_streamp strm, 403 int windowBits, 404 Const!(char)* ver, 405 int stream_size); 406 /// See original's library documentation for details. 407 int inflateBackInit_(z_streamp strm, 408 int windowBits, 409 ubyte* window, 410 Const!(char)* ver, 411 int stream_size); 412 413 extern (D) int deflateInit(z_streamp strm, 414 int level) 415 { 416 return deflateInit_(strm, 417 level, 418 ZLIB_VERSION, 419 z_stream.sizeof); 420 } 421 422 extern (D) int inflateInit(z_streamp strm) 423 { 424 return inflateInit_(strm, 425 ZLIB_VERSION, 426 z_stream.sizeof); 427 } 428 429 extern (D) int deflateInit2(z_streamp strm, 430 int level, 431 int method, 432 int windowBits, 433 int memLevel, 434 int strategy) 435 { 436 return deflateInit2_(strm, 437 level, 438 method, 439 windowBits, 440 memLevel, 441 strategy, 442 ZLIB_VERSION, 443 z_stream.sizeof); 444 } 445 446 extern (D) int inflateInit2(z_streamp strm, 447 int windowBits) 448 { 449 return inflateInit2_(strm, 450 windowBits, 451 ZLIB_VERSION, 452 z_stream.sizeof); 453 } 454 455 extern (D) int inflateBackInit(z_streamp strm, 456 int windowBits, 457 ubyte* window) 458 { 459 return inflateBackInit_(strm, 460 windowBits, 461 window, 462 ZLIB_VERSION, 463 z_stream.sizeof); 464 } 465 466 /// See original's library documentation for details. 467 char* zError(int); 468 /// See original's library documentation for details. 469 int inflateSyncPoint(z_streamp z); 470 /// See original's library documentation for details. 471 uLongf* get_crc_table();