OLD | NEW |
1 /* crypto/buffer/buffer.c */ | 1 /* crypto/buffer/buffer.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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 return; | 82 return; |
83 | 83 |
84 if (a->data != NULL) | 84 if (a->data != NULL) |
85 { | 85 { |
86 memset(a->data,0,(unsigned int)a->max); | 86 memset(a->data,0,(unsigned int)a->max); |
87 OPENSSL_free(a->data); | 87 OPENSSL_free(a->data); |
88 } | 88 } |
89 OPENSSL_free(a); | 89 OPENSSL_free(a); |
90 } | 90 } |
91 | 91 |
92 int BUF_MEM_grow(BUF_MEM *str, int len) | 92 int BUF_MEM_grow(BUF_MEM *str, size_t len) |
93 { | 93 { |
94 char *ret; | 94 char *ret; |
95 » unsigned int n; | 95 » size_t n; |
96 | 96 |
97 if (str->length >= len) | 97 if (str->length >= len) |
98 { | 98 { |
99 str->length=len; | 99 str->length=len; |
100 return(len); | 100 return(len); |
101 } | 101 } |
102 if (str->max >= len) | 102 if (str->max >= len) |
103 { | 103 { |
104 memset(&str->data[str->length],0,len-str->length); | 104 memset(&str->data[str->length],0,len-str->length); |
105 str->length=len; | 105 str->length=len; |
(...skipping 12 matching lines...) Expand all Loading... |
118 else | 118 else |
119 { | 119 { |
120 str->data=ret; | 120 str->data=ret; |
121 str->max=n; | 121 str->max=n; |
122 memset(&str->data[str->length],0,len-str->length); | 122 memset(&str->data[str->length],0,len-str->length); |
123 str->length=len; | 123 str->length=len; |
124 } | 124 } |
125 return(len); | 125 return(len); |
126 } | 126 } |
127 | 127 |
128 int BUF_MEM_grow_clean(BUF_MEM *str, int len) | 128 int BUF_MEM_grow_clean(BUF_MEM *str, size_t len) |
129 { | 129 { |
130 char *ret; | 130 char *ret; |
131 » unsigned int n; | 131 » size_t n; |
132 | 132 |
133 if (str->length >= len) | 133 if (str->length >= len) |
134 { | 134 { |
135 memset(&str->data[len],0,str->length-len); | 135 memset(&str->data[len],0,str->length-len); |
136 str->length=len; | 136 str->length=len; |
137 return(len); | 137 return(len); |
138 } | 138 } |
139 if (str->max >= len) | 139 if (str->max >= len) |
140 { | 140 { |
141 memset(&str->data[str->length],0,len-str->length); | 141 memset(&str->data[str->length],0,len-str->length); |
(...skipping 12 matching lines...) Expand all Loading... |
154 } | 154 } |
155 else | 155 else |
156 { | 156 { |
157 str->data=ret; | 157 str->data=ret; |
158 str->max=n; | 158 str->max=n; |
159 memset(&str->data[str->length],0,len-str->length); | 159 memset(&str->data[str->length],0,len-str->length); |
160 str->length=len; | 160 str->length=len; |
161 } | 161 } |
162 return(len); | 162 return(len); |
163 } | 163 } |
| 164 |
| 165 char *BUF_strdup(const char *str) |
| 166 { |
| 167 if (str == NULL) return(NULL); |
| 168 return BUF_strndup(str, strlen(str)); |
| 169 } |
| 170 |
| 171 char *BUF_strndup(const char *str, size_t siz) |
| 172 { |
| 173 char *ret; |
| 174 |
| 175 if (str == NULL) return(NULL); |
| 176 |
| 177 ret=OPENSSL_malloc(siz+1); |
| 178 if (ret == NULL) |
| 179 { |
| 180 BUFerr(BUF_F_BUF_STRNDUP,ERR_R_MALLOC_FAILURE); |
| 181 return(NULL); |
| 182 } |
| 183 BUF_strlcpy(ret,str,siz+1); |
| 184 return(ret); |
| 185 } |
| 186 |
| 187 void *BUF_memdup(const void *data, size_t siz) |
| 188 { |
| 189 void *ret; |
| 190 |
| 191 if (data == NULL) return(NULL); |
| 192 |
| 193 ret=OPENSSL_malloc(siz); |
| 194 if (ret == NULL) |
| 195 { |
| 196 BUFerr(BUF_F_BUF_MEMDUP,ERR_R_MALLOC_FAILURE); |
| 197 return(NULL); |
| 198 } |
| 199 return memcpy(ret, data, siz); |
| 200 } |
| 201 |
| 202 size_t BUF_strlcpy(char *dst, const char *src, size_t size) |
| 203 { |
| 204 size_t l = 0; |
| 205 for(; size > 1 && *src; size--) |
| 206 { |
| 207 *dst++ = *src++; |
| 208 l++; |
| 209 } |
| 210 if (size) |
| 211 *dst = '\0'; |
| 212 return l + strlen(src); |
| 213 } |
| 214 |
| 215 size_t BUF_strlcat(char *dst, const char *src, size_t size) |
| 216 { |
| 217 size_t l = 0; |
| 218 for(; size > 0 && *dst; size--, dst++) |
| 219 l++; |
| 220 return l + BUF_strlcpy(dst, src, size); |
| 221 } |
| 222 |
| 223 void BUF_reverse(unsigned char *out, unsigned char *in, size_t size) |
| 224 { |
| 225 size_t i; |
| 226 if (in) |
| 227 { |
| 228 out += size - 1; |
| 229 for (i = 0; i < size; i++) |
| 230 *in++ = *out--; |
| 231 } |
| 232 else |
| 233 { |
| 234 unsigned char *q; |
| 235 char c; |
| 236 q = out + size - 1; |
| 237 for (i = 0; i < size/2; i++) |
| 238 { |
| 239 c = *q; |
| 240 *q-- = *out; |
| 241 *out++ = c; |
| 242 } |
| 243 } |
| 244 } |
OLD | NEW |