]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
jsonbuilder: export {set,append}_string_from_bytes to C
authorJason Ish <jason.ish@oisf.net>
Mon, 8 Jun 2020 17:11:28 +0000 (11:11 -0600)
committerVictor Julien <victor@inliniac.net>
Tue, 9 Jun 2020 11:29:04 +0000 (13:29 +0200)
rust/src/jsonbuilder.rs

index 3394383ce6aefc6d2ec62480717e0998645e6b82..0cd9ed6b05fafebc993b39768fe9810edb1e1f5d 100644 (file)
@@ -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();