|
| 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_COMPRESS_WHITESPACE_ID: c_int = 0; |
| 28 | + |
| 29 | +#[no_mangle] |
| 30 | +unsafe extern "C" fn compress_whitespace_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_COMPRESS_WHITESPACE_ID, ptr::null_mut()); |
| 34 | +} |
| 35 | + |
| 36 | +fn compress_whitespace_transform_do(input: &[u8], output: &mut [u8]) -> u32 { |
| 37 | + let mut nb = 0; |
| 38 | + // seems faster than writing one byte at a time via |
| 39 | + // for (i, o) in input.iter().filter(|c| !matches!(*c, b'\t' | b'\n' | b'\x0B' | b'\x0C' | b'\r' | b' ')).zip(output) |
| 40 | + for subslice in |
| 41 | + input.split_inclusive(|c| matches!(*c, b'\t' | b'\n' | b'\x0B' | b'\x0C' | b'\r' | b' ')) |
| 42 | + { |
| 43 | + // a subslice of length 1 not at the beginning is a space following another space |
| 44 | + if nb == 0 |
| 45 | + || subslice.len() > 1 |
| 46 | + || !matches!( |
| 47 | + subslice[0], |
| 48 | + b'\t' | b'\n' | b'\x0B' | b'\x0C' | b'\r' | b' ' |
| 49 | + ) |
| 50 | + { |
| 51 | + output[nb..nb + subslice.len()].copy_from_slice(subslice); |
| 52 | + nb += subslice.len(); |
| 53 | + } |
| 54 | + } |
| 55 | + return nb as u32; |
| 56 | +} |
| 57 | + |
| 58 | +#[no_mangle] |
| 59 | +unsafe extern "C" fn compress_whitespace_transform(buffer: *mut c_void, _ctx: *mut c_void) { |
| 60 | + let input = InspectionBufferPtr(buffer); |
| 61 | + let input_len = InspectionBufferLength(buffer); |
| 62 | + if input.is_null() || input_len == 0 { |
| 63 | + return; |
| 64 | + } |
| 65 | + let input = build_slice!(input, input_len as usize); |
| 66 | + |
| 67 | + let output = InspectionBufferCheckAndExpand(buffer, input_len); |
| 68 | + if output.is_null() { |
| 69 | + // allocation failure |
| 70 | + return; |
| 71 | + } |
| 72 | + let output = std::slice::from_raw_parts_mut(output, input_len as usize); |
| 73 | + |
| 74 | + let output_len = compress_whitespace_transform_do(input, output); |
| 75 | + |
| 76 | + InspectionBufferTruncate(buffer, output_len); |
| 77 | +} |
| 78 | + |
| 79 | +fn compress_whitespace_validate_do(input: &[u8]) -> bool { |
| 80 | + let mut space = false; |
| 81 | + for &c in input { |
| 82 | + if space { |
| 83 | + if matches!(c, b'\t' | b'\n' | b'\x0B' | b'\x0C' | b'\r' | b' ') { |
| 84 | + return false; |
| 85 | + } |
| 86 | + space = false; |
| 87 | + } else if matches!(c, b'\t' | b'\n' | b'\x0B' | b'\x0C' | b'\r' | b' ') { |
| 88 | + space = true; |
| 89 | + } |
| 90 | + } |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +#[no_mangle] |
| 95 | +unsafe extern "C" fn compress_whitespace_validate( |
| 96 | + content: *const u8, len: u16, _ctx: *mut c_void, |
| 97 | +) -> bool { |
| 98 | + let input = build_slice!(content, len as usize); |
| 99 | + return compress_whitespace_validate_do(input); |
| 100 | +} |
| 101 | + |
| 102 | +#[no_mangle] |
| 103 | +pub unsafe extern "C" fn DetectTransformCompressWhitespaceRegister() { |
| 104 | + let kw = SCTransformTableElmt { |
| 105 | + name: b"compress_whitespace\0".as_ptr() as *const libc::c_char, |
| 106 | + desc: b"modify buffer to compress consecutive whitespace characters into a single one before inspection\0".as_ptr() |
| 107 | + as *const libc::c_char, |
| 108 | + url: b"/rules/transforms.html#compress-whitespace\0".as_ptr() as *const libc::c_char, |
| 109 | + Setup: compress_whitespace_setup, |
| 110 | + flags: SIGMATCH_NOOPT, |
| 111 | + Transform: compress_whitespace_transform, |
| 112 | + Free: None, |
| 113 | + TransformValidate: Some(compress_whitespace_validate), |
| 114 | + }; |
| 115 | + unsafe { |
| 116 | + G_TRANSFORM_COMPRESS_WHITESPACE_ID = DetectHelperTransformRegister(&kw); |
| 117 | + if G_TRANSFORM_COMPRESS_WHITESPACE_ID < 0 { |
| 118 | + SCLogWarning!("Failed registering transform compress_whitespace"); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +#[cfg(test)] |
| 124 | +mod tests { |
| 125 | + use super::*; |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn test_compress_whitespace_transform() { |
| 129 | + let buf = b" A B C D "; |
| 130 | + let mut out = vec![0; buf.len()]; |
| 131 | + let exp = b" A B C D "; |
| 132 | + assert_eq!( |
| 133 | + compress_whitespace_transform_do(buf, &mut out), |
| 134 | + exp.len() as u32 |
| 135 | + ); |
| 136 | + assert_eq!(&out[..exp.len()], exp); |
| 137 | + let buf = b"EFGH"; |
| 138 | + let mut out = vec![0; buf.len()]; |
| 139 | + let exp = b"EFGH"; |
| 140 | + assert_eq!( |
| 141 | + compress_whitespace_transform_do(buf, &mut out), |
| 142 | + exp.len() as u32 |
| 143 | + ); |
| 144 | + assert_eq!(&out[..exp.len()], exp); |
| 145 | + let buf = b"I \t J"; |
| 146 | + let mut out = vec![0; buf.len()]; |
| 147 | + let exp = b"I J"; |
| 148 | + assert_eq!( |
| 149 | + compress_whitespace_transform_do(buf, &mut out), |
| 150 | + exp.len() as u32 |
| 151 | + ); |
| 152 | + assert_eq!(&out[..exp.len()], exp); |
| 153 | + } |
| 154 | + |
| 155 | + #[test] |
| 156 | + fn test_compress_whitespace_validate() { |
| 157 | + assert!(compress_whitespace_validate_do(b" A B C D ")); |
| 158 | + assert!(!compress_whitespace_validate_do(b" A B C D ")); |
| 159 | + } |
| 160 | +} |
0 commit comments