forked from e8tools/tool1cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase64.cpp
151 lines (128 loc) · 3.64 KB
/
Base64.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
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Base64.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
// Êóñêè èñõîäíîãî êîäà âçÿòû ñ http://base64.sourceforge.net/
// Translation Table as described in RFC1113
static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//// Translation Table to decode (created by author)
static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
// encode
// base64 encode a stream adding padding and line breaks as per spec.
// linesize - äëèíà ñòðîêè. Åñëè linesize = 0, âûâîäèòñÿ âñ¸ â îäíó ñòðîêó, áåç ïåðåíîñîâ
void __fastcall base64_encode(TStream* infile, TStream* outfile, int linesize)
{
unsigned char in[3];
wchar_t out[4];
__int64 i;
int len, blocksout = 0;
__int64 inlenblock = infile->Size / 3;
infile->Seek(0i64, soBeginning);
for(i = 0; i < inlenblock; i++)
{
if(blocksout >= linesize/4 && linesize)
{
outfile->Write(L"\r\n", 4);
blocksout = 0;
}
blocksout++;
infile->Read(in, 3);
out[0] = cb64[in[0] >> 2];
out[1] = cb64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
out[2] = cb64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
out[3] = cb64[in[2] & 0x3f];
outfile->Write(out, 8);
}
len = infile->Size - infile->Position;
if(len > 0)
{
if(blocksout >= linesize / 4 && linesize) outfile->Write(L"\r\n", 4);
in[1] = 0;
in[2] = 0;
infile->Read(in, len);
out[0] = cb64[in[0] >> 2];
out[1] = cb64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
out[2] = len > 1 ? cb64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)] : L'=';
out[3] = len > 2 ? cb64[in[2] & 0x3f] : L'=';
outfile->Write(out, 8);
}
}
// decode
// decode a base64 encoded stream discarding padding, line breaks and noise
void __fastcall base64_decode(TStream* infile, TStream* outfile)
{
unsigned char in[4], out[3], v;
wchar_t s;
int i, len;
// infile->Seek(0i64, soBeginning);
while(infile->Position < infile->Size)
{
for(len = 0, i = 0; i < 4 && infile->Position < infile->Size; i++ )
{
v = 0;
while(infile->Position < infile->Size && v == 0)
{
infile->Read(&s, 2);
v = (unsigned char)((s < 43 || s > 122) ? 0 : cd64[s - 43]);
if(v) v = (unsigned char)((v == '$') ? 0 : v - 61);
}
if(v)
{
in[i] = (unsigned char)(v - 1);
len++;
}
// if(infile->Position < infile->Size)
// {
// len++;
// if(v) in[i] = (unsigned char)(v - 1);
// }
else in[i] = 0;
}
if(len)
{
out[0] = (unsigned char) (in[0] << 2 | in[1] >> 4);
out[1] = (unsigned char) (in[1] << 4 | in[2] >> 2);
out[2] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
outfile->Write(out, len - 1);
}
}
}
// decode
// decode a base64 encoded stream discarding padding, line breaks and noise
void __fastcall base64_decode(const String& instr, TStream* outfile, int start)
{
unsigned char in[4], out[3], v;
wchar_t s;
wchar_t* str;
int i, len, is, slen;
slen = instr.Length();
str = instr.c_str();
is = start;
while(is < slen)
{
for(len = 0, i = 0; i < 4 && is < slen; i++ )
{
v = 0;
while(is < slen && v == 0)
{
s = str[is++];
v = (unsigned char)((s < 43 || s > 122) ? 0 : cd64[s - 43]);
if(v) v = (unsigned char)((v == '$') ? 0 : v - 61);
}
if(v)
{
in[i] = (unsigned char)(v - 1);
len++;
}
else in[i] = 0;
}
if(len)
{
out[0] = (unsigned char) (in[0] << 2 | in[1] >> 4);
out[1] = (unsigned char) (in[1] << 4 | in[2] >> 2);
out[2] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
outfile->Write(out, len - 1);
}
}
}