1
/***************************************************************************
2
cbase64.cpp - description
4
begin : Tue Jul 09 2002
5
copyright : (C) 2002-2003 by Mathias K�ster
6
email : mathen@users.berlios.de
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
21
#include <dclib/core/cbytearray.h>
27
static int Index_64[128] = {
28
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
29
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
30
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, 63,-1,-1,-1,
31
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
32
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
33
15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
34
-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
35
41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1,
38
static char B64Chars[64] = {
39
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
40
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
41
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
42
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
46
#define base64val(c) Index_64[(unsigned int)(c)]
47
#define base64chars(c) B64Chars[(unsigned int)(c&0x3F)]
62
CString CBase64::Encode( CString string )
67
in.Append(string.Data(),string.Length()+1);
71
outs.Set((const char*)out.Data(),out.Size());
76
/* raw bytes to null-terminated base 64 string */
77
void CBase64::Encode( CByteArray * dst, CByteArray * src )
80
unsigned char *current = src->Data();
81
size_t len = src->Size();
85
c = base64chars(current[0] >> 2);
87
c = base64chars(((current[0] & 0x03) << 4) | (current[1] >> 4));
89
c = base64chars(((current[1] & 0x0f) << 2) | (current[2] >> 6));
91
c = base64chars(current[2] & 0x3f);
97
/* clean up remainder */
100
c = base64chars(current[0] >> 2);
104
c = base64chars(((current[0] & 0x03) << 4) | (current[1] >> 4));
106
c = base64chars((current[1] & 0x0f) << 2);
113
c = base64chars((current[0] & 0x03) << 4);
124
int CBase64::Decode( CByteArray * dst, CString * string )
128
in.Append(string->Data(),string->Length());
130
return Decode(dst,&in);
133
/* Convert '\0'-terminated base 64 string to raw bytes.
134
* Returns length of returned buffer, or -1 on error */
135
int CBase64::Decode( CByteArray * dst, CByteArray * src )
137
unsigned char *current,*result;
140
unsigned long i=0,j=0;
142
if ( (!dst) || (!src) )
145
if ( (src->Size() % 4) != 0 )
150
current = src->Data();
151
result = dst->Data();
153
for(i=0;i<src->Size();i++)
155
ch = (current[i]&0x7F);
157
if (ch == '=') break;
159
if (ch == ' ') ch = '+';
163
if (ch < 0) return -1;
168
c = (unsigned char)((ch << 2) & 0xFF);
172
dst->Data()[j] |= ((ch >> 4) & 0xFF);
174
if(current[i+1]!='=')
176
c = (unsigned char)((ch << 4) & 0xFF);
181
dst->Data()[j] |= ((ch >> 2) & 0x0f);
183
if(current[i+1]!='=')
185
c = (unsigned char)((ch << 6) & 0xFF);
190
dst->Data()[j] |= ch;