1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
60 #include "internal/cryptlib.h"
61 #include <openssl/bio.h>
62 #include <openssl/evp.h>
64 static int linebuffer_write(BIO *h, const char *buf, int num);
65 static int linebuffer_read(BIO *h, char *buf, int size);
66 static int linebuffer_puts(BIO *h, const char *str);
67 static int linebuffer_gets(BIO *h, char *str, int size);
68 static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69 static int linebuffer_new(BIO *h);
70 static int linebuffer_free(BIO *data);
71 static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
73 /* A 10k maximum should be enough for most purposes */
74 #define DEFAULT_LINEBUFFER_SIZE 1024*10
78 static BIO_METHOD methods_linebuffer = {
88 linebuffer_callback_ctrl,
91 BIO_METHOD *BIO_f_linebuffer(void)
93 return (&methods_linebuffer);
96 typedef struct bio_linebuffer_ctx_struct {
97 char *obuf; /* the output char array */
98 int obuf_size; /* how big is the output buffer */
99 int obuf_len; /* how many bytes are in it */
100 } BIO_LINEBUFFER_CTX;
102 static int linebuffer_new(BIO *bi)
104 BIO_LINEBUFFER_CTX *ctx;
106 ctx = OPENSSL_malloc(sizeof(*ctx));
109 ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
110 if (ctx->obuf == NULL) {
114 ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
118 bi->ptr = (char *)ctx;
123 static int linebuffer_free(BIO *a)
125 BIO_LINEBUFFER_CTX *b;
129 b = (BIO_LINEBUFFER_CTX *)a->ptr;
130 OPENSSL_free(b->obuf);
131 OPENSSL_free(a->ptr);
138 static int linebuffer_read(BIO *b, char *out, int outl)
144 if (b->next_bio == NULL)
146 ret = BIO_read(b->next_bio, out, outl);
147 BIO_clear_retry_flags(b);
148 BIO_copy_next_retry(b);
152 static int linebuffer_write(BIO *b, const char *in, int inl)
154 int i, num = 0, foundnl;
155 BIO_LINEBUFFER_CTX *ctx;
157 if ((in == NULL) || (inl <= 0))
159 ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
160 if ((ctx == NULL) || (b->next_bio == NULL))
163 BIO_clear_retry_flags(b);
168 for (p = in; p < in + inl && *p != '\n'; p++) ;
176 * If a NL was found and we already have text in the save buffer,
177 * concatenate them and write
179 while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
180 && ctx->obuf_len > 0) {
181 int orig_olen = ctx->obuf_len;
183 i = ctx->obuf_size - ctx->obuf_len;
186 memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
187 ctx->obuf_len += p - in;
192 memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
199 i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
201 ctx->obuf_len = orig_olen;
202 BIO_copy_next_retry(b);
205 return ((num > 0) ? num : i);
209 if (i < ctx->obuf_len)
210 memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
215 * Now that the save buffer is emptied, let's write the input buffer
216 * if a NL was found and there is anything to write.
218 if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
219 i = BIO_write(b->next_bio, in, p - in);
221 BIO_copy_next_retry(b);
223 return ((num > 0) ? num : i);
232 while (foundnl && inl > 0);
234 * We've written as much as we can. The rest of the input buffer, if
235 * any, is text that doesn't and with a NL and therefore needs to be
236 * saved for the next trip.
239 memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
240 ctx->obuf_len += inl;
246 static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
249 BIO_LINEBUFFER_CTX *ctx;
255 ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
260 if (b->next_bio == NULL)
262 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
265 ret = (long)ctx->obuf_len;
267 case BIO_CTRL_WPENDING:
268 ret = (long)ctx->obuf_len;
270 if (b->next_bio == NULL)
272 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
275 case BIO_C_SET_BUFF_SIZE:
278 if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
279 p = OPENSSL_malloc((int)num);
283 if (ctx->obuf != p) {
284 if (ctx->obuf_len > obs) {
287 memcpy(p, ctx->obuf, ctx->obuf_len);
288 OPENSSL_free(ctx->obuf);
290 ctx->obuf_size = obs;
293 case BIO_C_DO_STATE_MACHINE:
294 if (b->next_bio == NULL)
296 BIO_clear_retry_flags(b);
297 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
298 BIO_copy_next_retry(b);
302 if (b->next_bio == NULL)
304 if (ctx->obuf_len <= 0) {
305 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
310 BIO_clear_retry_flags(b);
311 if (ctx->obuf_len > 0) {
312 r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
313 BIO_copy_next_retry(b);
316 if (r < ctx->obuf_len)
317 memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
325 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
329 if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
333 if (b->next_bio == NULL)
335 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
340 BIOerr(BIO_F_LINEBUFFER_CTRL, ERR_R_MALLOC_FAILURE);
344 static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
348 if (b->next_bio == NULL)
352 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
358 static int linebuffer_gets(BIO *b, char *buf, int size)
360 if (b->next_bio == NULL)
362 return (BIO_gets(b->next_bio, buf, size));
365 static int linebuffer_puts(BIO *b, const char *str)
367 return (linebuffer_write(b, str, strlen(str)));