2021-12-18 21:05:00 -08:00
|
|
|
extern crate byteorder;
|
|
|
|
|
|
|
|
|
|
pub mod basic;
|
|
|
|
|
pub mod record;
|
|
|
|
|
pub mod records;
|
|
|
|
|
pub mod elements;
|
2022-03-20 16:28:34 -07:00
|
|
|
pub mod library;
|
2024-12-21 09:50:19 -08:00
|
|
|
|
2025-04-12 12:06:36 -07:00
|
|
|
use crate::library::read_library;
|
2024-12-21 09:50:19 -08:00
|
|
|
|
2024-12-21 15:42:50 -08:00
|
|
|
use byteorder::{ByteOrder, BigEndian};
|
2026-11-14 14:23:13 -08:00
|
|
|
use std::any::Any;
|
|
|
|
|
use std::cell::RefCell;
|
2024-12-21 15:42:50 -08:00
|
|
|
use std::mem::size_of;
|
2026-11-14 14:23:13 -08:00
|
|
|
use std::ffi::{CStr, CString, c_char};
|
|
|
|
|
use std::panic::{AssertUnwindSafe, catch_unwind};
|
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
use std::ptr;
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
use std::os::unix::ffi::OsStringExt;
|
2025-04-12 12:06:36 -07:00
|
|
|
use std::str;
|
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
|
|
use arrow::ffi::{to_ffi, FFI_ArrowArray, FFI_ArrowSchema};
|
2026-11-14 14:23:13 -08:00
|
|
|
use arrow::array::{Array, StructArray};
|
2025-04-12 12:06:36 -07:00
|
|
|
|
|
|
|
|
|
2026-11-14 14:23:13 -08:00
|
|
|
thread_local! {
|
|
|
|
|
static LAST_ERROR: RefCell<Option<CString>> = const { RefCell::new(None) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn clear_last_error() {
|
|
|
|
|
LAST_ERROR.with(|last_error| *last_error.borrow_mut() = None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn set_last_error(message: String) -> i32 {
|
|
|
|
|
let sanitized = message.replace('\0', "\\0");
|
|
|
|
|
LAST_ERROR.with(|last_error| *last_error.borrow_mut() = Some(CString::new(sanitized).unwrap()));
|
|
|
|
|
-1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn describe_panic(payload: Box<dyn Any + Send>) -> String {
|
|
|
|
|
if let Some(message) = payload.downcast_ref::<&str>() {
|
|
|
|
|
(*message).to_string()
|
|
|
|
|
} else if let Some(message) = payload.downcast_ref::<String>() {
|
|
|
|
|
message.clone()
|
|
|
|
|
} else {
|
|
|
|
|
"unexpected panic".to_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn export_struct_array(
|
|
|
|
|
op_name: &str,
|
|
|
|
|
arr: *mut FFI_ArrowArray,
|
|
|
|
|
schema: *mut FFI_ArrowSchema,
|
|
|
|
|
build: impl FnOnce() -> Result<StructArray, String>,
|
|
|
|
|
) -> i32 {
|
|
|
|
|
clear_last_error();
|
|
|
|
|
if arr.is_null() || schema.is_null() {
|
|
|
|
|
return set_last_error(format!("{op_name}: Arrow output pointer was null"));
|
|
|
|
|
}
|
|
|
|
|
match catch_unwind(AssertUnwindSafe(build)) {
|
|
|
|
|
Ok(Ok(struct_arr)) => match to_ffi(&struct_arr.to_data()) {
|
|
|
|
|
Ok((ffi_arr, ffi_schema)) => {
|
|
|
|
|
unsafe {
|
|
|
|
|
*arr = ffi_arr;
|
|
|
|
|
*schema = ffi_schema;
|
|
|
|
|
}
|
|
|
|
|
0
|
|
|
|
|
},
|
|
|
|
|
Err(err) => set_last_error(format!("{op_name}: failed to export Arrow data: {err}")),
|
|
|
|
|
},
|
|
|
|
|
Ok(Err(message)) => set_last_error(format!("{op_name}: {message}")),
|
|
|
|
|
Err(payload) => set_last_error(format!("{op_name}: {}", describe_panic(payload))),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn pathbuf_from_c(cpath: *const c_char) -> Result<PathBuf, String> {
|
|
|
|
|
if cpath.is_null() {
|
|
|
|
|
return Err("path pointer was null".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cstr = unsafe { CStr::from_ptr(cpath) };
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
{
|
|
|
|
|
return Ok(PathBuf::from(std::ffi::OsString::from_vec(cstr.to_bytes().to_vec())));
|
|
|
|
|
}
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
{
|
|
|
|
|
let ustr = str::from_utf8(cstr.to_bytes())
|
|
|
|
|
.map_err(|_| "Non-UTF8 paths are not supported".to_string())?;
|
|
|
|
|
return Ok(PathBuf::from(ustr));
|
|
|
|
|
}
|
|
|
|
|
#[allow(unreachable_code)]
|
|
|
|
|
Err("Unsupported OS".to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Return this thread's last parser error, or null if its last call succeeded.
|
|
|
|
|
/// The returned string remains valid until this thread's next parser call or exit.
|
|
|
|
|
#[unsafe(no_mangle)]
|
|
|
|
|
pub extern "C" fn last_error_message() -> *const c_char {
|
|
|
|
|
LAST_ERROR.with(|last_error| last_error.borrow()
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map_or(ptr::null(), |message| message.as_ptr()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Read a GDSII file and export its Arrow data. Return 0 on success or -1 on error.
|
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
/// `cpath` must point to a null-terminated path. Non-null output pointers must
|
|
|
|
|
/// point to initialized, writable Arrow FFI structures. Null pointers are rejected.
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-12 12:06:36 -07:00
|
|
|
pub unsafe extern "C" fn read_path(
|
|
|
|
|
cpath: *const c_char,
|
|
|
|
|
arr: *mut FFI_ArrowArray,
|
|
|
|
|
schema: *mut FFI_ArrowSchema,
|
2026-11-14 14:23:13 -08:00
|
|
|
) -> i32 {
|
|
|
|
|
export_struct_array("read_path", arr, schema, || {
|
|
|
|
|
let path = pathbuf_from_c(cpath)?;
|
|
|
|
|
let input = fs::read(Path::new(&path))
|
|
|
|
|
.map_err(|err| format!("file read failed for {}: {err}", path.display()))?;
|
|
|
|
|
let (_input, struct_arr) = read_library(&input)
|
|
|
|
|
.map_err(|err| format!("read failed: {err:?}"))?;
|
|
|
|
|
Ok(struct_arr)
|
|
|
|
|
})
|
2025-04-12 12:06:36 -07:00
|
|
|
}
|
2024-12-21 09:50:19 -08:00
|
|
|
|
|
|
|
|
|
2024-12-21 15:42:50 -08:00
|
|
|
macro_rules! impl_i16be {
|
|
|
|
|
( $tt:ty, $arr:ident, $size:ident ) => {
|
|
|
|
|
{
|
|
|
|
|
let sl = unsafe { std::slice::from_raw_parts_mut($arr, $size) };
|
2024-12-21 13:56:51 -08:00
|
|
|
for xx in sl.iter_mut() {
|
2024-12-21 15:42:50 -08:00
|
|
|
if *xx < i16::MIN as $tt { return *xx }
|
|
|
|
|
if *xx > i16::MAX as $tt { return *xx }
|
|
|
|
|
|
|
|
|
|
let mut buf = [0; size_of::<$tt>()];
|
|
|
|
|
BigEndian::write_i16(&mut buf, *xx as i16);
|
|
|
|
|
*xx = <$tt>::from_le_bytes(buf);
|
2024-12-21 09:50:19 -08:00
|
|
|
}
|
2024-12-21 13:56:51 -08:00
|
|
|
0 as $tt
|
2024-12-21 09:50:19 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-12-21 15:42:50 -08:00
|
|
|
macro_rules! impl_i32be {
|
|
|
|
|
( $tt:ty, $arr:ident, $size:ident ) => {
|
|
|
|
|
{
|
|
|
|
|
let sl = unsafe { std::slice::from_raw_parts_mut($arr, $size) };
|
|
|
|
|
for xx in sl.iter_mut() {
|
|
|
|
|
if *xx < i32::MIN as $tt { return *xx }
|
|
|
|
|
if *xx > i32::MAX as $tt { return *xx }
|
2024-12-21 09:50:19 -08:00
|
|
|
|
2024-12-21 15:42:50 -08:00
|
|
|
let mut buf = [0; size_of::<$tt>()];
|
|
|
|
|
BigEndian::write_i32(&mut buf, *xx as i32);
|
|
|
|
|
*xx = <$tt>::from_le_bytes(buf);
|
2024-12-21 09:50:19 -08:00
|
|
|
}
|
2024-12-21 15:42:50 -08:00
|
|
|
0 as $tt
|
2024-12-21 09:50:19 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-21 15:42:50 -08:00
|
|
|
|
|
|
|
|
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn f64_to_i16(arr: *mut f64, size: usize) -> f64 { impl_i16be!(f64, arr, size) }
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn f64_to_i32(arr: *mut f64, size: usize) -> f64 { impl_i32be!(f64, arr, size) }
|
2024-12-21 15:42:50 -08:00
|
|
|
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn f32_to_i16(arr: *mut f32, size: usize) -> f32 { impl_i16be!(f32, arr, size) }
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn f32_to_i32(arr: *mut f32, size: usize) -> f32 { impl_i32be!(f32, arr, size) }
|
2024-12-21 15:42:50 -08:00
|
|
|
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn u64_to_i16(arr: *mut u64, size: usize) -> u64 { impl_i16be!(u64, arr, size) }
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn u64_to_i32(arr: *mut u64, size: usize) -> u64 { impl_i32be!(u64, arr, size) }
|
2024-12-21 15:42:50 -08:00
|
|
|
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn i64_to_i16(arr: *mut i64, size: usize) -> i64 { impl_i16be!(i64, arr, size) }
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn i64_to_i32(arr: *mut i64, size: usize) -> i64 { impl_i32be!(i64, arr, size) }
|
2024-12-21 15:42:50 -08:00
|
|
|
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn u32_to_i16(arr: *mut u32, size: usize) -> u32 { impl_i16be!(u32, arr, size) }
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn u32_to_i32(arr: *mut u32, size: usize) -> u32 { impl_i32be!(u32, arr, size) }
|
2024-12-21 15:42:50 -08:00
|
|
|
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn i32_to_i16(arr: *mut i32, size: usize) -> i32 { impl_i16be!(i32, arr, size) }
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn i32_to_i32(arr: *mut i32, size: usize) -> i32 { impl_i32be!(i32, arr, size) }
|
2024-12-21 15:42:50 -08:00
|
|
|
|
|
|
|
|
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn u16_to_i16(arr: *mut u16, size: usize) -> u16 { impl_i16be!(u16, arr, size) }
|
2024-12-21 15:42:50 -08:00
|
|
|
|
2026-04-02 20:35:31 -07:00
|
|
|
#[unsafe(no_mangle)]
|
2025-04-11 11:18:56 -07:00
|
|
|
pub unsafe extern "C" fn i16_to_i16(arr: *mut i16, size: usize) -> i16 { impl_i16be!(i16, arr, size) }
|