source: npl/mailserver/dspam/dspam-3.10.2/src/buffer.c @ c5c522c

gcc484ntopperl-5.22
Last change on this file since c5c522c was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/* $Id: buffer.c,v 1.12 2011/06/28 00:13:48 sbajic Exp $ */
2
3/*
4 DSPAM
5 COPYRIGHT (C) 2002-2012 DSPAM PROJECT
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Affero General Public License as
9 published by the Free Software Foundation, either version 3 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU Affero General Public License for more details.
16
17 You should have received a copy of the GNU Affero General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20*/
21
22/* buffer.c - dynamic string data structure */
23
24#include <string.h>
25#include <stdlib.h>
26
27#include "buffer.h"
28
29buffer *
30buffer_create (const char *s)
31{
32  buffer *b;
33  long len;
34
35  b = malloc (sizeof (buffer));
36  if (!b)
37    return NULL;
38
39  if (!s)
40  {
41    b->size = 1024;
42    b->used = 0;
43    b->data = malloc(b->size);
44    if (!b->data)
45      return NULL;
46    b->data[0] = 0;
47    return b;
48  }
49
50  len = strlen (s);
51  b->size = len + 1;
52  b->used = len;
53  b->data = malloc (b->size);
54  if (b->data == NULL)
55  {
56    free (b);
57    return NULL;
58  }
59
60  memcpy (b->data, s, len);
61  b->data[len] = 0;
62  return b;
63}
64
65buffer *
66buffer_ncreate (const char *s, long plen)
67{
68  buffer *b;
69  long len;
70
71  b = malloc (sizeof (buffer));
72  if (!b)
73    return NULL;
74
75  if (!s)
76  {
77    if(plen == 0)
78      b->size = 1024;
79    else
80      b->size = plen;
81    b->used = 0;
82    b->data = malloc(b->size);
83    if (!b->data)
84      return NULL;
85    b->data[0] = 0;
86    return b;
87  }
88
89  if(plen == 0)
90    len = strlen (s);
91  else
92    len = plen;
93  b->size = len + 1;
94  b->used = len;
95  b->data = malloc (b->size);
96  if (b->data == NULL)
97  {
98    free (b);
99    return NULL;
100  }
101
102  memcpy (b->data, s, len);
103  b->data[len] = 0;
104  return b;
105}
106
107int
108buffer_clear (buffer * b)
109{
110  if (b == NULL)
111    return -1;
112
113  free (b->data);
114  b->size = 0;
115  b->used = 0;
116  b->data = NULL;
117
118  return 0;
119}
120
121void
122buffer_destroy (buffer * b)
123{
124  if (!b)
125    return;
126
127  if (b->data != NULL)
128  {
129    free (b->data);
130  }
131  free (b);
132  return;
133}
134
135int
136buffer_copy (buffer * b, const char *s)
137{
138  char *new_data;
139  long len;
140
141  if (s == NULL)
142    return -1;
143
144  len = strlen (s);
145  new_data = malloc (len + 1);
146  if (new_data == NULL)
147    return -1;
148
149  memcpy (new_data, s, len);
150  new_data[len] = 0;
151
152  if (b->data != NULL)
153  {
154    free (b->data);
155  }
156  b->size = len + 1;
157  b->used = len;
158  b->data = new_data;
159
160  return 0;
161}
162
163int
164buffer_ncopy (buffer * b, const char *s, long plen)
165{
166  if (s == NULL)
167    return -1;
168
169  char *new_data;
170  long len;
171
172  if(plen == 0)
173    len = strlen (s);
174  else
175    len = plen;
176  new_data = malloc (len + 1);
177  if (new_data == NULL)
178    return -1;
179
180  memcpy (new_data, s, len);
181  new_data[len] = 0;
182
183  if (b->data != NULL)
184    free (b->data);
185  b->size = len + 1;
186  b->used = len;
187  b->data = new_data;
188
189  return 0;
190}
191
192int
193buffer_cat (buffer * b, const char *s)
194{
195  char *new_data;
196  long size;
197  long len, used;
198
199  if (!b || !s)
200    return -1;
201
202  size = b->size;
203  len = strlen (s);
204  if (! b->data)
205    return buffer_copy (b, s);
206
207  used = b->used + len;
208  if (used >= size)
209  {
210    size *= 2;
211    size += len;
212    new_data = realloc (b->data, size);
213    if (!new_data)
214      return -1;
215    b->data = new_data;
216    b->size = size;
217  }
218  memcpy (b->data + b->used, s, len);
219  b->used = used;
220  b->data[b->used] = 0;
221  return 0;
222}
223
224int
225buffer_ncat (buffer * b, const char *s, long plen)
226{
227  if (!b || !s)
228    return -1;
229  if (! b->data)
230    return buffer_ncopy (b, s, 0);
231
232  char *new_data;
233  long size;
234  long len, used;
235
236  size = b->size;
237  if(plen == 0)
238    len = strlen (s);
239  else
240    len = plen;
241
242  used = b->used + len;
243  if (used >= size)
244  {
245    size *= 2;
246    size += len;
247    new_data = realloc (b->data, size);
248    if (!new_data)
249      return -1;
250    b->data = new_data;
251    b->size = size;
252  }
253  memcpy (b->data + b->used, s, len);
254  b->used = used;
255  b->data[b->used] = 0;
256  return 0;
257}
Note: See TracBrowser for help on using the repository browser.