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