1*0Sstevel@tonic-gate /* crypto/bio/bf_buff.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate * All rights reserved.
4*0Sstevel@tonic-gate *
5*0Sstevel@tonic-gate * This package is an SSL implementation written
6*0Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
11*0Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13*0Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate *
16*0Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate * the code are not to be removed.
18*0Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate * as the author of the parts of the library used.
20*0Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate *
23*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate * are met:
26*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate * must display the following acknowledgement:
33*0Sstevel@tonic-gate * "This product includes cryptographic software written by
34*0Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate * being used are not cryptographic related :-).
37*0Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate *
41*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate * SUCH DAMAGE.
52*0Sstevel@tonic-gate *
53*0Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
55*0Sstevel@tonic-gate * copied and put under another distribution licence
56*0Sstevel@tonic-gate * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate */
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include <errno.h>
61*0Sstevel@tonic-gate #include "cryptlib.h"
62*0Sstevel@tonic-gate #include <openssl/bio.h>
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate static int buffer_write(BIO *h, const char *buf,int num);
65*0Sstevel@tonic-gate static int buffer_read(BIO *h, char *buf, int size);
66*0Sstevel@tonic-gate static int buffer_puts(BIO *h, const char *str);
67*0Sstevel@tonic-gate static int buffer_gets(BIO *h, char *str, int size);
68*0Sstevel@tonic-gate static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69*0Sstevel@tonic-gate static int buffer_new(BIO *h);
70*0Sstevel@tonic-gate static int buffer_free(BIO *data);
71*0Sstevel@tonic-gate static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
72*0Sstevel@tonic-gate #define DEFAULT_BUFFER_SIZE 4096
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate static BIO_METHOD methods_buffer=
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate BIO_TYPE_BUFFER,
77*0Sstevel@tonic-gate "buffer",
78*0Sstevel@tonic-gate buffer_write,
79*0Sstevel@tonic-gate buffer_read,
80*0Sstevel@tonic-gate buffer_puts,
81*0Sstevel@tonic-gate buffer_gets,
82*0Sstevel@tonic-gate buffer_ctrl,
83*0Sstevel@tonic-gate buffer_new,
84*0Sstevel@tonic-gate buffer_free,
85*0Sstevel@tonic-gate buffer_callback_ctrl,
86*0Sstevel@tonic-gate };
87*0Sstevel@tonic-gate
BIO_f_buffer(void)88*0Sstevel@tonic-gate BIO_METHOD *BIO_f_buffer(void)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate return(&methods_buffer);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate
buffer_new(BIO * bi)93*0Sstevel@tonic-gate static int buffer_new(BIO *bi)
94*0Sstevel@tonic-gate {
95*0Sstevel@tonic-gate BIO_F_BUFFER_CTX *ctx;
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate ctx=(BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX));
98*0Sstevel@tonic-gate if (ctx == NULL) return(0);
99*0Sstevel@tonic-gate ctx->ibuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
100*0Sstevel@tonic-gate if (ctx->ibuf == NULL) { OPENSSL_free(ctx); return(0); }
101*0Sstevel@tonic-gate ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
102*0Sstevel@tonic-gate if (ctx->obuf == NULL) { OPENSSL_free(ctx->ibuf); OPENSSL_free(ctx); return(0); }
103*0Sstevel@tonic-gate ctx->ibuf_size=DEFAULT_BUFFER_SIZE;
104*0Sstevel@tonic-gate ctx->obuf_size=DEFAULT_BUFFER_SIZE;
105*0Sstevel@tonic-gate ctx->ibuf_len=0;
106*0Sstevel@tonic-gate ctx->ibuf_off=0;
107*0Sstevel@tonic-gate ctx->obuf_len=0;
108*0Sstevel@tonic-gate ctx->obuf_off=0;
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate bi->init=1;
111*0Sstevel@tonic-gate bi->ptr=(char *)ctx;
112*0Sstevel@tonic-gate bi->flags=0;
113*0Sstevel@tonic-gate return(1);
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate
buffer_free(BIO * a)116*0Sstevel@tonic-gate static int buffer_free(BIO *a)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate BIO_F_BUFFER_CTX *b;
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate if (a == NULL) return(0);
121*0Sstevel@tonic-gate b=(BIO_F_BUFFER_CTX *)a->ptr;
122*0Sstevel@tonic-gate if (b->ibuf != NULL) OPENSSL_free(b->ibuf);
123*0Sstevel@tonic-gate if (b->obuf != NULL) OPENSSL_free(b->obuf);
124*0Sstevel@tonic-gate OPENSSL_free(a->ptr);
125*0Sstevel@tonic-gate a->ptr=NULL;
126*0Sstevel@tonic-gate a->init=0;
127*0Sstevel@tonic-gate a->flags=0;
128*0Sstevel@tonic-gate return(1);
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate
buffer_read(BIO * b,char * out,int outl)131*0Sstevel@tonic-gate static int buffer_read(BIO *b, char *out, int outl)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate int i,num=0;
134*0Sstevel@tonic-gate BIO_F_BUFFER_CTX *ctx;
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate if (out == NULL) return(0);
137*0Sstevel@tonic-gate ctx=(BIO_F_BUFFER_CTX *)b->ptr;
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
140*0Sstevel@tonic-gate num=0;
141*0Sstevel@tonic-gate BIO_clear_retry_flags(b);
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate start:
144*0Sstevel@tonic-gate i=ctx->ibuf_len;
145*0Sstevel@tonic-gate /* If there is stuff left over, grab it */
146*0Sstevel@tonic-gate if (i != 0)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate if (i > outl) i=outl;
149*0Sstevel@tonic-gate memcpy(out,&(ctx->ibuf[ctx->ibuf_off]),i);
150*0Sstevel@tonic-gate ctx->ibuf_off+=i;
151*0Sstevel@tonic-gate ctx->ibuf_len-=i;
152*0Sstevel@tonic-gate num+=i;
153*0Sstevel@tonic-gate if (outl == i) return(num);
154*0Sstevel@tonic-gate outl-=i;
155*0Sstevel@tonic-gate out+=i;
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate /* We may have done a partial read. try to do more.
159*0Sstevel@tonic-gate * We have nothing in the buffer.
160*0Sstevel@tonic-gate * If we get an error and have read some data, just return it
161*0Sstevel@tonic-gate * and let them retry to get the error again.
162*0Sstevel@tonic-gate * copy direct to parent address space */
163*0Sstevel@tonic-gate if (outl > ctx->ibuf_size)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate for (;;)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate i=BIO_read(b->next_bio,out,outl);
168*0Sstevel@tonic-gate if (i <= 0)
169*0Sstevel@tonic-gate {
170*0Sstevel@tonic-gate BIO_copy_next_retry(b);
171*0Sstevel@tonic-gate if (i < 0) return((num > 0)?num:i);
172*0Sstevel@tonic-gate if (i == 0) return(num);
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate num+=i;
175*0Sstevel@tonic-gate if (outl == i) return(num);
176*0Sstevel@tonic-gate out+=i;
177*0Sstevel@tonic-gate outl-=i;
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate /* else */
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate /* we are going to be doing some buffering */
183*0Sstevel@tonic-gate i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size);
184*0Sstevel@tonic-gate if (i <= 0)
185*0Sstevel@tonic-gate {
186*0Sstevel@tonic-gate BIO_copy_next_retry(b);
187*0Sstevel@tonic-gate if (i < 0) return((num > 0)?num:i);
188*0Sstevel@tonic-gate if (i == 0) return(num);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate ctx->ibuf_off=0;
191*0Sstevel@tonic-gate ctx->ibuf_len=i;
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate /* Lets re-read using ourselves :-) */
194*0Sstevel@tonic-gate goto start;
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate
buffer_write(BIO * b,const char * in,int inl)197*0Sstevel@tonic-gate static int buffer_write(BIO *b, const char *in, int inl)
198*0Sstevel@tonic-gate {
199*0Sstevel@tonic-gate int i,num=0;
200*0Sstevel@tonic-gate BIO_F_BUFFER_CTX *ctx;
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate if ((in == NULL) || (inl <= 0)) return(0);
203*0Sstevel@tonic-gate ctx=(BIO_F_BUFFER_CTX *)b->ptr;
204*0Sstevel@tonic-gate if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate BIO_clear_retry_flags(b);
207*0Sstevel@tonic-gate start:
208*0Sstevel@tonic-gate i=ctx->obuf_size-(ctx->obuf_len+ctx->obuf_off);
209*0Sstevel@tonic-gate /* add to buffer and return */
210*0Sstevel@tonic-gate if (i >= inl)
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate memcpy(&(ctx->obuf[ctx->obuf_len]),in,inl);
213*0Sstevel@tonic-gate ctx->obuf_len+=inl;
214*0Sstevel@tonic-gate return(num+inl);
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate /* else */
217*0Sstevel@tonic-gate /* stuff already in buffer, so add to it first, then flush */
218*0Sstevel@tonic-gate if (ctx->obuf_len != 0)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate if (i > 0) /* lets fill it up if we can */
221*0Sstevel@tonic-gate {
222*0Sstevel@tonic-gate memcpy(&(ctx->obuf[ctx->obuf_len]),in,i);
223*0Sstevel@tonic-gate in+=i;
224*0Sstevel@tonic-gate inl-=i;
225*0Sstevel@tonic-gate num+=i;
226*0Sstevel@tonic-gate ctx->obuf_len+=i;
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate /* we now have a full buffer needing flushing */
229*0Sstevel@tonic-gate for (;;)
230*0Sstevel@tonic-gate {
231*0Sstevel@tonic-gate i=BIO_write(b->next_bio,&(ctx->obuf[ctx->obuf_off]),
232*0Sstevel@tonic-gate ctx->obuf_len);
233*0Sstevel@tonic-gate if (i <= 0)
234*0Sstevel@tonic-gate {
235*0Sstevel@tonic-gate BIO_copy_next_retry(b);
236*0Sstevel@tonic-gate
237*0Sstevel@tonic-gate if (i < 0) return((num > 0)?num:i);
238*0Sstevel@tonic-gate if (i == 0) return(num);
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate ctx->obuf_off+=i;
241*0Sstevel@tonic-gate ctx->obuf_len-=i;
242*0Sstevel@tonic-gate if (ctx->obuf_len == 0) break;
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate /* we only get here if the buffer has been flushed and we
246*0Sstevel@tonic-gate * still have stuff to write */
247*0Sstevel@tonic-gate ctx->obuf_off=0;
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate /* we now have inl bytes to write */
250*0Sstevel@tonic-gate while (inl >= ctx->obuf_size)
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate i=BIO_write(b->next_bio,in,inl);
253*0Sstevel@tonic-gate if (i <= 0)
254*0Sstevel@tonic-gate {
255*0Sstevel@tonic-gate BIO_copy_next_retry(b);
256*0Sstevel@tonic-gate if (i < 0) return((num > 0)?num:i);
257*0Sstevel@tonic-gate if (i == 0) return(num);
258*0Sstevel@tonic-gate }
259*0Sstevel@tonic-gate num+=i;
260*0Sstevel@tonic-gate in+=i;
261*0Sstevel@tonic-gate inl-=i;
262*0Sstevel@tonic-gate if (inl == 0) return(num);
263*0Sstevel@tonic-gate }
264*0Sstevel@tonic-gate
265*0Sstevel@tonic-gate /* copy the rest into the buffer since we have only a small
266*0Sstevel@tonic-gate * amount left */
267*0Sstevel@tonic-gate goto start;
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate
buffer_ctrl(BIO * b,int cmd,long num,void * ptr)270*0Sstevel@tonic-gate static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
271*0Sstevel@tonic-gate {
272*0Sstevel@tonic-gate BIO *dbio;
273*0Sstevel@tonic-gate BIO_F_BUFFER_CTX *ctx;
274*0Sstevel@tonic-gate long ret=1;
275*0Sstevel@tonic-gate char *p1,*p2;
276*0Sstevel@tonic-gate int r,i,*ip;
277*0Sstevel@tonic-gate int ibs,obs;
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate ctx=(BIO_F_BUFFER_CTX *)b->ptr;
280*0Sstevel@tonic-gate
281*0Sstevel@tonic-gate switch (cmd)
282*0Sstevel@tonic-gate {
283*0Sstevel@tonic-gate case BIO_CTRL_RESET:
284*0Sstevel@tonic-gate ctx->ibuf_off=0;
285*0Sstevel@tonic-gate ctx->ibuf_len=0;
286*0Sstevel@tonic-gate ctx->obuf_off=0;
287*0Sstevel@tonic-gate ctx->obuf_len=0;
288*0Sstevel@tonic-gate if (b->next_bio == NULL) return(0);
289*0Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
290*0Sstevel@tonic-gate break;
291*0Sstevel@tonic-gate case BIO_CTRL_INFO:
292*0Sstevel@tonic-gate ret=(long)ctx->obuf_len;
293*0Sstevel@tonic-gate break;
294*0Sstevel@tonic-gate case BIO_C_GET_BUFF_NUM_LINES:
295*0Sstevel@tonic-gate ret=0;
296*0Sstevel@tonic-gate p1=ctx->ibuf;
297*0Sstevel@tonic-gate for (i=ctx->ibuf_off; i<ctx->ibuf_len; i++)
298*0Sstevel@tonic-gate {
299*0Sstevel@tonic-gate if (p1[i] == '\n') ret++;
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate break;
302*0Sstevel@tonic-gate case BIO_CTRL_WPENDING:
303*0Sstevel@tonic-gate ret=(long)ctx->obuf_len;
304*0Sstevel@tonic-gate if (ret == 0)
305*0Sstevel@tonic-gate {
306*0Sstevel@tonic-gate if (b->next_bio == NULL) return(0);
307*0Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
308*0Sstevel@tonic-gate }
309*0Sstevel@tonic-gate break;
310*0Sstevel@tonic-gate case BIO_CTRL_PENDING:
311*0Sstevel@tonic-gate ret=(long)ctx->ibuf_len;
312*0Sstevel@tonic-gate if (ret == 0)
313*0Sstevel@tonic-gate {
314*0Sstevel@tonic-gate if (b->next_bio == NULL) return(0);
315*0Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
316*0Sstevel@tonic-gate }
317*0Sstevel@tonic-gate break;
318*0Sstevel@tonic-gate case BIO_C_SET_BUFF_READ_DATA:
319*0Sstevel@tonic-gate if (num > ctx->ibuf_size)
320*0Sstevel@tonic-gate {
321*0Sstevel@tonic-gate p1=OPENSSL_malloc((int)num);
322*0Sstevel@tonic-gate if (p1 == NULL) goto malloc_error;
323*0Sstevel@tonic-gate if (ctx->ibuf != NULL) OPENSSL_free(ctx->ibuf);
324*0Sstevel@tonic-gate ctx->ibuf=p1;
325*0Sstevel@tonic-gate }
326*0Sstevel@tonic-gate ctx->ibuf_off=0;
327*0Sstevel@tonic-gate ctx->ibuf_len=(int)num;
328*0Sstevel@tonic-gate memcpy(ctx->ibuf,ptr,(int)num);
329*0Sstevel@tonic-gate ret=1;
330*0Sstevel@tonic-gate break;
331*0Sstevel@tonic-gate case BIO_C_SET_BUFF_SIZE:
332*0Sstevel@tonic-gate if (ptr != NULL)
333*0Sstevel@tonic-gate {
334*0Sstevel@tonic-gate ip=(int *)ptr;
335*0Sstevel@tonic-gate if (*ip == 0)
336*0Sstevel@tonic-gate {
337*0Sstevel@tonic-gate ibs=(int)num;
338*0Sstevel@tonic-gate obs=ctx->obuf_size;
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate else /* if (*ip == 1) */
341*0Sstevel@tonic-gate {
342*0Sstevel@tonic-gate ibs=ctx->ibuf_size;
343*0Sstevel@tonic-gate obs=(int)num;
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate }
346*0Sstevel@tonic-gate else
347*0Sstevel@tonic-gate {
348*0Sstevel@tonic-gate ibs=(int)num;
349*0Sstevel@tonic-gate obs=(int)num;
350*0Sstevel@tonic-gate }
351*0Sstevel@tonic-gate p1=ctx->ibuf;
352*0Sstevel@tonic-gate p2=ctx->obuf;
353*0Sstevel@tonic-gate if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size))
354*0Sstevel@tonic-gate {
355*0Sstevel@tonic-gate p1=(char *)OPENSSL_malloc((int)num);
356*0Sstevel@tonic-gate if (p1 == NULL) goto malloc_error;
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size))
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate p2=(char *)OPENSSL_malloc((int)num);
361*0Sstevel@tonic-gate if (p2 == NULL)
362*0Sstevel@tonic-gate {
363*0Sstevel@tonic-gate if (p1 != ctx->ibuf) OPENSSL_free(p1);
364*0Sstevel@tonic-gate goto malloc_error;
365*0Sstevel@tonic-gate }
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate if (ctx->ibuf != p1)
368*0Sstevel@tonic-gate {
369*0Sstevel@tonic-gate OPENSSL_free(ctx->ibuf);
370*0Sstevel@tonic-gate ctx->ibuf=p1;
371*0Sstevel@tonic-gate ctx->ibuf_off=0;
372*0Sstevel@tonic-gate ctx->ibuf_len=0;
373*0Sstevel@tonic-gate ctx->ibuf_size=ibs;
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate if (ctx->obuf != p2)
376*0Sstevel@tonic-gate {
377*0Sstevel@tonic-gate OPENSSL_free(ctx->obuf);
378*0Sstevel@tonic-gate ctx->obuf=p2;
379*0Sstevel@tonic-gate ctx->obuf_off=0;
380*0Sstevel@tonic-gate ctx->obuf_len=0;
381*0Sstevel@tonic-gate ctx->obuf_size=obs;
382*0Sstevel@tonic-gate }
383*0Sstevel@tonic-gate break;
384*0Sstevel@tonic-gate case BIO_C_DO_STATE_MACHINE:
385*0Sstevel@tonic-gate if (b->next_bio == NULL) return(0);
386*0Sstevel@tonic-gate BIO_clear_retry_flags(b);
387*0Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
388*0Sstevel@tonic-gate BIO_copy_next_retry(b);
389*0Sstevel@tonic-gate break;
390*0Sstevel@tonic-gate
391*0Sstevel@tonic-gate case BIO_CTRL_FLUSH:
392*0Sstevel@tonic-gate if (b->next_bio == NULL) return(0);
393*0Sstevel@tonic-gate if (ctx->obuf_len <= 0)
394*0Sstevel@tonic-gate {
395*0Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
396*0Sstevel@tonic-gate break;
397*0Sstevel@tonic-gate }
398*0Sstevel@tonic-gate
399*0Sstevel@tonic-gate for (;;)
400*0Sstevel@tonic-gate {
401*0Sstevel@tonic-gate BIO_clear_retry_flags(b);
402*0Sstevel@tonic-gate if (ctx->obuf_len > ctx->obuf_off)
403*0Sstevel@tonic-gate {
404*0Sstevel@tonic-gate r=BIO_write(b->next_bio,
405*0Sstevel@tonic-gate &(ctx->obuf[ctx->obuf_off]),
406*0Sstevel@tonic-gate ctx->obuf_len-ctx->obuf_off);
407*0Sstevel@tonic-gate #if 0
408*0Sstevel@tonic-gate fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_off,r);
409*0Sstevel@tonic-gate #endif
410*0Sstevel@tonic-gate BIO_copy_next_retry(b);
411*0Sstevel@tonic-gate if (r <= 0) return((long)r);
412*0Sstevel@tonic-gate ctx->obuf_off+=r;
413*0Sstevel@tonic-gate }
414*0Sstevel@tonic-gate else
415*0Sstevel@tonic-gate {
416*0Sstevel@tonic-gate ctx->obuf_len=0;
417*0Sstevel@tonic-gate ctx->obuf_off=0;
418*0Sstevel@tonic-gate ret=1;
419*0Sstevel@tonic-gate break;
420*0Sstevel@tonic-gate }
421*0Sstevel@tonic-gate }
422*0Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
423*0Sstevel@tonic-gate break;
424*0Sstevel@tonic-gate case BIO_CTRL_DUP:
425*0Sstevel@tonic-gate dbio=(BIO *)ptr;
426*0Sstevel@tonic-gate if ( !BIO_set_read_buffer_size(dbio,ctx->ibuf_size) ||
427*0Sstevel@tonic-gate !BIO_set_write_buffer_size(dbio,ctx->obuf_size))
428*0Sstevel@tonic-gate ret=0;
429*0Sstevel@tonic-gate break;
430*0Sstevel@tonic-gate default:
431*0Sstevel@tonic-gate if (b->next_bio == NULL) return(0);
432*0Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
433*0Sstevel@tonic-gate break;
434*0Sstevel@tonic-gate }
435*0Sstevel@tonic-gate return(ret);
436*0Sstevel@tonic-gate malloc_error:
437*0Sstevel@tonic-gate BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE);
438*0Sstevel@tonic-gate return(0);
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate
buffer_callback_ctrl(BIO * b,int cmd,bio_info_cb * fp)441*0Sstevel@tonic-gate static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
442*0Sstevel@tonic-gate {
443*0Sstevel@tonic-gate long ret=1;
444*0Sstevel@tonic-gate
445*0Sstevel@tonic-gate if (b->next_bio == NULL) return(0);
446*0Sstevel@tonic-gate switch (cmd)
447*0Sstevel@tonic-gate {
448*0Sstevel@tonic-gate default:
449*0Sstevel@tonic-gate ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
450*0Sstevel@tonic-gate break;
451*0Sstevel@tonic-gate }
452*0Sstevel@tonic-gate return(ret);
453*0Sstevel@tonic-gate }
454*0Sstevel@tonic-gate
buffer_gets(BIO * b,char * buf,int size)455*0Sstevel@tonic-gate static int buffer_gets(BIO *b, char *buf, int size)
456*0Sstevel@tonic-gate {
457*0Sstevel@tonic-gate BIO_F_BUFFER_CTX *ctx;
458*0Sstevel@tonic-gate int num=0,i,flag;
459*0Sstevel@tonic-gate char *p;
460*0Sstevel@tonic-gate
461*0Sstevel@tonic-gate ctx=(BIO_F_BUFFER_CTX *)b->ptr;
462*0Sstevel@tonic-gate size--; /* reserve space for a '\0' */
463*0Sstevel@tonic-gate BIO_clear_retry_flags(b);
464*0Sstevel@tonic-gate
465*0Sstevel@tonic-gate for (;;)
466*0Sstevel@tonic-gate {
467*0Sstevel@tonic-gate if (ctx->ibuf_len > 0)
468*0Sstevel@tonic-gate {
469*0Sstevel@tonic-gate p= &(ctx->ibuf[ctx->ibuf_off]);
470*0Sstevel@tonic-gate flag=0;
471*0Sstevel@tonic-gate for (i=0; (i<ctx->ibuf_len) && (i<size); i++)
472*0Sstevel@tonic-gate {
473*0Sstevel@tonic-gate *(buf++)=p[i];
474*0Sstevel@tonic-gate if (p[i] == '\n')
475*0Sstevel@tonic-gate {
476*0Sstevel@tonic-gate flag=1;
477*0Sstevel@tonic-gate i++;
478*0Sstevel@tonic-gate break;
479*0Sstevel@tonic-gate }
480*0Sstevel@tonic-gate }
481*0Sstevel@tonic-gate num+=i;
482*0Sstevel@tonic-gate size-=i;
483*0Sstevel@tonic-gate ctx->ibuf_len-=i;
484*0Sstevel@tonic-gate ctx->ibuf_off+=i;
485*0Sstevel@tonic-gate if (flag || size == 0)
486*0Sstevel@tonic-gate {
487*0Sstevel@tonic-gate *buf='\0';
488*0Sstevel@tonic-gate return(num);
489*0Sstevel@tonic-gate }
490*0Sstevel@tonic-gate }
491*0Sstevel@tonic-gate else /* read another chunk */
492*0Sstevel@tonic-gate {
493*0Sstevel@tonic-gate i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size);
494*0Sstevel@tonic-gate if (i <= 0)
495*0Sstevel@tonic-gate {
496*0Sstevel@tonic-gate BIO_copy_next_retry(b);
497*0Sstevel@tonic-gate *buf='\0';
498*0Sstevel@tonic-gate if (i < 0) return((num > 0)?num:i);
499*0Sstevel@tonic-gate if (i == 0) return(num);
500*0Sstevel@tonic-gate }
501*0Sstevel@tonic-gate ctx->ibuf_len=i;
502*0Sstevel@tonic-gate ctx->ibuf_off=0;
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate }
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate
buffer_puts(BIO * b,const char * str)507*0Sstevel@tonic-gate static int buffer_puts(BIO *b, const char *str)
508*0Sstevel@tonic-gate {
509*0Sstevel@tonic-gate return(buffer_write(b,str,strlen(str)));
510*0Sstevel@tonic-gate }
511*0Sstevel@tonic-gate
512