suricata
util-buffer.h
Go to the documentation of this file.
1
/* Copyright (C) 2007-2012 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
uint8_t *
buffer
;
29
uint32_t
size
;
30
uint32_t
offset
;
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
#define MemBufferReset(mem_buffer) do { \
43
(mem_buffer)->buffer[0] = 0; \
44
(mem_buffer)->offset = 0; \
45
} while (0)
46
47
/**
48
* \brief Get the MemBuffers underlying buffer.
49
*/
50
#define MEMBUFFER_BUFFER(mem_buffer) (mem_buffer)->buffer
51
52
/**
53
* \brief Get the MemBuffers current offset.
54
*/
55
#define MEMBUFFER_OFFSET(mem_buffer) (mem_buffer)->offset
56
57
/**
58
* \brief Get the MemBuffers current size.
59
*/
60
#define MEMBUFFER_SIZE(mem_buffer) (mem_buffer)->size
61
62
/**
63
* \brief Write a buffer to the file pointer.
64
*
65
* Accepted buffers can contain both printable and non-printable
66
* characters. Printable characters are written in the printable
67
* format and the non-printable chars are written in hex codes
68
* using the |XX| format.
69
*
70
* For example this would be the kind of output in the file -
71
* onetwo|EF|three|ED|five
72
*
73
* \param buffer Pointer to the src MemBuffer instance to write.
74
* \param fp Pointer to the file instance to write to.
75
*/
76
#define MemBufferPrintToFP(buffer, fp) do { \
77
uint32_t i; \
78
\
79
for (i = 0; i < (buffer)->offset; i++) { \
80
if (isprint(buffer->buffer[i])) \
81
fprintf(fp, "%c", (buffer)->buffer[i]); \
82
else \
83
fprintf(fp, "|%02X|", (buffer)->buffer[i]); \
84
} \
85
} while (0)
86
87
/**
88
* \brief Write a buffer to the file pointer as a printable char string.
89
*
90
* \param buffer Pointer to the src MemBuffer instance to write.
91
* \param fp Pointer to the file instance to write to.
92
*/
93
#define MemBufferPrintToFPAsString(mem_buffer, fp) ({ \
94
fwrite((mem_buffer)->buffer, sizeof(uint8_t), (mem_buffer)->offset, fp); \
95
})
96
97
/**
98
* \brief Write a buffer in hex format.
99
*
100
* \param buffer Pointer to the src MemBuffer instance to write.
101
* \param fp Pointer to the file instance to write to.
102
*/
103
#define MemBufferPrintToFPAsHex(mem_buffer, fp) do { \
104
uint32_t i; \
105
\
106
for (i = 0; i < (mem_buffer)->offset; i++) { \
107
if (((mem_buffer)->offset % 8) == 0) \
108
fprintf(fp, "\n"); \
109
fprintf(fp, " %02X", (mem_buffer)->buffer[i]); \
110
} \
111
} while (0)
112
113
114
/**
115
* \brief Write a raw buffer to the MemBuffer dst.
116
*
117
* When we say raw buffer it indicates a buffer that need not be
118
* purely a string buffer. It can be a pure string buffer or not or
119
* a mixture of both. Hence we don't accept any format strings.
120
*
121
* If the remaining space on the buffer is lesser than the length of
122
* the buffer to write, it is truncated to fit into the empty space.
123
*
124
* Also after every write a '\0' is appended. This would indicate
125
* that the total available space to write in the buffer is
126
* MemBuffer->size - 1 and not Membuffer->size. The reason we
127
* append the '\0' is for supporting writing pure string buffers
128
* as well, that can later be used by other string handling funcs.
129
*
130
* \param raw_buffer The buffer to write.
131
* \param raw_buffer_len Length of the above buffer.
132
*/
133
#define MemBufferWriteRaw(dst, raw_buffer, raw_buffer_len) do { \
134
uint32_t write_len; \
135
\
136
if (((raw_buffer_len) >= (dst)->size - (dst)->offset)) { \
137
SCLogDebug("Truncating data write since it exceeded buffer limit of " \
138
"- %"PRIu32, (dst)->size); \
139
write_len = ((dst)->size - (dst)->offset) - 1; \
140
} else { \
141
write_len = (raw_buffer_len); \
142
} \
143
\
144
memcpy((dst)->buffer + (dst)->offset, (raw_buffer), write_len); \
145
(dst)->offset += write_len; \
146
dst->buffer[dst->offset] = '\0'; \
147
} while (0)
148
149
/**
150
* \brief Write a string buffer to the Membuffer dst.
151
*
152
* This function takes a format string and arguments for the format
153
* string like sprintf.
154
*
155
* An example usage of this is -
156
* MemBufferWriteString(mem_buffer_instance, \"%d - %s\", 10, \"one\");
157
*
158
* \param dst The dst MemBuffer instance.
159
* \param format The format string.
160
* \param ... Variable arguments.
161
*/
162
#define MemBufferWriteString(dst, ...) do { \
163
int cw = snprintf((char *)(dst)->buffer + (dst)->offset, \
164
(dst)->size - (dst)->offset, \
165
__VA_ARGS__); \
166
if (cw >= 0) { \
167
if ( ((dst)->offset + cw) >= (dst)->size) { \
168
SCLogDebug("Truncating data write since it exceeded buffer " \
169
"limit of - %"PRIu32"\n", (dst)->size); \
170
(dst)->offset = (dst)->size - 1; \
171
} else { \
172
(dst->offset) += cw; \
173
} \
174
} \
175
} while (0)
176
177
#endif
/* __UTIL_BUFFER_H__ */
MemBuffer_::offset
uint32_t offset
Definition:
util-buffer.h:30
MemBuffer_
Definition:
util-buffer.h:27
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition:
util-buffer.c:32
MemBuffer
struct MemBuffer_ MemBuffer
MemBuffer_::size
uint32_t size
Definition:
util-buffer.h:29
MemBufferExpand
int MemBufferExpand(MemBuffer **buffer, uint32_t expand_by)
expand membuffer by size of 'expand_by'
Definition:
util-buffer.c:64
MemBuffer_::buffer
uint8_t * buffer
Definition:
util-buffer.h:28
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition:
util-buffer.c:87
src
util-buffer.h
Generated on Mon Sep 25 2023 23:30:40 for suricata by
1.8.18