xref: /onnv-gate/usr/src/common/openssl/crypto/bio/bf_lbuf.c (revision 0:68f95e015346)
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 #include <openssl/evp.h>
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static int linebuffer_write(BIO *h, const char *buf,int num);
66*0Sstevel@tonic-gate static int linebuffer_read(BIO *h, char *buf, int size);
67*0Sstevel@tonic-gate static int linebuffer_puts(BIO *h, const char *str);
68*0Sstevel@tonic-gate static int linebuffer_gets(BIO *h, char *str, int size);
69*0Sstevel@tonic-gate static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
70*0Sstevel@tonic-gate static int linebuffer_new(BIO *h);
71*0Sstevel@tonic-gate static int linebuffer_free(BIO *data);
72*0Sstevel@tonic-gate static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /* A 10k maximum should be enough for most purposes */
75*0Sstevel@tonic-gate #define DEFAULT_LINEBUFFER_SIZE	1024*10
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate /* #define DEBUG */
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate static BIO_METHOD methods_linebuffer=
80*0Sstevel@tonic-gate 	{
81*0Sstevel@tonic-gate 	BIO_TYPE_LINEBUFFER,
82*0Sstevel@tonic-gate 	"linebuffer",
83*0Sstevel@tonic-gate 	linebuffer_write,
84*0Sstevel@tonic-gate 	linebuffer_read,
85*0Sstevel@tonic-gate 	linebuffer_puts,
86*0Sstevel@tonic-gate 	linebuffer_gets,
87*0Sstevel@tonic-gate 	linebuffer_ctrl,
88*0Sstevel@tonic-gate 	linebuffer_new,
89*0Sstevel@tonic-gate 	linebuffer_free,
90*0Sstevel@tonic-gate 	linebuffer_callback_ctrl,
91*0Sstevel@tonic-gate 	};
92*0Sstevel@tonic-gate 
BIO_f_linebuffer(void)93*0Sstevel@tonic-gate BIO_METHOD *BIO_f_linebuffer(void)
94*0Sstevel@tonic-gate 	{
95*0Sstevel@tonic-gate 	return(&methods_linebuffer);
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate typedef struct bio_linebuffer_ctx_struct
99*0Sstevel@tonic-gate 	{
100*0Sstevel@tonic-gate 	char *obuf;		/* the output char array */
101*0Sstevel@tonic-gate 	int obuf_size;		/* how big is the output buffer */
102*0Sstevel@tonic-gate 	int obuf_len;		/* how many bytes are in it */
103*0Sstevel@tonic-gate 	} BIO_LINEBUFFER_CTX;
104*0Sstevel@tonic-gate 
linebuffer_new(BIO * bi)105*0Sstevel@tonic-gate static int linebuffer_new(BIO *bi)
106*0Sstevel@tonic-gate 	{
107*0Sstevel@tonic-gate 	BIO_LINEBUFFER_CTX *ctx;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	ctx=(BIO_LINEBUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_LINEBUFFER_CTX));
110*0Sstevel@tonic-gate 	if (ctx == NULL) return(0);
111*0Sstevel@tonic-gate 	ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
112*0Sstevel@tonic-gate 	if (ctx->obuf == NULL) { OPENSSL_free(ctx); return(0); }
113*0Sstevel@tonic-gate 	ctx->obuf_size=DEFAULT_LINEBUFFER_SIZE;
114*0Sstevel@tonic-gate 	ctx->obuf_len=0;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	bi->init=1;
117*0Sstevel@tonic-gate 	bi->ptr=(char *)ctx;
118*0Sstevel@tonic-gate 	bi->flags=0;
119*0Sstevel@tonic-gate 	return(1);
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 
linebuffer_free(BIO * a)122*0Sstevel@tonic-gate static int linebuffer_free(BIO *a)
123*0Sstevel@tonic-gate 	{
124*0Sstevel@tonic-gate 	BIO_LINEBUFFER_CTX *b;
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	if (a == NULL) return(0);
127*0Sstevel@tonic-gate 	b=(BIO_LINEBUFFER_CTX *)a->ptr;
128*0Sstevel@tonic-gate 	if (b->obuf != NULL) OPENSSL_free(b->obuf);
129*0Sstevel@tonic-gate 	OPENSSL_free(a->ptr);
130*0Sstevel@tonic-gate 	a->ptr=NULL;
131*0Sstevel@tonic-gate 	a->init=0;
132*0Sstevel@tonic-gate 	a->flags=0;
133*0Sstevel@tonic-gate 	return(1);
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 
linebuffer_read(BIO * b,char * out,int outl)136*0Sstevel@tonic-gate static int linebuffer_read(BIO *b, char *out, int outl)
137*0Sstevel@tonic-gate 	{
138*0Sstevel@tonic-gate 	int ret=0;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (out == NULL) return(0);
141*0Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
142*0Sstevel@tonic-gate 	ret=BIO_read(b->next_bio,out,outl);
143*0Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
144*0Sstevel@tonic-gate 	BIO_copy_next_retry(b);
145*0Sstevel@tonic-gate 	return(ret);
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
linebuffer_write(BIO * b,const char * in,int inl)148*0Sstevel@tonic-gate static int linebuffer_write(BIO *b, const char *in, int inl)
149*0Sstevel@tonic-gate 	{
150*0Sstevel@tonic-gate 	int i,num=0,foundnl;
151*0Sstevel@tonic-gate 	BIO_LINEBUFFER_CTX *ctx;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	if ((in == NULL) || (inl <= 0)) return(0);
154*0Sstevel@tonic-gate 	ctx=(BIO_LINEBUFFER_CTX *)b->ptr;
155*0Sstevel@tonic-gate 	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	do
160*0Sstevel@tonic-gate 		{
161*0Sstevel@tonic-gate 		const char *p;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 		for(p = in; p < in + inl && *p != '\n'; p++)
164*0Sstevel@tonic-gate 			;
165*0Sstevel@tonic-gate 		if (*p == '\n')
166*0Sstevel@tonic-gate 			{
167*0Sstevel@tonic-gate 			p++;
168*0Sstevel@tonic-gate 			foundnl = 1;
169*0Sstevel@tonic-gate 			}
170*0Sstevel@tonic-gate 		else
171*0Sstevel@tonic-gate 			foundnl = 0;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 		/* If a NL was found and we already have text in the save
174*0Sstevel@tonic-gate 		   buffer, concatenate them and write */
175*0Sstevel@tonic-gate 		while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
176*0Sstevel@tonic-gate 			&& ctx->obuf_len > 0)
177*0Sstevel@tonic-gate 			{
178*0Sstevel@tonic-gate 			int orig_olen = ctx->obuf_len;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 			i = ctx->obuf_size - ctx->obuf_len;
181*0Sstevel@tonic-gate 			if (p - in > 0)
182*0Sstevel@tonic-gate 				{
183*0Sstevel@tonic-gate 				if (i >= p - in)
184*0Sstevel@tonic-gate 					{
185*0Sstevel@tonic-gate 					memcpy(&(ctx->obuf[ctx->obuf_len]),
186*0Sstevel@tonic-gate 						in,p - in);
187*0Sstevel@tonic-gate 					ctx->obuf_len += p - in;
188*0Sstevel@tonic-gate 					inl -= p - in;
189*0Sstevel@tonic-gate 					num += p - in;
190*0Sstevel@tonic-gate 					in = p;
191*0Sstevel@tonic-gate 					}
192*0Sstevel@tonic-gate 				else
193*0Sstevel@tonic-gate 					{
194*0Sstevel@tonic-gate 					memcpy(&(ctx->obuf[ctx->obuf_len]),
195*0Sstevel@tonic-gate 						in,i);
196*0Sstevel@tonic-gate 					ctx->obuf_len += i;
197*0Sstevel@tonic-gate 					inl -= i;
198*0Sstevel@tonic-gate 					in += i;
199*0Sstevel@tonic-gate 					num += i;
200*0Sstevel@tonic-gate 					}
201*0Sstevel@tonic-gate 				}
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate #if 0
204*0Sstevel@tonic-gate BIO_write(b->next_bio, "<*<", 3);
205*0Sstevel@tonic-gate #endif
206*0Sstevel@tonic-gate 			i=BIO_write(b->next_bio,
207*0Sstevel@tonic-gate 				ctx->obuf, ctx->obuf_len);
208*0Sstevel@tonic-gate 			if (i <= 0)
209*0Sstevel@tonic-gate 				{
210*0Sstevel@tonic-gate 				ctx->obuf_len = orig_olen;
211*0Sstevel@tonic-gate 				BIO_copy_next_retry(b);
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate #if 0
214*0Sstevel@tonic-gate BIO_write(b->next_bio, ">*>", 3);
215*0Sstevel@tonic-gate #endif
216*0Sstevel@tonic-gate 				if (i < 0) return((num > 0)?num:i);
217*0Sstevel@tonic-gate 				if (i == 0) return(num);
218*0Sstevel@tonic-gate 				}
219*0Sstevel@tonic-gate #if 0
220*0Sstevel@tonic-gate BIO_write(b->next_bio, ">*>", 3);
221*0Sstevel@tonic-gate #endif
222*0Sstevel@tonic-gate 			if (i < ctx->obuf_len)
223*0Sstevel@tonic-gate 				memmove(ctx->obuf, ctx->obuf + i,
224*0Sstevel@tonic-gate 					ctx->obuf_len - i);
225*0Sstevel@tonic-gate 			ctx->obuf_len-=i;
226*0Sstevel@tonic-gate 			}
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 		/* Now that the save buffer is emptied, let's write the input
229*0Sstevel@tonic-gate 		   buffer if a NL was found and there is anything to write. */
230*0Sstevel@tonic-gate 		if ((foundnl || p - in > ctx->obuf_size) && p - in > 0)
231*0Sstevel@tonic-gate 			{
232*0Sstevel@tonic-gate #if 0
233*0Sstevel@tonic-gate BIO_write(b->next_bio, "<*<", 3);
234*0Sstevel@tonic-gate #endif
235*0Sstevel@tonic-gate 			i=BIO_write(b->next_bio,in,p - in);
236*0Sstevel@tonic-gate 			if (i <= 0)
237*0Sstevel@tonic-gate 				{
238*0Sstevel@tonic-gate 				BIO_copy_next_retry(b);
239*0Sstevel@tonic-gate #if 0
240*0Sstevel@tonic-gate BIO_write(b->next_bio, ">*>", 3);
241*0Sstevel@tonic-gate #endif
242*0Sstevel@tonic-gate 				if (i < 0) return((num > 0)?num:i);
243*0Sstevel@tonic-gate 				if (i == 0) return(num);
244*0Sstevel@tonic-gate 				}
245*0Sstevel@tonic-gate #if 0
246*0Sstevel@tonic-gate BIO_write(b->next_bio, ">*>", 3);
247*0Sstevel@tonic-gate #endif
248*0Sstevel@tonic-gate 			num+=i;
249*0Sstevel@tonic-gate 			in+=i;
250*0Sstevel@tonic-gate 			inl-=i;
251*0Sstevel@tonic-gate 			}
252*0Sstevel@tonic-gate 		}
253*0Sstevel@tonic-gate 	while(foundnl && inl > 0);
254*0Sstevel@tonic-gate 	/* We've written as much as we can.  The rest of the input buffer, if
255*0Sstevel@tonic-gate 	   any, is text that doesn't and with a NL and therefore needs to be
256*0Sstevel@tonic-gate 	   saved for the next trip. */
257*0Sstevel@tonic-gate 	if (inl > 0)
258*0Sstevel@tonic-gate 		{
259*0Sstevel@tonic-gate 		memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
260*0Sstevel@tonic-gate 		ctx->obuf_len += inl;
261*0Sstevel@tonic-gate 		num += inl;
262*0Sstevel@tonic-gate 		}
263*0Sstevel@tonic-gate 	return num;
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 
linebuffer_ctrl(BIO * b,int cmd,long num,void * ptr)266*0Sstevel@tonic-gate static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
267*0Sstevel@tonic-gate 	{
268*0Sstevel@tonic-gate 	BIO *dbio;
269*0Sstevel@tonic-gate 	BIO_LINEBUFFER_CTX *ctx;
270*0Sstevel@tonic-gate 	long ret=1;
271*0Sstevel@tonic-gate 	char *p;
272*0Sstevel@tonic-gate 	int r;
273*0Sstevel@tonic-gate 	int obs;
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	ctx=(BIO_LINEBUFFER_CTX *)b->ptr;
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	switch (cmd)
278*0Sstevel@tonic-gate 		{
279*0Sstevel@tonic-gate 	case BIO_CTRL_RESET:
280*0Sstevel@tonic-gate 		ctx->obuf_len=0;
281*0Sstevel@tonic-gate 		if (b->next_bio == NULL) return(0);
282*0Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
283*0Sstevel@tonic-gate 		break;
284*0Sstevel@tonic-gate 	case BIO_CTRL_INFO:
285*0Sstevel@tonic-gate 		ret=(long)ctx->obuf_len;
286*0Sstevel@tonic-gate 		break;
287*0Sstevel@tonic-gate 	case BIO_CTRL_WPENDING:
288*0Sstevel@tonic-gate 		ret=(long)ctx->obuf_len;
289*0Sstevel@tonic-gate 		if (ret == 0)
290*0Sstevel@tonic-gate 			{
291*0Sstevel@tonic-gate 			if (b->next_bio == NULL) return(0);
292*0Sstevel@tonic-gate 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
293*0Sstevel@tonic-gate 			}
294*0Sstevel@tonic-gate 		break;
295*0Sstevel@tonic-gate 	case BIO_C_SET_BUFF_SIZE:
296*0Sstevel@tonic-gate 		obs=(int)num;
297*0Sstevel@tonic-gate 		p=ctx->obuf;
298*0Sstevel@tonic-gate 		if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size))
299*0Sstevel@tonic-gate 			{
300*0Sstevel@tonic-gate 			p=(char *)OPENSSL_malloc((int)num);
301*0Sstevel@tonic-gate 			if (p == NULL)
302*0Sstevel@tonic-gate 				goto malloc_error;
303*0Sstevel@tonic-gate 			}
304*0Sstevel@tonic-gate 		if (ctx->obuf != p)
305*0Sstevel@tonic-gate 			{
306*0Sstevel@tonic-gate 			if (ctx->obuf_len > obs)
307*0Sstevel@tonic-gate 				{
308*0Sstevel@tonic-gate 				ctx->obuf_len = obs;
309*0Sstevel@tonic-gate 				}
310*0Sstevel@tonic-gate 			memcpy(p, ctx->obuf, ctx->obuf_len);
311*0Sstevel@tonic-gate 			OPENSSL_free(ctx->obuf);
312*0Sstevel@tonic-gate 			ctx->obuf=p;
313*0Sstevel@tonic-gate 			ctx->obuf_size=obs;
314*0Sstevel@tonic-gate 			}
315*0Sstevel@tonic-gate 		break;
316*0Sstevel@tonic-gate 	case BIO_C_DO_STATE_MACHINE:
317*0Sstevel@tonic-gate 		if (b->next_bio == NULL) return(0);
318*0Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
319*0Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
320*0Sstevel@tonic-gate 		BIO_copy_next_retry(b);
321*0Sstevel@tonic-gate 		break;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	case BIO_CTRL_FLUSH:
324*0Sstevel@tonic-gate 		if (b->next_bio == NULL) return(0);
325*0Sstevel@tonic-gate 		if (ctx->obuf_len <= 0)
326*0Sstevel@tonic-gate 			{
327*0Sstevel@tonic-gate 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
328*0Sstevel@tonic-gate 			break;
329*0Sstevel@tonic-gate 			}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 		for (;;)
332*0Sstevel@tonic-gate 			{
333*0Sstevel@tonic-gate 			BIO_clear_retry_flags(b);
334*0Sstevel@tonic-gate 			if (ctx->obuf_len > 0)
335*0Sstevel@tonic-gate 				{
336*0Sstevel@tonic-gate 				r=BIO_write(b->next_bio,
337*0Sstevel@tonic-gate 					ctx->obuf, ctx->obuf_len);
338*0Sstevel@tonic-gate #if 0
339*0Sstevel@tonic-gate fprintf(stderr,"FLUSH %3d -> %3d\n",ctx->obuf_len,r);
340*0Sstevel@tonic-gate #endif
341*0Sstevel@tonic-gate 				BIO_copy_next_retry(b);
342*0Sstevel@tonic-gate 				if (r <= 0) return((long)r);
343*0Sstevel@tonic-gate 				if (r < ctx->obuf_len)
344*0Sstevel@tonic-gate 					memmove(ctx->obuf, ctx->obuf + r,
345*0Sstevel@tonic-gate 						ctx->obuf_len - r);
346*0Sstevel@tonic-gate 				ctx->obuf_len-=r;
347*0Sstevel@tonic-gate 				}
348*0Sstevel@tonic-gate 			else
349*0Sstevel@tonic-gate 				{
350*0Sstevel@tonic-gate 				ctx->obuf_len=0;
351*0Sstevel@tonic-gate 				ret=1;
352*0Sstevel@tonic-gate 				break;
353*0Sstevel@tonic-gate 				}
354*0Sstevel@tonic-gate 			}
355*0Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
356*0Sstevel@tonic-gate 		break;
357*0Sstevel@tonic-gate 	case BIO_CTRL_DUP:
358*0Sstevel@tonic-gate 		dbio=(BIO *)ptr;
359*0Sstevel@tonic-gate 		if (	!BIO_set_write_buffer_size(dbio,ctx->obuf_size))
360*0Sstevel@tonic-gate 			ret=0;
361*0Sstevel@tonic-gate 		break;
362*0Sstevel@tonic-gate 	default:
363*0Sstevel@tonic-gate 		if (b->next_bio == NULL) return(0);
364*0Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
365*0Sstevel@tonic-gate 		break;
366*0Sstevel@tonic-gate 		}
367*0Sstevel@tonic-gate 	return(ret);
368*0Sstevel@tonic-gate malloc_error:
369*0Sstevel@tonic-gate 	BIOerr(BIO_F_LINEBUFFER_CTRL,ERR_R_MALLOC_FAILURE);
370*0Sstevel@tonic-gate 	return(0);
371*0Sstevel@tonic-gate 	}
372*0Sstevel@tonic-gate 
linebuffer_callback_ctrl(BIO * b,int cmd,bio_info_cb * fp)373*0Sstevel@tonic-gate static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
374*0Sstevel@tonic-gate 	{
375*0Sstevel@tonic-gate 	long ret=1;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
378*0Sstevel@tonic-gate 	switch (cmd)
379*0Sstevel@tonic-gate 		{
380*0Sstevel@tonic-gate 	default:
381*0Sstevel@tonic-gate 		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
382*0Sstevel@tonic-gate 		break;
383*0Sstevel@tonic-gate 		}
384*0Sstevel@tonic-gate 	return(ret);
385*0Sstevel@tonic-gate 	}
386*0Sstevel@tonic-gate 
linebuffer_gets(BIO * b,char * buf,int size)387*0Sstevel@tonic-gate static int linebuffer_gets(BIO *b, char *buf, int size)
388*0Sstevel@tonic-gate 	{
389*0Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
390*0Sstevel@tonic-gate 	return(BIO_gets(b->next_bio,buf,size));
391*0Sstevel@tonic-gate 	}
392*0Sstevel@tonic-gate 
linebuffer_puts(BIO * b,const char * str)393*0Sstevel@tonic-gate static int linebuffer_puts(BIO *b, const char *str)
394*0Sstevel@tonic-gate 	{
395*0Sstevel@tonic-gate 	return(linebuffer_write(b,str,strlen(str)));
396*0Sstevel@tonic-gate 	}
397*0Sstevel@tonic-gate 
398