summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio/bf_buff.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bio/bf_buff.c')
-rw-r--r--src/lib/libcrypto/bio/bf_buff.c629
1 files changed, 321 insertions, 308 deletions
diff --git a/src/lib/libcrypto/bio/bf_buff.c b/src/lib/libcrypto/bio/bf_buff.c
index 4b5a132d8a..9518cf977e 100644
--- a/src/lib/libcrypto/bio/bf_buff.c
+++ b/src/lib/libcrypto/bio/bf_buff.c
@@ -61,7 +61,7 @@
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include <openssl/bio.h> 62#include <openssl/bio.h>
63 63
64static int buffer_write(BIO *h, const char *buf,int num); 64static int buffer_write(BIO *h, const char *buf, int num);
65static int buffer_read(BIO *h, char *buf, int size); 65static int buffer_read(BIO *h, char *buf, int size);
66static int buffer_puts(BIO *h, const char *str); 66static int buffer_puts(BIO *h, const char *str);
67static int buffer_gets(BIO *h, char *str, int size); 67static int buffer_gets(BIO *h, char *str, int size);
@@ -71,8 +71,7 @@ static int buffer_free(BIO *data);
71static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); 71static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
72#define DEFAULT_BUFFER_SIZE 4096 72#define DEFAULT_BUFFER_SIZE 4096
73 73
74static BIO_METHOD methods_buffer= 74static BIO_METHOD methods_buffer = {
75 {
76 BIO_TYPE_BUFFER, 75 BIO_TYPE_BUFFER,
77 "buffer", 76 "buffer",
78 buffer_write, 77 buffer_write,
@@ -83,430 +82,444 @@ static BIO_METHOD methods_buffer=
83 buffer_new, 82 buffer_new,
84 buffer_free, 83 buffer_free,
85 buffer_callback_ctrl, 84 buffer_callback_ctrl,
86 }; 85};
87 86
88BIO_METHOD *BIO_f_buffer(void) 87BIO_METHOD
89 { 88*BIO_f_buffer(void)
90 return(&methods_buffer); 89{
91 } 90 return (&methods_buffer);
91}
92 92
93static int buffer_new(BIO *bi) 93static int
94 { 94buffer_new(BIO *bi)
95{
95 BIO_F_BUFFER_CTX *ctx; 96 BIO_F_BUFFER_CTX *ctx;
96 97
97 ctx=(BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX)); 98 ctx = (BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX));
98 if (ctx == NULL) return(0); 99 if (ctx == NULL)
99 ctx->ibuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE); 100 return (0);
100 if (ctx->ibuf == NULL) { OPENSSL_free(ctx); return(0); } 101 ctx->ibuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
101 ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE); 102 if (ctx->ibuf == NULL) {
102 if (ctx->obuf == NULL) { OPENSSL_free(ctx->ibuf); OPENSSL_free(ctx); return(0); } 103 OPENSSL_free(ctx);
103 ctx->ibuf_size=DEFAULT_BUFFER_SIZE; 104 return (0);
104 ctx->obuf_size=DEFAULT_BUFFER_SIZE;
105 ctx->ibuf_len=0;
106 ctx->ibuf_off=0;
107 ctx->obuf_len=0;
108 ctx->obuf_off=0;
109
110 bi->init=1;
111 bi->ptr=(char *)ctx;
112 bi->flags=0;
113 return(1);
114 } 105 }
115 106 ctx->obuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
116static int buffer_free(BIO *a) 107 if (ctx->obuf == NULL) {
117 { 108 OPENSSL_free(ctx->ibuf);
109 OPENSSL_free(ctx);
110 return (0);
111 }
112 ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
113 ctx->obuf_size = DEFAULT_BUFFER_SIZE;
114 ctx->ibuf_len = 0;
115 ctx->ibuf_off = 0;
116 ctx->obuf_len = 0;
117 ctx->obuf_off = 0;
118
119 bi->init = 1;
120 bi->ptr = (char *)ctx;
121 bi->flags = 0;
122 return (1);
123}
124
125static int
126buffer_free(BIO *a)
127{
118 BIO_F_BUFFER_CTX *b; 128 BIO_F_BUFFER_CTX *b;
119 129
120 if (a == NULL) return(0); 130 if (a == NULL)
121 b=(BIO_F_BUFFER_CTX *)a->ptr; 131 return (0);
122 if (b->ibuf != NULL) OPENSSL_free(b->ibuf); 132 b = (BIO_F_BUFFER_CTX *)a->ptr;
123 if (b->obuf != NULL) OPENSSL_free(b->obuf); 133 if (b->ibuf != NULL)
134 OPENSSL_free(b->ibuf);
135 if (b->obuf != NULL)
136 OPENSSL_free(b->obuf);
124 OPENSSL_free(a->ptr); 137 OPENSSL_free(a->ptr);
125 a->ptr=NULL; 138 a->ptr = NULL;
126 a->init=0; 139 a->init = 0;
127 a->flags=0; 140 a->flags = 0;
128 return(1); 141 return (1);
129 } 142}
130 143
131static int buffer_read(BIO *b, char *out, int outl) 144static int
132 { 145buffer_read(BIO *b, char *out, int outl)
133 int i,num=0; 146{
147 int i, num = 0;
134 BIO_F_BUFFER_CTX *ctx; 148 BIO_F_BUFFER_CTX *ctx;
135 149
136 if (out == NULL) return(0); 150 if (out == NULL)
137 ctx=(BIO_F_BUFFER_CTX *)b->ptr; 151 return (0);
152 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
138 153
139 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 154 if ((ctx == NULL) || (b->next_bio == NULL))
140 num=0; 155 return (0);
156 num = 0;
141 BIO_clear_retry_flags(b); 157 BIO_clear_retry_flags(b);
142 158
143start: 159start:
144 i=ctx->ibuf_len; 160 i = ctx->ibuf_len;
145 /* If there is stuff left over, grab it */ 161 /* If there is stuff left over, grab it */
146 if (i != 0) 162 if (i != 0) {
147 { 163 if (i > outl)
148 if (i > outl) i=outl; 164 i = outl;
149 memcpy(out,&(ctx->ibuf[ctx->ibuf_off]),i); 165 memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
150 ctx->ibuf_off+=i; 166 ctx->ibuf_off += i;
151 ctx->ibuf_len-=i; 167 ctx->ibuf_len -= i;
152 num+=i; 168 num += i;
153 if (outl == i) return(num); 169 if (outl == i)
154 outl-=i; 170 return (num);
155 out+=i; 171 outl -= i;
156 } 172 out += i;
173 }
157 174
158 /* We may have done a partial read. try to do more. 175 /* We may have done a partial read. try to do more.
159 * We have nothing in the buffer. 176 * We have nothing in the buffer.
160 * If we get an error and have read some data, just return it 177 * If we get an error and have read some data, just return it
161 * and let them retry to get the error again. 178 * and let them retry to get the error again.
162 * copy direct to parent address space */ 179 * copy direct to parent address space */
163 if (outl > ctx->ibuf_size) 180 if (outl > ctx->ibuf_size) {
164 { 181 for (;;) {
165 for (;;) 182 i = BIO_read(b->next_bio, out, outl);
166 { 183 if (i <= 0) {
167 i=BIO_read(b->next_bio,out,outl);
168 if (i <= 0)
169 {
170 BIO_copy_next_retry(b); 184 BIO_copy_next_retry(b);
171 if (i < 0) return((num > 0)?num:i); 185 if (i < 0)
172 if (i == 0) return(num); 186 return ((num > 0) ? num : i);
173 } 187 if (i == 0)
174 num+=i; 188 return (num);
175 if (outl == i) return(num);
176 out+=i;
177 outl-=i;
178 } 189 }
190 num += i;
191 if (outl == i)
192 return (num);
193 out += i;
194 outl -= i;
179 } 195 }
196 }
180 /* else */ 197 /* else */
181 198
182 /* we are going to be doing some buffering */ 199 /* we are going to be doing some buffering */
183 i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size); 200 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
184 if (i <= 0) 201 if (i <= 0) {
185 {
186 BIO_copy_next_retry(b); 202 BIO_copy_next_retry(b);
187 if (i < 0) return((num > 0)?num:i); 203 if (i < 0)
188 if (i == 0) return(num); 204 return ((num > 0) ? num : i);
189 } 205 if (i == 0)
190 ctx->ibuf_off=0; 206 return (num);
191 ctx->ibuf_len=i; 207 }
208 ctx->ibuf_off = 0;
209 ctx->ibuf_len = i;
192 210
193 /* Lets re-read using ourselves :-) */ 211 /* Lets re-read using ourselves :-) */
194 goto start; 212 goto start;
195 } 213}
196 214
197static int buffer_write(BIO *b, const char *in, int inl) 215static int
198 { 216buffer_write(BIO *b, const char *in, int inl)
199 int i,num=0; 217{
218 int i, num = 0;
200 BIO_F_BUFFER_CTX *ctx; 219 BIO_F_BUFFER_CTX *ctx;
201 220
202 if ((in == NULL) || (inl <= 0)) return(0); 221 if ((in == NULL) || (inl <= 0))
203 ctx=(BIO_F_BUFFER_CTX *)b->ptr; 222 return (0);
204 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 223 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
224 if ((ctx == NULL) || (b->next_bio == NULL))
225 return (0);
205 226
206 BIO_clear_retry_flags(b); 227 BIO_clear_retry_flags(b);
207start: 228start:
208 i=ctx->obuf_size-(ctx->obuf_len+ctx->obuf_off); 229 i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
209 /* add to buffer and return */ 230 /* add to buffer and return */
210 if (i >= inl) 231 if (i >= inl) {
211 { 232 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
212 memcpy(&(ctx->obuf[ctx->obuf_off+ctx->obuf_len]),in,inl); 233 ctx->obuf_len += inl;
213 ctx->obuf_len+=inl; 234 return (num + inl);
214 return(num+inl); 235 }
215 }
216 /* else */ 236 /* else */
217 /* stuff already in buffer, so add to it first, then flush */ 237 /* stuff already in buffer, so add to it first, then flush */
218 if (ctx->obuf_len != 0) 238 if (ctx->obuf_len != 0) {
219 {
220 if (i > 0) /* lets fill it up if we can */ 239 if (i > 0) /* lets fill it up if we can */
221 { 240 {
222 memcpy(&(ctx->obuf[ctx->obuf_off+ctx->obuf_len]),in,i); 241 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
223 in+=i; 242 in += i;
224 inl-=i; 243 inl -= i;
225 num+=i; 244 num += i;
226 ctx->obuf_len+=i; 245 ctx->obuf_len += i;
227 } 246 }
228 /* we now have a full buffer needing flushing */ 247 /* we now have a full buffer needing flushing */
229 for (;;) 248 for (;;) {
230 { 249 i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
231 i=BIO_write(b->next_bio,&(ctx->obuf[ctx->obuf_off]), 250 ctx->obuf_len);
232 ctx->obuf_len); 251 if (i <= 0) {
233 if (i <= 0)
234 {
235 BIO_copy_next_retry(b); 252 BIO_copy_next_retry(b);
236 253
237 if (i < 0) return((num > 0)?num:i); 254 if (i < 0)
238 if (i == 0) return(num); 255 return ((num > 0) ? num : i);
239 } 256 if (i == 0)
240 ctx->obuf_off+=i; 257 return (num);
241 ctx->obuf_len-=i;
242 if (ctx->obuf_len == 0) break;
243 } 258 }
259 ctx->obuf_off += i;
260 ctx->obuf_len -= i;
261 if (ctx->obuf_len == 0)
262 break;
244 } 263 }
264 }
245 /* we only get here if the buffer has been flushed and we 265 /* we only get here if the buffer has been flushed and we
246 * still have stuff to write */ 266 * still have stuff to write */
247 ctx->obuf_off=0; 267 ctx->obuf_off = 0;
248 268
249 /* we now have inl bytes to write */ 269 /* we now have inl bytes to write */
250 while (inl >= ctx->obuf_size) 270 while (inl >= ctx->obuf_size) {
251 { 271 i = BIO_write(b->next_bio, in, inl);
252 i=BIO_write(b->next_bio,in,inl); 272 if (i <= 0) {
253 if (i <= 0)
254 {
255 BIO_copy_next_retry(b); 273 BIO_copy_next_retry(b);
256 if (i < 0) return((num > 0)?num:i); 274 if (i < 0)
257 if (i == 0) return(num); 275 return ((num > 0) ? num : i);
258 } 276 if (i == 0)
259 num+=i; 277 return (num);
260 in+=i;
261 inl-=i;
262 if (inl == 0) return(num);
263 } 278 }
279 num += i;
280 in += i;
281 inl -= i;
282 if (inl == 0)
283 return (num);
284 }
264 285
265 /* copy the rest into the buffer since we have only a small 286 /* copy the rest into the buffer since we have only a small
266 * amount left */ 287 * amount left */
267 goto start; 288 goto start;
268 } 289}
269 290
270static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr) 291static long
271 { 292buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
293{
272 BIO *dbio; 294 BIO *dbio;
273 BIO_F_BUFFER_CTX *ctx; 295 BIO_F_BUFFER_CTX *ctx;
274 long ret=1; 296 long ret = 1;
275 char *p1,*p2; 297 char *p1, *p2;
276 int r,i,*ip; 298 int r, i, *ip;
277 int ibs,obs; 299 int ibs, obs;
278 300
279 ctx=(BIO_F_BUFFER_CTX *)b->ptr; 301 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
280 302
281 switch (cmd) 303 switch (cmd) {
282 {
283 case BIO_CTRL_RESET: 304 case BIO_CTRL_RESET:
284 ctx->ibuf_off=0; 305 ctx->ibuf_off = 0;
285 ctx->ibuf_len=0; 306 ctx->ibuf_len = 0;
286 ctx->obuf_off=0; 307 ctx->obuf_off = 0;
287 ctx->obuf_len=0; 308 ctx->obuf_len = 0;
288 if (b->next_bio == NULL) return(0); 309 if (b->next_bio == NULL)
289 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 310 return (0);
311 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
290 break; 312 break;
291 case BIO_CTRL_INFO: 313 case BIO_CTRL_INFO:
292 ret=(long)ctx->obuf_len; 314 ret = (long)ctx->obuf_len;
293 break; 315 break;
294 case BIO_C_GET_BUFF_NUM_LINES: 316 case BIO_C_GET_BUFF_NUM_LINES:
295 ret=0; 317 ret = 0;
296 p1=ctx->ibuf; 318 p1 = ctx->ibuf;
297 for (i=0; i<ctx->ibuf_len; i++) 319 for (i = 0; i < ctx->ibuf_len; i++) {
298 { 320 if (p1[ctx->ibuf_off + i] == '\n')
299 if (p1[ctx->ibuf_off + i] == '\n') ret++; 321 ret++;
300 } 322 }
301 break; 323 break;
302 case BIO_CTRL_WPENDING: 324 case BIO_CTRL_WPENDING:
303 ret=(long)ctx->obuf_len; 325 ret = (long)ctx->obuf_len;
304 if (ret == 0) 326 if (ret == 0) {
305 { 327 if (b->next_bio == NULL)
306 if (b->next_bio == NULL) return(0); 328 return (0);
307 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 329 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
308 } 330 }
309 break; 331 break;
310 case BIO_CTRL_PENDING: 332 case BIO_CTRL_PENDING:
311 ret=(long)ctx->ibuf_len; 333 ret = (long)ctx->ibuf_len;
312 if (ret == 0) 334 if (ret == 0) {
313 { 335 if (b->next_bio == NULL)
314 if (b->next_bio == NULL) return(0); 336 return (0);
315 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 337 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
316 } 338 }
317 break; 339 break;
318 case BIO_C_SET_BUFF_READ_DATA: 340 case BIO_C_SET_BUFF_READ_DATA:
319 if (num > ctx->ibuf_size) 341 if (num > ctx->ibuf_size) {
320 { 342 p1 = OPENSSL_malloc((int)num);
321 p1=OPENSSL_malloc((int)num); 343 if (p1 == NULL)
322 if (p1 == NULL) goto malloc_error; 344 goto malloc_error;
323 if (ctx->ibuf != NULL) OPENSSL_free(ctx->ibuf); 345 if (ctx->ibuf != NULL)
324 ctx->ibuf=p1; 346 OPENSSL_free(ctx->ibuf);
325 } 347 ctx->ibuf = p1;
326 ctx->ibuf_off=0; 348 }
327 ctx->ibuf_len=(int)num; 349 ctx->ibuf_off = 0;
328 memcpy(ctx->ibuf,ptr,(int)num); 350 ctx->ibuf_len = (int)num;
329 ret=1; 351 memcpy(ctx->ibuf, ptr,(int)num);
352 ret = 1;
330 break; 353 break;
331 case BIO_C_SET_BUFF_SIZE: 354 case BIO_C_SET_BUFF_SIZE:
332 if (ptr != NULL) 355 if (ptr != NULL) {
333 { 356 ip = (int *)ptr;
334 ip=(int *)ptr; 357 if (*ip == 0) {
335 if (*ip == 0) 358 ibs = (int)num;
336 { 359 obs = ctx->obuf_size;
337 ibs=(int)num;
338 obs=ctx->obuf_size;
339 }
340 else /* if (*ip == 1) */
341 {
342 ibs=ctx->ibuf_size;
343 obs=(int)num;
344 }
345 }
346 else
347 {
348 ibs=(int)num;
349 obs=(int)num;
350 } 360 }
351 p1=ctx->ibuf; 361 else /* if (*ip == 1) */
352 p2=ctx->obuf;
353 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size))
354 { 362 {
355 p1=(char *)OPENSSL_malloc((int)num); 363 ibs = ctx->ibuf_size;
356 if (p1 == NULL) goto malloc_error; 364 obs = (int)num;
357 } 365 }
358 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) 366 } else {
359 { 367 ibs = (int)num;
360 p2=(char *)OPENSSL_malloc((int)num); 368 obs = (int)num;
361 if (p2 == NULL) 369 }
362 { 370 p1 = ctx->ibuf;
363 if (p1 != ctx->ibuf) OPENSSL_free(p1); 371 p2 = ctx->obuf;
372 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
373 p1 = (char *)OPENSSL_malloc((int)num);
374 if (p1 == NULL)
375 goto malloc_error;
376 }
377 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
378 p2 = (char *)OPENSSL_malloc((int)num);
379 if (p2 == NULL) {
380 if (p1 != ctx->ibuf)
381 OPENSSL_free(p1);
364 goto malloc_error; 382 goto malloc_error;
365 }
366 } 383 }
367 if (ctx->ibuf != p1) 384 }
368 { 385 if (ctx->ibuf != p1) {
369 OPENSSL_free(ctx->ibuf); 386 OPENSSL_free(ctx->ibuf);
370 ctx->ibuf=p1; 387 ctx->ibuf = p1;
371 ctx->ibuf_off=0; 388 ctx->ibuf_off = 0;
372 ctx->ibuf_len=0; 389 ctx->ibuf_len = 0;
373 ctx->ibuf_size=ibs; 390 ctx->ibuf_size = ibs;
374 } 391 }
375 if (ctx->obuf != p2) 392 if (ctx->obuf != p2) {
376 {
377 OPENSSL_free(ctx->obuf); 393 OPENSSL_free(ctx->obuf);
378 ctx->obuf=p2; 394 ctx->obuf = p2;
379 ctx->obuf_off=0; 395 ctx->obuf_off = 0;
380 ctx->obuf_len=0; 396 ctx->obuf_len = 0;
381 ctx->obuf_size=obs; 397 ctx->obuf_size = obs;
382 } 398 }
383 break; 399 break;
384 case BIO_C_DO_STATE_MACHINE: 400 case BIO_C_DO_STATE_MACHINE:
385 if (b->next_bio == NULL) return(0); 401 if (b->next_bio == NULL)
402 return (0);
386 BIO_clear_retry_flags(b); 403 BIO_clear_retry_flags(b);
387 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 404 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
388 BIO_copy_next_retry(b); 405 BIO_copy_next_retry(b);
389 break; 406 break;
390 407
391 case BIO_CTRL_FLUSH: 408 case BIO_CTRL_FLUSH:
392 if (b->next_bio == NULL) return(0); 409 if (b->next_bio == NULL)
393 if (ctx->obuf_len <= 0) 410 return (0);
394 { 411 if (ctx->obuf_len <= 0) {
395 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 412 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
396 break; 413 break;
397 } 414 }
398 415
399 for (;;) 416 for (;;) {
400 {
401 BIO_clear_retry_flags(b); 417 BIO_clear_retry_flags(b);
402 if (ctx->obuf_len > 0) 418 if (ctx->obuf_len > 0) {
403 { 419 r = BIO_write(b->next_bio,
404 r=BIO_write(b->next_bio, 420 &(ctx->obuf[ctx->obuf_off]),
405 &(ctx->obuf[ctx->obuf_off]), 421 ctx->obuf_len);
406 ctx->obuf_len);
407#if 0 422#if 0
408fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len,r); 423 fprintf(stderr, "FLUSH [%3d] %3d -> %3d\n", ctx->obuf_off, ctx->obuf_len, r);
409#endif 424#endif
410 BIO_copy_next_retry(b); 425 BIO_copy_next_retry(b);
411 if (r <= 0) return((long)r); 426 if (r <= 0)
412 ctx->obuf_off+=r; 427 return ((long)r);
413 ctx->obuf_len-=r; 428 ctx->obuf_off += r;
414 } 429 ctx->obuf_len -= r;
415 else 430 } else {
416 { 431 ctx->obuf_len = 0;
417 ctx->obuf_len=0; 432 ctx->obuf_off = 0;
418 ctx->obuf_off=0; 433 ret = 1;
419 ret=1;
420 break; 434 break;
421 }
422 } 435 }
423 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 436 }
437 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
424 break; 438 break;
425 case BIO_CTRL_DUP: 439 case BIO_CTRL_DUP:
426 dbio=(BIO *)ptr; 440 dbio = (BIO *)ptr;
427 if ( !BIO_set_read_buffer_size(dbio,ctx->ibuf_size) || 441 if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
428 !BIO_set_write_buffer_size(dbio,ctx->obuf_size)) 442 !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
429 ret=0; 443 ret = 0;
430 break; 444 break;
431 default: 445 default:
432 if (b->next_bio == NULL) return(0); 446 if (b->next_bio == NULL)
433 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 447 return (0);
448 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
434 break; 449 break;
435 }
436 return(ret);
437malloc_error:
438 BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE);
439 return(0);
440 } 450 }
441 451 return (ret);
442static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 452malloc_error:
443 { 453 BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
444 long ret=1; 454 return (0);
445 455}
446 if (b->next_bio == NULL) return(0); 456
447 switch (cmd) 457static long
448 { 458buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
459{
460 long ret = 1;
461
462 if (b->next_bio == NULL)
463 return (0);
464 switch (cmd) {
449 default: 465 default:
450 ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 466 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
451 break; 467 break;
452 }
453 return(ret);
454 } 468 }
469 return (ret);
470}
455 471
456static int buffer_gets(BIO *b, char *buf, int size) 472static int
457 { 473buffer_gets(BIO *b, char *buf, int size)
474{
458 BIO_F_BUFFER_CTX *ctx; 475 BIO_F_BUFFER_CTX *ctx;
459 int num=0,i,flag; 476 int num = 0, i, flag;
460 char *p; 477 char *p;
461 478
462 ctx=(BIO_F_BUFFER_CTX *)b->ptr; 479 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
463 size--; /* reserve space for a '\0' */ 480 size--; /* reserve space for a '\0' */
464 BIO_clear_retry_flags(b); 481 BIO_clear_retry_flags(b);
465 482
466 for (;;) 483 for (;;) {
467 { 484 if (ctx->ibuf_len > 0) {
468 if (ctx->ibuf_len > 0) 485 p = &(ctx->ibuf[ctx->ibuf_off]);
469 { 486 flag = 0;
470 p= &(ctx->ibuf[ctx->ibuf_off]); 487 for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
471 flag=0; 488 *(buf++) = p[i];
472 for (i=0; (i<ctx->ibuf_len) && (i<size); i++) 489 if (p[i] == '\n') {
473 { 490 flag = 1;
474 *(buf++)=p[i];
475 if (p[i] == '\n')
476 {
477 flag=1;
478 i++; 491 i++;
479 break; 492 break;
480 }
481 }
482 num+=i;
483 size-=i;
484 ctx->ibuf_len-=i;
485 ctx->ibuf_off+=i;
486 if (flag || size == 0)
487 {
488 *buf='\0';
489 return(num);
490 } 493 }
491 } 494 }
495 num += i;
496 size -= i;
497 ctx->ibuf_len -= i;
498 ctx->ibuf_off += i;
499 if (flag || size == 0) {
500 *buf = '\0';
501 return (num);
502 }
503 }
492 else /* read another chunk */ 504 else /* read another chunk */
493 { 505 {
494 i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size); 506 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
495 if (i <= 0) 507 if (i <= 0) {
496 {
497 BIO_copy_next_retry(b); 508 BIO_copy_next_retry(b);
498 *buf='\0'; 509 *buf = '\0';
499 if (i < 0) return((num > 0)?num:i); 510 if (i < 0)
500 if (i == 0) return(num); 511 return ((num > 0) ? num : i);
501 } 512 if (i == 0)
502 ctx->ibuf_len=i; 513 return (num);
503 ctx->ibuf_off=0;
504 } 514 }
515 ctx->ibuf_len = i;
516 ctx->ibuf_off = 0;
505 } 517 }
506 } 518 }
519}
507 520
508static int buffer_puts(BIO *b, const char *str) 521static int
509 { 522buffer_puts(BIO *b, const char *str)
510 return(buffer_write(b,str,strlen(str))); 523{
511 } 524 return (buffer_write(b, str, strlen(str)));
512 525}