dlopen-test: Use libnettle.dylib on MacOS.
[gd/nettle] / buffer.c
1 /* buffer.c
2
3    A bare-bones string stream.
4
5    Copyright (C) 2002 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "buffer.h"
43
44 int
45 nettle_buffer_grow(struct nettle_buffer *buffer,
46                    size_t length)
47 {
48   assert(buffer->size <= buffer->alloc);
49   
50   if (buffer->size + length > buffer->alloc)
51     {
52       size_t alloc;
53       uint8_t *p;
54       
55       if (!buffer->realloc)
56         return 0;
57       
58       alloc = buffer->alloc * 2 + length + 100;
59       p = buffer->realloc(buffer->realloc_ctx, buffer->contents, alloc);
60       if (!p)
61         return 0;
62       
63       buffer->contents = p;
64       buffer->alloc = alloc;
65     }
66   return 1;
67 }
68
69 void
70 nettle_buffer_init_realloc(struct nettle_buffer *buffer,
71                            void *realloc_ctx,
72                            nettle_realloc_func *realloc)
73 {
74   buffer->contents = NULL;
75   buffer->alloc = 0;
76   buffer->realloc = realloc;
77   buffer->realloc_ctx = realloc_ctx;
78   buffer->size = 0;
79 }
80
81 void
82 nettle_buffer_init_size(struct nettle_buffer *buffer,
83                         size_t length, uint8_t *space)
84 {
85   buffer->contents = space;
86   buffer->alloc = length;
87   buffer->realloc = NULL;
88   buffer->realloc_ctx = NULL;
89   buffer->size = 0;
90 }
91
92 void
93 nettle_buffer_clear(struct nettle_buffer *buffer)
94 {
95   if (buffer->realloc)
96     buffer->realloc(buffer->realloc_ctx, buffer->contents, 0);
97
98   buffer->contents = NULL;
99   buffer->alloc = 0;
100   buffer->size = 0;
101 }
102
103 void
104 nettle_buffer_reset(struct nettle_buffer *buffer)
105 {
106   buffer->size = 0;
107 }
108
109 uint8_t *
110 nettle_buffer_space(struct nettle_buffer *buffer,
111                     size_t length)
112 {
113   uint8_t *p;
114
115   if (!nettle_buffer_grow(buffer, length))
116     return NULL;
117
118   p = buffer->contents + buffer->size;
119   buffer->size += length;
120   return p;
121 }
122      
123 int
124 nettle_buffer_write(struct nettle_buffer *buffer,
125                     size_t length, const uint8_t *data)
126 {
127   uint8_t *p = nettle_buffer_space(buffer, length);
128   if (p)
129     {
130       memcpy(p, data, length);
131       return 1;
132     }
133   else
134     return 0;
135 }
136
137 int
138 nettle_buffer_copy(struct nettle_buffer *dst,
139                    const struct nettle_buffer *src)
140 {
141   return nettle_buffer_write(dst, src->size, src->contents);
142 }