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 }
SC_ERR_MEM_BUFFER_API
@ SC_ERR_MEM_BUFFER_API
Definition: util-error.h:239
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:60
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
util-debug.h
MAX_LIMIT
#define MAX_LIMIT
Definition: util-buffer.c:30
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:82
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
MemBuffer
struct MemBuffer_ MemBuffer
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
SCFree
#define SCFree(p)
Definition: util-mem.h:61
util-buffer.h
suricata.h
MemBuffer_::size
uint32_t size
Definition: util-buffer.h:29
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32