Skip to content

Commit c947268

Browse files
committed
feat(read_header): add core_file read header
1 parent 1faabae commit c947268

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

chd-rs-capi/chd.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,31 @@ chd_error chd_codec_config(const struct chd_file *_chd, int32_t _param, void *_c
166166
chd_error chd_read_header(const char *filename,
167167
struct chd_header *header);
168168

169+
/**
170+
* Read CHD header data from the file into the pointed struct.
171+
*
172+
* Ownership of the core_file is retained by the caller when calling this function.
173+
*
174+
* # Safety
175+
* * `filename` is a valid, null-terminated **UTF-8** string.
176+
* * `header` is either `NULL`, or an aligned pointer to a possibly uninitialized `chd_header` struct.
177+
* * If `header` is `NULL`, returns `CHDERR_INVALID_PARAMETER`
178+
*/
179+
chd_error chd_read_header_file(core_file *file,
180+
struct chd_header *header);
181+
182+
/**
183+
* Read CHD header data from the file into the pointed struct.
184+
*
185+
* Ownership is retained by the caller when calling this function.
186+
*
187+
* # Safety
188+
* * `file` is a valid pointer to a `core_file` with respect to the implementation of libchdcorefile that was linked.
189+
* * `header` is either `NULL`, or an aligned pointer to a possibly uninitialized `chd_header` struct.
190+
*/
191+
chd_error chd_read_header_core_file(core_file *file,
192+
struct chd_header *header);
193+
169194
/**
170195
* Returns the associated `core_file*`.
171196
*

chd-rs-capi/src/lib.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,63 @@ pub unsafe extern "C" fn chd_read_header(
358358
}
359359
}
360360

361+
#[no_mangle]
362+
#[cfg(feature = "chd_core_file")]
363+
#[cfg_attr(docsrs, doc(cfg(chd_core_file)))]
364+
/// Read CHD header data from the file into the pointed struct.
365+
///
366+
/// Ownership of the core_file is retained by the caller when calling this function.
367+
///
368+
/// # Safety
369+
/// * `filename` is a valid, null-terminated **UTF-8** string.
370+
/// * `header` is either `NULL`, or an aligned pointer to a possibly uninitialized `chd_header` struct.
371+
/// * If `header` is `NULL`, returns `CHDERR_INVALID_PARAMETER`
372+
pub unsafe extern "C" fn chd_read_header_file(
373+
file: *mut chdcorefile_sys::core_file,
374+
header: *mut MaybeUninit<chd_header>,
375+
) -> chd_error {
376+
let core_file = Box::new(crate::chdcorefile::CoreFile { file }) as Box<dyn SeekRead>;
377+
let chd = match Chd::open(core_file, None) {
378+
Ok(chd) => chd,
379+
Err(e) => return e,
380+
};
381+
382+
let result = match chd {
383+
Ok(chd) => {
384+
let chd_header = ffi_chd_get_header(&chd);
385+
match unsafe { header.as_mut() } {
386+
None => Error::InvalidParameter,
387+
Some(header) => {
388+
header.write(chd_header);
389+
Error::None
390+
}
391+
}
392+
}
393+
Err(e) => e,
394+
};
395+
396+
let (_file, _) = chd.into_inner();
397+
398+
result
399+
}
400+
401+
#[no_mangle]
402+
#[cfg(feature = "chd_virtio")]
403+
#[cfg_attr(docsrs, doc(cfg(chd_virtio)))]
404+
/// Read CHD header data from the file into the pointed struct.
405+
///
406+
/// Ownership is retained by the caller when calling this function.
407+
///
408+
/// # Safety
409+
/// * `file` is a valid pointer to a `core_file` with respect to the implementation of libchdcorefile that was linked.
410+
/// * `header` is either `NULL`, or an aligned pointer to a possibly uninitialized `chd_header` struct.
411+
pub unsafe extern "C" fn chd_read_header_core_file(
412+
file: *mut chdcorefile_sys::core_file,
413+
header: *mut MaybeUninit<chd_header>,
414+
) -> chd_error {
415+
unsafe { chd_read_header_core_file(file, header) }
416+
}
417+
361418
#[no_mangle]
362419
#[cfg(feature = "chd_core_file")]
363420
#[cfg_attr(docsrs, doc(cfg(chd_core_file)))]
@@ -431,7 +488,7 @@ pub unsafe extern "C" fn chd_open_file(
431488
Some(ffi_takeown_chd(parent))
432489
};
433490

434-
let core_file = Box::new(crate::chdcorefile::CoreFile { file: file }) as Box<dyn SeekRead>;
491+
let core_file = Box::new(crate::chdcorefile::CoreFile { file }) as Box<dyn SeekRead>;
435492
let chd = match Chd::open(core_file, parent) {
436493
Ok(chd) => chd,
437494
Err(e) => return e,

0 commit comments

Comments
 (0)