Skip to content

Commit 0d45c1b

Browse files
committed
rust/ffi: JsonBuilder wrapper around C JsonBuilder API
For use by plugins that need to work with JsonBuilder.
1 parent d73e476 commit 0d45c1b

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

rust/ffi/src/jsonbuilder.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* Copyright (C) 2026 Open Information Security Foundation
2+
*
3+
* You can copy, redistribute or modify this Program under the terms of
4+
* the GNU General Public License version 2 as published by the Free
5+
* Software Foundation.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* version 2 along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15+
* 02110-1301, USA.
16+
*/
17+
18+
//! Rust wrappers around the C JsonBuilder API.
19+
//!
20+
//! This is for plugin and library users.
21+
22+
use std::ffi::{CString, NulError};
23+
24+
use suricata_sys::sys::{
25+
SCJbClose, SCJbOpenObject, SCJbSetFormatted, SCJbSetString, SCJsonBuilder,
26+
};
27+
28+
// TODO: Map suricata::jsonbuilder::JsonBuilder errors as well,
29+
// however that will require extending that API to pass errors over
30+
// the FFI boundary as integer or strings.
31+
#[derive(Debug)]
32+
pub enum Error {
33+
NulError(NulError),
34+
Other,
35+
}
36+
37+
impl std::fmt::Display for Error {
38+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39+
match self {
40+
Self::NulError(err) => err.fmt(f),
41+
Self::Other => write!(f, "error"),
42+
}
43+
}
44+
}
45+
46+
impl std::error::Error for Error {}
47+
48+
impl From<NulError> for Error {
49+
fn from(err: NulError) -> Self {
50+
Self::NulError(err)
51+
}
52+
}
53+
54+
pub struct JsonBuilder {
55+
jb: *mut SCJsonBuilder,
56+
}
57+
58+
impl JsonBuilder {
59+
pub fn from_raw(jb: *mut SCJsonBuilder) -> Self {
60+
Self { jb }
61+
}
62+
63+
pub fn open_object(&mut self, key: &str) -> Result<&mut Self, Error> {
64+
let key = CString::new(key)?;
65+
if unsafe { SCJbOpenObject(self.jb, key.as_ptr()) } {
66+
Ok(self)
67+
} else {
68+
Err(Error::Other)
69+
}
70+
}
71+
72+
pub fn set_string(&mut self, key: &str, val: &str) -> Result<&mut Self, Error> {
73+
let key = CString::new(key)?;
74+
let val = CString::new(val.escape_default().to_string())?;
75+
if unsafe { SCJbSetString(self.jb, key.as_ptr(), val.as_ptr()) } {
76+
Ok(self)
77+
} else {
78+
Err(Error::Other)
79+
}
80+
}
81+
82+
pub fn set_formatted(&mut self, formatted: &str) -> Result<&mut Self, Error> {
83+
let formatted = CString::new(formatted)?;
84+
if unsafe { SCJbSetFormatted(self.jb, formatted.as_ptr()) } {
85+
Ok(self)
86+
} else {
87+
Err(Error::Other)
88+
}
89+
}
90+
91+
pub fn close(&mut self) -> Result<&mut Self, Error> {
92+
if unsafe { SCJbClose(self.jb) } {
93+
Ok(self)
94+
} else {
95+
Err(Error::Other)
96+
}
97+
}
98+
}

rust/ffi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717

1818
pub mod debug;
1919
pub mod eve;
20+
pub mod jsonbuilder;
2021
pub mod plugin;

0 commit comments

Comments
 (0)