155714Skris/* crypto/bio/bf_buff.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280297Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280297Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280297Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280297Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280297Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280297Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <errno.h>
6155714Skris#include "cryptlib.h"
6255714Skris#include <openssl/bio.h>
6355714Skris
64280297Sjkimstatic int buffer_write(BIO *h, const char *buf, int num);
6568651Skrisstatic int buffer_read(BIO *h, char *buf, int size);
6668651Skrisstatic int buffer_puts(BIO *h, const char *str);
6768651Skrisstatic int buffer_gets(BIO *h, char *str, int size);
6868651Skrisstatic long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
6955714Skrisstatic int buffer_new(BIO *h);
7055714Skrisstatic int buffer_free(BIO *data);
7168651Skrisstatic long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
72280297Sjkim#define DEFAULT_BUFFER_SIZE     4096
7355714Skris
74280297Sjkimstatic BIO_METHOD methods_buffer = {
75280297Sjkim    BIO_TYPE_BUFFER,
76280297Sjkim    "buffer",
77280297Sjkim    buffer_write,
78280297Sjkim    buffer_read,
79280297Sjkim    buffer_puts,
80280297Sjkim    buffer_gets,
81280297Sjkim    buffer_ctrl,
82280297Sjkim    buffer_new,
83280297Sjkim    buffer_free,
84280297Sjkim    buffer_callback_ctrl,
85280297Sjkim};
8655714Skris
8755714SkrisBIO_METHOD *BIO_f_buffer(void)
88280297Sjkim{
89280297Sjkim    return (&methods_buffer);
90280297Sjkim}
9155714Skris
9255714Skrisstatic int buffer_new(BIO *bi)
93280297Sjkim{
94280297Sjkim    BIO_F_BUFFER_CTX *ctx;
9555714Skris
96280297Sjkim    ctx = (BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX));
97280297Sjkim    if (ctx == NULL)
98280297Sjkim        return (0);
99280297Sjkim    ctx->ibuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
100280297Sjkim    if (ctx->ibuf == NULL) {
101280297Sjkim        OPENSSL_free(ctx);
102280297Sjkim        return (0);
103280297Sjkim    }
104280297Sjkim    ctx->obuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
105280297Sjkim    if (ctx->obuf == NULL) {
106280297Sjkim        OPENSSL_free(ctx->ibuf);
107280297Sjkim        OPENSSL_free(ctx);
108280297Sjkim        return (0);
109280297Sjkim    }
110280297Sjkim    ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
111280297Sjkim    ctx->obuf_size = DEFAULT_BUFFER_SIZE;
112280297Sjkim    ctx->ibuf_len = 0;
113280297Sjkim    ctx->ibuf_off = 0;
114280297Sjkim    ctx->obuf_len = 0;
115280297Sjkim    ctx->obuf_off = 0;
11655714Skris
117280297Sjkim    bi->init = 1;
118280297Sjkim    bi->ptr = (char *)ctx;
119280297Sjkim    bi->flags = 0;
120280297Sjkim    return (1);
121280297Sjkim}
12255714Skris
12355714Skrisstatic int buffer_free(BIO *a)
124280297Sjkim{
125280297Sjkim    BIO_F_BUFFER_CTX *b;
12655714Skris
127280297Sjkim    if (a == NULL)
128280297Sjkim        return (0);
129280297Sjkim    b = (BIO_F_BUFFER_CTX *)a->ptr;
130280297Sjkim    if (b->ibuf != NULL)
131280297Sjkim        OPENSSL_free(b->ibuf);
132280297Sjkim    if (b->obuf != NULL)
133280297Sjkim        OPENSSL_free(b->obuf);
134280297Sjkim    OPENSSL_free(a->ptr);
135280297Sjkim    a->ptr = NULL;
136280297Sjkim    a->init = 0;
137280297Sjkim    a->flags = 0;
138280297Sjkim    return (1);
139280297Sjkim}
140280297Sjkim
14155714Skrisstatic int buffer_read(BIO *b, char *out, int outl)
142280297Sjkim{
143280297Sjkim    int i, num = 0;
144280297Sjkim    BIO_F_BUFFER_CTX *ctx;
14555714Skris
146280297Sjkim    if (out == NULL)
147280297Sjkim        return (0);
148280297Sjkim    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
14955714Skris
150280297Sjkim    if ((ctx == NULL) || (b->next_bio == NULL))
151280297Sjkim        return (0);
152280297Sjkim    num = 0;
153280297Sjkim    BIO_clear_retry_flags(b);
15455714Skris
155280297Sjkim start:
156280297Sjkim    i = ctx->ibuf_len;
157280297Sjkim    /* If there is stuff left over, grab it */
158280297Sjkim    if (i != 0) {
159280297Sjkim        if (i > outl)
160280297Sjkim            i = outl;
161280297Sjkim        memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
162280297Sjkim        ctx->ibuf_off += i;
163280297Sjkim        ctx->ibuf_len -= i;
164280297Sjkim        num += i;
165280297Sjkim        if (outl == i)
166280297Sjkim            return (num);
167280297Sjkim        outl -= i;
168280297Sjkim        out += i;
169280297Sjkim    }
17055714Skris
171280297Sjkim    /*
172280297Sjkim     * We may have done a partial read. try to do more. We have nothing in
173280297Sjkim     * the buffer. If we get an error and have read some data, just return it
174280297Sjkim     * and let them retry to get the error again. copy direct to parent
175280297Sjkim     * address space
176280297Sjkim     */
177280297Sjkim    if (outl > ctx->ibuf_size) {
178280297Sjkim        for (;;) {
179280297Sjkim            i = BIO_read(b->next_bio, out, outl);
180280297Sjkim            if (i <= 0) {
181280297Sjkim                BIO_copy_next_retry(b);
182280297Sjkim                if (i < 0)
183280297Sjkim                    return ((num > 0) ? num : i);
184280297Sjkim                if (i == 0)
185280297Sjkim                    return (num);
186280297Sjkim            }
187280297Sjkim            num += i;
188280297Sjkim            if (outl == i)
189280297Sjkim                return (num);
190280297Sjkim            out += i;
191280297Sjkim            outl -= i;
192280297Sjkim        }
193280297Sjkim    }
194280297Sjkim    /* else */
19555714Skris
196280297Sjkim    /* we are going to be doing some buffering */
197280297Sjkim    i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
198280297Sjkim    if (i <= 0) {
199280297Sjkim        BIO_copy_next_retry(b);
200280297Sjkim        if (i < 0)
201280297Sjkim            return ((num > 0) ? num : i);
202280297Sjkim        if (i == 0)
203280297Sjkim            return (num);
204280297Sjkim    }
205280297Sjkim    ctx->ibuf_off = 0;
206280297Sjkim    ctx->ibuf_len = i;
20755714Skris
208280297Sjkim    /* Lets re-read using ourselves :-) */
209280297Sjkim    goto start;
210280297Sjkim}
21155714Skris
21268651Skrisstatic int buffer_write(BIO *b, const char *in, int inl)
213280297Sjkim{
214280297Sjkim    int i, num = 0;
215280297Sjkim    BIO_F_BUFFER_CTX *ctx;
21655714Skris
217280297Sjkim    if ((in == NULL) || (inl <= 0))
218280297Sjkim        return (0);
219280297Sjkim    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
220280297Sjkim    if ((ctx == NULL) || (b->next_bio == NULL))
221280297Sjkim        return (0);
22255714Skris
223280297Sjkim    BIO_clear_retry_flags(b);
224280297Sjkim start:
225280297Sjkim    i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
226280297Sjkim    /* add to buffer and return */
227280297Sjkim    if (i >= inl) {
228280297Sjkim        memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
229280297Sjkim        ctx->obuf_len += inl;
230280297Sjkim        return (num + inl);
231280297Sjkim    }
232280297Sjkim    /* else */
233280297Sjkim    /* stuff already in buffer, so add to it first, then flush */
234280297Sjkim    if (ctx->obuf_len != 0) {
235280297Sjkim        if (i > 0) {            /* lets fill it up if we can */
236280297Sjkim            memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
237280297Sjkim            in += i;
238280297Sjkim            inl -= i;
239280297Sjkim            num += i;
240280297Sjkim            ctx->obuf_len += i;
241280297Sjkim        }
242280297Sjkim        /* we now have a full buffer needing flushing */
243280297Sjkim        for (;;) {
244280297Sjkim            i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
245280297Sjkim                          ctx->obuf_len);
246280297Sjkim            if (i <= 0) {
247280297Sjkim                BIO_copy_next_retry(b);
24855714Skris
249280297Sjkim                if (i < 0)
250280297Sjkim                    return ((num > 0) ? num : i);
251280297Sjkim                if (i == 0)
252280297Sjkim                    return (num);
253280297Sjkim            }
254280297Sjkim            ctx->obuf_off += i;
255280297Sjkim            ctx->obuf_len -= i;
256280297Sjkim            if (ctx->obuf_len == 0)
257280297Sjkim                break;
258280297Sjkim        }
259280297Sjkim    }
260280297Sjkim    /*
261280297Sjkim     * we only get here if the buffer has been flushed and we still have
262280297Sjkim     * stuff to write
263280297Sjkim     */
264280297Sjkim    ctx->obuf_off = 0;
26555714Skris
266280297Sjkim    /* we now have inl bytes to write */
267280297Sjkim    while (inl >= ctx->obuf_size) {
268280297Sjkim        i = BIO_write(b->next_bio, in, inl);
269280297Sjkim        if (i <= 0) {
270280297Sjkim            BIO_copy_next_retry(b);
271280297Sjkim            if (i < 0)
272280297Sjkim                return ((num > 0) ? num : i);
273280297Sjkim            if (i == 0)
274280297Sjkim                return (num);
275280297Sjkim        }
276280297Sjkim        num += i;
277280297Sjkim        in += i;
278280297Sjkim        inl -= i;
279280297Sjkim        if (inl == 0)
280280297Sjkim            return (num);
281280297Sjkim    }
28255714Skris
283280297Sjkim    /*
284280297Sjkim     * copy the rest into the buffer since we have only a small amount left
285280297Sjkim     */
286280297Sjkim    goto start;
287280297Sjkim}
28855714Skris
28968651Skrisstatic long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
290280297Sjkim{
291280297Sjkim    BIO *dbio;
292280297Sjkim    BIO_F_BUFFER_CTX *ctx;
293280297Sjkim    long ret = 1;
294280297Sjkim    char *p1, *p2;
295280297Sjkim    int r, i, *ip;
296280297Sjkim    int ibs, obs;
29755714Skris
298280297Sjkim    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
29955714Skris
300280297Sjkim    switch (cmd) {
301280297Sjkim    case BIO_CTRL_RESET:
302280297Sjkim        ctx->ibuf_off = 0;
303280297Sjkim        ctx->ibuf_len = 0;
304280297Sjkim        ctx->obuf_off = 0;
305280297Sjkim        ctx->obuf_len = 0;
306280297Sjkim        if (b->next_bio == NULL)
307280297Sjkim            return (0);
308280297Sjkim        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
309280297Sjkim        break;
310280297Sjkim    case BIO_CTRL_INFO:
311280297Sjkim        ret = (long)ctx->obuf_len;
312280297Sjkim        break;
313280297Sjkim    case BIO_C_GET_BUFF_NUM_LINES:
314280297Sjkim        ret = 0;
315280297Sjkim        p1 = ctx->ibuf;
316280297Sjkim        for (i = 0; i < ctx->ibuf_len; i++) {
317280297Sjkim            if (p1[ctx->ibuf_off + i] == '\n')
318280297Sjkim                ret++;
319280297Sjkim        }
320280297Sjkim        break;
321280297Sjkim    case BIO_CTRL_WPENDING:
322280297Sjkim        ret = (long)ctx->obuf_len;
323280297Sjkim        if (ret == 0) {
324280297Sjkim            if (b->next_bio == NULL)
325280297Sjkim                return (0);
326280297Sjkim            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
327280297Sjkim        }
328280297Sjkim        break;
329280297Sjkim    case BIO_CTRL_PENDING:
330280297Sjkim        ret = (long)ctx->ibuf_len;
331280297Sjkim        if (ret == 0) {
332280297Sjkim            if (b->next_bio == NULL)
333280297Sjkim                return (0);
334280297Sjkim            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
335280297Sjkim        }
336280297Sjkim        break;
337280297Sjkim    case BIO_C_SET_BUFF_READ_DATA:
338280297Sjkim        if (num > ctx->ibuf_size) {
339280297Sjkim            p1 = OPENSSL_malloc((int)num);
340280297Sjkim            if (p1 == NULL)
341280297Sjkim                goto malloc_error;
342280297Sjkim            if (ctx->ibuf != NULL)
343280297Sjkim                OPENSSL_free(ctx->ibuf);
344280297Sjkim            ctx->ibuf = p1;
345280297Sjkim        }
346280297Sjkim        ctx->ibuf_off = 0;
347280297Sjkim        ctx->ibuf_len = (int)num;
348280297Sjkim        memcpy(ctx->ibuf, ptr, (int)num);
349280297Sjkim        ret = 1;
350280297Sjkim        break;
351280297Sjkim    case BIO_C_SET_BUFF_SIZE:
352280297Sjkim        if (ptr != NULL) {
353280297Sjkim            ip = (int *)ptr;
354280297Sjkim            if (*ip == 0) {
355280297Sjkim                ibs = (int)num;
356280297Sjkim                obs = ctx->obuf_size;
357280297Sjkim            } else {            /* if (*ip == 1) */
35855714Skris
359280297Sjkim                ibs = ctx->ibuf_size;
360280297Sjkim                obs = (int)num;
361280297Sjkim            }
362280297Sjkim        } else {
363280297Sjkim            ibs = (int)num;
364280297Sjkim            obs = (int)num;
365280297Sjkim        }
366280297Sjkim        p1 = ctx->ibuf;
367280297Sjkim        p2 = ctx->obuf;
368280297Sjkim        if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
369280297Sjkim            p1 = (char *)OPENSSL_malloc((int)num);
370280297Sjkim            if (p1 == NULL)
371280297Sjkim                goto malloc_error;
372280297Sjkim        }
373280297Sjkim        if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
374280297Sjkim            p2 = (char *)OPENSSL_malloc((int)num);
375280297Sjkim            if (p2 == NULL) {
376280297Sjkim                if (p1 != ctx->ibuf)
377280297Sjkim                    OPENSSL_free(p1);
378280297Sjkim                goto malloc_error;
379280297Sjkim            }
380280297Sjkim        }
381280297Sjkim        if (ctx->ibuf != p1) {
382280297Sjkim            OPENSSL_free(ctx->ibuf);
383280297Sjkim            ctx->ibuf = p1;
384280297Sjkim            ctx->ibuf_off = 0;
385280297Sjkim            ctx->ibuf_len = 0;
386280297Sjkim            ctx->ibuf_size = ibs;
387280297Sjkim        }
388280297Sjkim        if (ctx->obuf != p2) {
389280297Sjkim            OPENSSL_free(ctx->obuf);
390280297Sjkim            ctx->obuf = p2;
391280297Sjkim            ctx->obuf_off = 0;
392280297Sjkim            ctx->obuf_len = 0;
393280297Sjkim            ctx->obuf_size = obs;
394280297Sjkim        }
395280297Sjkim        break;
396280297Sjkim    case BIO_C_DO_STATE_MACHINE:
397280297Sjkim        if (b->next_bio == NULL)
398280297Sjkim            return (0);
399280297Sjkim        BIO_clear_retry_flags(b);
400280297Sjkim        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
401280297Sjkim        BIO_copy_next_retry(b);
402280297Sjkim        break;
40355714Skris
404280297Sjkim    case BIO_CTRL_FLUSH:
405280297Sjkim        if (b->next_bio == NULL)
406280297Sjkim            return (0);
407280297Sjkim        if (ctx->obuf_len <= 0) {
408280297Sjkim            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
409280297Sjkim            break;
410280297Sjkim        }
411280297Sjkim
412280297Sjkim        for (;;) {
413280297Sjkim            BIO_clear_retry_flags(b);
414280297Sjkim            if (ctx->obuf_len > 0) {
415280297Sjkim                r = BIO_write(b->next_bio,
416280297Sjkim                              &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
41755714Skris#if 0
418280297Sjkim                fprintf(stderr, "FLUSH [%3d] %3d -> %3d\n", ctx->obuf_off,
419280297Sjkim                        ctx->obuf_len, r);
42055714Skris#endif
421280297Sjkim                BIO_copy_next_retry(b);
422280297Sjkim                if (r <= 0)
423280297Sjkim                    return ((long)r);
424280297Sjkim                ctx->obuf_off += r;
425280297Sjkim                ctx->obuf_len -= r;
426280297Sjkim            } else {
427280297Sjkim                ctx->obuf_len = 0;
428280297Sjkim                ctx->obuf_off = 0;
429280297Sjkim                ret = 1;
430280297Sjkim                break;
431280297Sjkim            }
432280297Sjkim        }
433280297Sjkim        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
434280297Sjkim        break;
435280297Sjkim    case BIO_CTRL_DUP:
436280297Sjkim        dbio = (BIO *)ptr;
437280297Sjkim        if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
438280297Sjkim            !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
439280297Sjkim            ret = 0;
440280297Sjkim        break;
441280297Sjkim    default:
442280297Sjkim        if (b->next_bio == NULL)
443280297Sjkim            return (0);
444280297Sjkim        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
445280297Sjkim        break;
446280297Sjkim    }
447280297Sjkim    return (ret);
448280297Sjkim malloc_error:
449280297Sjkim    BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
450280297Sjkim    return (0);
451280297Sjkim}
45255714Skris
45368651Skrisstatic long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
454280297Sjkim{
455280297Sjkim    long ret = 1;
45659191Skris
457280297Sjkim    if (b->next_bio == NULL)
458280297Sjkim        return (0);
459280297Sjkim    switch (cmd) {
460280297Sjkim    default:
461280297Sjkim        ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
462280297Sjkim        break;
463280297Sjkim    }
464280297Sjkim    return (ret);
465280297Sjkim}
46659191Skris
46755714Skrisstatic int buffer_gets(BIO *b, char *buf, int size)
468280297Sjkim{
469280297Sjkim    BIO_F_BUFFER_CTX *ctx;
470280297Sjkim    int num = 0, i, flag;
471280297Sjkim    char *p;
47255714Skris
473280297Sjkim    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
474280297Sjkim    size--;                     /* reserve space for a '\0' */
475280297Sjkim    BIO_clear_retry_flags(b);
47655714Skris
477280297Sjkim    for (;;) {
478280297Sjkim        if (ctx->ibuf_len > 0) {
479280297Sjkim            p = &(ctx->ibuf[ctx->ibuf_off]);
480280297Sjkim            flag = 0;
481280297Sjkim            for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
482280297Sjkim                *(buf++) = p[i];
483280297Sjkim                if (p[i] == '\n') {
484280297Sjkim                    flag = 1;
485280297Sjkim                    i++;
486280297Sjkim                    break;
487280297Sjkim                }
488280297Sjkim            }
489280297Sjkim            num += i;
490280297Sjkim            size -= i;
491280297Sjkim            ctx->ibuf_len -= i;
492280297Sjkim            ctx->ibuf_off += i;
493280297Sjkim            if (flag || size == 0) {
494280297Sjkim                *buf = '\0';
495280297Sjkim                return (num);
496280297Sjkim            }
497280297Sjkim        } else {                /* read another chunk */
49855714Skris
499280297Sjkim            i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
500280297Sjkim            if (i <= 0) {
501280297Sjkim                BIO_copy_next_retry(b);
502280297Sjkim                *buf = '\0';
503280297Sjkim                if (i < 0)
504280297Sjkim                    return ((num > 0) ? num : i);
505280297Sjkim                if (i == 0)
506280297Sjkim                    return (num);
507280297Sjkim            }
508280297Sjkim            ctx->ibuf_len = i;
509280297Sjkim            ctx->ibuf_off = 0;
510280297Sjkim        }
511280297Sjkim    }
512280297Sjkim}
513280297Sjkim
51468651Skrisstatic int buffer_puts(BIO *b, const char *str)
515280297Sjkim{
516280297Sjkim    return (buffer_write(b, str, strlen(str)));
517280297Sjkim}
518