@@ -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