suricata
util-buffer.h File Reference

Go to the source code of this file.

Data Structures

struct  MemBuffer_
 

Macros

#define MemBufferReset(mem_buffer)
 Reset the mem buffer. More...
 
#define MEMBUFFER_BUFFER(mem_buffer)   (mem_buffer)->buffer
 Get the MemBuffers underlying buffer. More...
 
#define MEMBUFFER_OFFSET(mem_buffer)   (mem_buffer)->offset
 Get the MemBuffers current offset. More...
 
#define MEMBUFFER_SIZE(mem_buffer)   (mem_buffer)->size
 Get the MemBuffers current size. More...
 
#define MemBufferPrintToFP(buffer, fp)
 Write a buffer to the file pointer. More...
 
#define MemBufferPrintToFPAsString(mem_buffer, fp)
 Write a buffer to the file pointer as a printable char string. More...
 
#define MemBufferPrintToFPAsHex(mem_buffer, fp)
 Write a buffer in hex format. More...
 
#define MemBufferWriteRaw(dst, raw_buffer, raw_buffer_len)
 Write a raw buffer to the MemBuffer dst. More...
 
#define MemBufferWriteString(dst, ...)
 Write a string buffer to the Membuffer dst. More...
 

Typedefs

typedef struct MemBuffer_ MemBuffer
 

Functions

MemBufferMemBufferCreateNew (uint32_t size)
 
int MemBufferExpand (MemBuffer **buffer, uint32_t expand_by)
 expand membuffer by size of 'expand_by' More...
 
void MemBufferFree (MemBuffer *buffer)
 

Detailed Description

Macro Definition Documentation

#define MEMBUFFER_BUFFER (   mem_buffer)    (mem_buffer)->buffer

Get the MemBuffers underlying buffer.

Definition at line 50 of file util-buffer.h.

Referenced by LogFileWrite(), and TLSGetIPInformations().

#define MEMBUFFER_OFFSET (   mem_buffer)    (mem_buffer)->offset

Get the MemBuffers current offset.

Definition at line 55 of file util-buffer.h.

Referenced by LogFileWrite(), OutputJSONMemBufferCallback(), and TLSGetIPInformations().

#define MEMBUFFER_SIZE (   mem_buffer)    (mem_buffer)->size

Get the MemBuffers current size.

Definition at line 60 of file util-buffer.h.

Referenced by OutputJSONMemBufferCallback().

#define MemBufferPrintToFP (   buffer,
  fp 
)
Value:
do { \
uint32_t i; \
\
for (i = 0; i < (buffer)->offset; i++) { \
if (isprint(buffer->buffer[i])) \
fprintf(fp, "%c", (buffer)->buffer[i]); \
else \
fprintf(fp, "|%02X|", (buffer)->buffer[i]); \
} \
} while (0)
uint64_t offset

Write a buffer to the file pointer.

Accepted buffers can contain both printable and non-printable characters. Printable characters are written in the printable format and the non-printable chars are written in hex codes using the |XX| format.

For example this would be the kind of output in the file - onetwo|EF|three|ED|five

Parameters
bufferPointer to the src MemBuffer instance to write.
fpPointer to the file file instance to write to.

Definition at line 76 of file util-buffer.h.

#define MemBufferPrintToFPAsHex (   mem_buffer,
  fp 
)
Value:
do { \
uint32_t i; \
\
for (i = 0; i < (mem_buffer)->offset; i++) { \
if (((mem_buffer)->offset % 8) == 0) \
fprintf(fp, "\n"); \
fprintf(fp, " %02X", (mem_buffer)->buffer[i]); \
} \
} while (0)
uint64_t offset

Write a buffer in hex format.

Parameters
bufferPointer to the src MemBuffer instance to write.
fpPointer to the file file instance to write to.

Definition at line 103 of file util-buffer.h.

#define MemBufferPrintToFPAsString (   mem_buffer,
  fp 
)
Value:
({ \
fwrite((mem_buffer)->buffer, sizeof(uint8_t), (mem_buffer)->offset, fp); \
})
uint64_t offset

Write a buffer to the file pointer as a printable char string.

Parameters
bufferPointer to the src MemBuffer instance to write.
fpPointer to the file file instance to write to.

Definition at line 93 of file util-buffer.h.

Referenced by EngineAnalysisRules2(), and RulesDumpMatchArray().

#define MemBufferReset (   mem_buffer)
Value:
do { \
(mem_buffer)->buffer[0] = 0; \
(mem_buffer)->offset = 0; \
} while (0)
uint64_t offset

Reset the mem buffer.

Parameters
mem_bufferPointer to the mem buffer instance.

Definition at line 42 of file util-buffer.h.

Referenced by AlertJsonHeader(), JsonAddFlow(), JsonBuildFileInfoRecord(), JsonDNP3LogResponse(), JsonDNSLogAnswer(), JsonHttpLogJSONBodyBase64(), JsonNFSAddMetadata(), JsonSIPAddMetadata(), JsonSMBAddMetadata(), JsonSshLogJSON(), JsonTlsLogJSONExtended(), StatsToJSON(), and TLSGetIPInformations().

#define MemBufferWriteRaw (   dst,
  raw_buffer,
  raw_buffer_len 
)
Value:
do { \
uint32_t write_len; \
\
if (((raw_buffer_len) >= (dst)->size - (dst)->offset)) { \
SCLogDebug("Truncating data write since it exceeded buffer limit of " \
"- %"PRIu32, (dst)->size); \
write_len = ((dst)->size - (dst)->offset) - 1; \
} else { \
write_len = (raw_buffer_len); \
} \
\
memcpy((dst)->buffer + (dst)->offset, (raw_buffer), write_len); \
(dst)->offset += write_len; \
dst->buffer[dst->offset] = '\0'; \
} while (0)
#define SCLogDebug(...)
Definition: util-debug.h:335
uint64_t offset
uint16_t dst

Write a raw buffer to the MemBuffer dst.

When we say raw buffer it indicates a buffer that need not be purely a string buffer. It can be a pure string buffer or not or a mixture of both. Hence we don't accept any format strings.

If the remaining space on the buffer is lesser than the length of the buffer to write, it is truncated to fit into the empty space.

Also after every write a '\0' is appended. This would indicate that the total available space to write in the buffer is MemBuffer->size - 1 and not Membuffer->size. The reason we append the '\0' is for supporting writing pure string buffers as well, that can later be used by other string handling funcs.

Parameters
raw_bufferThe buffer to write.
raw_buffer_lenLength of the above buffer.

Definition at line 133 of file util-buffer.h.

Referenced by OutputJSONBuffer(), and OutputJSONMemBufferCallback().

#define MemBufferWriteString (   dst,
  ... 
)
Value:
do { \
int cw = snprintf((char *)(dst)->buffer + (dst)->offset, \
(dst)->size - (dst)->offset, \
__VA_ARGS__); \
if (cw >= 0) { \
if ( ((dst)->offset + cw) >= (dst)->size) { \
SCLogDebug("Truncating data write since it exceeded buffer " \
"limit of - %"PRIu32"\n", (dst)->size); \
(dst)->offset = (dst)->size - 1; \
} else { \
(dst->offset) += cw; \
} \
} \
} while (0)
#define SCLogDebug(...)
Definition: util-debug.h:335
uint64_t offset
uint16_t dst

Write a string buffer to the Membuffer dst.

This function takes a format string and arguments for the format string like sprintf.

An example usage of this is - MemBufferWriteString(mem_buffer_instance, "d - s", 10, "one");

Parameters
dstThe dst MemBuffer instance.
formatThe format string.
...Variable arguments.

Definition at line 162 of file util-buffer.h.

Referenced by EngineAnalysisRules2(), LogFileWrite(), RulesDumpMatchArray(), and TLSGetIPInformations().

Typedef Documentation

typedef struct MemBuffer_ MemBuffer

Function Documentation

int MemBufferExpand ( MemBuffer **  buffer,
uint32_t  expand_by 
)

expand membuffer by size of 'expand_by'

If expansion failed, buffer will still be valid.

Return values
result0 ok, -1 expansion failed

Definition at line 60 of file util-buffer.c.

References MAX_LIMIT, SC_ERR_MEM_BUFFER_API, SCLogDebug, SCLogWarning, SCRealloc, MemBuffer_::size, and unlikely.

Referenced by OutputJSONMemBufferCallback().

Here is the caller graph for this function: