Line data Source code
1 : /* data-mem.c - A memory based data object.
2 : Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH
3 :
4 : This file is part of GPGME.
5 :
6 : GPGME is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU Lesser General Public License as
8 : published by the Free Software Foundation; either version 2.1 of
9 : the License, or (at your option) any later version.
10 :
11 : GPGME is distributed in the hope that it will be useful, but
12 : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public
17 : License along with this program; if not, write to the Free Software
18 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 : 02111-1307, USA. */
20 :
21 : #if HAVE_CONFIG_H
22 : #include <config.h>
23 : #endif
24 :
25 : #include <errno.h>
26 : #include <stdlib.h>
27 : #ifdef HAVE_UNISTD_H
28 : # include <unistd.h>
29 : #endif
30 : #include <assert.h>
31 : #include <string.h>
32 :
33 : #include "data.h"
34 : #include "util.h"
35 : #include "debug.h"
36 :
37 :
38 : static gpgme_ssize_t
39 1254 : mem_read (gpgme_data_t dh, void *buffer, size_t size)
40 : {
41 1254 : size_t amt = dh->data.mem.length - dh->data.mem.offset;
42 : const char *src;
43 :
44 1254 : if (!amt)
45 532 : return 0;
46 :
47 722 : if (size < amt)
48 207 : amt = size;
49 :
50 722 : src = dh->data.mem.buffer ? dh->data.mem.buffer : dh->data.mem.orig_buffer;
51 722 : memcpy (buffer, src + dh->data.mem.offset, amt);
52 722 : dh->data.mem.offset += amt;
53 722 : return amt;
54 : }
55 :
56 :
57 : static gpgme_ssize_t
58 224 : mem_write (gpgme_data_t dh, const void *buffer, size_t size)
59 : {
60 : size_t unused;
61 :
62 224 : if (!dh->data.mem.buffer && dh->data.mem.orig_buffer)
63 : {
64 2 : size_t new_size = dh->data.mem.size;
65 : char *new_buffer;
66 :
67 2 : if (new_size < dh->data.mem.offset + size)
68 1 : new_size = dh->data.mem.offset + size;
69 :
70 2 : new_buffer = malloc (new_size);
71 2 : if (!new_buffer)
72 0 : return -1;
73 2 : memcpy (new_buffer, dh->data.mem.orig_buffer, dh->data.mem.length);
74 :
75 2 : dh->data.mem.buffer = new_buffer;
76 2 : dh->data.mem.size = new_size;
77 : }
78 :
79 224 : unused = dh->data.mem.size - dh->data.mem.offset;
80 224 : if (unused < size)
81 : {
82 : /* Allocate a large enough buffer with exponential backoff. */
83 : #define INITIAL_ALLOC 512
84 398 : size_t new_size = dh->data.mem.size
85 199 : ? (2 * dh->data.mem.size) : INITIAL_ALLOC;
86 : char *new_buffer;
87 :
88 199 : if (new_size < dh->data.mem.offset + size)
89 52 : new_size = dh->data.mem.offset + size;
90 :
91 199 : new_buffer = realloc (dh->data.mem.buffer, new_size);
92 199 : if (!new_buffer && new_size > dh->data.mem.offset + size)
93 : {
94 : /* Maybe we were too greedy, try again. */
95 0 : new_size = dh->data.mem.offset + size;
96 0 : new_buffer = realloc (dh->data.mem.buffer, new_size);
97 : }
98 199 : if (!new_buffer)
99 0 : return -1;
100 199 : dh->data.mem.buffer = new_buffer;
101 199 : dh->data.mem.size = new_size;
102 : }
103 :
104 224 : memcpy (dh->data.mem.buffer + dh->data.mem.offset, buffer, size);
105 224 : dh->data.mem.offset += size;
106 224 : if (dh->data.mem.length < dh->data.mem.offset)
107 223 : dh->data.mem.length = dh->data.mem.offset;
108 224 : return size;
109 : }
110 :
111 :
112 : static gpgme_off_t
113 129 : mem_seek (gpgme_data_t dh, gpgme_off_t offset, int whence)
114 : {
115 129 : switch (whence)
116 : {
117 : case SEEK_SET:
118 129 : if (offset < 0 || offset > dh->data.mem.length)
119 : {
120 0 : gpg_err_set_errno (EINVAL);
121 0 : return -1;
122 : }
123 129 : dh->data.mem.offset = offset;
124 129 : break;
125 : case SEEK_CUR:
126 0 : if ((offset > 0 && dh->data.mem.length - dh->data.mem.offset < offset)
127 0 : || (offset < 0 && dh->data.mem.offset < -offset))
128 : {
129 0 : gpg_err_set_errno (EINVAL);
130 0 : return -1;
131 : }
132 0 : dh->data.mem.offset += offset;
133 0 : break;
134 : case SEEK_END:
135 0 : if (offset > 0 || -offset > dh->data.mem.length)
136 : {
137 0 : gpg_err_set_errno (EINVAL);
138 0 : return -1;
139 : }
140 0 : dh->data.mem.offset = dh->data.mem.length + offset;
141 0 : break;
142 : default:
143 0 : gpg_err_set_errno (EINVAL);
144 0 : return -1;
145 : }
146 129 : return dh->data.mem.offset;
147 : }
148 :
149 :
150 : static void
151 384 : mem_release (gpgme_data_t dh)
152 : {
153 384 : if (dh->data.mem.buffer)
154 266 : free (dh->data.mem.buffer);
155 384 : }
156 :
157 :
158 : static struct _gpgme_data_cbs mem_cbs =
159 : {
160 : mem_read,
161 : mem_write,
162 : mem_seek,
163 : mem_release,
164 : NULL
165 : };
166 :
167 :
168 : /* Create a new data buffer and return it in R_DH. */
169 : gpgme_error_t
170 242 : gpgme_data_new (gpgme_data_t *r_dh)
171 : {
172 : gpgme_error_t err;
173 242 : TRACE_BEG (DEBUG_DATA, "gpgme_data_new", r_dh);
174 :
175 242 : err = _gpgme_data_new (r_dh, &mem_cbs);
176 :
177 242 : if (err)
178 1 : return TRACE_ERR (err);
179 :
180 241 : return TRACE_SUC1 ("dh=%p", *r_dh);
181 : }
182 :
183 :
184 : /* Create a new data buffer filled with SIZE bytes starting from
185 : BUFFER. If COPY is zero, copying is delayed until necessary, and
186 : the data is taken from the original location when needed. */
187 : gpgme_error_t
188 354 : gpgme_data_new_from_mem (gpgme_data_t *r_dh, const char *buffer,
189 : size_t size, int copy)
190 : {
191 : gpgme_error_t err;
192 354 : TRACE_BEG4 (DEBUG_DATA, "gpgme_data_new_from_mem", r_dh,
193 : "buffer=%p, size=%u, copy=%i (%s)", buffer, size,
194 : copy, copy ? "yes" : "no");
195 :
196 354 : err = _gpgme_data_new (r_dh, &mem_cbs);
197 353 : if (err)
198 0 : return TRACE_ERR (err);
199 :
200 353 : if (copy)
201 : {
202 51 : char *bufcpy = malloc (size);
203 51 : if (!bufcpy)
204 : {
205 0 : int saved_err = gpg_error_from_syserror ();
206 0 : _gpgme_data_release (*r_dh);
207 0 : return TRACE_ERR (saved_err);
208 : }
209 51 : memcpy (bufcpy, buffer, size);
210 51 : (*r_dh)->data.mem.buffer = bufcpy;
211 : }
212 : else
213 302 : (*r_dh)->data.mem.orig_buffer = buffer;
214 :
215 353 : (*r_dh)->data.mem.size = size;
216 353 : (*r_dh)->data.mem.length = size;
217 353 : return TRACE_SUC1 ("dh=%p", *r_dh);
218 : }
219 :
220 :
221 : /* Destroy the data buffer DH and return a pointer to its content.
222 : The memory has be to released with gpgme_free() by the user. It's
223 : size is returned in R_LEN. */
224 : char *
225 3 : gpgme_data_release_and_get_mem (gpgme_data_t dh, size_t *r_len)
226 : {
227 3 : char *str = NULL;
228 :
229 3 : TRACE_BEG1 (DEBUG_DATA, "gpgme_data_release_and_get_mem", dh,
230 : "r_len=%p", r_len);
231 :
232 3 : if (!dh || dh->cbs != &mem_cbs)
233 : {
234 0 : gpgme_data_release (dh);
235 0 : TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
236 0 : return NULL;
237 : }
238 :
239 3 : str = dh->data.mem.buffer;
240 3 : if (!str && dh->data.mem.orig_buffer)
241 : {
242 0 : str = malloc (dh->data.mem.length);
243 0 : if (!str)
244 : {
245 0 : int saved_err = gpg_error_from_syserror ();
246 0 : gpgme_data_release (dh);
247 0 : TRACE_ERR (saved_err);
248 0 : return NULL;
249 : }
250 0 : memcpy (str, dh->data.mem.orig_buffer, dh->data.mem.length);
251 : }
252 : else
253 : /* Prevent mem_release from releasing the buffer memory. We must
254 : not fail from this point. */
255 3 : dh->data.mem.buffer = NULL;
256 :
257 3 : if (r_len)
258 3 : *r_len = dh->data.mem.length;
259 :
260 3 : gpgme_data_release (dh);
261 :
262 3 : if (r_len)
263 : {
264 3 : TRACE_SUC2 ("buffer=%p, len=%u", str, *r_len);
265 : }
266 : else
267 : {
268 0 : TRACE_SUC1 ("buffer=%p", str);
269 : }
270 3 : return str;
271 : }
272 :
273 :
274 : /* Release the memory returned by gpgme_data_release_and_get_mem() and
275 : some other functions. */
276 : void
277 189 : gpgme_free (void *buffer)
278 : {
279 189 : TRACE (DEBUG_DATA, "gpgme_free", buffer);
280 :
281 189 : if (buffer)
282 186 : free (buffer);
283 189 : }
|