概述
#include <stdio.h>
#define isbase64(a) ( ('A' <= (a) && (a) <= 'Z')
|| ('a' <= (a) && (a) <= 'z')
|| ('0' <= (a) && (a) <= '9')
|| (a) == '+' || (a) == '/' )
char base64idx[128] = {
'377','377','377','377','377','377','377','377',
'377','377','377','377','377','377','377','377',
'377','377','377','377','377','377','377','377',
'377','377','377','377','377','377','377','377',
'377','377','377','377','377','377','377','377',
'377','377','377', 62,'377','377','377', 63,
52, 53, 54, 55, 56, 57, 58, 59,
60, 61,'377','377','377','377','377','377',
'377', 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25,'377','377','377','377','377',
'377', 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51,'377','377','377','377','377'
};
int decode_base64(const char* aIn, size_t aInLen, char* aOut,size_t aOutSize, size_t* aOutLen)
{
size_t inLen = aInLen;
char *out = aOut;
size_t outSize = (inLen+3)/4*3;
int isErr = 0, isEndSeen = 0;
int b1, b2, b3, a[5],k;
size_t inPos = 0, outPos = 0;
register char c64Tag;
if (!aIn || !aOut || !aOutLen) return -1;
if (aOutSize < outSize) return -1;
/* Get four input chars at a time and decode them. Ignore white space
* chars (CR, LF, SP, HT). If '=' is encountered, terminate input. If
* a char other than white space, base64 char, or '=' is encountered,
* flag an input error, but otherwise ignore the char.
*/
while (inPos < inLen) {
a[1] = a[2] = a[3] = a[4] = 0;
c64Tag = '