forked from madebits/cpp-base32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base32.cpp
157 lines (137 loc) · 3.77 KB
/
Base32.cpp
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
149
150
151
152
153
154
155
156
157
//////////////////////////////////////////////////////////////////////
// Base32.cpp
// (c) Vasian Cepa http://madebits.com
//////////////////////////////////////////////////////////////////////
#include "Base32.h"
int Base32::GetEncode32Length(int bytes)
{
int bits = bytes * 8;
int length = bits / 5;
if((bits % 5) > 0)
{
length++;
}
return length;
}
int Base32::GetDecode32Length(int bytes)
{
int bits = bytes * 5;
int length = bits / 8;
return length;
}
static bool Encode32Block(unsigned char* in5, unsigned char* out8)
{
// pack 5 bytes
unsigned __int64 buffer = 0;
for(int i = 0; i < 5; i++)
{
if(i != 0)
{
buffer = (buffer << 8);
}
buffer = buffer | in5[i];
}
// output 8 bytes
for(int j = 7; j >= 0; j--)
{
buffer = buffer << (24 + (7 - j) * 5);
buffer = buffer >> (24 + (7 - j) * 5);
unsigned char c = (unsigned char)(buffer >> (j * 5));
// self check
if(c >= 32) return false;
out8[7 - j] = c;
}
return true;
}
bool Base32::Encode32(unsigned char* in, int inLen, unsigned char* out)
{
if((in == 0) || (inLen <= 0) || (out == 0)) return false;
int d = inLen / 5;
int r = inLen % 5;
unsigned char outBuff[8];
for(int j = 0; j < d; j++)
{
if(!Encode32Block(&in[j * 5], &outBuff[0])) return false;
memmove(&out[j * 8], &outBuff[0], sizeof(unsigned char) * 8);
}
unsigned char padd[5];
memset(padd, 0, sizeof(unsigned char) * 5);
for(int i = 0; i < r; i++)
{
padd[i] = in[inLen - r + i];
}
if(!Encode32Block(&padd[0], &outBuff[0])) return false;
memmove(&out[d * 8], &outBuff[0], sizeof(unsigned char) * GetEncode32Length(r));
return true;
}
static bool Decode32Block(unsigned char* in8, unsigned char* out5)
{
// pack 8 bytes
unsigned __int64 buffer = 0;
for(int i = 0; i < 8; i++)
{
// input check
if(in8[i] >= 32) return false;
if(i != 0)
{
buffer = (buffer << 5);
}
buffer = buffer | in8[i];
}
// output 5 bytes
for(int j = 4; j >= 0; j--)
{
out5[4 - j] = (unsigned char)(buffer >> (j * 8));
}
return true;
}
bool Base32::Decode32(unsigned char* in, int inLen, unsigned char* out)
{
if((in == 0) || (inLen <= 0) || (out == 0)) return false;
int d = inLen / 8;
int r = inLen % 8;
unsigned char outBuff[5];
for(int j = 0; j < d; j++)
{
if(!Decode32Block(&in[j * 8], &outBuff[0])) return false;
memmove(&out[j * 5], &outBuff[0], sizeof(unsigned char) * 5);
}
unsigned char padd[8];
memset(padd, 0, sizeof(unsigned char) * 8);
for(int i = 0; i < r; i++)
{
padd[i] = in[inLen - r + i];
}
if(!Decode32Block(&padd[0], &outBuff[0])) return false;
memmove(&out[d * 5], &outBuff[0], sizeof(unsigned char) * GetDecode32Length(r));
return true;
}
bool Base32::Map32(unsigned char* inout32, int inout32Len, unsigned char* alpha32)
{
if((inout32 == 0) || (inout32Len <= 0) || (alpha32 == 0)) return false;
for(int i = 0; i < inout32Len; i++)
{
if(inout32[i] >=32) return false;
inout32[i] = alpha32[inout32[i]];
}
return true;
}
static void ReverseMap(unsigned char* inAlpha32, unsigned char* outMap)
{
memset(outMap, 0, sizeof(unsigned char) * 256);
for(int i = 0; i < 32; i++)
{
outMap[(int)inAlpha32[i]] = i;
}
}
bool Base32::Unmap32(unsigned char* inout32, int inout32Len, unsigned char* alpha32)
{
if((inout32 == 0) || (inout32Len <= 0) || (alpha32 == 0)) return false;
unsigned char rmap[256];
ReverseMap(alpha32, rmap);
for(int i = 0; i < inout32Len; i++)
{
inout32[i] = rmap[(int)inout32[i]];
}
return true;
}