From e3b7c582189a056be4d2e08dc1358535e8759803 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Mon, 8 Jun 2020 11:11:28 -0600 Subject: [PATCH] jsonbuilder: export {set,append}_string_from_bytes to C --- rust/src/jsonbuilder.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust/src/jsonbuilder.rs b/rust/src/jsonbuilder.rs index 3394383ce6..0cd9ed6b05 100644 --- a/rust/src/jsonbuilder.rs +++ b/rust/src/jsonbuilder.rs @@ -605,6 +605,20 @@ pub unsafe extern "C" fn jb_set_string( return false; } +#[no_mangle] +pub unsafe extern "C" fn jb_set_string_from_bytes( + js: &mut JsonBuilder, key: *const c_char, bytes: *const u8, len: u32, +) -> bool { + if bytes == std::ptr::null() || len == 0 { + return false; + } + if let Ok(key) = CStr::from_ptr(key).to_str() { + let val = std::slice::from_raw_parts(bytes, len as usize); + return js.set_string_from_bytes(key, val).is_ok(); + } + return false; +} + #[no_mangle] pub unsafe extern "C" fn jb_set_jsont( jb: &mut JsonBuilder, key: *const c_char, jsont: &mut json::JsonT, @@ -641,6 +655,17 @@ pub unsafe extern "C" fn jb_append_string(js: &mut JsonBuilder, val: *const c_ch return false; } +#[no_mangle] +pub unsafe extern "C" fn jb_append_string_from_bytes( + js: &mut JsonBuilder, bytes: *const u8, len: u32, +) -> bool { + if bytes == std::ptr::null() || len == 0 { + return false; + } + let val = std::slice::from_raw_parts(bytes, len as usize); + return js.append_string_from_bytes(val).is_ok(); +} + #[no_mangle] pub unsafe extern "C" fn jb_append_uint(js: &mut JsonBuilder, val: u64) -> bool { return js.append_uint(val).is_ok();