GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/crypto/../../libssl/src/crypto/bio/bss_mem.c Lines: 97 128 75.8 %
Date: 2016-12-06 Branches: 40 66 60.6 %

Line Branch Exec Source
1
/* $OpenBSD: bss_mem.c,v 1.14 2015/03/21 08:05:20 doug Exp $ */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
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).
15
 *
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.
22
 *
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)"
40
 *
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.
52
 *
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.]
57
 */
58
59
#include <errno.h>
60
#include <stdio.h>
61
#include <string.h>
62
63
#include <openssl/bio.h>
64
#include <openssl/err.h>
65
#include <openssl/buffer.h>
66
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);
74
75
static 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
85
};
86
87
/* bio->num is used to hold the value to return on 'empty', if it is
88
 * 0, should_retry is not set */
89
90
BIO_METHOD *
91
BIO_s_mem(void)
92
169
{
93
169
	return (&mem_method);
94
}
95
96
BIO *
97
BIO_new_mem_buf(void *buf, int len)
98
106
{
99
	BIO *ret;
100
	BUF_MEM *b;
101
	size_t sz;
102
103
106
	if (!buf) {
104
		BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);
105
		return NULL;
106
	}
107
106
	sz = (len < 0) ? strlen(buf) : (size_t)len;
108
106
	if (!(ret = BIO_new(BIO_s_mem())))
109
		return NULL;
110
106
	b = (BUF_MEM *)ret->ptr;
111
106
	b->data = buf;
112
106
	b->length = sz;
113
106
	b->max = sz;
114
106
	ret->flags |= BIO_FLAGS_MEM_RDONLY;
115
	/* Since this is static data retrying wont help */
116
106
	ret->num = 0;
117
106
	return ret;
118
}
119
120
static int
121
mem_new(BIO *bi)
122
169
{
123
	BUF_MEM *b;
124
125
169
	if ((b = BUF_MEM_new()) == NULL)
126
		return (0);
127
169
	bi->shutdown = 1;
128
169
	bi->init = 1;
129
169
	bi->num = -1;
130
169
	bi->ptr = (char *)b;
131
169
	return (1);
132
}
133
134
static int
135
mem_free(BIO *a)
136
166
{
137
166
	if (a == NULL)
138
		return (0);
139
166
	if (a->shutdown) {
140

166
		if ((a->init) && (a->ptr != NULL)) {
141
			BUF_MEM *b;
142
166
			b = (BUF_MEM *)a->ptr;
143
166
			if (a->flags & BIO_FLAGS_MEM_RDONLY)
144
106
				b->data = NULL;
145
166
			BUF_MEM_free(b);
146
166
			a->ptr = NULL;
147
		}
148
	}
149
166
	return (1);
150
}
151
152
static int
153
mem_read(BIO *b, char *out, int outl)
154
397
{
155
397
	int ret = -1;
156
	BUF_MEM *bm;
157
158
397
	bm = (BUF_MEM *)b->ptr;
159
397
	BIO_clear_retry_flags(b);
160

397
	ret = (outl >=0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
161
397
	if ((out != NULL) && (ret > 0)) {
162
302
		memcpy(out, bm->data, ret);
163
302
		bm->length -= ret;
164
302
		if (b->flags & BIO_FLAGS_MEM_RDONLY)
165
174
			bm->data += ret;
166
		else {
167
128
			memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);
168
		}
169
95
	} else if (bm->length == 0) {
170
95
		ret = b->num;
171
95
		if (ret != 0)
172
			BIO_set_retry_read(b);
173
	}
174
397
	return (ret);
175
}
176
177
static int
178
mem_write(BIO *b, const char *in, int inl)
179
107
{
180
107
	int ret = -1;
181
	int blen;
182
	BUF_MEM *bm;
183
184
107
	bm = (BUF_MEM *)b->ptr;
185
107
	if (in == NULL) {
186
		BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
187
		goto end;
188
	}
189
190
107
	if (b->flags & BIO_FLAGS_MEM_RDONLY) {
191
		BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
192
		goto end;
193
	}
194
195
107
	BIO_clear_retry_flags(b);
196
107
	blen = bm->length;
197
107
	if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))
198
		goto end;
199
107
	memcpy(&(bm->data[blen]), in, inl);
200
107
	ret = inl;
201
107
end:
202
107
	return (ret);
203
}
204
205
static long
206
mem_ctrl(BIO *b, int cmd, long num, void *ptr)
207
290
{
208
290
	long ret = 1;
209
	char **pptr;
210
211
290
	BUF_MEM *bm = (BUF_MEM *)b->ptr;
212
213



290
	switch (cmd) {
214
	case BIO_CTRL_RESET:
215
4
		if (bm->data != NULL) {
216
			/* For read only case reset to the start again */
217
4
			if (b->flags & BIO_FLAGS_MEM_RDONLY) {
218
4
				bm->data -= bm->max - bm->length;
219
4
				bm->length = bm->max;
220
			} else {
221
				memset(bm->data, 0, bm->max);
222
				bm->length = 0;
223
			}
224
		}
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
4
		b->num = (int)num;
231
4
		break;
232
	case BIO_CTRL_INFO:
233
61
		ret = (long)bm->length;
234
61
		if (ptr != NULL) {
235
61
			pptr = (char **)ptr;
236
61
			*pptr = (char *)&(bm->data[0]);
237
		}
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;
248
		}
249
		break;
250
	case BIO_CTRL_GET_CLOSE:
251
		ret = (long)b->shutdown;
252
		break;
253
	case BIO_CTRL_SET_CLOSE:
254
2
		b->shutdown = (int)num;
255
2
		break;
256
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
57
		ret = 1;
266
57
		break;
267
	case BIO_CTRL_PUSH:
268
	case BIO_CTRL_POP:
269
	default:
270
162
		ret = 0;
271
		break;
272
	}
273
290
	return (ret);
274
}
275
276
static int
277
mem_gets(BIO *bp, char *buf, int size)
278
204
{
279
	int i, j;
280
204
	int ret = -1;
281
	char *p;
282
204
	BUF_MEM *bm = (BUF_MEM *)bp->ptr;
283
284
204
	BIO_clear_retry_flags(bp);
285
204
	j = bm->length;
286
204
	if ((size - 1) < j)
287
155
		j = size - 1;
288
204
	if (j <= 0) {
289
3
		*buf = '\0';
290
3
		return 0;
291
	}
292
201
	p = bm->data;
293
12391
	for (i = 0; i < j; i++) {
294
12388
		if (p[i] == '\n') {
295
198
			i++;
296
198
			break;
297
		}
298
	}
299
300
	/*
301
	 * i is now the max num of bytes to copy, either j or up to
302
	 * and including the first newline
303
	 */
304
305
201
	i = mem_read(bp, buf, i);
306
201
	if (i > 0)
307
201
		buf[i] = '\0';
308
201
	ret = i;
309
201
	return (ret);
310
}
311
312
static int
313
mem_puts(BIO *bp, const char *str)
314
{
315
	int n, ret;
316
317
	n = strlen(str);
318
	ret = mem_write(bp, str, n);
319
	/* memory semantics is that it will always work */
320
	return (ret);
321
}