1  | 
     | 
     | 
    /* $OpenBSD: bss_mem.c,v 1.15 2017/01/29 17:49:22 beck Exp $ */  | 
    
    
    2  | 
     | 
     | 
    /* Copyright (C) 1995-1998 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 <errno.h>  | 
    
    
    60  | 
     | 
     | 
    #include <stdio.h>  | 
    
    
    61  | 
     | 
     | 
    #include <string.h>  | 
    
    
    62  | 
     | 
     | 
     | 
    
    
    63  | 
     | 
     | 
    #include <openssl/bio.h>  | 
    
    
    64  | 
     | 
     | 
    #include <openssl/err.h>  | 
    
    
    65  | 
     | 
     | 
    #include <openssl/buffer.h>  | 
    
    
    66  | 
     | 
     | 
     | 
    
    
    67  | 
     | 
     | 
    static int mem_write(BIO *h, const char *buf, int num);  | 
    
    
    68  | 
     | 
     | 
    static int mem_read(BIO *h, char *buf, int size);  | 
    
    
    69  | 
     | 
     | 
    static int mem_puts(BIO *h, const char *str);  | 
    
    
    70  | 
     | 
     | 
    static int mem_gets(BIO *h, char *str, int size);  | 
    
    
    71  | 
     | 
     | 
    static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);  | 
    
    
    72  | 
     | 
     | 
    static int mem_new(BIO *h);  | 
    
    
    73  | 
     | 
     | 
    static int mem_free(BIO *data);  | 
    
    
    74  | 
     | 
     | 
     | 
    
    
    75  | 
     | 
     | 
    static BIO_METHOD mem_method = { | 
    
    
    76  | 
     | 
     | 
    	.type = BIO_TYPE_MEM,  | 
    
    
    77  | 
     | 
     | 
    	.name = "memory buffer",  | 
    
    
    78  | 
     | 
     | 
    	.bwrite = mem_write,  | 
    
    
    79  | 
     | 
     | 
    	.bread = mem_read,  | 
    
    
    80  | 
     | 
     | 
    	.bputs = mem_puts,  | 
    
    
    81  | 
     | 
     | 
    	.bgets = mem_gets,  | 
    
    
    82  | 
     | 
     | 
    	.ctrl = mem_ctrl,  | 
    
    
    83  | 
     | 
     | 
    	.create = mem_new,  | 
    
    
    84  | 
     | 
     | 
    	.destroy = mem_free  | 
    
    
    85  | 
     | 
     | 
    };  | 
    
    
    86  | 
     | 
     | 
     | 
    
    
    87  | 
     | 
     | 
    /* bio->num is used to hold the value to return on 'empty', if it is  | 
    
    
    88  | 
     | 
     | 
     * 0, should_retry is not set */  | 
    
    
    89  | 
     | 
     | 
     | 
    
    
    90  | 
     | 
     | 
    BIO_METHOD *  | 
    
    
    91  | 
     | 
     | 
    BIO_s_mem(void)  | 
    
    
    92  | 
     | 
     | 
    { | 
    
    
    93  | 
     | 
    22528  | 
    	return (&mem_method);  | 
    
    
    94  | 
     | 
     | 
    }  | 
    
    
    95  | 
     | 
     | 
     | 
    
    
    96  | 
     | 
     | 
    BIO *  | 
    
    
    97  | 
     | 
     | 
    BIO_new_mem_buf(void *buf, int len)  | 
    
    
    98  | 
     | 
     | 
    { | 
    
    
    99  | 
     | 
     | 
    	BIO *ret;  | 
    
    
    100  | 
     | 
     | 
    	BUF_MEM *b;  | 
    
    
    101  | 
     | 
     | 
    	size_t sz;  | 
    
    
    102  | 
     | 
     | 
     | 
    
    
    103  | 
    ✗✓ | 
    1520  | 
    	if (!buf) { | 
    
    
    104  | 
     | 
     | 
    		BIOerror(BIO_R_NULL_PARAMETER);  | 
    
    
    105  | 
     | 
     | 
    		return NULL;  | 
    
    
    106  | 
     | 
     | 
    	}  | 
    
    
    107  | 
    ✓✓ | 
    2280  | 
    	sz = (len < 0) ? strlen(buf) : (size_t)len;  | 
    
    
    108  | 
    ✗✓ | 
    760  | 
    	if (!(ret = BIO_new(BIO_s_mem())))  | 
    
    
    109  | 
     | 
     | 
    		return NULL;  | 
    
    
    110  | 
     | 
    760  | 
    	b = (BUF_MEM *)ret->ptr;  | 
    
    
    111  | 
     | 
    760  | 
    	b->data = buf;  | 
    
    
    112  | 
     | 
    760  | 
    	b->length = sz;  | 
    
    
    113  | 
     | 
    760  | 
    	b->max = sz;  | 
    
    
    114  | 
     | 
    760  | 
    	ret->flags |= BIO_FLAGS_MEM_RDONLY;  | 
    
    
    115  | 
     | 
     | 
    	/* Since this is static data retrying wont help */  | 
    
    
    116  | 
     | 
    760  | 
    	ret->num = 0;  | 
    
    
    117  | 
     | 
    760  | 
    	return ret;  | 
    
    
    118  | 
     | 
    760  | 
    }  | 
    
    
    119  | 
     | 
     | 
     | 
    
    
    120  | 
     | 
     | 
    static int  | 
    
    
    121  | 
     | 
     | 
    mem_new(BIO *bi)  | 
    
    
    122  | 
     | 
     | 
    { | 
    
    
    123  | 
     | 
     | 
    	BUF_MEM *b;  | 
    
    
    124  | 
     | 
     | 
     | 
    
    
    125  | 
    ✗✓ | 
    22528  | 
    	if ((b = BUF_MEM_new()) == NULL)  | 
    
    
    126  | 
     | 
     | 
    		return (0);  | 
    
    
    127  | 
     | 
    11264  | 
    	bi->shutdown = 1;  | 
    
    
    128  | 
     | 
    11264  | 
    	bi->init = 1;  | 
    
    
    129  | 
     | 
    11264  | 
    	bi->num = -1;  | 
    
    
    130  | 
     | 
    11264  | 
    	bi->ptr = (char *)b;  | 
    
    
    131  | 
     | 
    11264  | 
    	return (1);  | 
    
    
    132  | 
     | 
    11264  | 
    }  | 
    
    
    133  | 
     | 
     | 
     | 
    
    
    134  | 
     | 
     | 
    static int  | 
    
    
    135  | 
     | 
     | 
    mem_free(BIO *a)  | 
    
    
    136  | 
     | 
     | 
    { | 
    
    
    137  | 
    ✗✓ | 
    22492  | 
    	if (a == NULL)  | 
    
    
    138  | 
     | 
     | 
    		return (0);  | 
    
    
    139  | 
    ✓✗ | 
    11246  | 
    	if (a->shutdown) { | 
    
    
    140  | 
    ✓✗✓✗
  | 
    22492  | 
    		if ((a->init) && (a->ptr != NULL)) { | 
    
    
    141  | 
     | 
     | 
    			BUF_MEM *b;  | 
    
    
    142  | 
     | 
    11246  | 
    			b = (BUF_MEM *)a->ptr;  | 
    
    
    143  | 
    ✓✓ | 
    11246  | 
    			if (a->flags & BIO_FLAGS_MEM_RDONLY)  | 
    
    
    144  | 
     | 
    770  | 
    				b->data = NULL;  | 
    
    
    145  | 
     | 
    11246  | 
    			BUF_MEM_free(b);  | 
    
    
    146  | 
     | 
    11246  | 
    			a->ptr = NULL;  | 
    
    
    147  | 
     | 
    11246  | 
    		}  | 
    
    
    148  | 
     | 
     | 
    	}  | 
    
    
    149  | 
     | 
    11246  | 
    	return (1);  | 
    
    
    150  | 
     | 
    11246  | 
    }  | 
    
    
    151  | 
     | 
     | 
     | 
    
    
    152  | 
     | 
     | 
    static int  | 
    
    
    153  | 
     | 
     | 
    mem_read(BIO *b, char *out, int outl)  | 
    
    
    154  | 
     | 
     | 
    { | 
    
    
    155  | 
     | 
     | 
    	int ret = -1;  | 
    
    
    156  | 
     | 
     | 
    	BUF_MEM *bm;  | 
    
    
    157  | 
     | 
     | 
     | 
    
    
    158  | 
     | 
    39202  | 
    	bm = (BUF_MEM *)b->ptr;  | 
    
    
    159  | 
     | 
    19601  | 
    	BIO_clear_retry_flags(b);  | 
    
    
    160  | 
    ✓✗✓✓
  | 
    78404  | 
    	ret = (outl >=0 && (size_t)outl > bm->length) ? (int)bm->length : outl;  | 
    
    
    161  | 
    ✓✓ | 
    19601  | 
    	if ((out != NULL) && (ret > 0)) { | 
    
    
    162  | 
     | 
    18397  | 
    		memcpy(out, bm->data, ret);  | 
    
    
    163  | 
     | 
    18397  | 
    		bm->length -= ret;  | 
    
    
    164  | 
    ✓✓ | 
    18397  | 
    		if (b->flags & BIO_FLAGS_MEM_RDONLY)  | 
    
    
    165  | 
     | 
    3884  | 
    			bm->data += ret;  | 
    
    
    166  | 
     | 
     | 
    		else { | 
    
    
    167  | 
     | 
    14513  | 
    			memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);  | 
    
    
    168  | 
     | 
     | 
    		}  | 
    
    
    169  | 
    ✓✗ | 
    1204  | 
    	} else if (bm->length == 0) { | 
    
    
    170  | 
     | 
    1204  | 
    		ret = b->num;  | 
    
    
    171  | 
    ✓✓ | 
    1204  | 
    		if (ret != 0)  | 
    
    
    172  | 
     | 
    576  | 
    			BIO_set_retry_read(b);  | 
    
    
    173  | 
     | 
     | 
    	}  | 
    
    
    174  | 
     | 
    19601  | 
    	return (ret);  | 
    
    
    175  | 
     | 
     | 
    }  | 
    
    
    176  | 
     | 
     | 
     | 
    
    
    177  | 
     | 
     | 
    static int  | 
    
    
    178  | 
     | 
     | 
    mem_write(BIO *b, const char *in, int inl)  | 
    
    
    179  | 
     | 
     | 
    { | 
    
    
    180  | 
     | 
     | 
    	int ret = -1;  | 
    
    
    181  | 
     | 
     | 
    	int blen;  | 
    
    
    182  | 
     | 
     | 
    	BUF_MEM *bm;  | 
    
    
    183  | 
     | 
     | 
     | 
    
    
    184  | 
     | 
    75280  | 
    	bm = (BUF_MEM *)b->ptr;  | 
    
    
    185  | 
    ✗✓ | 
    37640  | 
    	if (in == NULL) { | 
    
    
    186  | 
     | 
     | 
    		BIOerror(BIO_R_NULL_PARAMETER);  | 
    
    
    187  | 
     | 
     | 
    		goto end;  | 
    
    
    188  | 
     | 
     | 
    	}  | 
    
    
    189  | 
     | 
     | 
     | 
    
    
    190  | 
    ✗✓ | 
    37640  | 
    	if (b->flags & BIO_FLAGS_MEM_RDONLY) { | 
    
    
    191  | 
     | 
     | 
    		BIOerror(BIO_R_WRITE_TO_READ_ONLY_BIO);  | 
    
    
    192  | 
     | 
     | 
    		goto end;  | 
    
    
    193  | 
     | 
     | 
    	}  | 
    
    
    194  | 
     | 
     | 
     | 
    
    
    195  | 
     | 
    37640  | 
    	BIO_clear_retry_flags(b);  | 
    
    
    196  | 
     | 
    37640  | 
    	blen = bm->length;  | 
    
    
    197  | 
    ✓✗ | 
    37640  | 
    	if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))  | 
    
    
    198  | 
     | 
     | 
    		goto end;  | 
    
    
    199  | 
     | 
    37640  | 
    	memcpy(&(bm->data[blen]), in, inl);  | 
    
    
    200  | 
     | 
    37640  | 
    	ret = inl;  | 
    
    
    201  | 
     | 
     | 
    end:  | 
    
    
    202  | 
     | 
    37640  | 
    	return (ret);  | 
    
    
    203  | 
     | 
     | 
    }  | 
    
    
    204  | 
     | 
     | 
     | 
    
    
    205  | 
     | 
     | 
    static long  | 
    
    
    206  | 
     | 
     | 
    mem_ctrl(BIO *b, int cmd, long num, void *ptr)  | 
    
    
    207  | 
     | 
     | 
    { | 
    
    
    208  | 
     | 
     | 
    	long ret = 1;  | 
    
    
    209  | 
     | 
     | 
    	char **pptr;  | 
    
    
    210  | 
     | 
     | 
     | 
    
    
    211  | 
     | 
    49510  | 
    	BUF_MEM *bm = (BUF_MEM *)b->ptr;  | 
    
    
    212  | 
     | 
     | 
     | 
    
    
    213  | 
    ✓✗✓✓ ✗✓✗✓ ✓✓✗✓ ✓ | 
    24755  | 
    	switch (cmd) { | 
    
    
    214  | 
     | 
     | 
    	case BIO_CTRL_RESET:  | 
    
    
    215  | 
    ✓✗ | 
    26  | 
    		if (bm->data != NULL) { | 
    
    
    216  | 
     | 
     | 
    			/* For read only case reset to the start again */  | 
    
    
    217  | 
    ✓✓ | 
    26  | 
    			if (b->flags & BIO_FLAGS_MEM_RDONLY) { | 
    
    
    218  | 
     | 
    24  | 
    				bm->data -= bm->max - bm->length;  | 
    
    
    219  | 
     | 
    24  | 
    				bm->length = bm->max;  | 
    
    
    220  | 
     | 
    24  | 
    			} else { | 
    
    
    221  | 
     | 
    2  | 
    				memset(bm->data, 0, bm->max);  | 
    
    
    222  | 
     | 
     | 
    				bm->length = 0;  | 
    
    
    223  | 
     | 
     | 
    			}  | 
    
    
    224  | 
     | 
    26  | 
    		}  | 
    
    
    225  | 
     | 
     | 
    		break;  | 
    
    
    226  | 
     | 
     | 
    	case BIO_CTRL_EOF:  | 
    
    
    227  | 
     | 
     | 
    		ret = (long)(bm->length == 0);  | 
    
    
    228  | 
     | 
     | 
    		break;  | 
    
    
    229  | 
     | 
     | 
    	case BIO_C_SET_BUF_MEM_EOF_RETURN:  | 
    
    
    230  | 
     | 
    32  | 
    		b->num = (int)num;  | 
    
    
    231  | 
     | 
    32  | 
    		break;  | 
    
    
    232  | 
     | 
     | 
    	case BIO_CTRL_INFO:  | 
    
    
    233  | 
     | 
    10386  | 
    		ret = (long)bm->length;  | 
    
    
    234  | 
    ✓✓ | 
    10386  | 
    		if (ptr != NULL) { | 
    
    
    235  | 
     | 
    10384  | 
    			pptr = (char **)ptr;  | 
    
    
    236  | 
     | 
    10384  | 
    			*pptr = (char *)&(bm->data[0]);  | 
    
    
    237  | 
     | 
    10384  | 
    		}  | 
    
    
    238  | 
     | 
     | 
    		break;  | 
    
    
    239  | 
     | 
     | 
    	case BIO_C_SET_BUF_MEM:  | 
    
    
    240  | 
     | 
     | 
    		mem_free(b);  | 
    
    
    241  | 
     | 
     | 
    		b->shutdown = (int)num;  | 
    
    
    242  | 
     | 
     | 
    		b->ptr = ptr;  | 
    
    
    243  | 
     | 
     | 
    		break;  | 
    
    
    244  | 
     | 
     | 
    	case BIO_C_GET_BUF_MEM_PTR:  | 
    
    
    245  | 
    ✓✗ | 
    16  | 
    		if (ptr != NULL) { | 
    
    
    246  | 
     | 
    16  | 
    			pptr = (char **)ptr;  | 
    
    
    247  | 
     | 
    16  | 
    			*pptr = (char *)bm;  | 
    
    
    248  | 
     | 
    16  | 
    		}  | 
    
    
    249  | 
     | 
     | 
    		break;  | 
    
    
    250  | 
     | 
     | 
    	case BIO_CTRL_GET_CLOSE:  | 
    
    
    251  | 
     | 
     | 
    		ret = (long)b->shutdown;  | 
    
    
    252  | 
     | 
     | 
    		break;  | 
    
    
    253  | 
     | 
     | 
    	case BIO_CTRL_SET_CLOSE:  | 
    
    
    254  | 
     | 
    8485  | 
    		b->shutdown = (int)num;  | 
    
    
    255  | 
     | 
    8485  | 
    		break;  | 
    
    
    256  | 
     | 
     | 
     | 
    
    
    257  | 
     | 
     | 
    	case BIO_CTRL_WPENDING:  | 
    
    
    258  | 
     | 
     | 
    		ret = 0L;  | 
    
    
    259  | 
     | 
    196  | 
    		break;  | 
    
    
    260  | 
     | 
     | 
    	case BIO_CTRL_PENDING:  | 
    
    
    261  | 
     | 
    2304  | 
    		ret = (long)bm->length;  | 
    
    
    262  | 
     | 
    2304  | 
    		break;  | 
    
    
    263  | 
     | 
     | 
    	case BIO_CTRL_DUP:  | 
    
    
    264  | 
     | 
     | 
    	case BIO_CTRL_FLUSH:  | 
    
    
    265  | 
     | 
     | 
    		ret = 1;  | 
    
    
    266  | 
     | 
    828  | 
    		break;  | 
    
    
    267  | 
     | 
     | 
    	case BIO_CTRL_PUSH:  | 
    
    
    268  | 
     | 
     | 
    	case BIO_CTRL_POP:  | 
    
    
    269  | 
     | 
     | 
    	default:  | 
    
    
    270  | 
     | 
     | 
    		ret = 0;  | 
    
    
    271  | 
     | 
    2482  | 
    		break;  | 
    
    
    272  | 
     | 
     | 
    	}  | 
    
    
    273  | 
     | 
    24755  | 
    	return (ret);  | 
    
    
    274  | 
     | 
     | 
    }  | 
    
    
    275  | 
     | 
     | 
     | 
    
    
    276  | 
     | 
     | 
    static int  | 
    
    
    277  | 
     | 
     | 
    mem_gets(BIO *bp, char *buf, int size)  | 
    
    
    278  | 
     | 
     | 
    { | 
    
    
    279  | 
     | 
     | 
    	int i, j;  | 
    
    
    280  | 
     | 
     | 
    	int ret = -1;  | 
    
    
    281  | 
     | 
     | 
    	char *p;  | 
    
    
    282  | 
     | 
    27926  | 
    	BUF_MEM *bm = (BUF_MEM *)bp->ptr;  | 
    
    
    283  | 
     | 
     | 
     | 
    
    
    284  | 
     | 
    13963  | 
    	BIO_clear_retry_flags(bp);  | 
    
    
    285  | 
     | 
    13963  | 
    	j = bm->length;  | 
    
    
    286  | 
    ✓✓ | 
    13963  | 
    	if ((size - 1) < j)  | 
    
    
    287  | 
     | 
    8599  | 
    		j = size - 1;  | 
    
    
    288  | 
    ✓✓ | 
    13963  | 
    	if (j <= 0) { | 
    
    
    289  | 
     | 
    50  | 
    		*buf = '\0';  | 
    
    
    290  | 
     | 
    50  | 
    		return 0;  | 
    
    
    291  | 
     | 
     | 
    	}  | 
    
    
    292  | 
     | 
    13913  | 
    	p = bm->data;  | 
    
    
    293  | 
    ✓✓ | 
    790268  | 
    	for (i = 0; i < j; i++) { | 
    
    
    294  | 
    ✓✓ | 
    776337  | 
    		if (p[i] == '\n') { | 
    
    
    295  | 
     | 
     | 
    			i++;  | 
    
    
    296  | 
     | 
     | 
    			break;  | 
    
    
    297  | 
     | 
     | 
    		}  | 
    
    
    298  | 
     | 
     | 
    	}  | 
    
    
    299  | 
     | 
     | 
     | 
    
    
    300  | 
     | 
     | 
    	/*  | 
    
    
    301  | 
     | 
     | 
    	 * i is now the max num of bytes to copy, either j or up to  | 
    
    
    302  | 
     | 
     | 
    	 * and including the first newline  | 
    
    
    303  | 
     | 
     | 
    	 */  | 
    
    
    304  | 
     | 
     | 
     | 
    
    
    305  | 
     | 
    13913  | 
    	i = mem_read(bp, buf, i);  | 
    
    
    306  | 
    ✓✗ | 
    13913  | 
    	if (i > 0)  | 
    
    
    307  | 
     | 
    13913  | 
    		buf[i] = '\0';  | 
    
    
    308  | 
     | 
     | 
    	ret = i;  | 
    
    
    309  | 
     | 
    13913  | 
    	return (ret);  | 
    
    
    310  | 
     | 
    13963  | 
    }  | 
    
    
    311  | 
     | 
     | 
     | 
    
    
    312  | 
     | 
     | 
    static int  | 
    
    
    313  | 
     | 
     | 
    mem_puts(BIO *bp, const char *str)  | 
    
    
    314  | 
     | 
     | 
    { | 
    
    
    315  | 
     | 
     | 
    	int n, ret;  | 
    
    
    316  | 
     | 
     | 
     | 
    
    
    317  | 
     | 
     | 
    	n = strlen(str);  | 
    
    
    318  | 
     | 
     | 
    	ret = mem_write(bp, str, n);  | 
    
    
    319  | 
     | 
     | 
    	/* memory semantics is that it will always work */  | 
    
    
    320  | 
     | 
     | 
    	return (ret);  | 
    
    
    321  | 
     | 
     | 
    }  |