1 #![allow(non_camel_case_types)]
2 #![allow(non_snake_case)]
3
4 use std::os::raw::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_void};
5
6 // Macro for variances between zlib-ng in native mode and either zlib or zlib-ng in zlib compat
7 // mode. Note in particular that zlib-ng in compat mode does *not* use the zng case.
8 #[cfg(not(zng))]
9 macro_rules! if_zng {
10 ($_zng:tt, $not_zng:tt) => {
11 $not_zng
12 };
13 }
14
15 #[cfg(zng)]
16 macro_rules! if_zng {
17 ($zng:tt, $_not_zng:tt) => {
18 $zng
19 };
20 }
21
22 // zlib uses unsigned long for various sizes; zlib-ng uses size_t.
23 type z_size = if_zng!(usize, c_ulong);
24
25 // zlib stores Adler-32 and CRC-32 checksums in unsigned long; zlib-ng uses uint32_t.
26 type z_checksum = if_zng!(u32, c_ulong);
27
28 pub type alloc_func = unsafe extern "C" fn(voidpf, uInt, uInt) -> voidpf;
29 pub type Bytef = u8;
30 pub type free_func = unsafe extern "C" fn(voidpf, voidpf);
31 #[cfg(any(zng, feature = "libc"))]
32 pub type gzFile = *mut gzFile_s;
33 pub type in_func = unsafe extern "C" fn(*mut c_void, *mut *const c_uchar) -> c_uint;
34 pub type out_func = unsafe extern "C" fn(*mut c_void, *mut c_uchar, c_uint) -> c_int;
35 pub type uInt = c_uint;
36 pub type uLong = c_ulong;
37 pub type uLongf = c_ulong;
38 pub type voidp = *mut c_void;
39 pub type voidpc = *const c_void;
40 pub type voidpf = *mut c_void;
41
42 #[cfg(any(zng, feature = "libc"))]
43 pub enum gzFile_s {}
44 pub enum internal_state {}
45
46 #[cfg(all(
47 not(zng),
48 feature = "libc",
49 not(all(target_family = "wasm", target_os = "unknown"))
50 ))]
51 pub type z_off_t = libc::off_t;
52
53 #[cfg(all(
54 not(zng),
55 feature = "libc",
56 all(target_family = "wasm", target_os = "unknown")
57 ))]
58 pub type z_off_t = c_long;
59
60 #[cfg(all(zng, windows, not(target_env = "gnu")))]
61 pub type z_off_t = i64;
62
63 #[cfg(all(zng, not(all(windows, not(target_env = "gnu")))))]
64 pub type z_off_t = libc::off_t;
65
66 #[repr(C)]
67 #[derive(Copy, Clone)]
68 pub struct gz_header {
69 pub text: c_int,
70 pub time: uLong,
71 pub xflags: c_int,
72 pub os: c_int,
73 pub extra: *mut Bytef,
74 pub extra_len: uInt,
75 pub extra_max: uInt,
76 pub name: *mut Bytef,
77 pub name_max: uInt,
78 pub comment: *mut Bytef,
79 pub comm_max: uInt,
80 pub hcrc: c_int,
81 pub done: c_int,
82 }
83 pub type gz_headerp = *mut gz_header;
84
85 #[repr(C)]
86 #[derive(Copy, Clone)]
87 pub struct z_stream {
88 pub next_in: *mut Bytef,
89 pub avail_in: uInt,
90 pub total_in: z_size,
91 pub next_out: *mut Bytef,
92 pub avail_out: uInt,
93 pub total_out: z_size,
94 pub msg: *mut c_char,
95 pub state: *mut internal_state,
96 pub zalloc: alloc_func,
97 pub zfree: free_func,
98 pub opaque: voidpf,
99 pub data_type: c_int,
100 pub adler: z_checksum,
101 pub reserved: uLong,
102 }
103 pub type z_streamp = *mut z_stream;
104
105 // Ideally, this should instead use a macro that parses the whole block of externs, and generates
106 // the appropriate link_name attributes, without duplicating the function names. However, ctest2
107 // can't parse that.
108 #[cfg(not(zng))]
109 macro_rules! zng_prefix {
110 ($name:expr) => {
111 stringify!($name)
112 };
113 }
114
115 #[cfg(zng)]
116 macro_rules! zng_prefix {
117 ($name:expr) => {
118 concat!("zng_", stringify!($name))
119 };
120 }
121
122 extern "C" {
123 #[link_name = zng_prefix!(adler32)]
adler32(adler: z_checksum, buf: *const Bytef, len: uInt) -> z_checksum124 pub fn adler32(adler: z_checksum, buf: *const Bytef, len: uInt) -> z_checksum;
125 #[link_name = zng_prefix!(crc32)]
crc32(crc: z_checksum, buf: *const Bytef, len: uInt) -> z_checksum126 pub fn crc32(crc: z_checksum, buf: *const Bytef, len: uInt) -> z_checksum;
127 #[link_name = zng_prefix!(deflate)]
deflate(strm: z_streamp, flush: c_int) -> c_int128 pub fn deflate(strm: z_streamp, flush: c_int) -> c_int;
129 #[link_name = zng_prefix!(deflateBound)]
deflateBound(strm: z_streamp, sourceLen: uLong) -> uLong130 pub fn deflateBound(strm: z_streamp, sourceLen: uLong) -> uLong;
131 #[link_name = zng_prefix!(deflateCopy)]
deflateCopy(dest: z_streamp, source: z_streamp) -> c_int132 pub fn deflateCopy(dest: z_streamp, source: z_streamp) -> c_int;
133 #[link_name = zng_prefix!(deflateEnd)]
deflateEnd(strm: z_streamp) -> c_int134 pub fn deflateEnd(strm: z_streamp) -> c_int;
135 #[link_name = zng_prefix!(deflateParams)]
deflateParams(strm: z_streamp, level: c_int, strategy: c_int) -> c_int136 pub fn deflateParams(strm: z_streamp, level: c_int, strategy: c_int) -> c_int;
137 #[link_name = zng_prefix!(deflatePrime)]
deflatePrime(strm: z_streamp, bits: c_int, value: c_int) -> c_int138 pub fn deflatePrime(strm: z_streamp, bits: c_int, value: c_int) -> c_int;
139 #[link_name = zng_prefix!(deflateReset)]
deflateReset(strm: z_streamp) -> c_int140 pub fn deflateReset(strm: z_streamp) -> c_int;
141 #[link_name = zng_prefix!(deflateSetDictionary)]
deflateSetDictionary( strm: z_streamp, dictionary: *const Bytef, dictLength: uInt, ) -> c_int142 pub fn deflateSetDictionary(
143 strm: z_streamp,
144 dictionary: *const Bytef,
145 dictLength: uInt,
146 ) -> c_int;
147 #[link_name = zng_prefix!(deflateSetHeader)]
deflateSetHeader(strm: z_streamp, head: gz_headerp) -> c_int148 pub fn deflateSetHeader(strm: z_streamp, head: gz_headerp) -> c_int;
149 #[link_name = zng_prefix!(deflateTune)]
deflateTune( strm: z_streamp, good_length: c_int, max_lazy: c_int, nice_length: c_int, max_chain: c_int, ) -> c_int150 pub fn deflateTune(
151 strm: z_streamp,
152 good_length: c_int,
153 max_lazy: c_int,
154 nice_length: c_int,
155 max_chain: c_int,
156 ) -> c_int;
157 #[link_name = zng_prefix!(inflate)]
inflate(strm: z_streamp, flush: c_int) -> c_int158 pub fn inflate(strm: z_streamp, flush: c_int) -> c_int;
159 #[link_name = zng_prefix!(inflateBack)]
inflateBack( strm: z_streamp, _in: in_func, in_desc: *mut c_void, out: out_func, out_desc: *mut c_void, ) -> c_int160 pub fn inflateBack(
161 strm: z_streamp,
162 _in: in_func,
163 in_desc: *mut c_void,
164 out: out_func,
165 out_desc: *mut c_void,
166 ) -> c_int;
167 #[link_name = zng_prefix!(inflateBackEnd)]
inflateBackEnd(strm: z_streamp) -> c_int168 pub fn inflateBackEnd(strm: z_streamp) -> c_int;
169 #[link_name = zng_prefix!(inflateCopy)]
inflateCopy(dest: z_streamp, source: z_streamp) -> c_int170 pub fn inflateCopy(dest: z_streamp, source: z_streamp) -> c_int;
171 #[link_name = zng_prefix!(inflateEnd)]
inflateEnd(strm: z_streamp) -> c_int172 pub fn inflateEnd(strm: z_streamp) -> c_int;
173 #[link_name = zng_prefix!(inflateGetHeader)]
inflateGetHeader(strm: z_streamp, head: gz_headerp) -> c_int174 pub fn inflateGetHeader(strm: z_streamp, head: gz_headerp) -> c_int;
175 #[link_name = zng_prefix!(inflateMark)]
inflateMark(strm: z_streamp) -> c_long176 pub fn inflateMark(strm: z_streamp) -> c_long;
177 #[link_name = zng_prefix!(inflatePrime)]
inflatePrime(strm: z_streamp, bits: c_int, value: c_int) -> c_int178 pub fn inflatePrime(strm: z_streamp, bits: c_int, value: c_int) -> c_int;
179 #[link_name = zng_prefix!(inflateReset)]
inflateReset(strm: z_streamp) -> c_int180 pub fn inflateReset(strm: z_streamp) -> c_int;
181 #[link_name = zng_prefix!(inflateReset2)]
inflateReset2(strm: z_streamp, windowBits: c_int) -> c_int182 pub fn inflateReset2(strm: z_streamp, windowBits: c_int) -> c_int;
183 #[link_name = zng_prefix!(inflateSetDictionary)]
inflateSetDictionary( strm: z_streamp, dictionary: *const Bytef, dictLength: uInt, ) -> c_int184 pub fn inflateSetDictionary(
185 strm: z_streamp,
186 dictionary: *const Bytef,
187 dictLength: uInt,
188 ) -> c_int;
189 #[link_name = zng_prefix!(inflateSync)]
inflateSync(strm: z_streamp) -> c_int190 pub fn inflateSync(strm: z_streamp) -> c_int;
191 #[link_name = zng_prefix!(zlibCompileFlags)]
zlibCompileFlags() -> uLong192 pub fn zlibCompileFlags() -> uLong;
193
194 // The above set of functions currently target 1.2.3.4 (what's present on Ubuntu
195 // 12.04, but there's some other APIs that were added later. Should figure out
196 // how to expose them...
197 //
198 // Added in 1.2.5.1
199 //
200 // pub fn deflatePending(strm: z_streamp,
201 // pending: *mut c_uint,
202 // bits: *mut c_int) -> c_int;
203 //
204 // Addedin 1.2.7.1
205 // pub fn inflateGetDictionary(strm: z_streamp,
206 // dictionary: *mut Bytef,
207 // dictLength: *mut uInt) -> c_int;
208 //
209 // Added in 1.2.3.5
210 // pub fn gzbuffer(file: gzFile, size: c_uint) -> c_int;
211 // pub fn gzclose_r(file: gzFile) -> c_int;
212 // pub fn gzclose_w(file: gzFile) -> c_int;
213 // pub fn gzoffset(file: gzFile) -> z_off_t;
214 }
215
216 extern "C" {
217 #[link_name = if_zng!("zlibng_version", "zlibVersion")]
zlibVersion() -> *const c_char218 pub fn zlibVersion() -> *const c_char;
219 }
220
221 #[cfg(not(zng))]
222 extern "C" {
deflateInit_( strm: z_streamp, level: c_int, version: *const c_char, stream_size: c_int, ) -> c_int223 pub fn deflateInit_(
224 strm: z_streamp,
225 level: c_int,
226 version: *const c_char,
227 stream_size: c_int,
228 ) -> c_int;
deflateInit2_( strm: z_streamp, level: c_int, method: c_int, windowBits: c_int, memLevel: c_int, strategy: c_int, version: *const c_char, stream_size: c_int, ) -> c_int229 pub fn deflateInit2_(
230 strm: z_streamp,
231 level: c_int,
232 method: c_int,
233 windowBits: c_int,
234 memLevel: c_int,
235 strategy: c_int,
236 version: *const c_char,
237 stream_size: c_int,
238 ) -> c_int;
inflateBackInit_( strm: z_streamp, windowBits: c_int, window: *mut c_uchar, version: *const c_char, stream_size: c_int, ) -> c_int239 pub fn inflateBackInit_(
240 strm: z_streamp,
241 windowBits: c_int,
242 window: *mut c_uchar,
243 version: *const c_char,
244 stream_size: c_int,
245 ) -> c_int;
inflateInit_(strm: z_streamp, version: *const c_char, stream_size: c_int) -> c_int246 pub fn inflateInit_(strm: z_streamp, version: *const c_char, stream_size: c_int) -> c_int;
inflateInit2_( strm: z_streamp, windowBits: c_int, version: *const c_char, stream_size: c_int, ) -> c_int247 pub fn inflateInit2_(
248 strm: z_streamp,
249 windowBits: c_int,
250 version: *const c_char,
251 stream_size: c_int,
252 ) -> c_int;
253 }
254
255 #[cfg(zng)]
256 extern "C" {
zng_deflateInit(strm: z_streamp, level: c_int) -> c_int257 pub fn zng_deflateInit(strm: z_streamp, level: c_int) -> c_int;
zng_deflateInit2( strm: z_streamp, level: c_int, method: c_int, windowBits: c_int, memLevel: c_int, strategy: c_int, ) -> c_int258 pub fn zng_deflateInit2(
259 strm: z_streamp,
260 level: c_int,
261 method: c_int,
262 windowBits: c_int,
263 memLevel: c_int,
264 strategy: c_int,
265 ) -> c_int;
zng_inflateBackInit(strm: z_streamp, windowBits: c_int, window: *mut c_uchar) -> c_int266 pub fn zng_inflateBackInit(strm: z_streamp, windowBits: c_int, window: *mut c_uchar) -> c_int;
zng_inflateInit(strm: z_streamp) -> c_int267 pub fn zng_inflateInit(strm: z_streamp) -> c_int;
zng_inflateInit2(strm: z_streamp, windowBits: c_int) -> c_int268 pub fn zng_inflateInit2(strm: z_streamp, windowBits: c_int) -> c_int;
269 }
270
271 // These methods are required to keep BC with original zlib API since zlib-ng 2.1 that changed API
272 #[cfg(zng)]
273 #[inline(always)]
inflateInit2_( strm: z_streamp, windowBits: c_int, _version: *const c_char, _stream_size: c_int, ) -> c_int274 pub unsafe fn inflateInit2_(
275 strm: z_streamp,
276 windowBits: c_int,
277 _version: *const c_char,
278 _stream_size: c_int,
279 ) -> c_int {
280 zng_inflateInit2(strm, windowBits)
281 }
282
283 #[cfg(zng)]
284 #[inline(always)]
inflateInit_(strm: z_streamp, _version: *const c_char, _stream_size: c_int) -> c_int285 pub unsafe fn inflateInit_(strm: z_streamp, _version: *const c_char, _stream_size: c_int) -> c_int {
286 zng_inflateInit(strm)
287 }
288
289 #[cfg(zng)]
290 #[inline(always)]
inflateBackInit_( strm: z_streamp, windowBits: c_int, window: *mut c_uchar, _version: *const c_char, _stream_size: c_int, ) -> c_int291 pub unsafe fn inflateBackInit_(
292 strm: z_streamp,
293 windowBits: c_int,
294 window: *mut c_uchar,
295 _version: *const c_char,
296 _stream_size: c_int,
297 ) -> c_int {
298 zng_inflateBackInit(strm, windowBits, window)
299 }
300
301 #[cfg(zng)]
302 #[inline(always)]
deflateInit2_( strm: z_streamp, level: c_int, method: c_int, windowBits: c_int, memLevel: c_int, strategy: c_int, _version: *const c_char, _stream_size: c_int, ) -> c_int303 pub unsafe fn deflateInit2_(
304 strm: z_streamp,
305 level: c_int,
306 method: c_int,
307 windowBits: c_int,
308 memLevel: c_int,
309 strategy: c_int,
310 _version: *const c_char,
311 _stream_size: c_int,
312 ) -> c_int {
313 zng_deflateInit2(strm, level, method, windowBits, memLevel, strategy)
314 }
315
316 #[cfg(zng)]
317 #[inline]
deflateInit_( strm: z_streamp, level: c_int, _version: *const c_char, _stream_size: c_int, ) -> c_int318 pub unsafe fn deflateInit_(
319 strm: z_streamp,
320 level: c_int,
321 _version: *const c_char,
322 _stream_size: c_int,
323 ) -> c_int {
324 zng_deflateInit(strm, level)
325 }
326
327 #[cfg(any(zng, feature = "libc"))]
328 extern "C" {
329 #[link_name = zng_prefix!(adler32_combine)]
adler32_combine(adler1: z_checksum, adler2: z_checksum, len2: z_off_t) -> z_checksum330 pub fn adler32_combine(adler1: z_checksum, adler2: z_checksum, len2: z_off_t) -> z_checksum;
331 #[link_name = zng_prefix!(compress)]
compress( dest: *mut Bytef, destLen: *mut z_size, source: *const Bytef, sourceLen: z_size, ) -> c_int332 pub fn compress(
333 dest: *mut Bytef,
334 destLen: *mut z_size,
335 source: *const Bytef,
336 sourceLen: z_size,
337 ) -> c_int;
338 #[link_name = zng_prefix!(compress2)]
compress2( dest: *mut Bytef, destLen: *mut z_size, source: *const Bytef, sourceLen: z_size, level: c_int, ) -> c_int339 pub fn compress2(
340 dest: *mut Bytef,
341 destLen: *mut z_size,
342 source: *const Bytef,
343 sourceLen: z_size,
344 level: c_int,
345 ) -> c_int;
346 #[link_name = zng_prefix!(compressBound)]
compressBound(sourceLen: z_size) -> z_size347 pub fn compressBound(sourceLen: z_size) -> z_size;
348 #[link_name = zng_prefix!(crc32_combine)]
crc32_combine(crc1: z_checksum, crc2: z_checksum, len2: z_off_t) -> z_checksum349 pub fn crc32_combine(crc1: z_checksum, crc2: z_checksum, len2: z_off_t) -> z_checksum;
350 #[link_name = zng_prefix!(gzdirect)]
gzdirect(file: gzFile) -> c_int351 pub fn gzdirect(file: gzFile) -> c_int;
352 #[link_name = zng_prefix!(gzdopen)]
gzdopen(fd: c_int, mode: *const c_char) -> gzFile353 pub fn gzdopen(fd: c_int, mode: *const c_char) -> gzFile;
354 #[link_name = zng_prefix!(gzclearerr)]
gzclearerr(file: gzFile)355 pub fn gzclearerr(file: gzFile);
356 #[link_name = zng_prefix!(gzclose)]
gzclose(file: gzFile) -> c_int357 pub fn gzclose(file: gzFile) -> c_int;
358 #[link_name = zng_prefix!(gzeof)]
gzeof(file: gzFile) -> c_int359 pub fn gzeof(file: gzFile) -> c_int;
360 #[link_name = zng_prefix!(gzerror)]
gzerror(file: gzFile, errnum: *mut c_int) -> *const c_char361 pub fn gzerror(file: gzFile, errnum: *mut c_int) -> *const c_char;
362 #[link_name = zng_prefix!(gzflush)]
gzflush(file: gzFile, flush: c_int) -> c_int363 pub fn gzflush(file: gzFile, flush: c_int) -> c_int;
364 #[link_name = zng_prefix!(gzgetc)]
gzgetc(file: gzFile) -> c_int365 pub fn gzgetc(file: gzFile) -> c_int;
366 #[link_name = zng_prefix!(gzgets)]
gzgets(file: gzFile, buf: *mut c_char, len: c_int) -> *mut c_char367 pub fn gzgets(file: gzFile, buf: *mut c_char, len: c_int) -> *mut c_char;
368 #[link_name = zng_prefix!(gzopen)]
gzopen(path: *const c_char, mode: *const c_char) -> gzFile369 pub fn gzopen(path: *const c_char, mode: *const c_char) -> gzFile;
370 #[link_name = zng_prefix!(gzputc)]
gzputc(file: gzFile, c: c_int) -> c_int371 pub fn gzputc(file: gzFile, c: c_int) -> c_int;
372 #[link_name = zng_prefix!(gzputs)]
gzputs(file: gzFile, s: *const c_char) -> c_int373 pub fn gzputs(file: gzFile, s: *const c_char) -> c_int;
374 #[link_name = zng_prefix!(gzread)]
gzread(file: gzFile, buf: voidp, len: c_uint) -> c_int375 pub fn gzread(file: gzFile, buf: voidp, len: c_uint) -> c_int;
376 #[link_name = zng_prefix!(gzrewind)]
gzrewind(file: gzFile) -> c_int377 pub fn gzrewind(file: gzFile) -> c_int;
378 #[link_name = zng_prefix!(gzseek)]
gzseek(file: gzFile, offset: z_off_t, whence: c_int) -> z_off_t379 pub fn gzseek(file: gzFile, offset: z_off_t, whence: c_int) -> z_off_t;
380 #[link_name = zng_prefix!(gzsetparams)]
gzsetparams(file: gzFile, level: c_int, strategy: c_int) -> c_int381 pub fn gzsetparams(file: gzFile, level: c_int, strategy: c_int) -> c_int;
382 #[link_name = zng_prefix!(gztell)]
gztell(file: gzFile) -> z_off_t383 pub fn gztell(file: gzFile) -> z_off_t;
384 #[link_name = zng_prefix!(gzungetc)]
gzungetc(c: c_int, file: gzFile) -> c_int385 pub fn gzungetc(c: c_int, file: gzFile) -> c_int;
386 #[link_name = zng_prefix!(gzwrite)]
gzwrite(file: gzFile, buf: voidpc, len: c_uint) -> c_int387 pub fn gzwrite(file: gzFile, buf: voidpc, len: c_uint) -> c_int;
388 #[link_name = zng_prefix!(uncompress)]
uncompress( dest: *mut Bytef, destLen: *mut z_size, source: *const Bytef, sourceLen: z_size, ) -> c_int389 pub fn uncompress(
390 dest: *mut Bytef,
391 destLen: *mut z_size,
392 source: *const Bytef,
393 sourceLen: z_size,
394 ) -> c_int;
395 }
396
397 pub const Z_NO_FLUSH: c_int = 0;
398 pub const Z_PARTIAL_FLUSH: c_int = 1;
399 pub const Z_SYNC_FLUSH: c_int = 2;
400 pub const Z_FULL_FLUSH: c_int = 3;
401 pub const Z_FINISH: c_int = 4;
402 pub const Z_BLOCK: c_int = 5;
403 pub const Z_TREES: c_int = 6;
404
405 pub const Z_OK: c_int = 0;
406 pub const Z_STREAM_END: c_int = 1;
407 pub const Z_NEED_DICT: c_int = 2;
408 pub const Z_ERRNO: c_int = -1;
409 pub const Z_STREAM_ERROR: c_int = -2;
410 pub const Z_DATA_ERROR: c_int = -3;
411 pub const Z_MEM_ERROR: c_int = -4;
412 pub const Z_BUF_ERROR: c_int = -5;
413 pub const Z_VERSION_ERROR: c_int = -6;
414
415 pub const Z_NO_COMPRESSION: c_int = 0;
416 pub const Z_BEST_SPEED: c_int = 1;
417 pub const Z_BEST_COMPRESSION: c_int = 9;
418 pub const Z_DEFAULT_COMPRESSION: c_int = -1;
419
420 pub const Z_FILTERED: c_int = 1;
421 pub const Z_HUFFMAN_ONLY: c_int = 2;
422 pub const Z_RLE: c_int = 3;
423 pub const Z_FIXED: c_int = 4;
424 pub const Z_DEFAULT_STRATEGY: c_int = 0;
425
426 pub const Z_BINARY: c_int = 0;
427 pub const Z_TEXT: c_int = 1;
428 pub const Z_ASCII: c_int = Z_TEXT;
429 pub const Z_UNKNOWN: c_int = 2;
430
431 pub const Z_DEFLATED: c_int = 8;
432