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  if (size > MAX_LIMIT) {
35  SCLogWarning(SC_ERR_MEM_BUFFER_API, "Mem buffer asked to create "
36  "buffer with size greater than API limit - %d", MAX_LIMIT);
37  return NULL;
38  }
39 
40  uint32_t total_size = size + sizeof(MemBuffer);
41 
42  MemBuffer *buffer = SCMalloc(total_size);
43  if (unlikely(buffer == NULL)) {
44  return NULL;
45  }
46  memset(buffer, 0, total_size);
47 
48  buffer->size = size;
49  buffer->buffer = (uint8_t *)buffer + sizeof(MemBuffer);
50 
51  return buffer;
52 }
53 
54 /** \brief expand membuffer by size of 'expand_by'
55  *
56  * If expansion failed, buffer will still be valid.
57  *
58  * \retval result 0 ok, -1 expansion failed
59  */
60 int MemBufferExpand(MemBuffer **buffer, uint32_t expand_by) {
61  if (((*buffer)->size + expand_by) > MAX_LIMIT) {
62  SCLogWarning(SC_ERR_MEM_BUFFER_API, "Mem buffer asked to create "
63  "buffer with size greater than API limit - %d", MAX_LIMIT);
64  return -1;
65  }
66 
67  uint32_t total_size = (*buffer)->size + sizeof(MemBuffer) + expand_by;
68 
69  MemBuffer *tbuffer = SCRealloc(*buffer, total_size);
70  if (unlikely(tbuffer == NULL)) {
71  return -1;
72  }
73 
74  *buffer = tbuffer;
75  (*buffer)->size += expand_by;
76  (*buffer)->buffer = (uint8_t *)tbuffer + sizeof(MemBuffer);
77 
78  SCLogDebug("expanded buffer by %u, size is now %u", expand_by, (*buffer)->size);
79  return 0;
80 }
81 
82 void MemBufferFree(MemBuffer *buffer)
83 {
84  SCFree(buffer);
85 
86  return;
87 }
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define SCLogDebug(...)
Definition: util-debug.h:335
#define unlikely(expr)
Definition: util-optimize.h:35
uint32_t size
Definition: util-buffer.h:29
#define MAX_LIMIT
Definition: util-buffer.c:30
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
#define SCRealloc(x, a)
Definition: util-mem.h:238
#define SCMalloc(a)
Definition: util-mem.h:222
#define SCFree(a)
Definition: util-mem.h:322
struct MemBuffer_ MemBuffer
int MemBufferExpand(MemBuffer **buffer, uint32_t expand_by)
expand membuffer by size of &#39;expand_by&#39;
Definition: util-buffer.c:60
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82