| OLD | NEW |
| 1 /* crypto/bio/bss_mem.c */ | 1 /* crypto/bio/bss_mem.c */ |
| 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
| 6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
| 7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
| 8 * | 8 * |
| 9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
| 10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 87 |
| 88 BIO_METHOD *BIO_s_mem(void) | 88 BIO_METHOD *BIO_s_mem(void) |
| 89 { | 89 { |
| 90 return(&mem_method); | 90 return(&mem_method); |
| 91 } | 91 } |
| 92 | 92 |
| 93 BIO *BIO_new_mem_buf(void *buf, int len) | 93 BIO *BIO_new_mem_buf(void *buf, int len) |
| 94 { | 94 { |
| 95 BIO *ret; | 95 BIO *ret; |
| 96 BUF_MEM *b; | 96 BUF_MEM *b; |
| 97 size_t sz; |
| 98 |
| 97 if (!buf) { | 99 if (!buf) { |
| 98 BIOerr(BIO_F_BIO_NEW_MEM_BUF,BIO_R_NULL_PARAMETER); | 100 BIOerr(BIO_F_BIO_NEW_MEM_BUF,BIO_R_NULL_PARAMETER); |
| 99 return NULL; | 101 return NULL; |
| 100 } | 102 } |
| 101 » if(len == -1) len = strlen(buf); | 103 » sz = (len<0) ? strlen(buf) : (size_t)len; |
| 102 if(!(ret = BIO_new(BIO_s_mem())) ) return NULL; | 104 if(!(ret = BIO_new(BIO_s_mem())) ) return NULL; |
| 103 b = (BUF_MEM *)ret->ptr; | 105 b = (BUF_MEM *)ret->ptr; |
| 104 b->data = buf; | 106 b->data = buf; |
| 105 » b->length = len; | 107 » b->length = sz; |
| 106 » b->max = len; | 108 » b->max = sz; |
| 107 ret->flags |= BIO_FLAGS_MEM_RDONLY; | 109 ret->flags |= BIO_FLAGS_MEM_RDONLY; |
| 108 /* Since this is static data retrying wont help */ | 110 /* Since this is static data retrying wont help */ |
| 109 ret->num = 0; | 111 ret->num = 0; |
| 110 return ret; | 112 return ret; |
| 111 } | 113 } |
| 112 | 114 |
| 113 static int mem_new(BIO *bi) | 115 static int mem_new(BIO *bi) |
| 114 { | 116 { |
| 115 BUF_MEM *b; | 117 BUF_MEM *b; |
| 116 | 118 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 137 a->ptr=NULL; | 139 a->ptr=NULL; |
| 138 } | 140 } |
| 139 } | 141 } |
| 140 return(1); | 142 return(1); |
| 141 } | 143 } |
| 142 | 144 |
| 143 static int mem_read(BIO *b, char *out, int outl) | 145 static int mem_read(BIO *b, char *out, int outl) |
| 144 { | 146 { |
| 145 int ret= -1; | 147 int ret= -1; |
| 146 BUF_MEM *bm; | 148 BUF_MEM *bm; |
| 147 int i; | |
| 148 char *from,*to; | |
| 149 | 149 |
| 150 bm=(BUF_MEM *)b->ptr; | 150 bm=(BUF_MEM *)b->ptr; |
| 151 BIO_clear_retry_flags(b); | 151 BIO_clear_retry_flags(b); |
| 152 » ret=(outl > bm->length)?bm->length:outl; | 152 » ret=(outl >=0 && (size_t)outl > bm->length)?(int)bm->length:outl; |
| 153 if ((out != NULL) && (ret > 0)) { | 153 if ((out != NULL) && (ret > 0)) { |
| 154 memcpy(out,bm->data,ret); | 154 memcpy(out,bm->data,ret); |
| 155 bm->length-=ret; | 155 bm->length-=ret; |
| 156 /* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */ | |
| 157 if(b->flags & BIO_FLAGS_MEM_RDONLY) bm->data += ret; | 156 if(b->flags & BIO_FLAGS_MEM_RDONLY) bm->data += ret; |
| 158 else { | 157 else { |
| 159 » » » from=(char *)&(bm->data[ret]); | 158 » » » memmove(&(bm->data[0]),&(bm->data[ret]),bm->length); |
| 160 » » » to=(char *)&(bm->data[0]); | |
| 161 » » » for (i=0; i<bm->length; i++) | |
| 162 » » » » to[i]=from[i]; | |
| 163 } | 159 } |
| 164 } else if (bm->length == 0) | 160 } else if (bm->length == 0) |
| 165 { | 161 { |
| 166 ret = b->num; | 162 ret = b->num; |
| 167 if (ret != 0) | 163 if (ret != 0) |
| 168 BIO_set_retry_read(b); | 164 BIO_set_retry_read(b); |
| 169 } | 165 } |
| 170 return(ret); | 166 return(ret); |
| 171 } | 167 } |
| 172 | 168 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 static int mem_puts(BIO *bp, const char *str) | 310 static int mem_puts(BIO *bp, const char *str) |
| 315 { | 311 { |
| 316 int n,ret; | 312 int n,ret; |
| 317 | 313 |
| 318 n=strlen(str); | 314 n=strlen(str); |
| 319 ret=mem_write(bp,str,n); | 315 ret=mem_write(bp,str,n); |
| 320 /* memory semantics is that it will always work */ | 316 /* memory semantics is that it will always work */ |
| 321 return(ret); | 317 return(ret); |
| 322 } | 318 } |
| 323 | 319 |
| OLD | NEW |