diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:40:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:40:14 +0000 |
commit | 666c8268e8bdf745f609f7f389e5c67be45f0065 (patch) | |
tree | 93948ffd948bd0f56bd3b454cef09bd00a01224e /rust/src/jsonbuilder.rs | |
parent | Adding upstream version 1:7.0.3. (diff) | |
download | suricata-666c8268e8bdf745f609f7f389e5c67be45f0065.tar.xz suricata-666c8268e8bdf745f609f7f389e5c67be45f0065.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'rust/src/jsonbuilder.rs')
-rw-r--r-- | rust/src/jsonbuilder.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/rust/src/jsonbuilder.rs b/rust/src/jsonbuilder.rs index 9ff6234..7264be5 100644 --- a/rust/src/jsonbuilder.rs +++ b/rust/src/jsonbuilder.rs @@ -527,6 +527,22 @@ impl JsonBuilder { } } + /// Set a key and a string value (from bytes) on an object, with a limited size + pub fn set_string_from_bytes_limited(&mut self, key: &str, val: &[u8], limit: usize) -> Result<&mut Self, JsonError> { + let mut valtrunc = Vec::new(); + let val = if val.len() > limit { + valtrunc.extend_from_slice(&val[..limit]); + valtrunc.extend_from_slice(b"[truncated]"); + &valtrunc + } else { + val + }; + match std::str::from_utf8(val) { + Ok(s) => self.set_string(key, s), + Err(_) => self.set_string(key, &try_string_from_bytes(val)?), + } + } + /// Set a key and a string field as the base64 encoded string of the value. pub fn set_base64(&mut self, key: &str, val: &[u8]) -> Result<&mut Self, JsonError> { match self.current_state() { |