os/ossrv/ssl/libcrypto/src/crypto/bio/bf_lbuf.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/bio/bf_buff.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
#include <stdio.h>
sl@0
    60
#include <errno.h>
sl@0
    61
#include "cryptlib.h"
sl@0
    62
#include <openssl/bio.h>
sl@0
    63
#include <openssl/evp.h>
sl@0
    64
sl@0
    65
static int linebuffer_write(BIO *h, const char *buf,int num);
sl@0
    66
static int linebuffer_read(BIO *h, char *buf, int size);
sl@0
    67
static int linebuffer_puts(BIO *h, const char *str);
sl@0
    68
static int linebuffer_gets(BIO *h, char *str, int size);
sl@0
    69
static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
sl@0
    70
static int linebuffer_new(BIO *h);
sl@0
    71
static int linebuffer_free(BIO *data);
sl@0
    72
static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
sl@0
    73
sl@0
    74
/* A 10k maximum should be enough for most purposes */
sl@0
    75
#define DEFAULT_LINEBUFFER_SIZE	1024*10
sl@0
    76
sl@0
    77
/* #define DEBUG */
sl@0
    78
sl@0
    79
static BIO_METHOD methods_linebuffer=
sl@0
    80
	{
sl@0
    81
	BIO_TYPE_LINEBUFFER,
sl@0
    82
	"linebuffer",
sl@0
    83
	linebuffer_write,
sl@0
    84
	linebuffer_read,
sl@0
    85
	linebuffer_puts,
sl@0
    86
	linebuffer_gets,
sl@0
    87
	linebuffer_ctrl,
sl@0
    88
	linebuffer_new,
sl@0
    89
	linebuffer_free,
sl@0
    90
	linebuffer_callback_ctrl,
sl@0
    91
	};
sl@0
    92
sl@0
    93
BIO_METHOD *BIO_f_linebuffer(void)
sl@0
    94
	{
sl@0
    95
	return(&methods_linebuffer);
sl@0
    96
	}
sl@0
    97
sl@0
    98
typedef struct bio_linebuffer_ctx_struct
sl@0
    99
	{
sl@0
   100
	char *obuf;		/* the output char array */
sl@0
   101
	int obuf_size;		/* how big is the output buffer */
sl@0
   102
	int obuf_len;		/* how many bytes are in it */
sl@0
   103
	} BIO_LINEBUFFER_CTX;
sl@0
   104
sl@0
   105
static int linebuffer_new(BIO *bi)
sl@0
   106
	{
sl@0
   107
	BIO_LINEBUFFER_CTX *ctx;
sl@0
   108
sl@0
   109
	ctx=(BIO_LINEBUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_LINEBUFFER_CTX));
sl@0
   110
	if (ctx == NULL) return(0);
sl@0
   111
	ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
sl@0
   112
	if (ctx->obuf == NULL) { OPENSSL_free(ctx); return(0); }
sl@0
   113
	ctx->obuf_size=DEFAULT_LINEBUFFER_SIZE;
sl@0
   114
	ctx->obuf_len=0;
sl@0
   115
sl@0
   116
	bi->init=1;
sl@0
   117
	bi->ptr=(char *)ctx;
sl@0
   118
	bi->flags=0;
sl@0
   119
	return(1);
sl@0
   120
	}
sl@0
   121
sl@0
   122
static int linebuffer_free(BIO *a)
sl@0
   123
	{
sl@0
   124
	BIO_LINEBUFFER_CTX *b;
sl@0
   125
sl@0
   126
	if (a == NULL) return(0);
sl@0
   127
	b=(BIO_LINEBUFFER_CTX *)a->ptr;
sl@0
   128
	if (b->obuf != NULL) OPENSSL_free(b->obuf);
sl@0
   129
	OPENSSL_free(a->ptr);
sl@0
   130
	a->ptr=NULL;
sl@0
   131
	a->init=0;
sl@0
   132
	a->flags=0;
sl@0
   133
	return(1);
sl@0
   134
	}
sl@0
   135
	
sl@0
   136
static int linebuffer_read(BIO *b, char *out, int outl)
sl@0
   137
	{
sl@0
   138
	int ret=0;
sl@0
   139
 
sl@0
   140
	if (out == NULL) return(0);
sl@0
   141
	if (b->next_bio == NULL) return(0);
sl@0
   142
	ret=BIO_read(b->next_bio,out,outl);
sl@0
   143
	BIO_clear_retry_flags(b);
sl@0
   144
	BIO_copy_next_retry(b);
sl@0
   145
	return(ret);
sl@0
   146
	}
sl@0
   147
sl@0
   148
static int linebuffer_write(BIO *b, const char *in, int inl)
sl@0
   149
	{
sl@0
   150
	int i,num=0,foundnl;
sl@0
   151
	BIO_LINEBUFFER_CTX *ctx;
sl@0
   152
sl@0
   153
	if ((in == NULL) || (inl <= 0)) return(0);
sl@0
   154
	ctx=(BIO_LINEBUFFER_CTX *)b->ptr;
sl@0
   155
	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
sl@0
   156
sl@0
   157
	BIO_clear_retry_flags(b);
sl@0
   158
sl@0
   159
	do
sl@0
   160
		{
sl@0
   161
		const char *p;
sl@0
   162
sl@0
   163
		for(p = in; p < in + inl && *p != '\n'; p++)
sl@0
   164
			;
sl@0
   165
		if (*p == '\n')
sl@0
   166
			{
sl@0
   167
			p++;
sl@0
   168
			foundnl = 1;
sl@0
   169
			}
sl@0
   170
		else
sl@0
   171
			foundnl = 0;
sl@0
   172
sl@0
   173
		/* If a NL was found and we already have text in the save
sl@0
   174
		   buffer, concatenate them and write */
sl@0
   175
		while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
sl@0
   176
			&& ctx->obuf_len > 0)
sl@0
   177
			{
sl@0
   178
			int orig_olen = ctx->obuf_len;
sl@0
   179
			
sl@0
   180
			i = ctx->obuf_size - ctx->obuf_len;
sl@0
   181
			if (p - in > 0)
sl@0
   182
				{
sl@0
   183
				if (i >= p - in)
sl@0
   184
					{
sl@0
   185
					memcpy(&(ctx->obuf[ctx->obuf_len]),
sl@0
   186
						in,p - in);
sl@0
   187
					ctx->obuf_len += p - in;
sl@0
   188
					inl -= p - in;
sl@0
   189
					num += p - in;
sl@0
   190
					in = p;
sl@0
   191
					}
sl@0
   192
				else
sl@0
   193
					{
sl@0
   194
					memcpy(&(ctx->obuf[ctx->obuf_len]),
sl@0
   195
						in,i);
sl@0
   196
					ctx->obuf_len += i;
sl@0
   197
					inl -= i;
sl@0
   198
					in += i;
sl@0
   199
					num += i;
sl@0
   200
					}
sl@0
   201
				}
sl@0
   202
sl@0
   203
#if 0
sl@0
   204
BIO_write(b->next_bio, "<*<", 3);
sl@0
   205
#endif
sl@0
   206
			i=BIO_write(b->next_bio,
sl@0
   207
				ctx->obuf, ctx->obuf_len);
sl@0
   208
			if (i <= 0)
sl@0
   209
				{
sl@0
   210
				ctx->obuf_len = orig_olen;
sl@0
   211
				BIO_copy_next_retry(b);
sl@0
   212
sl@0
   213
#if 0
sl@0
   214
BIO_write(b->next_bio, ">*>", 3);
sl@0
   215
#endif
sl@0
   216
				if (i < 0) return((num > 0)?num:i);
sl@0
   217
				if (i == 0) return(num);
sl@0
   218
				}
sl@0
   219
#if 0
sl@0
   220
BIO_write(b->next_bio, ">*>", 3);
sl@0
   221
#endif
sl@0
   222
			if (i < ctx->obuf_len)
sl@0
   223
				memmove(ctx->obuf, ctx->obuf + i,
sl@0
   224
					ctx->obuf_len - i);
sl@0
   225
			ctx->obuf_len-=i;
sl@0
   226
			}
sl@0
   227
sl@0
   228
		/* Now that the save buffer is emptied, let's write the input
sl@0
   229
		   buffer if a NL was found and there is anything to write. */
sl@0
   230
		if ((foundnl || p - in > ctx->obuf_size) && p - in > 0)
sl@0
   231
			{
sl@0
   232
#if 0
sl@0
   233
BIO_write(b->next_bio, "<*<", 3);
sl@0
   234
#endif
sl@0
   235
			i=BIO_write(b->next_bio,in,p - in);
sl@0
   236
			if (i <= 0)
sl@0
   237
				{
sl@0
   238
				BIO_copy_next_retry(b);
sl@0
   239
#if 0
sl@0
   240
BIO_write(b->next_bio, ">*>", 3);
sl@0
   241
#endif
sl@0
   242
				if (i < 0) return((num > 0)?num:i);
sl@0
   243
				if (i == 0) return(num);
sl@0
   244
				}
sl@0
   245
#if 0
sl@0
   246
BIO_write(b->next_bio, ">*>", 3);
sl@0
   247
#endif
sl@0
   248
			num+=i;
sl@0
   249
			in+=i;
sl@0
   250
			inl-=i;
sl@0
   251
			}
sl@0
   252
		}
sl@0
   253
	while(foundnl && inl > 0);
sl@0
   254
	/* We've written as much as we can.  The rest of the input buffer, if
sl@0
   255
	   any, is text that doesn't and with a NL and therefore needs to be
sl@0
   256
	   saved for the next trip. */
sl@0
   257
	if (inl > 0)
sl@0
   258
		{
sl@0
   259
		memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
sl@0
   260
		ctx->obuf_len += inl;
sl@0
   261
		num += inl;
sl@0
   262
		}
sl@0
   263
	return num;
sl@0
   264
	}
sl@0
   265
sl@0
   266
static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
sl@0
   267
	{
sl@0
   268
	BIO *dbio;
sl@0
   269
	BIO_LINEBUFFER_CTX *ctx;
sl@0
   270
	long ret=1;
sl@0
   271
	char *p;
sl@0
   272
	int r;
sl@0
   273
	int obs;
sl@0
   274
sl@0
   275
	ctx=(BIO_LINEBUFFER_CTX *)b->ptr;
sl@0
   276
sl@0
   277
	switch (cmd)
sl@0
   278
		{
sl@0
   279
	case BIO_CTRL_RESET:
sl@0
   280
		ctx->obuf_len=0;
sl@0
   281
		if (b->next_bio == NULL) return(0);
sl@0
   282
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   283
		break;
sl@0
   284
	case BIO_CTRL_INFO:
sl@0
   285
		ret=(long)ctx->obuf_len;
sl@0
   286
		break;
sl@0
   287
	case BIO_CTRL_WPENDING:
sl@0
   288
		ret=(long)ctx->obuf_len;
sl@0
   289
		if (ret == 0)
sl@0
   290
			{
sl@0
   291
			if (b->next_bio == NULL) return(0);
sl@0
   292
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   293
			}
sl@0
   294
		break;
sl@0
   295
	case BIO_C_SET_BUFF_SIZE:
sl@0
   296
		obs=(int)num;
sl@0
   297
		p=ctx->obuf;
sl@0
   298
		if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size))
sl@0
   299
			{
sl@0
   300
			p=(char *)OPENSSL_malloc((int)num);
sl@0
   301
			if (p == NULL)
sl@0
   302
				goto malloc_error;
sl@0
   303
			}
sl@0
   304
		if (ctx->obuf != p)
sl@0
   305
			{
sl@0
   306
			if (ctx->obuf_len > obs)
sl@0
   307
				{
sl@0
   308
				ctx->obuf_len = obs;
sl@0
   309
				}
sl@0
   310
			memcpy(p, ctx->obuf, ctx->obuf_len);
sl@0
   311
			OPENSSL_free(ctx->obuf);
sl@0
   312
			ctx->obuf=p;
sl@0
   313
			ctx->obuf_size=obs;
sl@0
   314
			}
sl@0
   315
		break;
sl@0
   316
	case BIO_C_DO_STATE_MACHINE:
sl@0
   317
		if (b->next_bio == NULL) return(0);
sl@0
   318
		BIO_clear_retry_flags(b);
sl@0
   319
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   320
		BIO_copy_next_retry(b);
sl@0
   321
		break;
sl@0
   322
sl@0
   323
	case BIO_CTRL_FLUSH:
sl@0
   324
		if (b->next_bio == NULL) return(0);
sl@0
   325
		if (ctx->obuf_len <= 0)
sl@0
   326
			{
sl@0
   327
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   328
			break;
sl@0
   329
			}
sl@0
   330
sl@0
   331
		for (;;)
sl@0
   332
			{
sl@0
   333
			BIO_clear_retry_flags(b);
sl@0
   334
			if (ctx->obuf_len > 0)
sl@0
   335
				{
sl@0
   336
				r=BIO_write(b->next_bio,
sl@0
   337
					ctx->obuf, ctx->obuf_len);
sl@0
   338
#if 0
sl@0
   339
fprintf(stderr,"FLUSH %3d -> %3d\n",ctx->obuf_len,r);
sl@0
   340
#endif
sl@0
   341
				BIO_copy_next_retry(b);
sl@0
   342
				if (r <= 0) return((long)r);
sl@0
   343
				if (r < ctx->obuf_len)
sl@0
   344
					memmove(ctx->obuf, ctx->obuf + r,
sl@0
   345
						ctx->obuf_len - r);
sl@0
   346
				ctx->obuf_len-=r;
sl@0
   347
				}
sl@0
   348
			else
sl@0
   349
				{
sl@0
   350
				ctx->obuf_len=0;
sl@0
   351
				ret=1;
sl@0
   352
				break;
sl@0
   353
				}
sl@0
   354
			}
sl@0
   355
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   356
		break;
sl@0
   357
	case BIO_CTRL_DUP:
sl@0
   358
		dbio=(BIO *)ptr;
sl@0
   359
		if (	!BIO_set_write_buffer_size(dbio,ctx->obuf_size))
sl@0
   360
			ret=0;
sl@0
   361
		break;
sl@0
   362
	default:
sl@0
   363
		if (b->next_bio == NULL) return(0);
sl@0
   364
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   365
		break;
sl@0
   366
		}
sl@0
   367
	return(ret);
sl@0
   368
malloc_error:
sl@0
   369
	BIOerr(BIO_F_LINEBUFFER_CTRL,ERR_R_MALLOC_FAILURE);
sl@0
   370
	return(0);
sl@0
   371
	}
sl@0
   372
sl@0
   373
static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
sl@0
   374
	{
sl@0
   375
	long ret=1;
sl@0
   376
sl@0
   377
	if (b->next_bio == NULL) return(0);
sl@0
   378
	switch (cmd)
sl@0
   379
		{
sl@0
   380
	default:
sl@0
   381
		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
sl@0
   382
		break;
sl@0
   383
		}
sl@0
   384
	return(ret);
sl@0
   385
	}
sl@0
   386
sl@0
   387
static int linebuffer_gets(BIO *b, char *buf, int size)
sl@0
   388
	{
sl@0
   389
	if (b->next_bio == NULL) return(0);
sl@0
   390
	return(BIO_gets(b->next_bio,buf,size));
sl@0
   391
	}
sl@0
   392
sl@0
   393
static int linebuffer_puts(BIO *b, const char *str)
sl@0
   394
	{
sl@0
   395
	return(linebuffer_write(b,str,strlen(str)));
sl@0
   396
	}
sl@0
   397