Commit d3442bc7 authored by Richard Levitte's avatar Richard Levitte
Browse files

Move the registration of callback functions to special functions

designed for that.  This removes the potential error to mix data and
function pointers.

Please note that I'm a little unsure how incorrect calls to the old
ctrl functions should be handled, in som cases.  I currently return 0
and that's it, but it may be more correct to generate a genuine error
in those cases.
parent dab6f095
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ static int buffer_gets(BIO *h,char *str,int size);
static long buffer_ctrl(BIO *h,int cmd,long arg1,char *arg2);
static int buffer_new(BIO *h);
static int buffer_free(BIO *data);
static long buffer_callback_ctrl(BIO *h,int cmd, void (*fp)());
#define DEFAULT_BUFFER_SIZE	1024

static BIO_METHOD methods_buffer=
@@ -82,6 +83,7 @@ static BIO_METHOD methods_buffer=
	buffer_ctrl,
	buffer_new,
	buffer_free,
	buffer_callback_ctrl,
	};

BIO_METHOD *BIO_f_buffer(void)
@@ -284,6 +286,7 @@ static long buffer_ctrl(BIO *b, int cmd, long num, char *ptr)
		ctx->ibuf_len=0;
		ctx->obuf_off=0;
		ctx->obuf_len=0;
		if (b->next_bio == NULL) return(0);
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
		break;
	case BIO_CTRL_INFO:
@@ -300,12 +303,18 @@ static long buffer_ctrl(BIO *b, int cmd, long num, char *ptr)
	case BIO_CTRL_WPENDING:
		ret=(long)ctx->obuf_len;
		if (ret == 0)
			{
			if (b->next_bio == NULL) return(0);
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
			}
		break;
	case BIO_CTRL_PENDING:
		ret=(long)ctx->ibuf_len;
		if (ret == 0)
			{
			if (b->next_bio == NULL) return(0);
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
			}
		break;
	case BIO_C_SET_BUFF_READ_DATA:
		if (num > ctx->ibuf_size)
@@ -374,12 +383,14 @@ static long buffer_ctrl(BIO *b, int cmd, long num, char *ptr)
			}
		break;
	case BIO_C_DO_STATE_MACHINE:
		if (b->next_bio == NULL) return(0);
		BIO_clear_retry_flags(b);
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
		BIO_copy_next_retry(b);
		break;

	case BIO_CTRL_FLUSH:
		if (b->next_bio == NULL) return(0);
		if (ctx->obuf_len <= 0)
			{
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
@@ -418,6 +429,7 @@ fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_
			ret=0;
		break;
	default:
		if (b->next_bio == NULL) return(0);
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
		break;
		}
@@ -427,6 +439,20 @@ malloc_error:
	return(0);
	}

static long buffer_callback_ctrl(BIO *b, int cmd, void (*fp)())
	{
	long ret=1;

	if (b->next_bio == NULL) return(0);
	switch (cmd)
		{
	default:
		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
		break;
		}
	return(ret);
	}

static int buffer_gets(BIO *b, char *buf, int size)
	{
	BIO_F_BUFFER_CTX *ctx;
+16 −0
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ static int nbiof_gets(BIO *h,char *str,int size);
static long nbiof_ctrl(BIO *h,int cmd,long arg1,char *arg2);
static int nbiof_new(BIO *h);
static int nbiof_free(BIO *data);
static long nbiof_callback_ctrl(BIO *h,int cmd,void (*fp)());
typedef struct nbio_test_st
	{
	/* only set if we sent a 'should retry' error */
@@ -91,6 +92,7 @@ static BIO_METHOD methods_nbiof=
	nbiof_ctrl,
	nbiof_new,
	nbiof_free,
	nbiof_callback_ctrl,
	};

BIO_METHOD *BIO_f_nbio_test(void)
@@ -224,6 +226,20 @@ static long nbiof_ctrl(BIO *b, int cmd, long num, char *ptr)
	return(ret);
	}

static long nbiof_callback_ctrl(BIO *b, int cmd, void (*fp)())
	{
	long ret=1;

	if (b->next_bio == NULL) return(0);
	switch (cmd)
		{
	default:
		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
		break;
		}
	return(ret);
	}

static int nbiof_gets(BIO *bp, char *buf, int size)
	{
	if (bp->next_bio == NULL) return(0);
+16 −0
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ static int nullf_gets(BIO *h,char *str,int size);
static long nullf_ctrl(BIO *h,int cmd,long arg1,char *arg2);
static int nullf_new(BIO *h);
static int nullf_free(BIO *data);
static long nullf_callback_ctrl(BIO *h,int cmd,void (*fp)());
static BIO_METHOD methods_nullf=
	{
	BIO_TYPE_NULL_FILTER,
@@ -83,6 +84,7 @@ static BIO_METHOD methods_nullf=
	nullf_ctrl,
	nullf_new,
	nullf_free,
	nullf_callback_ctrl,
	};

BIO_METHOD *BIO_f_null(void)
@@ -152,6 +154,20 @@ static long nullf_ctrl(BIO *b, int cmd, long num, char *ptr)
	return(ret);
	}

static long nullf_callback_ctrl(BIO *b, int cmd, void (*fp)())
	{
	long ret=1;

	if (b->next_bio == NULL) return(0);
	switch (cmd)
		{
	default:
		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
		break;
		}
	return(ret);
	}

static int nullf_gets(BIO *bp, char *buf, int size)
	{
	if (bp->next_bio == NULL) return(0);
+6 −3
Original line number Diff line number Diff line
@@ -219,6 +219,7 @@ typedef struct bio_method_st
	long (*ctrl)();
	int (*create)();
	int (*destroy)();
	long (*callback_ctrl)();
	} BIO_METHOD;
#else
typedef struct bio_method_st
@@ -232,6 +233,7 @@ typedef struct bio_method_st
	long (_far *ctrl)();
	int (_far *create)();
	int (_far *destroy)();
	long (_fat *callback_ctrl)();
	} BIO_METHOD;
#endif

@@ -373,7 +375,7 @@ typedef struct bio_f_buffer_ctx_struct
/* BIO_set_nbio(b,n) */
#define BIO_set_filter_bio(b,s) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,2,(char *)(s))
/* BIO *BIO_get_filter_bio(BIO *bio); */
#define BIO_set_proxy_cb(b,cb) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(char *)(cb))
#define BIO_set_proxy_cb(b,cb) BIO_callback_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(void *(*cb)()))
#define BIO_set_proxy_header(b,sk) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,4,(char *)sk)
#define BIO_set_no_connect_return(b,bool) BIO_int_ctrl(b,BIO_C_SET_PROXY_PARAM,5,bool)

@@ -452,8 +454,8 @@ int BIO_read_filename(BIO *b,const char *name);
size_t BIO_ctrl_pending(BIO *b);
size_t BIO_ctrl_wpending(BIO *b);
#define BIO_flush(b)		(int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL)
#define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0,(char *)cbp)
#define BIO_set_info_callback(b,cb) (int)BIO_ctrl(b,BIO_CTRL_SET_CALLBACK,0,(char *)cb)
#define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0,(void (**)())(cbp))
#define BIO_set_info_callback(b,cb) (int)BIO_callback_ctrl(b,BIO_CTRL_SET_CALLBACK,(void (*)())(cb))

/* For the BIO_f_buffer() type */
#define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL)
@@ -508,6 +510,7 @@ int BIO_gets(BIO *bp,char *buf, int size);
int	BIO_write(BIO *b, const char *data, int len);
int	BIO_puts(BIO *bp,const char *buf);
long	BIO_ctrl(BIO *bp,int cmd,long larg,void *parg);
long	BIO_callback_ctrl(BIO *bp,int cmd,void (*fp)());
char *	BIO_ptr_ctrl(BIO *bp,int cmd,long larg);
long	BIO_int_ctrl(BIO *bp,int cmd,long larg,int iarg);
BIO *	BIO_push(BIO *b,BIO *append);
+29 −2
Original line number Diff line number Diff line
@@ -317,6 +317,33 @@ long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
	return(ret);
	}

long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)())
	{
	long ret;
	long (*cb)();

	if (b == NULL) return(0);

	if ((b->method == NULL) || (b->method->callback_ctrl == NULL))
		{
		BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD);
		return(-2);
		}

	cb=b->callback;

	if ((cb != NULL) &&
		((ret=cb(b,BIO_CB_CTRL,(void *)&fp,cmd,0,1L)) <= 0))
		return(ret);

	ret=b->method->callback_ctrl(b,cmd,fp);

	if (cb != NULL)
		ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,(void *)&fp,cmd,
			0,ret);
	return(ret);
	}

/* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
 * do; but those macros have inappropriate return type, and for interfacing
 * from other programming languages, C macros aren't much of a help anyway. */
Loading