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 8, 2024
1 parent f20da3a commit b18fa93
Showing 1 changed file with 36 additions and 49 deletions.
85 changes: 36 additions & 49 deletions bzlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,19 +473,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 @@ -1867,7 +1864,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 @@ -1885,7 +1882,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 @@ -1912,11 +1909,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 1916 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L1916

Added line #L1916 was not covered by tests

core::ptr::null_mut()
}
Expand Down Expand Up @@ -1957,12 +1954,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 1960 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L1960

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

Expand Down Expand Up @@ -2109,10 +2104,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 2108 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2108

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

Expand Down Expand Up @@ -2194,7 +2187,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 @@ -2248,7 +2241,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 @@ -2267,7 +2260,7 @@ pub unsafe extern "C" fn BZ2_bzReadOpen(
return std::ptr::null_mut();

Check warning on line 2260 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2260

Added line #L2260 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 @@ -2302,12 +2295,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 2298 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2298

Added line #L2298 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 @@ -2335,12 +2328,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 2334 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2334

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

Expand All @@ -2353,7 +2344,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 @@ -2397,12 +2388,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 2394 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2394

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

Expand Down Expand Up @@ -2510,10 +2499,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 2503 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2503

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

Expand Down Expand Up @@ -2878,7 +2865,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 2865 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2865

Added line #L2865 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 2868 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2868

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

Check warning on line 2871 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L2871

Added line #L2871 was not covered by tests
Expand Down Expand Up @@ -2953,7 +2940,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 @@ -3026,7 +3013,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 3017 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L3015-L3017

Added lines #L3015 - L3017 were not covered by tests
*errnum = err;
};

Check warning on line 3019 in bzlib.rs

View check run for this annotation

Codecov / codecov/patch

bzlib.rs#L3019

Added line #L3019 was not covered by tests
Expand All @@ -3043,7 +3030,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 Expand Up @@ -3074,7 +3061,7 @@ mod tests {
bz_file.lastErr = return_code;

let mut errnum = 0;
let ptr = unsafe { BZ2_bzerror(&bz_file as *const _ as *const BZFILE, &mut errnum) };
let ptr = unsafe { BZ2_bzerror(&bz_file as *const BZFILE, &mut errnum) };
assert!(!ptr.is_null());
let cstr = unsafe { CStr::from_ptr(ptr) };

Expand Down

0 comments on commit b18fa93

Please sign in to comment.