/src/suricata7/src/util-buffer.h
Line | Count | Source |
1 | | /* Copyright (C) 2007-2023 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 | | /** |
19 | | * \file |
20 | | * |
21 | | * \author Anoop Saldanha <anoopsaldanha@gmail.com> |
22 | | */ |
23 | | |
24 | | #ifndef __UTIL_BUFFER_H__ |
25 | | #define __UTIL_BUFFER_H__ |
26 | | |
27 | | typedef struct MemBuffer_ { |
28 | | uint32_t size; |
29 | | uint32_t offset; |
30 | | uint8_t buffer[]; |
31 | | } MemBuffer; |
32 | | |
33 | | MemBuffer *MemBufferCreateNew(uint32_t size); |
34 | | int MemBufferExpand(MemBuffer **buffer, uint32_t expand_by); |
35 | | void MemBufferFree(MemBuffer *buffer); |
36 | | |
37 | | /** |
38 | | * \brief Reset the mem buffer. |
39 | | * |
40 | | * \param mem_buffer Pointer to the mem buffer instance. |
41 | | */ |
42 | | static inline void MemBufferReset(MemBuffer *b) |
43 | 13.5M | { |
44 | 13.5M | b->buffer[0] = 0; |
45 | 13.5M | b->offset = 0; |
46 | 13.5M | } Unexecuted instantiation: runmode-unix-socket.c:MemBufferReset Unexecuted instantiation: tm-modules.c:MemBufferReset Unexecuted instantiation: unix-manager.c:MemBufferReset Unexecuted instantiation: util-buffer.c:MemBufferReset Unexecuted instantiation: output.c:MemBufferReset Unexecuted instantiation: output-eve-stream.c:MemBufferReset output-json-alert.c:MemBufferReset Line | Count | Source | 43 | 238k | { | 44 | 238k | b->buffer[0] = 0; | 45 | 238k | b->offset = 0; | 46 | 238k | } |
Unexecuted instantiation: output-json-anomaly.c:MemBufferReset Unexecuted instantiation: output-json-bittorrent-dht.c:MemBufferReset output-json.c:MemBufferReset Line | Count | Source | 43 | 13.1M | { | 44 | 13.1M | b->buffer[0] = 0; | 45 | 13.1M | b->offset = 0; | 46 | 13.1M | } |
Unexecuted instantiation: output-json-common.c:MemBufferReset Unexecuted instantiation: output-json-dcerpc.c:MemBufferReset Unexecuted instantiation: output-json-dhcp.c:MemBufferReset Unexecuted instantiation: output-json-dnp3.c:MemBufferReset Unexecuted instantiation: output-json-dnp3-objects.c:MemBufferReset Unexecuted instantiation: output-json-dns.c:MemBufferReset Unexecuted instantiation: output-json-drop.c:MemBufferReset Unexecuted instantiation: output-json-email-common.c:MemBufferReset Unexecuted instantiation: output-json-file.c:MemBufferReset output-json-frame.c:MemBufferReset Line | Count | Source | 43 | 49.2k | { | 44 | 49.2k | b->buffer[0] = 0; | 45 | 49.2k | b->offset = 0; | 46 | 49.2k | } |
Unexecuted instantiation: output-json-ftp.c:MemBufferReset Unexecuted instantiation: output-json-http2.c:MemBufferReset Unexecuted instantiation: output-json-http.c:MemBufferReset Unexecuted instantiation: output-json-ike.c:MemBufferReset Unexecuted instantiation: output-json-krb5.c:MemBufferReset Unexecuted instantiation: output-json-metadata.c:MemBufferReset Unexecuted instantiation: output-json-modbus.c:MemBufferReset Unexecuted instantiation: output-json-quic.c:MemBufferReset Unexecuted instantiation: output-json-mqtt.c:MemBufferReset Unexecuted instantiation: output-json-netflow.c:MemBufferReset output-json-nfs.c:MemBufferReset Line | Count | Source | 43 | 22.0k | { | 44 | 22.0k | b->buffer[0] = 0; | 45 | 22.0k | b->offset = 0; | 46 | 22.0k | } |
Unexecuted instantiation: output-json-pgsql.c:MemBufferReset Unexecuted instantiation: output-json-rdp.c:MemBufferReset Unexecuted instantiation: output-json-rfb.c:MemBufferReset Unexecuted instantiation: output-json-sip.c:MemBufferReset Unexecuted instantiation: output-json-smb.c:MemBufferReset Unexecuted instantiation: output-json-smtp.c:MemBufferReset Unexecuted instantiation: output-json-snmp.c:MemBufferReset Unexecuted instantiation: output-json-ssh.c:MemBufferReset Unexecuted instantiation: output-json-stats.c:MemBufferReset Unexecuted instantiation: output-json-template.c:MemBufferReset Unexecuted instantiation: output-json-tftp.c:MemBufferReset Unexecuted instantiation: output-json-tls.c:MemBufferReset Unexecuted instantiation: util-logopenfile.c:MemBufferReset Unexecuted instantiation: alert-debuglog.c:MemBufferReset Unexecuted instantiation: alert-fastlog.c:MemBufferReset Unexecuted instantiation: alert-syslog.c:MemBufferReset Unexecuted instantiation: log-cf-common.c:MemBufferReset log-httplog.c:MemBufferReset Line | Count | Source | 43 | 81.2k | { | 44 | 81.2k | b->buffer[0] = 0; | 45 | 81.2k | b->offset = 0; | 46 | 81.2k | } |
Unexecuted instantiation: log-pcap.c:MemBufferReset Unexecuted instantiation: log-stats.c:MemBufferReset Unexecuted instantiation: log-tcp-data.c:MemBufferReset log-tlslog.c:MemBufferReset Line | Count | Source | 43 | 3.95k | { | 44 | 3.95k | b->buffer[0] = 0; | 45 | 3.95k | b->offset = 0; | 46 | 3.95k | } |
|
47 | | |
48 | | /** |
49 | | * \brief Get the MemBuffers underlying buffer. |
50 | | */ |
51 | 13.2M | #define MEMBUFFER_BUFFER(mem_buffer) (mem_buffer)->buffer |
52 | | |
53 | | /** |
54 | | * \brief Get the MemBuffers current offset. |
55 | | */ |
56 | 19.3M | #define MEMBUFFER_OFFSET(mem_buffer) (mem_buffer)->offset |
57 | | |
58 | | /** |
59 | | * \brief Get the MemBuffers current size. |
60 | | */ |
61 | 6.13M | #define MEMBUFFER_SIZE(mem_buffer) (mem_buffer)->size |
62 | | |
63 | | /** |
64 | | * \brief Write a buffer to the file pointer. |
65 | | * |
66 | | * Accepted buffers can contain both printable and non-printable |
67 | | * characters. Printable characters are written in the printable |
68 | | * format and the non-printable chars are written in hex codes |
69 | | * using the |XX| format. |
70 | | * |
71 | | * For example this would be the kind of output in the file - |
72 | | * onetwo|EF|three|ED|five |
73 | | * |
74 | | * \param buffer Pointer to the src MemBuffer instance to write. |
75 | | * \param fp Pointer to the file instance to write to. |
76 | | */ |
77 | | void MemBufferPrintToFP(MemBuffer *buffer, FILE *fp); |
78 | | |
79 | | /** |
80 | | * \brief Write a buffer to the file pointer as a printable char string. |
81 | | * |
82 | | * \param b Pointer to the src MemBuffer instance to write. |
83 | | * \param fp Pointer to the file instance to write to. |
84 | | * \retval size_t bytes written by fwrite() |
85 | | */ |
86 | | size_t MemBufferPrintToFPAsString(MemBuffer *b, FILE *fp); |
87 | | |
88 | | /** |
89 | | * \brief Write a buffer in hex format. |
90 | | * |
91 | | * \param b Pointer to the src MemBuffer instance to write. |
92 | | * \param fp Pointer to the file instance to write to. |
93 | | */ |
94 | | void MemBufferPrintToFPAsHex(MemBuffer *b, FILE *fp); |
95 | | |
96 | | /** |
97 | | * \brief Write a raw buffer to the MemBuffer dst. |
98 | | * |
99 | | * When we say raw buffer it indicates a buffer that need not be |
100 | | * purely a string buffer. It can be a pure string buffer or not or |
101 | | * a mixture of both. Hence we don't accept any format strings. |
102 | | * |
103 | | * If the remaining space on the buffer is lesser than the length of |
104 | | * the buffer to write, it is truncated to fit into the empty space. |
105 | | * |
106 | | * Also after every write a '\0' is appended. This would indicate |
107 | | * that the total available space to write in the buffer is |
108 | | * MemBuffer->size - 1 and not Membuffer->size. The reason we |
109 | | * append the '\0' is for supporting writing pure string buffers |
110 | | * as well, that can later be used by other string handling funcs. |
111 | | * |
112 | | * \param raw_buffer The buffer to write. |
113 | | * \param raw_buffer_len Length of the above buffer. |
114 | | * \retval write_len Bytes written. If less than raw_len, the buffer is full. |
115 | | */ |
116 | | uint32_t MemBufferWriteRaw(MemBuffer *dst, const uint8_t *raw, const uint32_t raw_len); |
117 | | |
118 | | /** |
119 | | * \brief Write a string buffer to the Membuffer dst. |
120 | | * |
121 | | * This function takes a format string and arguments for the format |
122 | | * string like sprintf. |
123 | | * |
124 | | * An example usage of this is - |
125 | | * MemBufferWriteString(mem_buffer_instance, \"%d - %s\", 10, \"one\"); |
126 | | * |
127 | | * \param dst The dst MemBuffer instance. |
128 | | * \param format The format string. |
129 | | * \param ... Variable arguments. |
130 | | */ |
131 | | void MemBufferWriteString(MemBuffer *dst, const char *fmt, ...) ATTR_FMT_PRINTF(2, 3); |
132 | | |
133 | | #endif /* __UTIL_BUFFER_H__ */ |