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 | unsafeextern "C" { |
123 | #[link_name = zng_prefix!(adler32)] |
124 | pub unsafefn adler32(adler: z_checksum, buf: *const Bytef, len: uInt) -> z_checksum; |
125 | #[link_name = zng_prefix!(crc32)] |
126 | pub unsafefn crc32(crc: z_checksum, buf: *const Bytef, len: uInt) -> z_checksum; |
127 | #[link_name = zng_prefix!(deflate)] |
128 | pub unsafefn deflate(strm: z_streamp, flush: c_int) -> c_int; |
129 | #[link_name = zng_prefix!(deflateBound)] |
130 | pub unsafefn deflateBound(strm: z_streamp, sourceLen: uLong) -> uLong; |
131 | #[link_name = zng_prefix!(deflateCopy)] |
132 | pub unsafefn deflateCopy(dest: z_streamp, source: z_streamp) -> c_int; |
133 | #[link_name = zng_prefix!(deflateEnd)] |
134 | pub unsafefn deflateEnd(strm: z_streamp) -> c_int; |
135 | #[link_name = zng_prefix!(deflateParams)] |
136 | pub unsafefn deflateParams(strm: z_streamp, level: c_int, strategy: c_int) -> c_int; |
137 | #[link_name = zng_prefix!(deflatePrime)] |
138 | pub unsafefn deflatePrime(strm: z_streamp, bits: c_int, value: c_int) -> c_int; |
139 | #[link_name = zng_prefix!(deflateReset)] |
140 | pub unsafefn deflateReset(strm: z_streamp) -> c_int; |
141 | #[link_name = zng_prefix!(deflateSetDictionary)] |
142 | pub unsafefn deflateSetDictionary( |
143 | strm: z_streamp, |
144 | dictionary: *const Bytef, |
145 | dictLength: uInt, |
146 | ) -> c_int; |
147 | #[link_name = zng_prefix!(deflateSetHeader)] |
148 | pub unsafefn deflateSetHeader(strm: z_streamp, head: gz_headerp) -> c_int; |
149 | #[link_name = zng_prefix!(deflateTune)] |
150 | pub unsafefn 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)] |
158 | pub unsafefn inflate(strm: z_streamp, flush: c_int) -> c_int; |
159 | #[link_name = zng_prefix!(inflateBack)] |
160 | pub unsafefn 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)] |
168 | pub unsafefn inflateBackEnd(strm: z_streamp) -> c_int; |
169 | #[link_name = zng_prefix!(inflateCopy)] |
170 | pub unsafefn inflateCopy(dest: z_streamp, source: z_streamp) -> c_int; |
171 | #[link_name = zng_prefix!(inflateEnd)] |
172 | pub unsafefn inflateEnd(strm: z_streamp) -> c_int; |
173 | #[link_name = zng_prefix!(inflateGetHeader)] |
174 | pub unsafefn inflateGetHeader(strm: z_streamp, head: gz_headerp) -> c_int; |
175 | #[link_name = zng_prefix!(inflateMark)] |
176 | pub unsafefn inflateMark(strm: z_streamp) -> c_long; |
177 | #[link_name = zng_prefix!(inflatePrime)] |
178 | pub unsafefn inflatePrime(strm: z_streamp, bits: c_int, value: c_int) -> c_int; |
179 | #[link_name = zng_prefix!(inflateReset)] |
180 | pub unsafefn inflateReset(strm: z_streamp) -> c_int; |
181 | #[link_name = zng_prefix!(inflateReset2)] |
182 | pub unsafefn inflateReset2(strm: z_streamp, windowBits: c_int) -> c_int; |
183 | #[link_name = zng_prefix!(inflateSetDictionary)] |
184 | pub unsafefn inflateSetDictionary( |
185 | strm: z_streamp, |
186 | dictionary: *const Bytef, |
187 | dictLength: uInt, |
188 | ) -> c_int; |
189 | #[link_name = zng_prefix!(inflateSync)] |
190 | pub unsafefn inflateSync(strm: z_streamp) -> c_int; |
191 | #[link_name = zng_prefix!(zlibCompileFlags)] |
192 | pub unsafefn 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 | unsafeextern "C" { |
217 | #[link_name = if_zng!("zlibng_version" , "zlibVersion" )] |
218 | pub unsafefn zlibVersion() -> *const c_char; |
219 | } |
220 | |
221 | #[cfg (not(zng))] |
222 | unsafeextern "C" { |
223 | pub unsafefn deflateInit_( |
224 | strm: z_streamp, |
225 | level: c_int, |
226 | version: *const c_char, |
227 | stream_size: c_int, |
228 | ) -> c_int; |
229 | pub unsafefn 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; |
239 | pub unsafefn 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; |
246 | pub unsafefn inflateInit_(strm: z_streamp, version: *const c_char, stream_size: c_int) -> c_int; |
247 | pub unsafefn 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" { |
257 | pub fn zng_deflateInit(strm: z_streamp, level: c_int) -> c_int; |
258 | 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; |
266 | pub fn zng_inflateBackInit(strm: z_streamp, windowBits: c_int, window: *mut c_uchar) -> c_int; |
267 | pub fn zng_inflateInit(strm: z_streamp) -> c_int; |
268 | 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)] |
274 | 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)] |
285 | 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)] |
291 | 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)] |
303 | 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 ] |
318 | 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 | unsafeextern "C" { |
329 | #[link_name = zng_prefix!(adler32_combine)] |
330 | pub unsafefn adler32_combine(adler1: z_checksum, adler2: z_checksum, len2: z_off_t) -> z_checksum; |
331 | #[link_name = zng_prefix!(compress)] |
332 | pub unsafefn 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)] |
339 | pub unsafefn 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)] |
347 | pub unsafefn compressBound(sourceLen: z_size) -> z_size; |
348 | #[link_name = zng_prefix!(crc32_combine)] |
349 | pub unsafefn crc32_combine(crc1: z_checksum, crc2: z_checksum, len2: z_off_t) -> z_checksum; |
350 | #[link_name = zng_prefix!(gzdirect)] |
351 | pub unsafefn gzdirect(file: gzFile) -> c_int; |
352 | #[link_name = zng_prefix!(gzdopen)] |
353 | pub unsafefn gzdopen(fd: c_int, mode: *const c_char) -> gzFile; |
354 | #[link_name = zng_prefix!(gzclearerr)] |
355 | pub unsafefn gzclearerr(file: gzFile); |
356 | #[link_name = zng_prefix!(gzclose)] |
357 | pub unsafefn gzclose(file: gzFile) -> c_int; |
358 | #[link_name = zng_prefix!(gzeof)] |
359 | pub unsafefn gzeof(file: gzFile) -> c_int; |
360 | #[link_name = zng_prefix!(gzerror)] |
361 | pub unsafefn gzerror(file: gzFile, errnum: *mut c_int) -> *const c_char; |
362 | #[link_name = zng_prefix!(gzflush)] |
363 | pub unsafefn gzflush(file: gzFile, flush: c_int) -> c_int; |
364 | #[link_name = zng_prefix!(gzgetc)] |
365 | pub unsafefn gzgetc(file: gzFile) -> c_int; |
366 | #[link_name = zng_prefix!(gzgets)] |
367 | pub unsafefn gzgets(file: gzFile, buf: *mut c_char, len: c_int) -> *mut c_char; |
368 | #[link_name = zng_prefix!(gzopen)] |
369 | pub unsafefn gzopen(path: *const c_char, mode: *const c_char) -> gzFile; |
370 | #[link_name = zng_prefix!(gzputc)] |
371 | pub unsafefn gzputc(file: gzFile, c: c_int) -> c_int; |
372 | #[link_name = zng_prefix!(gzputs)] |
373 | pub unsafefn gzputs(file: gzFile, s: *const c_char) -> c_int; |
374 | #[link_name = zng_prefix!(gzread)] |
375 | pub unsafefn gzread(file: gzFile, buf: voidp, len: c_uint) -> c_int; |
376 | #[link_name = zng_prefix!(gzrewind)] |
377 | pub unsafefn gzrewind(file: gzFile) -> c_int; |
378 | #[link_name = zng_prefix!(gzseek)] |
379 | pub unsafefn gzseek(file: gzFile, offset: z_off_t, whence: c_int) -> z_off_t; |
380 | #[link_name = zng_prefix!(gzsetparams)] |
381 | pub unsafefn gzsetparams(file: gzFile, level: c_int, strategy: c_int) -> c_int; |
382 | #[link_name = zng_prefix!(gztell)] |
383 | pub unsafefn gztell(file: gzFile) -> z_off_t; |
384 | #[link_name = zng_prefix!(gzungetc)] |
385 | pub unsafefn gzungetc(c: c_int, file: gzFile) -> c_int; |
386 | #[link_name = zng_prefix!(gzwrite)] |
387 | pub unsafefn gzwrite(file: gzFile, buf: voidpc, len: c_uint) -> c_int; |
388 | #[link_name = zng_prefix!(uncompress)] |
389 | pub unsafefn 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 | |