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