1 /* crypto/bio/bf_buff.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
62 #include <openssl/bio.h>
63 #include <openssl/evp.h>
65 static int buffer_write(BIO
*h
,char *buf
,int num
);
66 static int buffer_read(BIO
*h
,char *buf
,int size
);
67 static int buffer_puts(BIO
*h
,char *str
);
68 static int buffer_gets(BIO
*h
,char *str
,int size
);
69 static long buffer_ctrl(BIO
*h
,int cmd
,long arg1
,char *arg2
);
70 static int buffer_new(BIO
*h
);
71 static int buffer_free(BIO
*data
);
72 static long buffer_callback_ctrl(BIO
*h
,int cmd
, void (*fp
)());
73 #define DEFAULT_BUFFER_SIZE 1024
75 static BIO_METHOD methods_buffer
=
89 BIO_METHOD
*BIO_f_buffer(void)
91 return(&methods_buffer
);
94 static int buffer_new(BIO
*bi
)
96 BIO_F_BUFFER_CTX
*ctx
;
98 ctx
=(BIO_F_BUFFER_CTX
*)Malloc(sizeof(BIO_F_BUFFER_CTX
));
99 if (ctx
== NULL
) return(0);
100 ctx
->ibuf
=(char *)Malloc(DEFAULT_BUFFER_SIZE
);
101 if (ctx
->ibuf
== NULL
) { Free(ctx
); return(0); }
102 ctx
->obuf
=(char *)Malloc(DEFAULT_BUFFER_SIZE
);
103 if (ctx
->obuf
== NULL
) { Free(ctx
->ibuf
); Free(ctx
); return(0); }
104 ctx
->ibuf_size
=DEFAULT_BUFFER_SIZE
;
105 ctx
->obuf_size
=DEFAULT_BUFFER_SIZE
;
117 static int buffer_free(BIO
*a
)
121 if (a
== NULL
) return(0);
122 b
=(BIO_F_BUFFER_CTX
*)a
->ptr
;
123 if (b
->ibuf
!= NULL
) Free(b
->ibuf
);
124 if (b
->obuf
!= NULL
) Free(b
->obuf
);
132 static int buffer_read(BIO
*b
, char *out
, int outl
)
135 BIO_F_BUFFER_CTX
*ctx
;
137 if (out
== NULL
) return(0);
138 ctx
=(BIO_F_BUFFER_CTX
*)b
->ptr
;
140 if ((ctx
== NULL
) || (b
->next_bio
== NULL
)) return(0);
142 BIO_clear_retry_flags(b
);
146 /* If there is stuff left over, grab it */
149 if (i
> outl
) i
=outl
;
150 memcpy(out
,&(ctx
->ibuf
[ctx
->ibuf_off
]),i
);
154 if (outl
== i
) return(num
);
159 /* We may have done a partial read. try to do more.
160 * We have nothing in the buffer.
161 * If we get an error and have read some data, just return it
162 * and let them retry to get the error again.
163 * copy direct to parent address space */
164 if (outl
> ctx
->ibuf_size
)
168 i
=BIO_read(b
->next_bio
,out
,outl
);
171 BIO_copy_next_retry(b
);
172 if (i
< 0) return((num
> 0)?num
:i
);
173 if (i
== 0) return(num
);
176 if (outl
== i
) return(num
);
183 /* we are going to be doing some buffering */
184 i
=BIO_read(b
->next_bio
,ctx
->ibuf
,ctx
->ibuf_size
);
187 BIO_copy_next_retry(b
);
188 if (i
< 0) return((num
> 0)?num
:i
);
189 if (i
== 0) return(num
);
194 /* Lets re-read using ourselves :-) */
198 static int buffer_write(BIO
*b
, char *in
, int inl
)
201 BIO_F_BUFFER_CTX
*ctx
;
203 if ((in
== NULL
) || (inl
<= 0)) return(0);
204 ctx
=(BIO_F_BUFFER_CTX
*)b
->ptr
;
205 if ((ctx
== NULL
) || (b
->next_bio
== NULL
)) return(0);
207 BIO_clear_retry_flags(b
);
209 i
=ctx
->obuf_size
-(ctx
->obuf_len
+ctx
->obuf_off
);
210 /* add to buffer and return */
213 memcpy(&(ctx
->obuf
[ctx
->obuf_len
]),in
,inl
);
218 /* stuff already in buffer, so add to it first, then flush */
219 if (ctx
->obuf_len
!= 0)
221 if (i
> 0) /* lets fill it up if we can */
223 memcpy(&(ctx
->obuf
[ctx
->obuf_len
]),in
,i
);
229 /* we now have a full buffer needing flushing */
232 i
=BIO_write(b
->next_bio
,&(ctx
->obuf
[ctx
->obuf_off
]),
236 BIO_copy_next_retry(b
);
238 if (i
< 0) return((num
> 0)?num
:i
);
239 if (i
== 0) return(num
);
243 if (ctx
->obuf_len
== 0) break;
246 /* we only get here if the buffer has been flushed and we
247 * still have stuff to write */
250 /* we now have inl bytes to write */
251 while (inl
>= ctx
->obuf_size
)
253 i
=BIO_write(b
->next_bio
,in
,inl
);
256 BIO_copy_next_retry(b
);
257 if (i
< 0) return((num
> 0)?num
:i
);
258 if (i
== 0) return(num
);
263 if (inl
== 0) return(num
);
266 /* copy the rest into the buffer since we have only a small
271 static long buffer_ctrl(BIO
*b
, int cmd
, long num
, char *ptr
)
274 BIO_F_BUFFER_CTX
*ctx
;
280 ctx
=(BIO_F_BUFFER_CTX
*)b
->ptr
;
289 if (b
->next_bio
== NULL
) return(0);
290 ret
=BIO_ctrl(b
->next_bio
,cmd
,num
,ptr
);
293 ret
=(long)ctx
->obuf_len
;
295 case BIO_C_GET_BUFF_NUM_LINES
:
298 for (i
=ctx
->ibuf_off
; i
<ctx
->ibuf_len
; i
++)
300 if (p1
[i
] == '\n') ret
++;
303 case BIO_CTRL_WPENDING
:
304 ret
=(long)ctx
->obuf_len
;
307 if (b
->next_bio
== NULL
) return(0);
308 ret
=BIO_ctrl(b
->next_bio
,cmd
,num
,ptr
);
311 case BIO_CTRL_PENDING
:
312 ret
=(long)ctx
->ibuf_len
;
315 if (b
->next_bio
== NULL
) return(0);
316 ret
=BIO_ctrl(b
->next_bio
,cmd
,num
,ptr
);
319 case BIO_C_SET_BUFF_READ_DATA
:
320 if (num
> ctx
->ibuf_size
)
323 if (p1
== NULL
) goto malloc_error
;
324 if (ctx
->ibuf
!= NULL
) Free(ctx
->ibuf
);
328 ctx
->ibuf_len
=(int)num
;
329 memcpy(ctx
->ibuf
,ptr
,(int)num
);
332 case BIO_C_SET_BUFF_SIZE
:
341 else /* if (*ip == 1) */
354 if ((ibs
> DEFAULT_BUFFER_SIZE
) && (ibs
!= ctx
->ibuf_size
))
356 p1
=(char *)Malloc((int)num
);
357 if (p1
== NULL
) goto malloc_error
;
359 if ((obs
> DEFAULT_BUFFER_SIZE
) && (obs
!= ctx
->obuf_size
))
361 p2
=(char *)Malloc((int)num
);
364 if (p1
!= ctx
->ibuf
) Free(p1
);
385 case BIO_C_DO_STATE_MACHINE
:
386 if (b
->next_bio
== NULL
) return(0);
387 BIO_clear_retry_flags(b
);
388 ret
=BIO_ctrl(b
->next_bio
,cmd
,num
,ptr
);
389 BIO_copy_next_retry(b
);
393 if (b
->next_bio
== NULL
) return(0);
394 if (ctx
->obuf_len
<= 0)
396 ret
=BIO_ctrl(b
->next_bio
,cmd
,num
,ptr
);
402 BIO_clear_retry_flags(b
);
403 if (ctx
->obuf_len
> ctx
->obuf_off
)
405 r
=BIO_write(b
->next_bio
,
406 &(ctx
->obuf
[ctx
->obuf_off
]),
407 ctx
->obuf_len
-ctx
->obuf_off
);
409 fprintf(stderr
,"FLUSH [%3d] %3d -> %3d\n",ctx
->obuf_off
,ctx
->obuf_len
-ctx
->obuf_off
,r
);
411 BIO_copy_next_retry(b
);
412 if (r
<= 0) return((long)r
);
423 ret
=BIO_ctrl(b
->next_bio
,cmd
,num
,ptr
);
427 if ( !BIO_set_read_buffer_size(dbio
,ctx
->ibuf_size
) ||
428 !BIO_set_write_buffer_size(dbio
,ctx
->obuf_size
))
432 if (b
->next_bio
== NULL
) return(0);
433 ret
=BIO_ctrl(b
->next_bio
,cmd
,num
,ptr
);
438 BIOerr(BIO_F_BUFFER_CTRL
,ERR_R_MALLOC_FAILURE
);
442 static long buffer_callback_ctrl(BIO
*b
, int cmd
, void (*fp
)())
446 if (b
->next_bio
== NULL
) return(0);
450 ret
=BIO_callback_ctrl(b
->next_bio
,cmd
,fp
);
456 static int buffer_gets(BIO
*b
, char *buf
, int size
)
458 BIO_F_BUFFER_CTX
*ctx
;
462 ctx
=(BIO_F_BUFFER_CTX
*)b
->ptr
;
463 size
--; /* reserve space for a '\0' */
464 BIO_clear_retry_flags(b
);
468 if (ctx
->ibuf_len
> 0)
470 p
= &(ctx
->ibuf
[ctx
->ibuf_off
]);
472 for (i
=0; (i
<ctx
->ibuf_len
) && (i
<size
); i
++)
486 if ((flag
) || (i
== size
))
492 else /* read another chunk */
494 i
=BIO_read(b
->next_bio
,ctx
->ibuf
,ctx
->ibuf_size
);
497 BIO_copy_next_retry(b
);
498 if (i
< 0) return((num
> 0)?num
:i
);
499 if (i
== 0) return(num
);
507 static int buffer_puts(BIO
*b
, char *str
)
509 return(BIO_write(b
,str
,strlen(str
)));