Skip to content

Commit

Permalink
struct BZFILE: just make it a struct with private fields
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Nov 7, 2024
1 parent f1e8737 commit 4b499e7
Showing 1 changed file with 35 additions and 48 deletions.
83 changes: 35 additions & 48 deletions bzlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,19 +472,16 @@ impl<T> DSlice<T> {
/// - [`BZ2_bzReadClose`]
/// - [`BZ2_bzWriteClose`]
/// - [`BZ2_bzclose`]
pub enum BZFILE {}

#[allow(non_camel_case_types)]
#[derive(Copy, Clone)]
#[repr(C)]
pub struct bzFile {
pub handle: *mut FILE,
pub buf: [i8; BZ_MAX_UNUSED_U32 as usize],
pub bufN: i32,
pub strm: bz_stream,
pub lastErr: ReturnCode,
pub operation: Operation,
pub initialisedOk: bool,
pub struct BZFILE {
handle: *mut FILE,
buf: [i8; BZ_MAX_UNUSED_U32 as usize],
bufN: i32,
strm: bz_stream,
lastErr: ReturnCode,
operation: Operation,
initialisedOk: bool,
}

pub fn BZ2_bz__AssertH__fail(errcode: libc::c_int) {
Expand Down Expand Up @@ -1866,7 +1863,7 @@ pub unsafe extern "C" fn BZ2_bzWriteOpen(
verbosity: libc::c_int,
mut workFactor: libc::c_int,
) -> *mut BZFILE {
let bzf: *mut bzFile = std::ptr::null_mut::<bzFile>();
let bzf = std::ptr::null_mut::<BZFILE>();

BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_OK);

Expand All @@ -1884,7 +1881,7 @@ pub unsafe extern "C" fn BZ2_bzWriteOpen(
return core::ptr::null_mut();
}

let Some(bzf) = bzalloc_array::<bzFile>(default_bzalloc, core::ptr::null_mut(), 1) else {
let Some(bzf) = bzalloc_array::<BZFILE>(default_bzalloc, core::ptr::null_mut(), 1) else {
BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_MEM_ERROR);
return core::ptr::null_mut();
};
Expand All @@ -1911,11 +1908,11 @@ pub unsafe extern "C" fn BZ2_bzWriteOpen(
bzf.strm.avail_in = 0;
bzf.initialisedOk = true;

bzf as *mut bzFile as *mut BZFILE
bzf as *mut BZFILE
}
error => {
BZ_SETERR!(bzerror, bzf, error);
free(bzf as *mut bzFile as *mut libc::c_void);
free(bzf as *mut BZFILE as *mut libc::c_void);

Check warning on line 1915 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L1915

Added line #L1915 was not covered by tests

core::ptr::null_mut()
}
Expand Down Expand Up @@ -1956,12 +1953,10 @@ pub unsafe extern "C" fn BZ2_bzWrite(
buf: *const libc::c_void,
len: libc::c_int,
) {
let bzf = b as *mut bzFile;

BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_OK);
BZ_SETERR_RAW!(bzerror, b, ReturnCode::BZ_OK);

let Some(bzf) = bzf.as_mut() else {
BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_PARAM_ERROR);
let Some(bzf) = b.as_mut() else {
BZ_SETERR_RAW!(bzerror, b, ReturnCode::BZ_PARAM_ERROR);

Check warning on line 1959 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L1959

Added line #L1959 was not covered by tests
return;
};

Expand Down Expand Up @@ -2108,10 +2103,8 @@ pub unsafe extern "C" fn BZ2_bzWriteClose64(
nbytes_out_lo32: *mut libc::c_uint,
nbytes_out_hi32: *mut libc::c_uint,
) {
let bzf: *mut bzFile = b as *mut bzFile;

let Some(bzf) = bzf.as_mut() else {
BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_PARAM_ERROR);
let Some(bzf) = b.as_mut() else {
BZ_SETERR_RAW!(bzerror, b, ReturnCode::BZ_PARAM_ERROR);

Check warning on line 2107 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2107

Added line #L2107 was not covered by tests
return;
};

Expand Down Expand Up @@ -2193,7 +2186,7 @@ pub unsafe extern "C" fn BZ2_bzWriteClose64(
BZ_SETERR!(bzerror, bzf, ReturnCode::BZ_OK);

BZ2_bzCompressEnd(&mut bzf.strm);
free(bzf as *mut bzFile as *mut libc::c_void);
free(bzf as *mut BZFILE as *mut libc::c_void);
}

/// Prepare to read compressed data from a file handle.
Expand Down Expand Up @@ -2247,7 +2240,7 @@ pub unsafe extern "C" fn BZ2_bzReadOpen(
unused: *const c_void,
nUnused: c_int,
) -> *mut BZFILE {
let bzf: *mut bzFile = std::ptr::null_mut::<bzFile>();
let bzf = std::ptr::null_mut::<BZFILE>();

BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_OK);

Expand All @@ -2266,7 +2259,7 @@ pub unsafe extern "C" fn BZ2_bzReadOpen(
return std::ptr::null_mut();

Check warning on line 2259 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2259

Added line #L2259 was not covered by tests
}

let Some(bzf) = bzalloc_array::<bzFile>(default_bzalloc, core::ptr::null_mut(), 1) else {
let Some(bzf) = bzalloc_array::<BZFILE>(default_bzalloc, core::ptr::null_mut(), 1) else {
BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_MEM_ERROR);
return core::ptr::null_mut();
};
Expand Down Expand Up @@ -2301,12 +2294,12 @@ pub unsafe extern "C" fn BZ2_bzReadOpen(
}
ret => {
BZ_SETERR!(bzerror, bzf, ret);
free(bzf as *mut bzFile as *mut c_void);
free(bzf as *mut BZFILE as *mut c_void);

Check warning on line 2297 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2297

Added line #L2297 was not covered by tests
return core::ptr::null_mut();
}
}

bzf as *mut bzFile as *mut BZFILE
bzf as *mut BZFILE
}

/// Releases all memory associated with a [`BZFILE`] opened with [`BZ2_bzReadOpen`].
Expand Down Expand Up @@ -2334,12 +2327,10 @@ pub unsafe extern "C" fn BZ2_bzReadOpen(
/// [`pointer::as_mut`]: https://doc.rust-lang.org/core/primitive.pointer.html#method.as_mut
#[export_name = prefix!(BZ2_bzReadClose)]
pub unsafe extern "C" fn BZ2_bzReadClose(bzerror: *mut libc::c_int, b: *mut BZFILE) {
let bzf: *mut bzFile = b as *mut bzFile;
BZ_SETERR_RAW!(bzerror, b, ReturnCode::BZ_OK);

BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_OK);

let Some(bzf) = bzf.as_mut() else {
BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_OK);
let Some(bzf) = b.as_mut() else {
BZ_SETERR_RAW!(bzerror, b, ReturnCode::BZ_OK);

Check warning on line 2333 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2333

Added line #L2333 was not covered by tests
return;
};

Expand All @@ -2352,7 +2343,7 @@ pub unsafe extern "C" fn BZ2_bzReadClose(bzerror: *mut libc::c_int, b: *mut BZFI
BZ2_bzDecompressEnd(&mut bzf.strm);
}

free(bzf as *mut bzFile as *mut libc::c_void);
free(bzf as *mut BZFILE as *mut libc::c_void);
}

/// Reads up to `len` (uncompressed) bytes from the compressed file `b` into the buffer `buf`.
Expand Down Expand Up @@ -2396,12 +2387,10 @@ pub unsafe extern "C" fn BZ2_bzRead(
buf: *mut c_void,
len: c_int,
) -> c_int {
let bzf: *mut bzFile = b as *mut bzFile;

BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_OK);
BZ_SETERR_RAW!(bzerror, b, ReturnCode::BZ_OK);

let Some(bzf) = bzf.as_mut() else {
BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_PARAM_ERROR);
let Some(bzf) = b.as_mut() else {
BZ_SETERR_RAW!(bzerror, b, ReturnCode::BZ_PARAM_ERROR);

Check warning on line 2393 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2393

Added line #L2393 was not covered by tests
return 0;
};

Expand Down Expand Up @@ -2509,10 +2498,8 @@ pub unsafe extern "C" fn BZ2_bzReadGetUnused(
unused: *mut *mut libc::c_void,
nUnused: *mut libc::c_int,
) {
let bzf: *mut bzFile = b as *mut bzFile;

let Some(bzf) = bzf.as_mut() else {
BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_PARAM_ERROR);
let Some(bzf) = b.as_mut() else {
BZ_SETERR_RAW!(bzerror, b, ReturnCode::BZ_PARAM_ERROR);

Check warning on line 2502 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2502

Added line #L2502 was not covered by tests
return;
};

Expand Down Expand Up @@ -2877,7 +2864,7 @@ pub unsafe extern "C" fn BZ2_bzdopen(fd: c_int, mode: *const c_char) -> *mut BZF
pub unsafe extern "C" fn BZ2_bzread(b: *mut BZFILE, buf: *mut c_void, len: c_int) -> c_int {

Check warning on line 2864 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2864

Added line #L2864 was not covered by tests
let mut bzerr = 0;

if (*(b as *mut bzFile)).lastErr == ReturnCode::BZ_STREAM_END {
if (*b).lastErr == ReturnCode::BZ_STREAM_END {

Check warning on line 2867 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2867

Added line #L2867 was not covered by tests
return 0;
}
let nread = BZ2_bzRead(&mut bzerr, b, buf, len);

Check warning on line 2870 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2870

Added line #L2870 was not covered by tests
Expand Down Expand Up @@ -2952,7 +2939,7 @@ pub unsafe extern "C" fn BZ2_bzclose(b: *mut BZFILE) {
let mut bzerr: libc::c_int = 0;

let (fp, operation) = {
let Some(bzf) = (b as *mut bzFile).as_mut() else {
let Some(bzf) = b.as_mut() else {
return;
};

Expand Down Expand Up @@ -3025,7 +3012,7 @@ const BZERRORSTRINGS: [&str; 16] = [
/// [`pointer::as_mut`]: https://doc.rust-lang.org/core/primitive.pointer.html#method.as_mut
#[export_name = prefix!(BZ2_bzerror)]
pub unsafe extern "C" fn BZ2_bzerror(b: *const BZFILE, errnum: *mut c_int) -> *const c_char {
let err = Ord::min(0, (*(b as *const bzFile)).lastErr as c_int);
let err = Ord::min(0, (*(b)).lastErr as c_int);
if let Some(errnum) = errnum.as_mut() {

Check warning on line 3016 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L3014-L3016

Added lines #L3014 - L3016 were not covered by tests
*errnum = err;
};

Check warning on line 3018 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L3018

Added line #L3018 was not covered by tests
Expand All @@ -3042,7 +3029,7 @@ mod tests {

#[test]
fn error_messages() {
let mut bz_file = bzFile {
let mut bz_file = BZFILE {
handle: std::ptr::null_mut(),
buf: [0; 5000],
bufN: 0,
Expand Down

0 comments on commit 4b499e7

Please sign in to comment.