aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/rijndael/UglyBase64.cpp
blob: 4cb4424f71d1d12c3da4c6d2dbf5be974d034b3e (about) (plain) (blame)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//=============================================================================
//
//   File : UglyBase64.cpp
//   Creation date : Sun Feb 6 2011 22:25:10 CEST by Fabio Bas
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2011 Fabio Bas (ctrlaltca at gmail dot com)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your option) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#include "UglyBase64.h"
#include "KviCString.h"
#include "KviMemory.h"

namespace UglyBase64
{
	static unsigned char fake_base64[] = "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	void byteswap_buffer(unsigned char * p, int len)
	{
		while(len > 0)
		{
			unsigned char aux = p[0];
			p[0] = p[3];
			p[3] = aux;
			aux = p[1];
			p[1] = p[2];
			p[2] = aux;
			p += 4;
			len -= 4;
		}
	}

	unsigned int fake_base64dec(unsigned char c)
	{
		static char base64unmap[255];
		static bool didinit = false;

		if(!didinit)
		{
			// initialize base64unmap
			for(char & i : base64unmap)
				i = 0;
			for(int i = 0; i < 64; ++i)
				base64unmap[fake_base64[i]] = i;
			didinit = true;
		}

		return base64unmap[c];
	}

	void encode(const unsigned char * out, const int len, KviCString & szText)
	{
		// FIXME: this is probably needed only on LittleEndian machines!
		byteswap_buffer((unsigned char *)out, len);

		// da uglybase64 encoding
		const unsigned char * outb = out;
		const unsigned char * oute = out + len;

		int ll = (len * 3) / 2;
		szText.setLen(ll);

		unsigned char * p = (unsigned char *)szText.ptr();
		while(outb < oute)
		{
			quint32 * dd1 = (quint32 *)outb;
			outb += 4;
			quint32 * dd2 = (quint32 *)outb;
			outb += 4;
			*p++ = fake_base64[*dd2 & 0x3f];
			*dd2 >>= 6;
			*p++ = fake_base64[*dd2 & 0x3f];
			*dd2 >>= 6;
			*p++ = fake_base64[*dd2 & 0x3f];
			*dd2 >>= 6;
			*p++ = fake_base64[*dd2 & 0x3f];
			*dd2 >>= 6;
			*p++ = fake_base64[*dd2 & 0x3f];
			*dd2 >>= 6;
			*p++ = fake_base64[*dd2 & 0x3f];

			*p++ = fake_base64[*dd1 & 0x3f];
			*dd1 >>= 6;
			*p++ = fake_base64[*dd1 & 0x3f];
			*dd1 >>= 6;
			*p++ = fake_base64[*dd1 & 0x3f];
			*dd1 >>= 6;
			*p++ = fake_base64[*dd1 & 0x3f];
			*dd1 >>= 6;
			*p++ = fake_base64[*dd1 & 0x3f];
			*dd1 >>= 6;
			*p++ = fake_base64[*dd1 & 0x3f];
		}
	}

	void decode(KviCString & szText, unsigned char ** buf, int * len)
	{
		// make sure its length is multiple of 12 (eventually pad with zeroes)
		if(szText.len() % 12)
		{
			int oldL = szText.len();
			szText.setLen(szText.len() + (12 - (szText.len() % 12)));
			char * padB = szText.ptr() + oldL;
			char * padE = szText.ptr() + szText.len();
			while(padB < padE)
				*padB++ = 0;
		}

		*len = (int)(szText.len() * 2) / 3;

		*buf = (unsigned char *)KviMemory::allocate(*len);
		unsigned char * p = (unsigned char *)szText.ptr();
		unsigned char * e = p + szText.len();
		int i;
		unsigned char * bufp = *buf;
		while(p < e)
		{
			quint32 * dw1 = (quint32 *)bufp;
			bufp += 4;
			quint32 * dw2 = (quint32 *)bufp;
			bufp += 4;
			*dw2 = 0;
			for(i = 0; i < 6; i++)
				*dw2 |= (fake_base64dec(*p++)) << (i * 6);
			*dw1 = 0;
			for(i = 0; i < 6; i++)
				*dw1 |= (fake_base64dec(*p++)) << (i * 6);
		}

		// FIXME: this is probably needed only on LittleEndian machines!
		byteswap_buffer(*buf, *len);
	}
};