|
1 | 1 | use zlib_rs::allocate::*; |
2 | 2 | pub use zlib_rs::c_api::*; |
3 | 3 |
|
4 | | -use core::ffi::{c_char, c_int, c_uint, CStr }; |
| 4 | +use core::ffi::{c_char, c_int, c_uint, CStr}; |
5 | 5 | use core::mem::size_of; |
6 | 6 | use core::ptr; |
7 | 7 | use libc::{O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_TRUNC, O_WRONLY, SEEK_CUR, SEEK_END}; |
8 | 8 | use zlib_rs::deflate::Strategy; |
9 | 9 |
|
10 | | -/// For compatibility with the zlib C API, this structure exposes just enough of the |
11 | | -/// internal state of an open gzFile to support the gzgetc() C macro. |
12 | | -#[repr(C)] |
13 | | -#[derive(Copy, Clone)] |
14 | | -pub struct gzFile_s { |
15 | | - have: c_uint, // number of bytes available at next |
16 | | - next: *const Bytef, // next byte of uncompressed data |
17 | | - pos: u64, // current offset in uncompressed data stream |
18 | | -} |
| 10 | +/// In the zlib C API, this structure exposes just enough of the internal state |
| 11 | +/// of an open gzFile to support the gzgetc() C macro. Since Rust code won't be |
| 12 | +/// using that C macro, we define gzFile_s as an empty structure. The first fields |
| 13 | +/// in GzState match what would be in the C version of gzFile_s. |
| 14 | +pub enum gzFile_s {} |
19 | 15 |
|
20 | | -// File handle for an open gzip file. |
| 16 | +/// File handle for an open gzip file. |
21 | 17 | pub type gzFile = *mut gzFile_s; |
22 | 18 |
|
23 | 19 | // The internals of a gzip file handle (the thing gzFile actually points to, with the |
24 | 20 | // public gzFile_s part at the front for ABI compatibility). |
25 | 21 | #[repr(C)] |
26 | 22 | struct GzState { |
27 | | - x: gzFile_s, // "x" for the exposed part of the data structure |
| 23 | + // Public interface: |
| 24 | + // These first three fields must match the structure gzFile_s in the C version |
| 25 | + // of zlib. In the C library, a macro called gzgetc() reads and writes these |
| 26 | + // fields directly. |
| 27 | + have: c_uint, // number of bytes available at next |
| 28 | + next: *const Bytef, // next byte of uncompressed data |
| 29 | + pos: u64, // current offset in uncompressed data stream |
| 30 | + |
| 31 | + // End of public interface: |
| 32 | + // All fields after this point are opaque to C code using this library, |
| 33 | + // so they can be rearranged without breaking compatibility. |
28 | 34 |
|
29 | 35 | // Fields used for both reading and writing |
30 | 36 | mode: GzMode, |
31 | | - fd: c_int, // file descriptor |
| 37 | + fd: c_int, // file descriptor |
32 | 38 | path: *const c_char, |
33 | | - size: usize, // buffer size; can be 0 if not yet allocated |
34 | | - want: usize, // requested buffer size |
35 | | - input: *mut Bytef, // input buffer |
36 | | - output: *mut Bytef, // output buffer |
37 | | - direct: bool, // true in pass-through mode, false if processing gzip data |
| 39 | + size: usize, // buffer size; can be 0 if not yet allocated |
| 40 | + want: usize, // requested buffer size |
| 41 | + input: *mut Bytef, // input buffer |
| 42 | + output: *mut Bytef, // output buffer |
| 43 | + direct: bool, // true in pass-through mode, false if processing gzip data |
38 | 44 |
|
39 | 45 | // Fields used just for reading |
40 | 46 | // FIXME: add the 'how' field when read support is implemented |
41 | 47 | start: i64, |
42 | | - eof: bool, // whether we have reached the end of the input file |
43 | | - past: bool, // whether a read past the end has been requested |
| 48 | + eof: bool, // whether we have reached the end of the input file |
| 49 | + past: bool, // whether a read past the end has been requested |
44 | 50 |
|
45 | 51 | // Fields used just for writing |
46 | 52 | level: i8, |
47 | 53 | strategy: Strategy, |
48 | | - reset: bool, // whether a reset is pending after a Z_FINISH |
| 54 | + reset: bool, // whether a reset is pending after a Z_FINISH |
49 | 55 |
|
50 | 56 | // Fields used for seek requests |
51 | | - skip: i64, // amount to skip (already rewound if backwards) |
52 | | - seek: bool, // whether a seek request is pending |
| 57 | + skip: i64, // amount to skip (already rewound if backwards) |
| 58 | + seek: bool, // whether a seek request is pending |
53 | 59 |
|
54 | 60 | // Error information |
55 | | - err: c_int, // last error (0 if no error) |
56 | | - msg: *const c_char, // error message from last error (NULL if none) |
| 61 | + err: c_int, // last error (0 if no error) |
| 62 | + msg: *const c_char, // error message from last error (NULL if none) |
57 | 63 |
|
58 | | - // FIXME: add the zstream field when read/write support is implemented |
| 64 | + // FIXME: add the zstream field when read/write support is implemented |
59 | 65 | } |
60 | 66 |
|
61 | 67 | // Gzip operating modes |
62 | 68 | // NOTE: These values match what zlib-ng uses. |
63 | | -#[derive(Eq, PartialEq)] |
| 69 | +#[derive(PartialEq, Eq)] |
64 | 70 | enum GzMode { |
65 | 71 | GZ_NONE = 0, |
66 | 72 | GZ_READ = 7247, |
@@ -124,7 +130,7 @@ unsafe fn gzopen_help(path: *const c_char, fd: c_int, mode: *const c_char) -> gz |
124 | 130 | b'r' => state.mode = GzMode::GZ_READ, |
125 | 131 | b'w' => state.mode = GzMode::GZ_WRITE, |
126 | 132 | b'a' => state.mode = GzMode::GZ_APPEND, |
127 | | - b'b' => {}, // binary mode is the default |
| 133 | + b'b' => {} // binary mode is the default |
128 | 134 | b'x' => exclusive = true, |
129 | 135 | b'f' => state.strategy = Strategy::Filtered, |
130 | 136 | b'h' => state.strategy = Strategy::HuffmanOnly, |
@@ -222,7 +228,7 @@ unsafe fn free_state(state: &mut GzState) { |
222 | 228 | /// # Safety |
223 | 229 | /// This function may be called at most once for any file handle. |
224 | 230 | #[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(gzclose))] |
225 | | -pub unsafe extern "C-unwind" fn gzclose(file: gzFile) -> c_int{ |
| 231 | +pub unsafe extern "C-unwind" fn gzclose(file: gzFile) -> c_int { |
226 | 232 | let Some(state) = file.cast::<GzState>().as_mut() else { |
227 | 233 | return Z_STREAM_ERROR; |
228 | 234 | }; |
|
0 commit comments