Skip to content

Commit 431529a

Browse files
committed
transforms: move dotprefix to rust
Ticket: 7229
1 parent 66dc788 commit 431529a

File tree

6 files changed

+98
-219
lines changed

6 files changed

+98
-219
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* Copyright (C) 2024 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+
use super::{
19+
DetectHelperTransformRegister, DetectSignatureAddTransform, InspectionBufferCheckAndExpand,
20+
InspectionBufferLength, InspectionBufferPtr, InspectionBufferTruncate, SCTransformTableElmt,
21+
};
22+
use crate::detect::SIGMATCH_NOOPT;
23+
24+
use std::os::raw::{c_int, c_void};
25+
use std::ptr;
26+
27+
static mut G_TRANSFORM_DOT_PREFIX_ID: c_int = 0;
28+
29+
#[no_mangle]
30+
unsafe extern "C" fn dot_prefix_setup(
31+
_de: *mut c_void, s: *mut c_void, _raw: *const std::os::raw::c_char,
32+
) -> c_int {
33+
return DetectSignatureAddTransform(s, G_TRANSFORM_DOT_PREFIX_ID, ptr::null_mut());
34+
}
35+
36+
fn dot_prefix_transform_do(input: &[u8], output: &mut [u8]) {
37+
output[0] = b'.';
38+
output[1..].copy_from_slice(input);
39+
}
40+
41+
#[no_mangle]
42+
unsafe extern "C" fn dot_prefix_transform(buffer: *mut c_void, _ctx: *mut c_void) {
43+
let input = InspectionBufferPtr(buffer);
44+
let input_len = InspectionBufferLength(buffer);
45+
if input.is_null() || input_len == 0 {
46+
return;
47+
}
48+
let input = build_slice!(input, input_len as usize);
49+
50+
let output = InspectionBufferCheckAndExpand(buffer, input_len + 1);
51+
if output.is_null() {
52+
// allocation failure
53+
return;
54+
}
55+
let output = std::slice::from_raw_parts_mut(output, (input_len + 1) as usize);
56+
57+
dot_prefix_transform_do(input, output);
58+
59+
InspectionBufferTruncate(buffer, input_len + 1);
60+
}
61+
62+
#[no_mangle]
63+
pub unsafe extern "C" fn DetectTransformDotPrefixRegister() {
64+
let kw = SCTransformTableElmt {
65+
name: b"dotprefix\0".as_ptr() as *const libc::c_char,
66+
desc: b"modify buffer to extract the dotprefix\0".as_ptr() as *const libc::c_char,
67+
url: b"/rules/transforms.html#dotprefix\0".as_ptr() as *const libc::c_char,
68+
Setup: dot_prefix_setup,
69+
flags: SIGMATCH_NOOPT,
70+
Transform: dot_prefix_transform,
71+
Free: None,
72+
TransformValidate: None,
73+
};
74+
unsafe {
75+
G_TRANSFORM_DOT_PREFIX_ID = DetectHelperTransformRegister(&kw);
76+
if G_TRANSFORM_DOT_PREFIX_ID < 0 {
77+
SCLogWarning!("Failed registering transform dot_prefix");
78+
}
79+
}
80+
}
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use super::*;
85+
86+
#[test]
87+
fn test_dot_prefix_transform() {
88+
let buf = b"example.com";
89+
let mut out = vec![0; b"example.com".len() + 1];
90+
dot_prefix_transform_do(buf, &mut out);
91+
assert_eq!(out, b".example.com");
92+
let buf = b"hello.example.com";
93+
let mut out = vec![0; b"hello.example.com".len() + 1];
94+
dot_prefix_transform_do(buf, &mut out);
95+
assert_eq!(out, b".hello.example.com");
96+
}
97+
}

rust/src/detect/transforms/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use std::os::raw::{c_char, c_int, c_void};
2121

2222
pub mod compress_whitespace;
23+
pub mod dotprefix;
2324
pub mod strip_whitespace;
2425

2526
#[repr(C)]

src/Makefile.am

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ noinst_HEADERS = \
308308
detect-tos.h \
309309
detect-transform-base64.h \
310310
detect-transform-casechange.h \
311-
detect-transform-dotprefix.h \
312311
detect-transform-header-lowercase.h \
313312
detect-transform-md5.h \
314313
detect-transform-pcrexform.h \
@@ -879,7 +878,6 @@ libsuricata_c_a_SOURCES = \
879878
detect-tos.c \
880879
detect-transform-base64.c \
881880
detect-transform-casechange.c \
882-
detect-transform-dotprefix.c \
883881
detect-transform-header-lowercase.c \
884882
detect-transform-md5.c \
885883
detect-transform-pcrexform.c \

src/detect-engine-register.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@
220220
#include "detect-transform-md5.h"
221221
#include "detect-transform-sha1.h"
222222
#include "detect-transform-sha256.h"
223-
#include "detect-transform-dotprefix.h"
224223
#include "detect-transform-pcrexform.h"
225224
#include "detect-transform-urldecode.h"
226225
#include "detect-transform-xor.h"

src/detect-transform-dotprefix.c

Lines changed: 0 additions & 186 deletions
This file was deleted.

src/detect-transform-dotprefix.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)