Skip to content

Commit f16836d

Browse files
committed
Code review cleanups
1 parent 3e6d010 commit f16836d

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

libz-rs-sys/src/gz.rs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,72 @@
11
use zlib_rs::allocate::*;
22
pub use zlib_rs::c_api::*;
33

4-
use core::ffi::{c_char, c_int, c_uint, CStr };
4+
use core::ffi::{c_char, c_int, c_uint, CStr};
55
use core::mem::size_of;
66
use core::ptr;
77
use libc::{O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_TRUNC, O_WRONLY, SEEK_CUR, SEEK_END};
88
use zlib_rs::deflate::Strategy;
99

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 {}
1915

20-
// File handle for an open gzip file.
16+
/// File handle for an open gzip file.
2117
pub type gzFile = *mut gzFile_s;
2218

2319
// The internals of a gzip file handle (the thing gzFile actually points to, with the
2420
// public gzFile_s part at the front for ABI compatibility).
2521
#[repr(C)]
2622
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.
2834

2935
// Fields used for both reading and writing
3036
mode: GzMode,
31-
fd: c_int, // file descriptor
37+
fd: c_int, // file descriptor
3238
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
3844

3945
// Fields used just for reading
4046
// FIXME: add the 'how' field when read support is implemented
4147
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
4450

4551
// Fields used just for writing
4652
level: i8,
4753
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
4955

5056
// 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
5359

5460
// 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)
5763

58-
// FIXME: add the zstream field when read/write support is implemented
64+
// FIXME: add the zstream field when read/write support is implemented
5965
}
6066

6167
// Gzip operating modes
6268
// NOTE: These values match what zlib-ng uses.
63-
#[derive(Eq, PartialEq)]
69+
#[derive(PartialEq, Eq)]
6470
enum GzMode {
6571
GZ_NONE = 0,
6672
GZ_READ = 7247,
@@ -124,7 +130,7 @@ unsafe fn gzopen_help(path: *const c_char, fd: c_int, mode: *const c_char) -> gz
124130
b'r' => state.mode = GzMode::GZ_READ,
125131
b'w' => state.mode = GzMode::GZ_WRITE,
126132
b'a' => state.mode = GzMode::GZ_APPEND,
127-
b'b' => {}, // binary mode is the default
133+
b'b' => {} // binary mode is the default
128134
b'x' => exclusive = true,
129135
b'f' => state.strategy = Strategy::Filtered,
130136
b'h' => state.strategy = Strategy::HuffmanOnly,
@@ -222,7 +228,7 @@ unsafe fn free_state(state: &mut GzState) {
222228
/// # Safety
223229
/// This function may be called at most once for any file handle.
224230
#[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 {
226232
let Some(state) = file.cast::<GzState>().as_mut() else {
227233
return Z_STREAM_ERROR;
228234
};

0 commit comments

Comments
 (0)