Coverage Report

Created: 2026-09-06 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/rust/src/mime/detect.rs
Line
Count
Source
1
/* Copyright (C) 2025 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::mime;
19
use super::smtp::MimeStateSMTP;
20
use std::ffi::CStr;
21
use std::ptr;
22
23
/// Intermediary function used in detect-email.c to access data from the MimeStateSMTP structure.
24
/// The hname parameter determines which data will be returned.
25
#[no_mangle]
26
0
pub unsafe extern "C" fn SCDetectMimeEmailGetData(
27
0
    ctx: &MimeStateSMTP, buffer: *mut *const u8, buffer_len: *mut u32,
28
0
    hname: *const std::os::raw::c_char,
29
0
) -> u8 {
30
0
    let c_str = CStr::from_ptr(hname); //unsafe
31
0
    let str = c_str.to_str().unwrap_or("");
32
33
0
    for h in &ctx.headers[..ctx.main_headers_nb] {
34
0
        if mime::slice_equals_lowercase(&h.name, str.as_bytes()) {
35
0
            *buffer = h.value.as_ptr();
36
0
            *buffer_len = h.value.len() as u32;
37
0
            return 1;
38
0
        }
39
    }
40
41
0
    *buffer = ptr::null();
42
0
    *buffer_len = 0;
43
44
0
    return 0;
45
0
}
46
47
#[no_mangle]
48
0
pub unsafe extern "C" fn SCDetectMimeEmailGetUrl(
49
0
    ctx: &MimeStateSMTP, buffer: *mut *const u8, buffer_len: *mut u32, idx: u32,
50
0
) -> u8 {
51
0
    if !ctx.urls.is_empty() && idx < ctx.urls.len() as u32 {
52
0
        let url = &ctx.urls[idx as usize];
53
0
        *buffer = url.as_ptr();
54
0
        *buffer_len = url.len() as u32;
55
0
        return 1;
56
0
    }
57
58
0
    *buffer = ptr::null();
59
0
    *buffer_len = 0;
60
61
0
    return 0;
62
0
}
63
64
/// Intermediary function used in detect-email.c to access data from the MimeStateSMTP structure
65
/// for array header fields.
66
/// The hname parameter determines which data will be returned.
67
#[no_mangle]
68
pub unsafe extern "C" fn SCDetectMimeEmailGetDataArray(
69
    ctx: &MimeStateSMTP, buffer: *mut *const u8, buffer_len: *mut u32,
70
    hname: *const std::os::raw::c_char, idx: u32,
71
) -> u8 {
72
    let c_str = CStr::from_ptr(hname); //unsafe
73
    let str = c_str.to_str().unwrap_or("");
74
75
    let mut i = 0;
76
    for h in &ctx.headers[..ctx.main_headers_nb] {
77
        if mime::slice_equals_lowercase(&h.name, str.as_bytes()) {
78
            if i == idx {
79
                *buffer = h.value.as_ptr();
80
                *buffer_len = h.value.len() as u32;
81
                return 1;
82
            }
83
            i += 1;
84
        }
85
    }
86
87
    *buffer = ptr::null();
88
    *buffer_len = 0;
89
90
    return 0;
91
}