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