bf_buff.c (59191) | bf_buff.c (68651) |
---|---|
1/* crypto/bio/bf_buff.c */ 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 * --- 48 unchanged lines hidden (view full) --- 57 */ 58 59#include <stdio.h> 60#include <errno.h> 61#include "cryptlib.h" 62#include <openssl/bio.h> 63#include <openssl/evp.h> 64 | 1/* crypto/bio/bf_buff.c */ 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 * --- 48 unchanged lines hidden (view full) --- 57 */ 58 59#include <stdio.h> 60#include <errno.h> 61#include "cryptlib.h" 62#include <openssl/bio.h> 63#include <openssl/evp.h> 64 |
65static int buffer_write(BIO *h,char *buf,int num); 66static int buffer_read(BIO *h,char *buf,int size); 67static int buffer_puts(BIO *h,char *str); 68static int buffer_gets(BIO *h,char *str,int size); 69static long buffer_ctrl(BIO *h,int cmd,long arg1,char *arg2); | 65static int buffer_write(BIO *h, const char *buf,int num); 66static int buffer_read(BIO *h, char *buf, int size); 67static int buffer_puts(BIO *h, const char *str); 68static int buffer_gets(BIO *h, char *str, int size); 69static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
70static int buffer_new(BIO *h); 71static int buffer_free(BIO *data); | 70static int buffer_new(BIO *h); 71static int buffer_free(BIO *data); |
72static long buffer_callback_ctrl(BIO *h,int cmd, void (*fp)()); | 72static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); |
73#define DEFAULT_BUFFER_SIZE 1024 74 75static BIO_METHOD methods_buffer= 76 { 77 BIO_TYPE_BUFFER, 78 "buffer", 79 buffer_write, 80 buffer_read, --- 9 unchanged lines hidden (view full) --- 90 { 91 return(&methods_buffer); 92 } 93 94static int buffer_new(BIO *bi) 95 { 96 BIO_F_BUFFER_CTX *ctx; 97 | 73#define DEFAULT_BUFFER_SIZE 1024 74 75static BIO_METHOD methods_buffer= 76 { 77 BIO_TYPE_BUFFER, 78 "buffer", 79 buffer_write, 80 buffer_read, --- 9 unchanged lines hidden (view full) --- 90 { 91 return(&methods_buffer); 92 } 93 94static int buffer_new(BIO *bi) 95 { 96 BIO_F_BUFFER_CTX *ctx; 97 |
98 ctx=(BIO_F_BUFFER_CTX *)Malloc(sizeof(BIO_F_BUFFER_CTX)); | 98 ctx=(BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX)); |
99 if (ctx == NULL) return(0); | 99 if (ctx == NULL) return(0); |
100 ctx->ibuf=(char *)Malloc(DEFAULT_BUFFER_SIZE); 101 if (ctx->ibuf == NULL) { Free(ctx); return(0); } 102 ctx->obuf=(char *)Malloc(DEFAULT_BUFFER_SIZE); 103 if (ctx->obuf == NULL) { Free(ctx->ibuf); Free(ctx); return(0); } | 100 ctx->ibuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE); 101 if (ctx->ibuf == NULL) { OPENSSL_free(ctx); return(0); } 102 ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE); 103 if (ctx->obuf == NULL) { OPENSSL_free(ctx->ibuf); OPENSSL_free(ctx); return(0); } |
104 ctx->ibuf_size=DEFAULT_BUFFER_SIZE; 105 ctx->obuf_size=DEFAULT_BUFFER_SIZE; 106 ctx->ibuf_len=0; 107 ctx->ibuf_off=0; 108 ctx->obuf_len=0; 109 ctx->obuf_off=0; 110 111 bi->init=1; 112 bi->ptr=(char *)ctx; 113 bi->flags=0; 114 return(1); 115 } 116 117static int buffer_free(BIO *a) 118 { 119 BIO_F_BUFFER_CTX *b; 120 121 if (a == NULL) return(0); 122 b=(BIO_F_BUFFER_CTX *)a->ptr; | 104 ctx->ibuf_size=DEFAULT_BUFFER_SIZE; 105 ctx->obuf_size=DEFAULT_BUFFER_SIZE; 106 ctx->ibuf_len=0; 107 ctx->ibuf_off=0; 108 ctx->obuf_len=0; 109 ctx->obuf_off=0; 110 111 bi->init=1; 112 bi->ptr=(char *)ctx; 113 bi->flags=0; 114 return(1); 115 } 116 117static int buffer_free(BIO *a) 118 { 119 BIO_F_BUFFER_CTX *b; 120 121 if (a == NULL) return(0); 122 b=(BIO_F_BUFFER_CTX *)a->ptr; |
123 if (b->ibuf != NULL) Free(b->ibuf); 124 if (b->obuf != NULL) Free(b->obuf); 125 Free(a->ptr); | 123 if (b->ibuf != NULL) OPENSSL_free(b->ibuf); 124 if (b->obuf != NULL) OPENSSL_free(b->obuf); 125 OPENSSL_free(a->ptr); |
126 a->ptr=NULL; 127 a->init=0; 128 a->flags=0; 129 return(1); 130 } 131 132static int buffer_read(BIO *b, char *out, int outl) 133 { --- 56 unchanged lines hidden (view full) --- 190 } 191 ctx->ibuf_off=0; 192 ctx->ibuf_len=i; 193 194 /* Lets re-read using ourselves :-) */ 195 goto start; 196 } 197 | 126 a->ptr=NULL; 127 a->init=0; 128 a->flags=0; 129 return(1); 130 } 131 132static int buffer_read(BIO *b, char *out, int outl) 133 { --- 56 unchanged lines hidden (view full) --- 190 } 191 ctx->ibuf_off=0; 192 ctx->ibuf_len=i; 193 194 /* Lets re-read using ourselves :-) */ 195 goto start; 196 } 197 |
198static int buffer_write(BIO *b, char *in, int inl) | 198static int buffer_write(BIO *b, const char *in, int inl) |
199 { 200 int i,num=0; 201 BIO_F_BUFFER_CTX *ctx; 202 203 if ((in == NULL) || (inl <= 0)) return(0); 204 ctx=(BIO_F_BUFFER_CTX *)b->ptr; 205 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 206 --- 56 unchanged lines hidden (view full) --- 263 if (inl == 0) return(num); 264 } 265 266 /* copy the rest into the buffer since we have only a small 267 * amount left */ 268 goto start; 269 } 270 | 199 { 200 int i,num=0; 201 BIO_F_BUFFER_CTX *ctx; 202 203 if ((in == NULL) || (inl <= 0)) return(0); 204 ctx=(BIO_F_BUFFER_CTX *)b->ptr; 205 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 206 --- 56 unchanged lines hidden (view full) --- 263 if (inl == 0) return(num); 264 } 265 266 /* copy the rest into the buffer since we have only a small 267 * amount left */ 268 goto start; 269 } 270 |
271static long buffer_ctrl(BIO *b, int cmd, long num, char *ptr) | 271static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr) |
272 { 273 BIO *dbio; 274 BIO_F_BUFFER_CTX *ctx; 275 long ret=1; 276 char *p1,*p2; 277 int r,i,*ip; 278 int ibs,obs; 279 --- 34 unchanged lines hidden (view full) --- 314 { 315 if (b->next_bio == NULL) return(0); 316 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 317 } 318 break; 319 case BIO_C_SET_BUFF_READ_DATA: 320 if (num > ctx->ibuf_size) 321 { | 272 { 273 BIO *dbio; 274 BIO_F_BUFFER_CTX *ctx; 275 long ret=1; 276 char *p1,*p2; 277 int r,i,*ip; 278 int ibs,obs; 279 --- 34 unchanged lines hidden (view full) --- 314 { 315 if (b->next_bio == NULL) return(0); 316 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 317 } 318 break; 319 case BIO_C_SET_BUFF_READ_DATA: 320 if (num > ctx->ibuf_size) 321 { |
322 p1=Malloc((int)num); | 322 p1=OPENSSL_malloc((int)num); |
323 if (p1 == NULL) goto malloc_error; | 323 if (p1 == NULL) goto malloc_error; |
324 if (ctx->ibuf != NULL) Free(ctx->ibuf); | 324 if (ctx->ibuf != NULL) OPENSSL_free(ctx->ibuf); |
325 ctx->ibuf=p1; 326 } 327 ctx->ibuf_off=0; 328 ctx->ibuf_len=(int)num; 329 memcpy(ctx->ibuf,ptr,(int)num); 330 ret=1; 331 break; 332 case BIO_C_SET_BUFF_SIZE: --- 15 unchanged lines hidden (view full) --- 348 { 349 ibs=(int)num; 350 obs=(int)num; 351 } 352 p1=ctx->ibuf; 353 p2=ctx->obuf; 354 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) 355 { | 325 ctx->ibuf=p1; 326 } 327 ctx->ibuf_off=0; 328 ctx->ibuf_len=(int)num; 329 memcpy(ctx->ibuf,ptr,(int)num); 330 ret=1; 331 break; 332 case BIO_C_SET_BUFF_SIZE: --- 15 unchanged lines hidden (view full) --- 348 { 349 ibs=(int)num; 350 obs=(int)num; 351 } 352 p1=ctx->ibuf; 353 p2=ctx->obuf; 354 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) 355 { |
356 p1=(char *)Malloc((int)num); | 356 p1=(char *)OPENSSL_malloc((int)num); |
357 if (p1 == NULL) goto malloc_error; 358 } 359 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) 360 { | 357 if (p1 == NULL) goto malloc_error; 358 } 359 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) 360 { |
361 p2=(char *)Malloc((int)num); | 361 p2=(char *)OPENSSL_malloc((int)num); |
362 if (p2 == NULL) 363 { | 362 if (p2 == NULL) 363 { |
364 if (p1 != ctx->ibuf) Free(p1); | 364 if (p1 != ctx->ibuf) OPENSSL_free(p1); |
365 goto malloc_error; 366 } 367 } 368 if (ctx->ibuf != p1) 369 { | 365 goto malloc_error; 366 } 367 } 368 if (ctx->ibuf != p1) 369 { |
370 Free(ctx->ibuf); | 370 OPENSSL_free(ctx->ibuf); |
371 ctx->ibuf=p1; 372 ctx->ibuf_off=0; 373 ctx->ibuf_len=0; 374 ctx->ibuf_size=ibs; 375 } 376 if (ctx->obuf != p2) 377 { | 371 ctx->ibuf=p1; 372 ctx->ibuf_off=0; 373 ctx->ibuf_len=0; 374 ctx->ibuf_size=ibs; 375 } 376 if (ctx->obuf != p2) 377 { |
378 Free(ctx->obuf); | 378 OPENSSL_free(ctx->obuf); |
379 ctx->obuf=p2; 380 ctx->obuf_off=0; 381 ctx->obuf_len=0; 382 ctx->obuf_size=obs; 383 } 384 break; 385 case BIO_C_DO_STATE_MACHINE: 386 if (b->next_bio == NULL) return(0); --- 47 unchanged lines hidden (view full) --- 434 break; 435 } 436 return(ret); 437malloc_error: 438 BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE); 439 return(0); 440 } 441 | 379 ctx->obuf=p2; 380 ctx->obuf_off=0; 381 ctx->obuf_len=0; 382 ctx->obuf_size=obs; 383 } 384 break; 385 case BIO_C_DO_STATE_MACHINE: 386 if (b->next_bio == NULL) return(0); --- 47 unchanged lines hidden (view full) --- 434 break; 435 } 436 return(ret); 437malloc_error: 438 BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE); 439 return(0); 440 } 441 |
442static long buffer_callback_ctrl(BIO *b, int cmd, void (*fp)()) | 442static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) |
443 { 444 long ret=1; 445 446 if (b->next_bio == NULL) return(0); 447 switch (cmd) 448 { 449 default: 450 ret=BIO_callback_ctrl(b->next_bio,cmd,fp); --- 48 unchanged lines hidden (view full) --- 499 if (i == 0) return(num); 500 } 501 ctx->ibuf_len=i; 502 ctx->ibuf_off=0; 503 } 504 } 505 } 506 | 443 { 444 long ret=1; 445 446 if (b->next_bio == NULL) return(0); 447 switch (cmd) 448 { 449 default: 450 ret=BIO_callback_ctrl(b->next_bio,cmd,fp); --- 48 unchanged lines hidden (view full) --- 499 if (i == 0) return(num); 500 } 501 ctx->ibuf_len=i; 502 ctx->ibuf_off=0; 503 } 504 } 505 } 506 |
507static int buffer_puts(BIO *b, char *str) | 507static int buffer_puts(BIO *b, const char *str) |
508 { | 508 { |
509 return(BIO_write(b,str,strlen(str))); | 509 return(buffer_write(b,str,strlen(str))); |
510 } 511 | 510 } 511 |