os/ossrv/ssl/libcrypto/src/crypto/evp/bio_b64.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/evp/bio_b64.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
/*
sl@0
    60
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    61
 */
sl@0
    62
sl@0
    63
sl@0
    64
#include <stdio.h>
sl@0
    65
#include <errno.h>
sl@0
    66
#include "cryptlib.h"
sl@0
    67
#include <openssl/buffer.h>
sl@0
    68
#include <openssl/evp.h>
sl@0
    69
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    70
#include "libcrypto_wsd_macros.h"
sl@0
    71
#include "libcrypto_wsd.h"
sl@0
    72
#endif
sl@0
    73
sl@0
    74
static int b64_write(BIO *h, const char *buf, int num);
sl@0
    75
static int b64_read(BIO *h, char *buf, int size);
sl@0
    76
/*static int b64_puts(BIO *h, const char *str); */
sl@0
    77
/*static int b64_gets(BIO *h, char *str, int size); */
sl@0
    78
static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
sl@0
    79
static int b64_new(BIO *h);
sl@0
    80
static int b64_free(BIO *data);
sl@0
    81
static long b64_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
sl@0
    82
#define B64_BLOCK_SIZE	1024
sl@0
    83
#define B64_BLOCK_SIZE2	768
sl@0
    84
#define B64_NONE	0
sl@0
    85
#define B64_ENCODE	1
sl@0
    86
#define B64_DECODE	2
sl@0
    87
sl@0
    88
typedef struct b64_struct
sl@0
    89
	{
sl@0
    90
	/*BIO *bio; moved to the BIO structure */
sl@0
    91
	int buf_len;
sl@0
    92
	int buf_off;
sl@0
    93
	int tmp_len;		/* used to find the start when decoding */
sl@0
    94
	int tmp_nl;		/* If true, scan until '\n' */
sl@0
    95
	int encode;
sl@0
    96
	int start;		/* have we started decoding yet? */
sl@0
    97
	int cont;		/* <= 0 when finished */
sl@0
    98
	EVP_ENCODE_CTX base64;
sl@0
    99
	char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE)+10];
sl@0
   100
	char tmp[B64_BLOCK_SIZE];
sl@0
   101
	} BIO_B64_CTX;
sl@0
   102
sl@0
   103
#ifndef EMULATOR
sl@0
   104
static BIO_METHOD methods_b64=
sl@0
   105
	{
sl@0
   106
	BIO_TYPE_BASE64,"base64 encoding",
sl@0
   107
	b64_write,
sl@0
   108
	b64_read,
sl@0
   109
	NULL, /* b64_puts, */
sl@0
   110
	NULL, /* b64_gets, */
sl@0
   111
	b64_ctrl,
sl@0
   112
	b64_new,
sl@0
   113
	b64_free,
sl@0
   114
	b64_callback_ctrl,
sl@0
   115
	};
sl@0
   116
#else
sl@0
   117
GET_STATIC_VAR_FROM_TLS(methods_b64,bio_b64,BIO_METHOD)
sl@0
   118
#define methods_b64 (*GET_WSD_VAR_NAME(methods_b64,bio_b64, s)())
sl@0
   119
const BIO_METHOD temp_s_methods_b64=
sl@0
   120
	{
sl@0
   121
	BIO_TYPE_BASE64,"base64 encoding",
sl@0
   122
	b64_write,
sl@0
   123
	b64_read,
sl@0
   124
	NULL, /* b64_puts, */
sl@0
   125
	NULL, /* b64_gets, */
sl@0
   126
	b64_ctrl,
sl@0
   127
	b64_new,
sl@0
   128
	b64_free,
sl@0
   129
	b64_callback_ctrl,
sl@0
   130
	};
sl@0
   131
#endif
sl@0
   132
EXPORT_C BIO_METHOD *BIO_f_base64(void)
sl@0
   133
	{
sl@0
   134
	return(&methods_b64);
sl@0
   135
	}
sl@0
   136
sl@0
   137
static int b64_new(BIO *bi)
sl@0
   138
	{
sl@0
   139
	BIO_B64_CTX *ctx;
sl@0
   140
sl@0
   141
	ctx=(BIO_B64_CTX *)OPENSSL_malloc(sizeof(BIO_B64_CTX));
sl@0
   142
	if (ctx == NULL) return(0);
sl@0
   143
sl@0
   144
	ctx->buf_len=0;
sl@0
   145
	ctx->tmp_len=0;
sl@0
   146
	ctx->tmp_nl=0;
sl@0
   147
	ctx->buf_off=0;
sl@0
   148
	ctx->cont=1;
sl@0
   149
	ctx->start=1;
sl@0
   150
	ctx->encode=0;
sl@0
   151
sl@0
   152
	bi->init=1;
sl@0
   153
	bi->ptr=(char *)ctx;
sl@0
   154
	bi->flags=0;
sl@0
   155
	return(1);
sl@0
   156
	}
sl@0
   157
sl@0
   158
static int b64_free(BIO *a)
sl@0
   159
	{
sl@0
   160
	if (a == NULL) return(0);
sl@0
   161
	OPENSSL_free(a->ptr);
sl@0
   162
	a->ptr=NULL;
sl@0
   163
	a->init=0;
sl@0
   164
	a->flags=0;
sl@0
   165
	return(1);
sl@0
   166
	}
sl@0
   167
	
sl@0
   168
static int b64_read(BIO *b, char *out, int outl)
sl@0
   169
	{
sl@0
   170
	int ret=0,i,ii,j,k,x,n,num,ret_code=0;
sl@0
   171
	BIO_B64_CTX *ctx;
sl@0
   172
	unsigned char *p,*q;
sl@0
   173
sl@0
   174
	if (out == NULL) return(0);
sl@0
   175
	ctx=(BIO_B64_CTX *)b->ptr;
sl@0
   176
sl@0
   177
	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
sl@0
   178
sl@0
   179
	if (ctx->encode != B64_DECODE)
sl@0
   180
		{
sl@0
   181
		ctx->encode=B64_DECODE;
sl@0
   182
		ctx->buf_len=0;
sl@0
   183
		ctx->buf_off=0;
sl@0
   184
		ctx->tmp_len=0;
sl@0
   185
		EVP_DecodeInit(&(ctx->base64));
sl@0
   186
		}
sl@0
   187
sl@0
   188
	/* First check if there are bytes decoded/encoded */
sl@0
   189
	if (ctx->buf_len > 0)
sl@0
   190
		{
sl@0
   191
		i=ctx->buf_len-ctx->buf_off;
sl@0
   192
		if (i > outl) i=outl;
sl@0
   193
		OPENSSL_assert(ctx->buf_off+i < (int)sizeof(ctx->buf));
sl@0
   194
		memcpy(out,&(ctx->buf[ctx->buf_off]),i);
sl@0
   195
		ret=i;
sl@0
   196
		out+=i;
sl@0
   197
		outl-=i;
sl@0
   198
		ctx->buf_off+=i;
sl@0
   199
		if (ctx->buf_len == ctx->buf_off)
sl@0
   200
			{
sl@0
   201
			ctx->buf_len=0;
sl@0
   202
			ctx->buf_off=0;
sl@0
   203
			}
sl@0
   204
		}
sl@0
   205
sl@0
   206
	/* At this point, we have room of outl bytes and an empty
sl@0
   207
	 * buffer, so we should read in some more. */
sl@0
   208
sl@0
   209
	ret_code=0;
sl@0
   210
	while (outl > 0)
sl@0
   211
		{
sl@0
   212
sl@0
   213
		if (ctx->cont <= 0)
sl@0
   214
			break;
sl@0
   215
sl@0
   216
		i=BIO_read(b->next_bio,&(ctx->tmp[ctx->tmp_len]),
sl@0
   217
			B64_BLOCK_SIZE-ctx->tmp_len);
sl@0
   218
sl@0
   219
		if (i <= 0)
sl@0
   220
			{
sl@0
   221
			ret_code=i;
sl@0
   222
sl@0
   223
			/* Should be continue next time we are called? */
sl@0
   224
			if (!BIO_should_retry(b->next_bio))
sl@0
   225
				{
sl@0
   226
				ctx->cont=i;
sl@0
   227
				/* If buffer empty break */
sl@0
   228
				if(ctx->tmp_len == 0)
sl@0
   229
					break;
sl@0
   230
				/* Fall through and process what we have */
sl@0
   231
				else
sl@0
   232
					i = 0;
sl@0
   233
				}
sl@0
   234
			/* else we retry and add more data to buffer */
sl@0
   235
			else
sl@0
   236
				break;
sl@0
   237
			}
sl@0
   238
		i+=ctx->tmp_len;
sl@0
   239
		ctx->tmp_len = i;
sl@0
   240
sl@0
   241
		/* We need to scan, a line at a time until we
sl@0
   242
		 * have a valid line if we are starting. */
sl@0
   243
		if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL))
sl@0
   244
			{
sl@0
   245
			/* ctx->start=1; */
sl@0
   246
			ctx->tmp_len=0;
sl@0
   247
			}
sl@0
   248
		else if (ctx->start)
sl@0
   249
			{
sl@0
   250
			q=p=(unsigned char *)ctx->tmp;
sl@0
   251
			for (j=0; j<i; j++)
sl@0
   252
				{
sl@0
   253
				if (*(q++) != '\n') continue;
sl@0
   254
sl@0
   255
				/* due to a previous very long line,
sl@0
   256
				 * we need to keep on scanning for a '\n'
sl@0
   257
				 * before we even start looking for
sl@0
   258
				 * base64 encoded stuff. */
sl@0
   259
				if (ctx->tmp_nl)
sl@0
   260
					{
sl@0
   261
					p=q;
sl@0
   262
					ctx->tmp_nl=0;
sl@0
   263
					continue;
sl@0
   264
					}
sl@0
   265
sl@0
   266
				k=EVP_DecodeUpdate(&(ctx->base64),
sl@0
   267
					(unsigned char *)ctx->buf,
sl@0
   268
					&num,p,q-p);
sl@0
   269
				if ((k <= 0) && (num == 0) && (ctx->start))
sl@0
   270
					EVP_DecodeInit(&ctx->base64);
sl@0
   271
				else 
sl@0
   272
					{
sl@0
   273
					if (p != (unsigned char *)
sl@0
   274
						&(ctx->tmp[0]))
sl@0
   275
						{
sl@0
   276
						i-=(p- (unsigned char *)
sl@0
   277
							&(ctx->tmp[0]));
sl@0
   278
						for (x=0; x < i; x++)
sl@0
   279
							ctx->tmp[x]=p[x];
sl@0
   280
						}
sl@0
   281
					EVP_DecodeInit(&ctx->base64);
sl@0
   282
					ctx->start=0;
sl@0
   283
					break;
sl@0
   284
					}
sl@0
   285
				p=q;
sl@0
   286
				}
sl@0
   287
sl@0
   288
			/* we fell off the end without starting */
sl@0
   289
			if (j == i)
sl@0
   290
				{
sl@0
   291
				/* Is this is one long chunk?, if so, keep on
sl@0
   292
				 * reading until a new line. */
sl@0
   293
				if (p == (unsigned char *)&(ctx->tmp[0]))
sl@0
   294
					{
sl@0
   295
					/* Check buffer full */
sl@0
   296
					if (i == B64_BLOCK_SIZE)
sl@0
   297
						{
sl@0
   298
						ctx->tmp_nl=1;
sl@0
   299
						ctx->tmp_len=0;
sl@0
   300
						}
sl@0
   301
					}
sl@0
   302
				else if (p != q) /* finished on a '\n' */
sl@0
   303
					{
sl@0
   304
					n=q-p;
sl@0
   305
					for (ii=0; ii<n; ii++)
sl@0
   306
						ctx->tmp[ii]=p[ii];
sl@0
   307
					ctx->tmp_len=n;
sl@0
   308
					}
sl@0
   309
				/* else finished on a '\n' */
sl@0
   310
				continue;
sl@0
   311
				}
sl@0
   312
			else
sl@0
   313
				ctx->tmp_len=0;
sl@0
   314
			}
sl@0
   315
		/* If buffer isn't full and we can retry then
sl@0
   316
		 * restart to read in more data.
sl@0
   317
		 */
sl@0
   318
		else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0))
sl@0
   319
			continue;
sl@0
   320
sl@0
   321
		if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
sl@0
   322
			{
sl@0
   323
			int z,jj;
sl@0
   324
sl@0
   325
			jj=(i>>2)<<2;
sl@0
   326
			z=EVP_DecodeBlock((unsigned char *)ctx->buf,
sl@0
   327
				(unsigned char *)ctx->tmp,jj);
sl@0
   328
			if (jj > 2)
sl@0
   329
				{
sl@0
   330
				if (ctx->tmp[jj-1] == '=')
sl@0
   331
					{
sl@0
   332
					z--;
sl@0
   333
					if (ctx->tmp[jj-2] == '=')
sl@0
   334
						z--;
sl@0
   335
					}
sl@0
   336
				}
sl@0
   337
			/* z is now number of output bytes and jj is the
sl@0
   338
			 * number consumed */
sl@0
   339
			if (jj != i)
sl@0
   340
				{
sl@0
   341
				memcpy((unsigned char *)ctx->tmp,
sl@0
   342
					(unsigned char *)&(ctx->tmp[jj]),i-jj);
sl@0
   343
				ctx->tmp_len=i-jj;
sl@0
   344
				}
sl@0
   345
			ctx->buf_len=0;
sl@0
   346
			if (z > 0)
sl@0
   347
				{
sl@0
   348
				ctx->buf_len=z;
sl@0
   349
				i=1;
sl@0
   350
				}
sl@0
   351
			else
sl@0
   352
				i=z;
sl@0
   353
			}
sl@0
   354
		else
sl@0
   355
			{
sl@0
   356
			i=EVP_DecodeUpdate(&(ctx->base64),
sl@0
   357
				(unsigned char *)ctx->buf,&ctx->buf_len,
sl@0
   358
				(unsigned char *)ctx->tmp,i);
sl@0
   359
			ctx->tmp_len = 0;
sl@0
   360
			}
sl@0
   361
		ctx->buf_off=0;
sl@0
   362
		if (i < 0)
sl@0
   363
			{
sl@0
   364
			ret_code=0;
sl@0
   365
			ctx->buf_len=0;
sl@0
   366
			break;
sl@0
   367
			}
sl@0
   368
sl@0
   369
		if (ctx->buf_len <= outl)
sl@0
   370
			i=ctx->buf_len;
sl@0
   371
		else
sl@0
   372
			i=outl;
sl@0
   373
sl@0
   374
		memcpy(out,ctx->buf,i);
sl@0
   375
		ret+=i;
sl@0
   376
		ctx->buf_off=i;
sl@0
   377
		if (ctx->buf_off == ctx->buf_len)
sl@0
   378
			{
sl@0
   379
			ctx->buf_len=0;
sl@0
   380
			ctx->buf_off=0;
sl@0
   381
			}
sl@0
   382
		outl-=i;
sl@0
   383
		out+=i;
sl@0
   384
		}
sl@0
   385
	BIO_clear_retry_flags(b);
sl@0
   386
	BIO_copy_next_retry(b);
sl@0
   387
	return((ret == 0)?ret_code:ret);
sl@0
   388
	}
sl@0
   389
sl@0
   390
static int b64_write(BIO *b, const char *in, int inl)
sl@0
   391
	{
sl@0
   392
	int ret=inl,n,i;
sl@0
   393
	BIO_B64_CTX *ctx;
sl@0
   394
sl@0
   395
	ctx=(BIO_B64_CTX *)b->ptr;
sl@0
   396
	BIO_clear_retry_flags(b);
sl@0
   397
sl@0
   398
	if (ctx->encode != B64_ENCODE)
sl@0
   399
		{
sl@0
   400
		ctx->encode=B64_ENCODE;
sl@0
   401
		ctx->buf_len=0;
sl@0
   402
		ctx->buf_off=0;
sl@0
   403
		ctx->tmp_len=0;
sl@0
   404
		EVP_EncodeInit(&(ctx->base64));
sl@0
   405
		}
sl@0
   406
sl@0
   407
	n=ctx->buf_len-ctx->buf_off;
sl@0
   408
	while (n > 0)
sl@0
   409
		{
sl@0
   410
		i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
sl@0
   411
		if (i <= 0)
sl@0
   412
			{
sl@0
   413
			BIO_copy_next_retry(b);
sl@0
   414
			return(i);
sl@0
   415
			}
sl@0
   416
		ctx->buf_off+=i;
sl@0
   417
		n-=i;
sl@0
   418
		}
sl@0
   419
	/* at this point all pending data has been written */
sl@0
   420
	ctx->buf_off=0;
sl@0
   421
	ctx->buf_len=0;
sl@0
   422
sl@0
   423
	if ((in == NULL) || (inl <= 0)) return(0);
sl@0
   424
sl@0
   425
	while (inl > 0)
sl@0
   426
		{
sl@0
   427
		n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl;
sl@0
   428
sl@0
   429
		if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
sl@0
   430
			{
sl@0
   431
			if (ctx->tmp_len > 0)
sl@0
   432
				{
sl@0
   433
				n=3-ctx->tmp_len;
sl@0
   434
				/* There's a teoretical possibility for this */
sl@0
   435
				if (n > inl) 
sl@0
   436
					n=inl;
sl@0
   437
				memcpy(&(ctx->tmp[ctx->tmp_len]),in,n);
sl@0
   438
				ctx->tmp_len+=n;
sl@0
   439
				if (ctx->tmp_len < 3)
sl@0
   440
					break;
sl@0
   441
				ctx->buf_len=EVP_EncodeBlock(
sl@0
   442
					(unsigned char *)ctx->buf,
sl@0
   443
					(unsigned char *)ctx->tmp,
sl@0
   444
					ctx->tmp_len);
sl@0
   445
				/* Since we're now done using the temporary
sl@0
   446
				   buffer, the length should be 0'd */
sl@0
   447
				ctx->tmp_len=0;
sl@0
   448
				}
sl@0
   449
			else
sl@0
   450
				{
sl@0
   451
				if (n < 3)
sl@0
   452
					{
sl@0
   453
					memcpy(&(ctx->tmp[0]),in,n);
sl@0
   454
					ctx->tmp_len=n;
sl@0
   455
					break;
sl@0
   456
					}
sl@0
   457
				n-=n%3;
sl@0
   458
				ctx->buf_len=EVP_EncodeBlock(
sl@0
   459
					(unsigned char *)ctx->buf,
sl@0
   460
					(unsigned char *)in,n);
sl@0
   461
				}
sl@0
   462
			}
sl@0
   463
		else
sl@0
   464
			{
sl@0
   465
			EVP_EncodeUpdate(&(ctx->base64),
sl@0
   466
				(unsigned char *)ctx->buf,&ctx->buf_len,
sl@0
   467
				(unsigned char *)in,n);
sl@0
   468
			}
sl@0
   469
		inl-=n;
sl@0
   470
		in+=n;
sl@0
   471
sl@0
   472
		ctx->buf_off=0;
sl@0
   473
		n=ctx->buf_len;
sl@0
   474
		while (n > 0)
sl@0
   475
			{
sl@0
   476
			i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
sl@0
   477
			if (i <= 0)
sl@0
   478
				{
sl@0
   479
				BIO_copy_next_retry(b);
sl@0
   480
				return((ret == 0)?i:ret);
sl@0
   481
				}
sl@0
   482
			n-=i;
sl@0
   483
			ctx->buf_off+=i;
sl@0
   484
			}
sl@0
   485
		ctx->buf_len=0;
sl@0
   486
		ctx->buf_off=0;
sl@0
   487
		}
sl@0
   488
	return(ret);
sl@0
   489
	}
sl@0
   490
sl@0
   491
static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
sl@0
   492
	{
sl@0
   493
	BIO_B64_CTX *ctx;
sl@0
   494
	long ret=1;
sl@0
   495
	int i;
sl@0
   496
sl@0
   497
	ctx=(BIO_B64_CTX *)b->ptr;
sl@0
   498
sl@0
   499
	switch (cmd)
sl@0
   500
		{
sl@0
   501
	case BIO_CTRL_RESET:
sl@0
   502
		ctx->cont=1;
sl@0
   503
		ctx->start=1;
sl@0
   504
		ctx->encode=B64_NONE;
sl@0
   505
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   506
		break;
sl@0
   507
	case BIO_CTRL_EOF:	/* More to read */
sl@0
   508
		if (ctx->cont <= 0)
sl@0
   509
			ret=1;
sl@0
   510
		else
sl@0
   511
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   512
		break;
sl@0
   513
	case BIO_CTRL_WPENDING: /* More to write in buffer */
sl@0
   514
		ret=ctx->buf_len-ctx->buf_off;
sl@0
   515
		if ((ret == 0) && (ctx->encode != B64_NONE)
sl@0
   516
			&& (ctx->base64.num != 0))
sl@0
   517
			ret=1;
sl@0
   518
		else if (ret <= 0)
sl@0
   519
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   520
		break;
sl@0
   521
	case BIO_CTRL_PENDING: /* More to read in buffer */
sl@0
   522
		ret=ctx->buf_len-ctx->buf_off;
sl@0
   523
		if (ret <= 0)
sl@0
   524
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   525
		break;
sl@0
   526
	case BIO_CTRL_FLUSH:
sl@0
   527
		/* do a final write */
sl@0
   528
again:
sl@0
   529
		while (ctx->buf_len != ctx->buf_off)
sl@0
   530
			{
sl@0
   531
			i=b64_write(b,NULL,0);
sl@0
   532
			if (i < 0)
sl@0
   533
				return i;
sl@0
   534
			}
sl@0
   535
		if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
sl@0
   536
			{
sl@0
   537
			if (ctx->tmp_len != 0)
sl@0
   538
				{
sl@0
   539
				ctx->buf_len=EVP_EncodeBlock(
sl@0
   540
					(unsigned char *)ctx->buf,
sl@0
   541
					(unsigned char *)ctx->tmp,
sl@0
   542
					ctx->tmp_len);
sl@0
   543
				ctx->buf_off=0;
sl@0
   544
				ctx->tmp_len=0;
sl@0
   545
				goto again;
sl@0
   546
				}
sl@0
   547
			}
sl@0
   548
		else if (ctx->encode != B64_NONE && ctx->base64.num != 0)
sl@0
   549
			{
sl@0
   550
			ctx->buf_off=0;
sl@0
   551
			EVP_EncodeFinal(&(ctx->base64),
sl@0
   552
				(unsigned char *)ctx->buf,
sl@0
   553
				&(ctx->buf_len));
sl@0
   554
			/* push out the bytes */
sl@0
   555
			goto again;
sl@0
   556
			}
sl@0
   557
		/* Finally flush the underlying BIO */
sl@0
   558
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   559
		break;
sl@0
   560
sl@0
   561
	case BIO_C_DO_STATE_MACHINE:
sl@0
   562
		BIO_clear_retry_flags(b);
sl@0
   563
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   564
		BIO_copy_next_retry(b);
sl@0
   565
		break;
sl@0
   566
sl@0
   567
	case BIO_CTRL_DUP:
sl@0
   568
		break;
sl@0
   569
	case BIO_CTRL_INFO:
sl@0
   570
	case BIO_CTRL_GET:
sl@0
   571
	case BIO_CTRL_SET:
sl@0
   572
	default:
sl@0
   573
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   574
		break;
sl@0
   575
		}
sl@0
   576
	return(ret);
sl@0
   577
	}
sl@0
   578
sl@0
   579
static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
sl@0
   580
	{
sl@0
   581
	long ret=1;
sl@0
   582
sl@0
   583
	if (b->next_bio == NULL) return(0);
sl@0
   584
	switch (cmd)
sl@0
   585
		{
sl@0
   586
	default:
sl@0
   587
		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
sl@0
   588
		break;
sl@0
   589
		}
sl@0
   590
	return(ret);
sl@0
   591
	}
sl@0
   592