suricata
util-buffer.c
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 #include "suricata-common.h"
25 #include "suricata.h"
26 #include "util-debug.h"
27 #include "util-buffer.h"
28 
29 /* 10 mb */
30 #define MAX_LIMIT 10485760
31 
33 {
34  sc_errno = SC_OK;
35  if (size > MAX_LIMIT) {
36  SCLogWarning("Mem buffer asked to create "
37  "buffer with size greater than API limit - %d",
38  MAX_LIMIT);
40  return NULL;
41  }
42 
43  uint32_t total_size = size + sizeof(MemBuffer);
44 
45  MemBuffer *buffer = SCCalloc(1, total_size);
46  if (unlikely(buffer == NULL)) {
48  return NULL;
49  }
50 
51  buffer->size = size;
52  buffer->buffer = (uint8_t *)buffer + sizeof(MemBuffer);
53 
54  return buffer;
55 }
56 
57 /** \brief expand membuffer by size of 'expand_by'
58  *
59  * If expansion failed, buffer will still be valid.
60  *
61  * \retval result 0 ok, -1 expansion failed
62  */
63 int MemBufferExpand(MemBuffer **buffer, uint32_t expand_by) {
64  if (((*buffer)->size + expand_by) > MAX_LIMIT) {
65  SCLogWarning("Mem buffer asked to create "
66  "buffer with size greater than API limit - %d",
67  MAX_LIMIT);
68  return -1;
69  }
70 
71  uint32_t total_size = (*buffer)->size + sizeof(MemBuffer) + expand_by;
72 
73  MemBuffer *tbuffer = SCRealloc(*buffer, total_size);
74  if (unlikely(tbuffer == NULL)) {
75  return -1;
76  }
77 
78  *buffer = tbuffer;
79  (*buffer)->size += expand_by;
80  (*buffer)->buffer = (uint8_t *)tbuffer + sizeof(MemBuffer);
81 
82  SCLogDebug("expanded buffer by %u, size is now %u", expand_by, (*buffer)->size);
83  return 0;
84 }
85 
86 void MemBufferFree(MemBuffer *buffer)
87 {
88  SCFree(buffer);
89 
90  return;
91 }
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
MemBufferExpand
int MemBufferExpand(MemBuffer **buffer, uint32_t expand_by)
expand membuffer by size of 'expand_by'
Definition: util-buffer.c:63
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
SC_EINVAL
@ SC_EINVAL
Definition: util-error.h:30
SC_ENOMEM
@ SC_ENOMEM
Definition: util-error.h:29
util-debug.h
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
MAX_LIMIT
#define MAX_LIMIT
Definition: util-buffer.c:30
SC_OK
@ SC_OK
Definition: util-error.h:27
MemBuffer_
Definition: util-buffer.h:27
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
suricata-common.h
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:86
MemBuffer
struct MemBuffer_ MemBuffer
SCFree
#define SCFree(p)
Definition: util-mem.h:61
util-buffer.h
sc_errno
thread_local SCError sc_errno
Definition: util-error.c:31
suricata.h
MemBuffer_::size
uint32_t size
Definition: util-buffer.h:29
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32