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

◆ MEMBUFFER_BUFFER

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

Get the MemBuffers underlying buffer.

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

◆ MEMBUFFER_OFFSET

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

Get the MemBuffers current offset.

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

◆ MEMBUFFER_SIZE

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

Get the MemBuffers current size.

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

◆ MemBufferPrintToFP

#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)

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 instance to write to.

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

◆ MemBufferPrintToFPAsHex

#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)

Write a buffer in hex format.

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

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

◆ MemBufferPrintToFPAsString

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

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 instance to write to.

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

◆ MemBufferReset

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

Reset the mem buffer.

Parameters
mem_bufferPointer to the mem buffer instance.

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

◆ MemBufferWriteRaw

#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)

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 134 of file util-buffer.h.

◆ MemBufferWriteString

#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)

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 163 of file util-buffer.h.

Typedef Documentation

◆ MemBuffer

typedef struct MemBuffer_ MemBuffer

Function Documentation

◆ MemBufferCreateNew()

MemBuffer* MemBufferCreateNew ( uint32_t  size)

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

References MAX_LIMIT, SC_EINVAL, SC_ENOMEM, sc_errno, SC_OK, SCLogWarning, SCMalloc, and unlikely.

Referenced by CreateEveThreadCtx(), JsonLogThreadInit(), LogHttpLogThreadInit(), LogStatsLogThreadInit(), and LogTcpDataLogThreadInit().

Here is the caller graph for this function:

◆ MemBufferExpand()

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 64 of file util-buffer.c.

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

Referenced by OutputJsonBuilderBuffer(), and OutputJSONMemBufferCallback().

Here is the caller graph for this function:

◆ MemBufferFree()

void MemBufferFree ( MemBuffer buffer)

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

References SCFree.

Referenced by CreateEveThreadCtx(), FreeEveThreadCtx(), JsonLogThreadInit(), LogHttpLogThreadDeinit(), LogStatsLogThreadDeinit(), and LogTcpDataLogThreadDeinit().

Here is the caller graph for this function:
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
dst
uint16_t dst
Definition: app-layer-dnp3.h:4