Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ name = "cookbook_rust"
test = true

[features]
default = []
default = ["bzip2"]
bzip2 = ["dep:libbz2-rs-sys"]
shared_mem= []
net_services = []

[dependencies]
libz-rs-sys = { version = "0.5.1" }
libbz2-rs-sys = { version = "0.2.1", optional = true}
libbz2-rs-sys = { version = "0.2.2", optional = true}
libc="0.2.174"
bytemuck = "1.23.1"
tempfile = "3.19"
Expand Down
1 change: 0 additions & 1 deletion src/drvrfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub(crate) struct diskdriver {
last_io_op: c_int,
}

//pub static mut handleTable: Lazy<Mutex<[diskdriver; NMAXFILES]>> = Lazy::new(|| Mutex::new([diskdriver::default(); NMAXFILES]));
/* allocate diskfile handle tables */
pub(crate) static HANDLE_TABLE: Mutex<Vec<diskdriver>> = Mutex::new(Vec::new());

Expand Down
104 changes: 94 additions & 10 deletions src/drvrmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
use core::slice;
use std::ffi::CStr;
use std::io::{Read, Seek, SeekFrom, stdin};

#[cfg(windows)]
use std::os::windows::io::AsRawHandle;

#[cfg(unix)]
use std::os::fd::AsRawFd;

use std::sync::Mutex;
use std::{cmp, mem, ptr};

use crate::c_types::{FILE, c_char, c_int, c_long, c_uchar, c_uint, c_ushort, c_void};
use crate::helpers::cfile::CFile;
use crate::helpers::vec_raw_parts::vec_into_raw_parts;
use crate::zuncompress::zuncompress2mem;
use libc::{EOF, fclose, fgetc, fopen, fread, fwrite, memcmp, memcpy, memset, realloc, ungetc};

use bytemuck::{cast_slice, cast_slice_mut};
Expand Down Expand Up @@ -1143,7 +1151,8 @@ pub(crate) fn mem_rawfile_open(filename: &mut [c_char], rwmode: c_int, hdl: &mut
/*--------------------------------------------------------------------------*/
/// lower level routine to uncompress a file into memory. The file
/// has already been opened and the memory buffer has been allocated.
pub(crate) unsafe fn mem_uncompress2mem<T: Read>(
#[cfg(unix)]
pub(crate) unsafe fn mem_uncompress2mem<T: Read + AsRawFd>(
filename: &[c_char],
diskfile: &mut T,
hdl: c_int,
Expand All @@ -1156,21 +1165,34 @@ pub(crate) unsafe fn mem_uncompress2mem<T: Read>(
let mut m = MEM_TABLE.lock().unwrap();

if strstr_safe(filename, cs!(c".Z")).is_some() {
todo!();
/*
let raw_file_handle = unsafe {
use libc::fdopen;
let fd = diskfile.as_raw_fd();

fdopen(fd, c"rb".as_ptr() as *const c_char)
};

zuncompress2mem(
filename,
diskfile,
raw_file_handle,
m[hdl as usize].memaddrptr as *mut *mut u8, /* pointer to memory address */
m[hdl as usize].memsizeptr.as_mut().unwrap(), /* pointer to size of memory */
Some(realloc), /* reallocation function */
Some(realloc), /* reallocation function */
&mut finalsize,
&mut status, /* returned file size and status*/
&mut status, /* returned file size and status*/
);
*/
} else if strstr_safe(filename, cs!(c".bz2")).is_some() {
#[cfg(feature = "bzip2")]
bzip2uncompress2mem(filename, diskfile, hdl, &mut finalsize, &mut status);
{
let raw_file_handle = unsafe {
use libc::fdopen;
let fd = diskfile.as_raw_fd();

fdopen(fd, c"rb".as_ptr() as *const c_char)
};

bzip2uncompress2mem(filename, raw_file_handle, hdl, &mut finalsize, &mut status);
}
} else {
uncompress2mem(
filename,
Expand All @@ -1189,6 +1211,69 @@ pub(crate) unsafe fn mem_uncompress2mem<T: Read>(
}
}

/*--------------------------------------------------------------------------*/
/// lower level routine to uncompress a file into memory. The file
/// has already been opened and the memory buffer has been allocated.
#[cfg(windows)]
pub(crate) unsafe fn mem_uncompress2mem<T: Read + AsRawHandle>(
filename: &[c_char],
diskfile: &mut T,
hdl: c_int,
) -> c_int {
let mut finalsize = 0;
let mut status = 0;
/* uncompress file into memory */

let mut m = MEM_TABLE.lock().unwrap();

if strstr_safe(filename, cs!(c".Z")).is_some() {
let raw_file_handle = unsafe {
use libc::{open_osfhandle, fdopen};

let handle = diskfile.as_raw_handle();
let fd = open_osfhandle(handle as isize, 0);
fdopen(fd, c"rb".as_ptr() as *const c_char)
};

zuncompress2mem(
filename,
raw_file_handle,
m[hdl as usize].memaddrptr as *mut *mut u8, /* pointer to memory address */
m[hdl as usize].memsizeptr.as_mut().unwrap(), /* pointer to size of memory */
Some(realloc), /* reallocation function */
&mut finalsize,
&mut status, /* returned file size and status*/
);
} else if strstr_safe(filename, cs!(c".bz2")).is_some() {
#[cfg(feature = "bzip2")]
{
let raw_file_handle = unsafe {
use libc::{open_osfhandle, fdopen};

let handle = diskfile.as_raw_handle();
let fd = open_osfhandle(handle as isize, 0);
fdopen(fd, c"rb".as_ptr() as *const c_char)
};

bzip2uncompress2mem(filename, raw_file_handle, hdl, &mut finalsize, &mut status);
}
} else {
uncompress2mem(
filename,
diskfile,
m[hdl as usize].memaddrptr as *mut *mut u8, /* pointer to memory address */
m[hdl as usize].memsizeptr.as_mut().expect(NULL_MSG), /* pointer to size of memory */
Some(realloc), /* reallocation function */
&mut finalsize,
&mut status,
); /* returned file size nd status*/
}

m[hdl as usize].currentpos = 0; /* save starting position */
m[hdl as usize].fitsfilesize = finalsize as LONGLONG; /* and initial file size */
status
}

/*--------------------------------------------------------------------------*/
/// return the size of the file; only called when the file is first opened
pub(crate) fn mem_size(handle: c_int, filesize: &mut usize) -> c_int {
Expand Down Expand Up @@ -1319,7 +1404,6 @@ pub(crate) fn mem_write_unsafe(hdl: c_int, buffer: &[u8], nbytes: usize) -> c_in
let hdl = hdl as usize;
let nbytes = nbytes as LONGLONG;
let mut m = MEM_TABLE.lock().unwrap();
//let m = &mut memTable;

if (m[hdl].currentpos + nbytes) as usize > *(m[hdl].memsizeptr) {
if m[hdl].mem_realloc.is_none() {
Expand Down Expand Up @@ -1403,7 +1487,7 @@ pub(crate) unsafe fn mem_zuncompress_and_write(hdl: c_int, buffer: &[u8], nbytes
#[cfg(feature = "bzip2")]
pub(crate) unsafe fn bzip2uncompress2mem(
filename: &[c_char],
diskfile: &mut FILE,
diskfile: *mut FILE,
hdl: c_int,
filesize: &mut usize,
status: &mut c_int,
Expand Down
18 changes: 8 additions & 10 deletions src/fitscore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ pub unsafe fn ffpmsg_safer(err_message: &[c_char]) {
}
}

pub fn ffpmsg_cstr(err_message: &CStr) {
pub(crate) fn ffpmsg_cstr(err_message: &CStr) {
ffpmsg_slice(cast_slice(err_message.to_bytes_with_nul()))
}

Expand Down Expand Up @@ -532,16 +532,15 @@ pub unsafe extern "C" fn ffcmsg() {
/*--------------------------------------------------------------------------*/
/// erase all messages in the error stack
pub fn ffcmsg_safe() {
let dummy = ptr::null_mut();
unsafe { ffxmsg(DEL_ALL, dummy) };
ffxmsg_safer(DEL_ALL, None);
}

/*--------------------------------------------------------------------------*/
/// erase newest messages in the error stack, stopping if a marker is found.
/// The marker is also erased in this case.
#[cfg_attr(not(test), unsafe(no_mangle), deprecated)]
pub unsafe extern "C" fn ffcmrk() {
ffxmsg_safer(DEL_MARK, None);
ffcmrk_safe();
}

/*--------------------------------------------------------------------------*/
Expand Down Expand Up @@ -589,11 +588,10 @@ pub fn ffxmsg_safer(action: c_int, errmsg: Option<&mut [c_char; FLEN_ERRMSG]>) {
/// PutMark 6 add a marker to the stack
pub(crate) unsafe fn ffxmsg(action: c_int, errmsg: *mut c_char) {
unsafe {
pub static mut TXTBUFF: [*mut c_char; ERRMSGSIZ] = [ptr::null_mut(); ERRMSGSIZ]; /* shift remaining pointers */
pub static mut TMPBUFF: *mut c_char = ptr::null_mut(); /* set pointer for the new message */
pub static mut MSGPTR: *mut c_char = ptr::null_mut(); /* find first empty buffer */
pub static mut ERRBUFF: [[c_char; FLEN_ERRMSG]; ERRMSGSIZ] = [[0; FLEN_ERRMSG]; ERRMSGSIZ]; /* put a marker on the stack */
pub static mut NUMMSG: usize = 0; /* buffers full; reuse oldest buffer */
static mut TXTBUFF: [*mut c_char; ERRMSGSIZ] = [ptr::null_mut(); ERRMSGSIZ]; /* shift remaining pointers */
static mut TMPBUFF: *mut c_char = ptr::null_mut(); /* set pointer for the new message */
static mut ERRBUFF: [[c_char; FLEN_ERRMSG]; ERRMSGSIZ] = [[0; FLEN_ERRMSG]; ERRMSGSIZ]; /* put a marker on the stack */
static mut NUMMSG: usize = 0; /* buffers full; reuse oldest buffer */

let mut ii: usize;
let mut markflag: c_char;
Expand Down Expand Up @@ -651,7 +649,7 @@ pub(crate) unsafe fn ffxmsg(action: c_int, errmsg: *mut c_char) {
*errmsg.offset(0) = 0; /* no messages in the stack */
} else if action == PUT_MESG {
/* add new message to stack */
MSGPTR = errmsg;
let mut MSGPTR = errmsg;
while strlen(MSGPTR) != 0 {
if NUMMSG == ERRMSGSIZ {
TMPBUFF = TXTBUFF[0]; /* buffers full; reuse oldest buffer */
Expand Down
15 changes: 9 additions & 6 deletions src/imcompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2909,6 +2909,8 @@ unsafe fn imcomp_compress_tile(
} else if (outfptr.Fptr).compress_type == BZIP2_1 {
#[cfg(feature = "bzip2")]
{
use libbz2_rs_sys::BZ2_bzBuffToBuffCompress;

if BYTESWAPPED {
let idata: &mut [c_int] = cast_slice_mut(tiledata);

Expand All @@ -2919,21 +2921,22 @@ unsafe fn imcomp_compress_tile(
}
}

let bzlen: c_uint = clen as c_uint;
let mut bzlen: c_uint = clen as c_uint;

/* call bzip2 with blocksize = 900K, verbosity = 0, and default workfactor */

/* bzip2 is not supported in the public release. This is only for
test purposes. */
if (BZ2_bzBuffToBuffCompress(
cbuf,
if BZ2_bzBuffToBuffCompress(
cbuf.as_ptr() as *mut c_char,
&mut bzlen,
cast_slice(tiledata),
(tilelen * intlength),
tiledata.as_ptr() as *mut _,
(tilelen * intlength as c_long) as c_uint,
9,
0,
0,
)) {
) != 0
{
ffpmsg_str("bzip2 compression error");
*status = DATA_COMPRESSION_ERR;
return *status;
Expand Down
4 changes: 2 additions & 2 deletions src/zuncompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,9 @@ mod tests {
#[cfg(windows)]
{
use libc::open_osfhandle;
use std::os::windows::io::IntoRawHandle;
use std::os::windows::io::AsRawHandle;

let handle = compressed_file.into_raw_handle();
let handle = compressed_file.as_raw_handle();
let fd = open_osfhandle(handle as isize, 0);
fdopen(fd, c"rb".as_ptr() as *const c_char)
}
Expand Down
Loading