From: Victor Julien Date: Fri, 8 Dec 2023 09:30:25 +0000 (+0100) Subject: jsonbuilder: add set_int for signed ints X-Git-Tag: suricata-8.0.0-beta1~1918 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8440a0917548061049df19e4d42200df67cb478;p=thirdparty%2Fsuricata.git jsonbuilder: add set_int for signed ints Bug: #6615 --- diff --git a/rust/src/jsonbuilder.rs b/rust/src/jsonbuilder.rs index 82be09953c..9ff6234295 100644 --- a/rust/src/jsonbuilder.rs +++ b/rust/src/jsonbuilder.rs @@ -597,6 +597,27 @@ impl JsonBuilder { Ok(self) } + /// Set a key and a signed integer type on an object. + pub fn set_int(&mut self, key: &str, val: i64) -> Result<&mut Self, JsonError> { + match self.current_state() { + State::ObjectNth => { + self.push(',')?; + } + State::ObjectFirst => { + self.set_state(State::ObjectNth); + } + _ => { + debug_validate_fail!("invalid state"); + return Err(JsonError::InvalidState); + } + } + self.push('"')?; + self.push_str(key)?; + self.push_str("\":")?; + self.push_str(&val.to_string())?; + Ok(self) + } + pub fn set_float(&mut self, key: &str, val: f64) -> Result<&mut Self, JsonError> { match self.current_state() { State::ObjectNth => { @@ -940,6 +961,14 @@ pub unsafe extern "C" fn jb_set_uint(js: &mut JsonBuilder, key: *const c_char, v return false; } +#[no_mangle] +pub unsafe extern "C" fn jb_set_int(js: &mut JsonBuilder, key: *const c_char, val: i64) -> bool { + if let Ok(key) = CStr::from_ptr(key).to_str() { + return js.set_int(key, val).is_ok(); + } + return false; +} + #[no_mangle] pub unsafe extern "C" fn jb_set_float(js: &mut JsonBuilder, key: *const c_char, val: f64) -> bool { if let Ok(key) = CStr::from_ptr(key).to_str() {