1 /* crypto/bio/bf_buff.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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
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
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.]
61 #include "internal/cryptlib.h"
62 #include <openssl/bio.h>
63 #include <openssl/evp.h>
65 static int linebuffer_write(BIO *h, const char *buf, int num);
66 static int linebuffer_read(BIO *h, char *buf, int size);
67 static int linebuffer_puts(BIO *h, const char *str);
68 static int linebuffer_gets(BIO *h, char *str, int size);
69 static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
70 static int linebuffer_new(BIO *h);
71 static int linebuffer_free(BIO *data);
72 static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
74 /* A 10k maximum should be enough for most purposes */
75 #define DEFAULT_LINEBUFFER_SIZE 1024*10
79 static BIO_METHOD methods_linebuffer = {
89 linebuffer_callback_ctrl,
92 BIO_METHOD *BIO_f_linebuffer(void)
94 return (&methods_linebuffer);
97 typedef struct bio_linebuffer_ctx_struct {
98 char *obuf; /* the output char array */
99 int obuf_size; /* how big is the output buffer */
100 int obuf_len; /* how many bytes are in it */
101 } BIO_LINEBUFFER_CTX;
103 static int linebuffer_new(BIO *bi)
105 BIO_LINEBUFFER_CTX *ctx;
107 ctx = OPENSSL_malloc(sizeof(*ctx));
110 ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
111 if (ctx->obuf == NULL) {
115 ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
119 bi->ptr = (char *)ctx;
124 static int linebuffer_free(BIO *a)
126 BIO_LINEBUFFER_CTX *b;
130 b = (BIO_LINEBUFFER_CTX *)a->ptr;
131 OPENSSL_free(b->obuf);
132 OPENSSL_free(a->ptr);
139 static int linebuffer_read(BIO *b, char *out, int outl)
145 if (b->next_bio == NULL)
147 ret = BIO_read(b->next_bio, out, outl);
148 BIO_clear_retry_flags(b);
149 BIO_copy_next_retry(b);
153 static int linebuffer_write(BIO *b, const char *in, int inl)
155 int i, num = 0, foundnl;
156 BIO_LINEBUFFER_CTX *ctx;
158 if ((in == NULL) || (inl <= 0))
160 ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
161 if ((ctx == NULL) || (b->next_bio == NULL))
164 BIO_clear_retry_flags(b);
169 for (p = in; p < in + inl && *p != '\n'; p++) ;
177 * If a NL was found and we already have text in the save buffer,
178 * concatenate them and write
180 while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
181 && ctx->obuf_len > 0) {
182 int orig_olen = ctx->obuf_len;
184 i = ctx->obuf_size - ctx->obuf_len;
187 memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
188 ctx->obuf_len += p - in;
193 memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
200 i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
202 ctx->obuf_len = orig_olen;
203 BIO_copy_next_retry(b);
206 return ((num > 0) ? num : i);
210 if (i < ctx->obuf_len)
211 memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
216 * Now that the save buffer is emptied, let's write the input buffer
217 * if a NL was found and there is anything to write.
219 if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
220 i = BIO_write(b->next_bio, in, p - in);
222 BIO_copy_next_retry(b);
224 return ((num > 0) ? num : i);
233 while (foundnl && inl > 0);
235 * We've written as much as we can. The rest of the input buffer, if
236 * any, is text that doesn't and with a NL and therefore needs to be
237 * saved for the next trip.
240 memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
241 ctx->obuf_len += inl;
247 static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
250 BIO_LINEBUFFER_CTX *ctx;
256 ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
261 if (b->next_bio == NULL)
263 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
266 ret = (long)ctx->obuf_len;
268 case BIO_CTRL_WPENDING:
269 ret = (long)ctx->obuf_len;
271 if (b->next_bio == NULL)
273 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
276 case BIO_C_SET_BUFF_SIZE:
279 if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
280 p = OPENSSL_malloc((int)num);
284 if (ctx->obuf != p) {
285 if (ctx->obuf_len > obs) {
288 memcpy(p, ctx->obuf, ctx->obuf_len);
289 OPENSSL_free(ctx->obuf);
291 ctx->obuf_size = obs;
294 case BIO_C_DO_STATE_MACHINE:
295 if (b->next_bio == NULL)
297 BIO_clear_retry_flags(b);
298 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
299 BIO_copy_next_retry(b);
303 if (b->next_bio == NULL)
305 if (ctx->obuf_len <= 0) {
306 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
311 BIO_clear_retry_flags(b);
312 if (ctx->obuf_len > 0) {
313 r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
314 BIO_copy_next_retry(b);
317 if (r < ctx->obuf_len)
318 memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
326 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
330 if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
334 if (b->next_bio == NULL)
336 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
341 BIOerr(BIO_F_LINEBUFFER_CTRL, ERR_R_MALLOC_FAILURE);
345 static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
349 if (b->next_bio == NULL)
353 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
359 static int linebuffer_gets(BIO *b, char *buf, int size)
361 if (b->next_bio == NULL)
363 return (BIO_gets(b->next_bio, buf, size));
366 static int linebuffer_puts(BIO *b, const char *str)
368 return (linebuffer_write(b, str, strlen(str)));