4fd8d15405bab45e88bc00089334e8f5dfdd0f64
[openssl.git] / crypto / bio / bf_buff.c
1 /* crypto/bio/bf_buff.c */
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 <stdio.h>
60 #include <errno.h>
61 #include "internal/cryptlib.h"
62 #include <openssl/bio.h>
63
64 static int buffer_write(BIO *h, const char *buf, int num);
65 static int buffer_read(BIO *h, char *buf, int size);
66 static int buffer_puts(BIO *h, const char *str);
67 static int buffer_gets(BIO *h, char *str, int size);
68 static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69 static int buffer_new(BIO *h);
70 static int buffer_free(BIO *data);
71 static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
72 #define DEFAULT_BUFFER_SIZE     4096
73
74 static BIO_METHOD methods_buffer = {
75     BIO_TYPE_BUFFER,
76     "buffer",
77     buffer_write,
78     buffer_read,
79     buffer_puts,
80     buffer_gets,
81     buffer_ctrl,
82     buffer_new,
83     buffer_free,
84     buffer_callback_ctrl,
85 };
86
87 BIO_METHOD *BIO_f_buffer(void)
88 {
89     return (&methods_buffer);
90 }
91
92 static int buffer_new(BIO *bi)
93 {
94     BIO_F_BUFFER_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
95
96     if (ctx == NULL)
97         return (0);
98     ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
99     if (ctx->ibuf == NULL) {
100         OPENSSL_free(ctx);
101         return (0);
102     }
103     ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
104     if (ctx->obuf == NULL) {
105         OPENSSL_free(ctx->ibuf);
106         OPENSSL_free(ctx);
107         return (0);
108     }
109     ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
110     ctx->obuf_size = DEFAULT_BUFFER_SIZE;
111     ctx->ibuf_len = 0;
112     ctx->ibuf_off = 0;
113     ctx->obuf_len = 0;
114     ctx->obuf_off = 0;
115
116     bi->init = 1;
117     bi->ptr = (char *)ctx;
118     bi->flags = 0;
119     return (1);
120 }
121
122 static int buffer_free(BIO *a)
123 {
124     BIO_F_BUFFER_CTX *b;
125
126     if (a == NULL)
127         return (0);
128     b = (BIO_F_BUFFER_CTX *)a->ptr;
129     OPENSSL_free(b->ibuf);
130     OPENSSL_free(b->obuf);
131     OPENSSL_free(a->ptr);
132     a->ptr = NULL;
133     a->init = 0;
134     a->flags = 0;
135     return (1);
136 }
137
138 static int buffer_read(BIO *b, char *out, int outl)
139 {
140     int i, num = 0;
141     BIO_F_BUFFER_CTX *ctx;
142
143     if (out == NULL)
144         return (0);
145     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
146
147     if ((ctx == NULL) || (b->next_bio == NULL))
148         return (0);
149     num = 0;
150     BIO_clear_retry_flags(b);
151
152  start:
153     i = ctx->ibuf_len;
154     /* If there is stuff left over, grab it */
155     if (i != 0) {
156         if (i > outl)
157             i = outl;
158         memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
159         ctx->ibuf_off += i;
160         ctx->ibuf_len -= i;
161         num += i;
162         if (outl == i)
163             return (num);
164         outl -= i;
165         out += i;
166     }
167
168     /*
169      * We may have done a partial read. try to do more. We have nothing in
170      * the buffer. If we get an error and have read some data, just return it
171      * and let them retry to get the error again. copy direct to parent
172      * address space
173      */
174     if (outl > ctx->ibuf_size) {
175         for (;;) {
176             i = BIO_read(b->next_bio, out, outl);
177             if (i <= 0) {
178                 BIO_copy_next_retry(b);
179                 if (i < 0)
180                     return ((num > 0) ? num : i);
181                 if (i == 0)
182                     return (num);
183             }
184             num += i;
185             if (outl == i)
186                 return (num);
187             out += i;
188             outl -= i;
189         }
190     }
191     /* else */
192
193     /* we are going to be doing some buffering */
194     i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
195     if (i <= 0) {
196         BIO_copy_next_retry(b);
197         if (i < 0)
198             return ((num > 0) ? num : i);
199         if (i == 0)
200             return (num);
201     }
202     ctx->ibuf_off = 0;
203     ctx->ibuf_len = i;
204
205     /* Lets re-read using ourselves :-) */
206     goto start;
207 }
208
209 static int buffer_write(BIO *b, const char *in, int inl)
210 {
211     int i, num = 0;
212     BIO_F_BUFFER_CTX *ctx;
213
214     if ((in == NULL) || (inl <= 0))
215         return (0);
216     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
217     if ((ctx == NULL) || (b->next_bio == NULL))
218         return (0);
219
220     BIO_clear_retry_flags(b);
221  start:
222     i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
223     /* add to buffer and return */
224     if (i >= inl) {
225         memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
226         ctx->obuf_len += inl;
227         return (num + inl);
228     }
229     /* else */
230     /* stuff already in buffer, so add to it first, then flush */
231     if (ctx->obuf_len != 0) {
232         if (i > 0) {            /* lets fill it up if we can */
233             memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
234             in += i;
235             inl -= i;
236             num += i;
237             ctx->obuf_len += i;
238         }
239         /* we now have a full buffer needing flushing */
240         for (;;) {
241             i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
242                           ctx->obuf_len);
243             if (i <= 0) {
244                 BIO_copy_next_retry(b);
245
246                 if (i < 0)
247                     return ((num > 0) ? num : i);
248                 if (i == 0)
249                     return (num);
250             }
251             ctx->obuf_off += i;
252             ctx->obuf_len -= i;
253             if (ctx->obuf_len == 0)
254                 break;
255         }
256     }
257     /*
258      * we only get here if the buffer has been flushed and we still have
259      * stuff to write
260      */
261     ctx->obuf_off = 0;
262
263     /* we now have inl bytes to write */
264     while (inl >= ctx->obuf_size) {
265         i = BIO_write(b->next_bio, in, inl);
266         if (i <= 0) {
267             BIO_copy_next_retry(b);
268             if (i < 0)
269                 return ((num > 0) ? num : i);
270             if (i == 0)
271                 return (num);
272         }
273         num += i;
274         in += i;
275         inl -= i;
276         if (inl == 0)
277             return (num);
278     }
279
280     /*
281      * copy the rest into the buffer since we have only a small amount left
282      */
283     goto start;
284 }
285
286 static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
287 {
288     BIO *dbio;
289     BIO_F_BUFFER_CTX *ctx;
290     long ret = 1;
291     char *p1, *p2;
292     int r, i, *ip;
293     int ibs, obs;
294
295     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
296
297     switch (cmd) {
298     case BIO_CTRL_RESET:
299         ctx->ibuf_off = 0;
300         ctx->ibuf_len = 0;
301         ctx->obuf_off = 0;
302         ctx->obuf_len = 0;
303         if (b->next_bio == NULL)
304             return (0);
305         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
306         break;
307     case BIO_CTRL_INFO:
308         ret = (long)ctx->obuf_len;
309         break;
310     case BIO_C_GET_BUFF_NUM_LINES:
311         ret = 0;
312         p1 = ctx->ibuf;
313         for (i = 0; i < ctx->ibuf_len; i++) {
314             if (p1[ctx->ibuf_off + i] == '\n')
315                 ret++;
316         }
317         break;
318     case BIO_CTRL_WPENDING:
319         ret = (long)ctx->obuf_len;
320         if (ret == 0) {
321             if (b->next_bio == NULL)
322                 return (0);
323             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
324         }
325         break;
326     case BIO_CTRL_PENDING:
327         ret = (long)ctx->ibuf_len;
328         if (ret == 0) {
329             if (b->next_bio == NULL)
330                 return (0);
331             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
332         }
333         break;
334     case BIO_C_SET_BUFF_READ_DATA:
335         if (num > ctx->ibuf_size) {
336             p1 = OPENSSL_malloc((int)num);
337             if (p1 == NULL)
338                 goto malloc_error;
339             OPENSSL_free(ctx->ibuf);
340             ctx->ibuf = p1;
341         }
342         ctx->ibuf_off = 0;
343         ctx->ibuf_len = (int)num;
344         memcpy(ctx->ibuf, ptr, (int)num);
345         ret = 1;
346         break;
347     case BIO_C_SET_BUFF_SIZE:
348         if (ptr != NULL) {
349             ip = (int *)ptr;
350             if (*ip == 0) {
351                 ibs = (int)num;
352                 obs = ctx->obuf_size;
353             } else {            /* if (*ip == 1) */
354
355                 ibs = ctx->ibuf_size;
356                 obs = (int)num;
357             }
358         } else {
359             ibs = (int)num;
360             obs = (int)num;
361         }
362         p1 = ctx->ibuf;
363         p2 = ctx->obuf;
364         if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
365             p1 = OPENSSL_malloc((int)num);
366             if (p1 == NULL)
367                 goto malloc_error;
368         }
369         if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
370             p2 = OPENSSL_malloc((int)num);
371             if (p2 == NULL) {
372                 if (p1 != ctx->ibuf)
373                     OPENSSL_free(p1);
374                 goto malloc_error;
375             }
376         }
377         if (ctx->ibuf != p1) {
378             OPENSSL_free(ctx->ibuf);
379             ctx->ibuf = p1;
380             ctx->ibuf_off = 0;
381             ctx->ibuf_len = 0;
382             ctx->ibuf_size = ibs;
383         }
384         if (ctx->obuf != p2) {
385             OPENSSL_free(ctx->obuf);
386             ctx->obuf = p2;
387             ctx->obuf_off = 0;
388             ctx->obuf_len = 0;
389             ctx->obuf_size = obs;
390         }
391         break;
392     case BIO_C_DO_STATE_MACHINE:
393         if (b->next_bio == NULL)
394             return (0);
395         BIO_clear_retry_flags(b);
396         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
397         BIO_copy_next_retry(b);
398         break;
399
400     case BIO_CTRL_FLUSH:
401         if (b->next_bio == NULL)
402             return (0);
403         if (ctx->obuf_len <= 0) {
404             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
405             break;
406         }
407
408         for (;;) {
409             BIO_clear_retry_flags(b);
410             if (ctx->obuf_len > 0) {
411                 r = BIO_write(b->next_bio,
412                               &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
413                 BIO_copy_next_retry(b);
414                 if (r <= 0)
415                     return ((long)r);
416                 ctx->obuf_off += r;
417                 ctx->obuf_len -= r;
418             } else {
419                 ctx->obuf_len = 0;
420                 ctx->obuf_off = 0;
421                 ret = 1;
422                 break;
423             }
424         }
425         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
426         break;
427     case BIO_CTRL_DUP:
428         dbio = (BIO *)ptr;
429         if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
430             !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
431             ret = 0;
432         break;
433     default:
434         if (b->next_bio == NULL)
435             return (0);
436         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
437         break;
438     }
439     return (ret);
440  malloc_error:
441     BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
442     return (0);
443 }
444
445 static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
446 {
447     long ret = 1;
448
449     if (b->next_bio == NULL)
450         return (0);
451     switch (cmd) {
452     default:
453         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
454         break;
455     }
456     return (ret);
457 }
458
459 static int buffer_gets(BIO *b, char *buf, int size)
460 {
461     BIO_F_BUFFER_CTX *ctx;
462     int num = 0, i, flag;
463     char *p;
464
465     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
466     size--;                     /* reserve space for a '\0' */
467     BIO_clear_retry_flags(b);
468
469     for (;;) {
470         if (ctx->ibuf_len > 0) {
471             p = &(ctx->ibuf[ctx->ibuf_off]);
472             flag = 0;
473             for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
474                 *(buf++) = p[i];
475                 if (p[i] == '\n') {
476                     flag = 1;
477                     i++;
478                     break;
479                 }
480             }
481             num += i;
482             size -= i;
483             ctx->ibuf_len -= i;
484             ctx->ibuf_off += i;
485             if (flag || size == 0) {
486                 *buf = '\0';
487                 return (num);
488             }
489         } else {                /* read another chunk */
490
491             i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
492             if (i <= 0) {
493                 BIO_copy_next_retry(b);
494                 *buf = '\0';
495                 if (i < 0)
496                     return ((num > 0) ? num : i);
497                 if (i == 0)
498                     return (num);
499             }
500             ctx->ibuf_len = i;
501             ctx->ibuf_off = 0;
502         }
503     }
504 }
505
506 static int buffer_puts(BIO *b, const char *str)
507 {
508     return (buffer_write(b, str, strlen(str)));
509 }