diff options
Diffstat (limited to 'src/lib/libcrypto/bio/bss_mem.c')
-rw-r--r-- | src/lib/libcrypto/bio/bss_mem.c | 264 |
1 files changed, 132 insertions, 132 deletions
diff --git a/src/lib/libcrypto/bio/bss_mem.c b/src/lib/libcrypto/bio/bss_mem.c index 37d4194e4b..a5192202bd 100644 --- a/src/lib/libcrypto/bio/bss_mem.c +++ b/src/lib/libcrypto/bio/bss_mem.c | |||
@@ -68,8 +68,8 @@ static int mem_gets(BIO *h, char *str, int size); | |||
68 | static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2); | 68 | static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
69 | static int mem_new(BIO *h); | 69 | static int mem_new(BIO *h); |
70 | static int mem_free(BIO *data); | 70 | static int mem_free(BIO *data); |
71 | static BIO_METHOD mem_method= | 71 | |
72 | { | 72 | static BIO_METHOD mem_method = { |
73 | BIO_TYPE_MEM, | 73 | BIO_TYPE_MEM, |
74 | "memory buffer", | 74 | "memory buffer", |
75 | mem_write, | 75 | mem_write, |
@@ -80,28 +80,31 @@ static BIO_METHOD mem_method= | |||
80 | mem_new, | 80 | mem_new, |
81 | mem_free, | 81 | mem_free, |
82 | NULL, | 82 | NULL, |
83 | }; | 83 | }; |
84 | 84 | ||
85 | /* bio->num is used to hold the value to return on 'empty', if it is | 85 | /* bio->num is used to hold the value to return on 'empty', if it is |
86 | * 0, should_retry is not set */ | 86 | * 0, should_retry is not set */ |
87 | 87 | ||
88 | BIO_METHOD *BIO_s_mem(void) | 88 | BIO_METHOD |
89 | { | 89 | *BIO_s_mem(void) |
90 | return(&mem_method); | 90 | { |
91 | } | 91 | return (&mem_method); |
92 | } | ||
92 | 93 | ||
93 | BIO *BIO_new_mem_buf(void *buf, int len) | 94 | BIO |
95 | *BIO_new_mem_buf(void *buf, int len) | ||
94 | { | 96 | { |
95 | BIO *ret; | 97 | BIO *ret; |
96 | BUF_MEM *b; | 98 | BUF_MEM *b; |
97 | size_t sz; | 99 | size_t sz; |
98 | 100 | ||
99 | if (!buf) { | 101 | if (!buf) { |
100 | BIOerr(BIO_F_BIO_NEW_MEM_BUF,BIO_R_NULL_PARAMETER); | 102 | BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER); |
101 | return NULL; | 103 | return NULL; |
102 | } | 104 | } |
103 | sz = (len<0) ? strlen(buf) : (size_t)len; | 105 | sz = (len < 0) ? strlen(buf) : (size_t)len; |
104 | if(!(ret = BIO_new(BIO_s_mem())) ) return NULL; | 106 | if (!(ret = BIO_new(BIO_s_mem()))) |
107 | return NULL; | ||
105 | b = (BUF_MEM *)ret->ptr; | 108 | b = (BUF_MEM *)ret->ptr; |
106 | b->data = buf; | 109 | b->data = buf; |
107 | b->length = sz; | 110 | b->length = sz; |
@@ -112,208 +115,205 @@ BIO *BIO_new_mem_buf(void *buf, int len) | |||
112 | return ret; | 115 | return ret; |
113 | } | 116 | } |
114 | 117 | ||
115 | static int mem_new(BIO *bi) | 118 | static int |
116 | { | 119 | mem_new(BIO *bi) |
120 | { | ||
117 | BUF_MEM *b; | 121 | BUF_MEM *b; |
118 | 122 | ||
119 | if ((b=BUF_MEM_new()) == NULL) | 123 | if ((b = BUF_MEM_new()) == NULL) |
120 | return(0); | 124 | return (0); |
121 | bi->shutdown=1; | 125 | bi->shutdown = 1; |
122 | bi->init=1; | 126 | bi->init = 1; |
123 | bi->num= -1; | 127 | bi->num = -1; |
124 | bi->ptr=(char *)b; | 128 | bi->ptr = (char *)b; |
125 | return(1); | 129 | return (1); |
126 | } | 130 | } |
127 | 131 | ||
128 | static int mem_free(BIO *a) | 132 | static int |
129 | { | 133 | mem_free(BIO *a) |
130 | if (a == NULL) return(0); | 134 | { |
131 | if (a->shutdown) | 135 | if (a == NULL) |
132 | { | 136 | return (0); |
133 | if ((a->init) && (a->ptr != NULL)) | 137 | if (a->shutdown) { |
134 | { | 138 | if ((a->init) && (a->ptr != NULL)) { |
135 | BUF_MEM *b; | 139 | BUF_MEM *b; |
136 | b = (BUF_MEM *)a->ptr; | 140 | b = (BUF_MEM *)a->ptr; |
137 | if(a->flags & BIO_FLAGS_MEM_RDONLY) b->data = NULL; | 141 | if (a->flags & BIO_FLAGS_MEM_RDONLY) |
138 | BUF_MEM_free(b); | 142 | b->data = NULL; |
139 | a->ptr=NULL; | 143 | BUF_MEM_free(b); |
140 | } | 144 | a->ptr = NULL; |
141 | } | 145 | } |
142 | return(1); | ||
143 | } | 146 | } |
144 | 147 | return (1); | |
145 | static int mem_read(BIO *b, char *out, int outl) | 148 | } |
146 | { | 149 | |
147 | int ret= -1; | 150 | static int |
151 | mem_read(BIO *b, char *out, int outl) | ||
152 | { | ||
153 | int ret = -1; | ||
148 | BUF_MEM *bm; | 154 | BUF_MEM *bm; |
149 | 155 | ||
150 | bm=(BUF_MEM *)b->ptr; | 156 | bm = (BUF_MEM *)b->ptr; |
151 | BIO_clear_retry_flags(b); | 157 | BIO_clear_retry_flags(b); |
152 | ret=(outl >=0 && (size_t)outl > bm->length)?(int)bm->length:outl; | 158 | ret = (outl >=0 && (size_t)outl > bm->length) ? (int)bm->length : outl; |
153 | if ((out != NULL) && (ret > 0)) { | 159 | if ((out != NULL) && (ret > 0)) { |
154 | memcpy(out,bm->data,ret); | 160 | memcpy(out, bm->data, ret); |
155 | bm->length-=ret; | 161 | bm->length -= ret; |
156 | if(b->flags & BIO_FLAGS_MEM_RDONLY) bm->data += ret; | 162 | if (b->flags & BIO_FLAGS_MEM_RDONLY) |
163 | bm->data += ret; | ||
157 | else { | 164 | else { |
158 | memmove(&(bm->data[0]),&(bm->data[ret]),bm->length); | 165 | memmove(&(bm->data[0]), &(bm->data[ret]), bm->length); |
159 | } | 166 | } |
160 | } else if (bm->length == 0) | 167 | } else if (bm->length == 0) { |
161 | { | ||
162 | ret = b->num; | 168 | ret = b->num; |
163 | if (ret != 0) | 169 | if (ret != 0) |
164 | BIO_set_retry_read(b); | 170 | BIO_set_retry_read(b); |
165 | } | ||
166 | return(ret); | ||
167 | } | 171 | } |
172 | return (ret); | ||
173 | } | ||
168 | 174 | ||
169 | static int mem_write(BIO *b, const char *in, int inl) | 175 | static int |
170 | { | 176 | mem_write(BIO *b, const char *in, int inl) |
171 | int ret= -1; | 177 | { |
178 | int ret = -1; | ||
172 | int blen; | 179 | int blen; |
173 | BUF_MEM *bm; | 180 | BUF_MEM *bm; |
174 | 181 | ||
175 | bm=(BUF_MEM *)b->ptr; | 182 | bm = (BUF_MEM *)b->ptr; |
176 | if (in == NULL) | 183 | if (in == NULL) { |
177 | { | 184 | BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER); |
178 | BIOerr(BIO_F_MEM_WRITE,BIO_R_NULL_PARAMETER); | ||
179 | goto end; | 185 | goto end; |
180 | } | 186 | } |
181 | 187 | ||
182 | if(b->flags & BIO_FLAGS_MEM_RDONLY) { | 188 | if (b->flags & BIO_FLAGS_MEM_RDONLY) { |
183 | BIOerr(BIO_F_MEM_WRITE,BIO_R_WRITE_TO_READ_ONLY_BIO); | 189 | BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO); |
184 | goto end; | 190 | goto end; |
185 | } | 191 | } |
186 | 192 | ||
187 | BIO_clear_retry_flags(b); | 193 | BIO_clear_retry_flags(b); |
188 | blen=bm->length; | 194 | blen = bm->length; |
189 | if (BUF_MEM_grow_clean(bm,blen+inl) != (blen+inl)) | 195 | if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl)) |
190 | goto end; | 196 | goto end; |
191 | memcpy(&(bm->data[blen]),in,inl); | 197 | memcpy(&(bm->data[blen]), in, inl); |
192 | ret=inl; | 198 | ret = inl; |
193 | end: | 199 | end: |
194 | return(ret); | 200 | return (ret); |
195 | } | 201 | } |
196 | 202 | ||
197 | static long mem_ctrl(BIO *b, int cmd, long num, void *ptr) | 203 | static long |
198 | { | 204 | mem_ctrl(BIO *b, int cmd, long num, void *ptr) |
199 | long ret=1; | 205 | { |
206 | long ret = 1; | ||
200 | char **pptr; | 207 | char **pptr; |
201 | 208 | ||
202 | BUF_MEM *bm=(BUF_MEM *)b->ptr; | 209 | BUF_MEM *bm = (BUF_MEM *)b->ptr; |
203 | 210 | ||
204 | switch (cmd) | 211 | switch (cmd) { |
205 | { | ||
206 | case BIO_CTRL_RESET: | 212 | case BIO_CTRL_RESET: |
207 | if (bm->data != NULL) | 213 | if (bm->data != NULL) { |
208 | { | ||
209 | /* For read only case reset to the start again */ | 214 | /* For read only case reset to the start again */ |
210 | if(b->flags & BIO_FLAGS_MEM_RDONLY) | 215 | if (b->flags & BIO_FLAGS_MEM_RDONLY) { |
211 | { | ||
212 | bm->data -= bm->max - bm->length; | 216 | bm->data -= bm->max - bm->length; |
213 | bm->length = bm->max; | 217 | bm->length = bm->max; |
214 | } | 218 | } else { |
215 | else | 219 | memset(bm->data, 0, bm->max); |
216 | { | 220 | bm->length = 0; |
217 | memset(bm->data,0,bm->max); | ||
218 | bm->length=0; | ||
219 | } | ||
220 | } | 221 | } |
222 | } | ||
221 | break; | 223 | break; |
222 | case BIO_CTRL_EOF: | 224 | case BIO_CTRL_EOF: |
223 | ret=(long)(bm->length == 0); | 225 | ret = (long)(bm->length == 0); |
224 | break; | 226 | break; |
225 | case BIO_C_SET_BUF_MEM_EOF_RETURN: | 227 | case BIO_C_SET_BUF_MEM_EOF_RETURN: |
226 | b->num=(int)num; | 228 | b->num = (int)num; |
227 | break; | 229 | break; |
228 | case BIO_CTRL_INFO: | 230 | case BIO_CTRL_INFO: |
229 | ret=(long)bm->length; | 231 | ret = (long)bm->length; |
230 | if (ptr != NULL) | 232 | if (ptr != NULL) { |
231 | { | 233 | pptr = (char **)ptr; |
232 | pptr=(char **)ptr; | 234 | *pptr = (char *)&(bm->data[0]); |
233 | *pptr=(char *)&(bm->data[0]); | 235 | } |
234 | } | ||
235 | break; | 236 | break; |
236 | case BIO_C_SET_BUF_MEM: | 237 | case BIO_C_SET_BUF_MEM: |
237 | mem_free(b); | 238 | mem_free(b); |
238 | b->shutdown=(int)num; | 239 | b->shutdown = (int)num; |
239 | b->ptr=ptr; | 240 | b->ptr = ptr; |
240 | break; | 241 | break; |
241 | case BIO_C_GET_BUF_MEM_PTR: | 242 | case BIO_C_GET_BUF_MEM_PTR: |
242 | if (ptr != NULL) | 243 | if (ptr != NULL) { |
243 | { | 244 | pptr = (char **)ptr; |
244 | pptr=(char **)ptr; | 245 | *pptr = (char *)bm; |
245 | *pptr=(char *)bm; | 246 | } |
246 | } | ||
247 | break; | 247 | break; |
248 | case BIO_CTRL_GET_CLOSE: | 248 | case BIO_CTRL_GET_CLOSE: |
249 | ret=(long)b->shutdown; | 249 | ret = (long)b->shutdown; |
250 | break; | 250 | break; |
251 | case BIO_CTRL_SET_CLOSE: | 251 | case BIO_CTRL_SET_CLOSE: |
252 | b->shutdown=(int)num; | 252 | b->shutdown = (int)num; |
253 | break; | 253 | break; |
254 | 254 | ||
255 | case BIO_CTRL_WPENDING: | 255 | case BIO_CTRL_WPENDING: |
256 | ret=0L; | 256 | ret = 0L; |
257 | break; | 257 | break; |
258 | case BIO_CTRL_PENDING: | 258 | case BIO_CTRL_PENDING: |
259 | ret=(long)bm->length; | 259 | ret = (long)bm->length; |
260 | break; | 260 | break; |
261 | case BIO_CTRL_DUP: | 261 | case BIO_CTRL_DUP: |
262 | case BIO_CTRL_FLUSH: | 262 | case BIO_CTRL_FLUSH: |
263 | ret=1; | 263 | ret = 1; |
264 | break; | 264 | break; |
265 | case BIO_CTRL_PUSH: | 265 | case BIO_CTRL_PUSH: |
266 | case BIO_CTRL_POP: | 266 | case BIO_CTRL_POP: |
267 | default: | 267 | default: |
268 | ret=0; | 268 | ret = 0; |
269 | break; | 269 | break; |
270 | } | ||
271 | return(ret); | ||
272 | } | 270 | } |
271 | return (ret); | ||
272 | } | ||
273 | 273 | ||
274 | static int mem_gets(BIO *bp, char *buf, int size) | 274 | static int |
275 | { | 275 | mem_gets(BIO *bp, char *buf, int size) |
276 | int i,j; | 276 | { |
277 | int ret= -1; | 277 | int i, j; |
278 | int ret = -1; | ||
278 | char *p; | 279 | char *p; |
279 | BUF_MEM *bm=(BUF_MEM *)bp->ptr; | 280 | BUF_MEM *bm = (BUF_MEM *)bp->ptr; |
280 | 281 | ||
281 | BIO_clear_retry_flags(bp); | 282 | BIO_clear_retry_flags(bp); |
282 | j=bm->length; | 283 | j = bm->length; |
283 | if ((size-1) < j) j=size-1; | 284 | if ((size - 1) < j) |
284 | if (j <= 0) | 285 | j = size - 1; |
285 | { | 286 | if (j <= 0) { |
286 | *buf='\0'; | 287 | *buf = '\0'; |
287 | return 0; | 288 | return 0; |
288 | } | 289 | } |
289 | p=bm->data; | 290 | p = bm->data; |
290 | for (i=0; i<j; i++) | 291 | for (i = 0; i < j; i++) { |
291 | { | 292 | if (p[i] == '\n') { |
292 | if (p[i] == '\n') | ||
293 | { | ||
294 | i++; | 293 | i++; |
295 | break; | 294 | break; |
296 | } | ||
297 | } | 295 | } |
296 | } | ||
298 | 297 | ||
299 | /* | 298 | /* |
300 | * i is now the max num of bytes to copy, either j or up to | 299 | * i is now the max num of bytes to copy, either j or up to |
301 | * and including the first newline | 300 | * and including the first newline |
302 | */ | 301 | */ |
303 | 302 | ||
304 | i=mem_read(bp,buf,i); | 303 | i = mem_read(bp, buf, i); |
305 | if (i > 0) buf[i]='\0'; | 304 | if (i > 0) |
306 | ret=i; | 305 | buf[i] = '\0'; |
307 | return(ret); | 306 | ret = i; |
308 | } | 307 | return (ret); |
308 | } | ||
309 | 309 | ||
310 | static int mem_puts(BIO *bp, const char *str) | 310 | static int |
311 | { | 311 | mem_puts(BIO *bp, const char *str) |
312 | int n,ret; | 312 | { |
313 | int n, ret; | ||
313 | 314 | ||
314 | n=strlen(str); | 315 | n = strlen(str); |
315 | ret=mem_write(bp,str,n); | 316 | ret = mem_write(bp, str, n); |
316 | /* memory semantics is that it will always work */ | 317 | /* memory semantics is that it will always work */ |
317 | return(ret); | 318 | return (ret); |
318 | } | 319 | } |
319 | |||