From 4b499e751b3162be796311523c94e763ae3088a5 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Thu, 7 Nov 2024 01:14:09 +0100 Subject: [PATCH] `struct BZFILE`: just make it a struct with private fields --- bzlib.rs | 83 ++++++++++++++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 48 deletions(-) diff --git a/bzlib.rs b/bzlib.rs index d1972c5f5..7283d2486 100644 --- a/bzlib.rs +++ b/bzlib.rs @@ -472,19 +472,16 @@ impl DSlice { /// - [`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) { @@ -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::(); + let bzf = std::ptr::null_mut::(); BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_OK); @@ -1884,7 +1881,7 @@ pub unsafe extern "C" fn BZ2_bzWriteOpen( return core::ptr::null_mut(); } - let Some(bzf) = bzalloc_array::(default_bzalloc, core::ptr::null_mut(), 1) else { + let Some(bzf) = bzalloc_array::(default_bzalloc, core::ptr::null_mut(), 1) else { BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_MEM_ERROR); return core::ptr::null_mut(); }; @@ -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); core::ptr::null_mut() } @@ -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); return; }; @@ -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); return; }; @@ -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. @@ -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::(); + let bzf = std::ptr::null_mut::(); BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_OK); @@ -2266,7 +2259,7 @@ pub unsafe extern "C" fn BZ2_bzReadOpen( return std::ptr::null_mut(); } - let Some(bzf) = bzalloc_array::(default_bzalloc, core::ptr::null_mut(), 1) else { + let Some(bzf) = bzalloc_array::(default_bzalloc, core::ptr::null_mut(), 1) else { BZ_SETERR_RAW!(bzerror, bzf, ReturnCode::BZ_MEM_ERROR); return core::ptr::null_mut(); }; @@ -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); 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`]. @@ -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); return; }; @@ -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`. @@ -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); return 0; }; @@ -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); return; }; @@ -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 { let mut bzerr = 0; - if (*(b as *mut bzFile)).lastErr == ReturnCode::BZ_STREAM_END { + if (*b).lastErr == ReturnCode::BZ_STREAM_END { return 0; } let nread = BZ2_bzRead(&mut bzerr, b, buf, len); @@ -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; }; @@ -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() { *errnum = err; }; @@ -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,