update libressl to 2.8.2
[unleashed.git] / lib / libcrypto / bio / bss_mem.c
blobe76e1ad2e7208b3ecf815efefa0834c8c38ad3fd
1 /* $OpenBSD: bss_mem.c,v 1.17 2018/05/12 18:51:59 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
59 #include <errno.h>
60 #include <stdio.h>
61 #include <string.h>
63 #include <openssl/bio.h>
64 #include <openssl/err.h>
65 #include <openssl/buffer.h>
67 static int mem_write(BIO *h, const char *buf, int num);
68 static int mem_read(BIO *h, char *buf, int size);
69 static int mem_puts(BIO *h, const char *str);
70 static int mem_gets(BIO *h, char *str, int size);
71 static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
72 static int mem_new(BIO *h);
73 static int mem_free(BIO *data);
75 static const BIO_METHOD mem_method = {
76 .type = BIO_TYPE_MEM,
77 .name = "memory buffer",
78 .bwrite = mem_write,
79 .bread = mem_read,
80 .bputs = mem_puts,
81 .bgets = mem_gets,
82 .ctrl = mem_ctrl,
83 .create = mem_new,
84 .destroy = mem_free
87 /* bio->num is used to hold the value to return on 'empty', if it is
88 * 0, should_retry is not set */
90 const BIO_METHOD *
91 BIO_s_mem(void)
93 return (&mem_method);
96 BIO *
97 BIO_new_mem_buf(const void *buf, int len)
99 BIO *ret;
100 BUF_MEM *b;
101 size_t sz;
103 if (!buf) {
104 BIOerror(BIO_R_NULL_PARAMETER);
105 return NULL;
107 sz = (len < 0) ? strlen(buf) : (size_t)len;
108 if (!(ret = BIO_new(BIO_s_mem())))
109 return NULL;
110 b = (BUF_MEM *)ret->ptr;
111 b->data = (void *)buf; /* Trust in the BIO_FLAGS_MEM_RDONLY flag. */
112 b->length = sz;
113 b->max = sz;
114 ret->flags |= BIO_FLAGS_MEM_RDONLY;
115 /* Since this is static data retrying wont help */
116 ret->num = 0;
117 return ret;
120 static int
121 mem_new(BIO *bi)
123 BUF_MEM *b;
125 if ((b = BUF_MEM_new()) == NULL)
126 return (0);
127 bi->shutdown = 1;
128 bi->init = 1;
129 bi->num = -1;
130 bi->ptr = (char *)b;
131 return (1);
134 static int
135 mem_free(BIO *a)
137 if (a == NULL)
138 return (0);
139 if (a->shutdown) {
140 if ((a->init) && (a->ptr != NULL)) {
141 BUF_MEM *b;
142 b = (BUF_MEM *)a->ptr;
143 if (a->flags & BIO_FLAGS_MEM_RDONLY)
144 b->data = NULL;
145 BUF_MEM_free(b);
146 a->ptr = NULL;
149 return (1);
152 static int
153 mem_read(BIO *b, char *out, int outl)
155 int ret = -1;
156 BUF_MEM *bm;
158 bm = (BUF_MEM *)b->ptr;
159 BIO_clear_retry_flags(b);
160 ret = (outl >=0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
161 if ((out != NULL) && (ret > 0)) {
162 memcpy(out, bm->data, ret);
163 bm->length -= ret;
164 if (b->flags & BIO_FLAGS_MEM_RDONLY)
165 bm->data += ret;
166 else {
167 memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);
169 } else if (bm->length == 0) {
170 ret = b->num;
171 if (ret != 0)
172 BIO_set_retry_read(b);
174 return (ret);
177 static int
178 mem_write(BIO *b, const char *in, int inl)
180 int ret = -1;
181 int blen;
182 BUF_MEM *bm;
184 bm = (BUF_MEM *)b->ptr;
185 if (in == NULL) {
186 BIOerror(BIO_R_NULL_PARAMETER);
187 goto end;
190 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
191 BIOerror(BIO_R_WRITE_TO_READ_ONLY_BIO);
192 goto end;
195 BIO_clear_retry_flags(b);
196 blen = bm->length;
197 if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))
198 goto end;
199 memcpy(&(bm->data[blen]), in, inl);
200 ret = inl;
201 end:
202 return (ret);
205 static long
206 mem_ctrl(BIO *b, int cmd, long num, void *ptr)
208 long ret = 1;
209 char **pptr;
211 BUF_MEM *bm = (BUF_MEM *)b->ptr;
213 switch (cmd) {
214 case BIO_CTRL_RESET:
215 if (bm->data != NULL) {
216 /* For read only case reset to the start again */
217 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
218 bm->data -= bm->max - bm->length;
219 bm->length = bm->max;
220 } else {
221 memset(bm->data, 0, bm->max);
222 bm->length = 0;
225 break;
226 case BIO_CTRL_EOF:
227 ret = (long)(bm->length == 0);
228 break;
229 case BIO_C_SET_BUF_MEM_EOF_RETURN:
230 b->num = (int)num;
231 break;
232 case BIO_CTRL_INFO:
233 ret = (long)bm->length;
234 if (ptr != NULL) {
235 pptr = (char **)ptr;
236 *pptr = (char *)&(bm->data[0]);
238 break;
239 case BIO_C_SET_BUF_MEM:
240 mem_free(b);
241 b->shutdown = (int)num;
242 b->ptr = ptr;
243 break;
244 case BIO_C_GET_BUF_MEM_PTR:
245 if (ptr != NULL) {
246 pptr = (char **)ptr;
247 *pptr = (char *)bm;
249 break;
250 case BIO_CTRL_GET_CLOSE:
251 ret = (long)b->shutdown;
252 break;
253 case BIO_CTRL_SET_CLOSE:
254 b->shutdown = (int)num;
255 break;
257 case BIO_CTRL_WPENDING:
258 ret = 0L;
259 break;
260 case BIO_CTRL_PENDING:
261 ret = (long)bm->length;
262 break;
263 case BIO_CTRL_DUP:
264 case BIO_CTRL_FLUSH:
265 ret = 1;
266 break;
267 case BIO_CTRL_PUSH:
268 case BIO_CTRL_POP:
269 default:
270 ret = 0;
271 break;
273 return (ret);
276 static int
277 mem_gets(BIO *bp, char *buf, int size)
279 int i, j;
280 int ret = -1;
281 char *p;
282 BUF_MEM *bm = (BUF_MEM *)bp->ptr;
284 BIO_clear_retry_flags(bp);
285 j = bm->length;
286 if ((size - 1) < j)
287 j = size - 1;
288 if (j <= 0) {
289 *buf = '\0';
290 return 0;
292 p = bm->data;
293 for (i = 0; i < j; i++) {
294 if (p[i] == '\n') {
295 i++;
296 break;
301 * i is now the max num of bytes to copy, either j or up to
302 * and including the first newline
305 i = mem_read(bp, buf, i);
306 if (i > 0)
307 buf[i] = '\0';
308 ret = i;
309 return (ret);
312 static int
313 mem_puts(BIO *bp, const char *str)
315 int n, ret;
317 n = strlen(str);
318 ret = mem_write(bp, str, n);
319 /* memory semantics is that it will always work */
320 return (ret);