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