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 = SCMalloc(total_size);
46  if (unlikely(buffer == NULL)) {
48  return NULL;
49  }
50  memset(buffer, 0, total_size);
51 
52  buffer->size = size;
53  buffer->buffer = (uint8_t *)buffer + sizeof(MemBuffer);
54 
55  return buffer;
56 }
57 
58 /** \brief expand membuffer by size of 'expand_by'
59  *
60  * If expansion failed, buffer will still be valid.
61  *
62  * \retval result 0 ok, -1 expansion failed
63  */
64 int MemBufferExpand(MemBuffer **buffer, uint32_t expand_by) {
65  if (((*buffer)->size + expand_by) > MAX_LIMIT) {
66  SCLogWarning("Mem buffer asked to create "
67  "buffer with size greater than API limit - %d",
68  MAX_LIMIT);
69  return -1;
70  }
71 
72  uint32_t total_size = (*buffer)->size + sizeof(MemBuffer) + expand_by;
73 
74  MemBuffer *tbuffer = SCRealloc(*buffer, total_size);
75  if (unlikely(tbuffer == NULL)) {
76  return -1;
77  }
78 
79  *buffer = tbuffer;
80  (*buffer)->size += expand_by;
81  (*buffer)->buffer = (uint8_t *)tbuffer + sizeof(MemBuffer);
82 
83  SCLogDebug("expanded buffer by %u, size is now %u", expand_by, (*buffer)->size);
84  return 0;
85 }
86 
87 void MemBufferFree(MemBuffer *buffer)
88 {
89  SCFree(buffer);
90 
91  return;
92 }
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:64
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
SC_EINVAL
@ SC_EINVAL
Definition: util-error.h:32
SC_ENOMEM
@ SC_ENOMEM
Definition: util-error.h:31
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:29
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:87
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
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
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32