-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_file.cpp
248 lines (204 loc) · 7.46 KB
/
main_file.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
//********************************************************************************************
const char* keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const char* keyStr_url = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
//********************************************************************************************
int indexof(const char* str, char toSearch)
{
for (int i = 0; i < 64; i++)
{
if (str[i] == toSearch)
return i;
}
return 64;
}
//********************************************************************************************
unsigned char test(unsigned char incoming)
{
return ((incoming == 64) ? 0x00 : incoming);
}
//********************************************************************************************
int base64_encode(const char* str, unsigned char* input, int input_len, unsigned char* output, int output_len)
{
unsigned char chr1, chr2, chr3;
unsigned char enc1, enc2, enc3, enc4;
int i = 0;
int j = 0;
int flag1 = 0;
int flag2 = 0;
while (i < input_len)
{
chr1 = input[i++];
if (i >= input_len)
flag1 = 1;
chr2 = input[i++];
if (i >= input_len)
flag2 = 1;
chr3 = input[i++];
enc1 = chr1 >> 2;
enc2 = ((chr1 & 0x03) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 0x0F) << 2) | (chr3 >> 6);
enc4 = chr3 & 0x3F;
if (flag1 == 1)
enc3 = enc4 = 64;
else
{
if (flag2 == 1)
enc4 = 64;
}
output[j++] = str[enc1];
output[j++] = str[enc2];
output[j++] = str[enc3];
output[j++] = str[enc4];
}
return j;
}
//********************************************************************************************
int base64_decode(const char* str, unsigned char* input, int input_len, unsigned char* output, int output_len)
{
unsigned char chr1, chr2, chr3;
unsigned char enc1, enc2, enc3, enc4;
int i = 0;
int j = 0;
while (i < input_len)
{
enc1 = indexof(str, input[i++]);
enc2 = (i >= input_len) ? 0x00 : indexof(str, input[i++]);
enc3 = (i >= input_len) ? 0x00 : indexof(str, input[i++]);
enc4 = (i >= input_len) ? 0x00 : indexof(str, input[i++]);
chr1 = (test(enc1) << 2) | (test(enc2) >> 4);
chr2 = ((test(enc2) & 0x0F) << 4) | (test(enc3) >> 2);
chr3 = ((test(enc3) & 0x03) << 6) | test(enc4);
output[j++] = chr1;
if (enc3 != 64)
output[j++] = chr2;
if (enc4 != 64)
output[j++] = chr3;
}
return j;
}
//********************************************************************************************
void usage()
{
std::cout << "BASE-64 coder/decoder, v1.0" << std::endl;
std::cout << "(c) 2014, Yury Strozhevsky, www.strozhevsky.com" << std::endl;
std::cout << std::endl << "Usage:" << std::endl;
std::cout << "\tb64.exe -e <file to encode> <output file>" << std::endl;
std::cout << "\tb64.exe -eu <file to encode> <output file>" << std::endl;
std::cout << "\tb64.exe -ed <file to encode> <output file with 64-length strings>" << std::endl;
std::cout << "\tb64.exe -eud <file to encode> <output file with 64-length strings>" << std::endl;
std::cout << "\tb64.exe -d <file to decode> <output file>" << std::endl;
std::cout << "\tb64.exe -du <file to decode> <output file>" << std::endl;
}
//********************************************************************************************
void encode_file(const char* str, char* input_file, char* output_file, bool divide)
{
#pragma region Read input file into a buffer
std::ifstream in_file(input_file, std::ios_base::binary);
if (!in_file.good())
return;
in_file.unsetf(std::ios_base::skipws);
std::istreambuf_iterator< char > _in_iterator(in_file);
std::istreambuf_iterator< char > _end_of_in;
std::vector< char > input_buffer;
std::copy(_in_iterator, _end_of_in, std::back_inserter(input_buffer));
in_file.close();
int input_buffer_size = input_buffer.size();
unsigned char* output_buffer = (unsigned char*)malloc(input_buffer_size * 3);
memset(output_buffer, 0, input_buffer_size * 3);
#pragma endregion
#pragma region Encode input buffer
int output_length = base64_encode(str, (unsigned char*)&input_buffer[0], input_buffer_size, output_buffer, input_buffer_size * 3);
#pragma endregion
#pragma region Initialization of output file
std::ofstream out_file(output_file, std::ios_base::binary);
std::ostream_iterator< char > _out_iterator(out_file);
#pragma endregion
#pragma region Divide output down to 63 chars if needed
if (divide)
{
int sum_len = 0;
int part_size = 0;
while (sum_len < output_length)
{
part_size = ((sum_len + 64) > output_length) ? (output_length - sum_len) : 64;
std::copy(output_buffer + sum_len, output_buffer + sum_len + part_size, *_out_iterator);
_out_iterator = '\r';
_out_iterator = '\n';
sum_len += part_size;
}
}
else
std::copy(output_buffer, output_buffer + output_length, *_out_iterator);
#pragma endregion
#pragma region Close all handlers
free(output_buffer);
out_file.flush();
out_file.close();
#pragma endregion
}
//********************************************************************************************
void decode_file(const char* str, char* input_file, char* output_file)
{
#pragma region Read input file into a buffer
std::ifstream in_file(input_file, std::ios_base::binary);
if (!in_file.good())
return;
in_file.unsetf(std::ios_base::skipws);
std::istreambuf_iterator< char > _in_iterator(in_file);
std::istreambuf_iterator< char > _end_of_in;
std::vector< char > input_buffer;
std::copy(_in_iterator, _end_of_in, std::back_inserter(input_buffer));
in_file.close();
#pragma endregion
#pragma region Remove all "\r\n" chars
std::vector< char > input_buffer_clear;
for (std::vector< char >::iterator i = input_buffer.begin(); i != input_buffer.end(); i++)
{
if ((*i != '\r') && (*i != '\n'))
input_buffer_clear.push_back(*i);
}
int input_length = input_buffer_clear.size();
#pragma endregion
#pragma region Decode buffer
unsigned char* output_buffer = (unsigned char*)malloc(input_length);
memset(output_buffer, 0, input_length);
int output_length = base64_decode(str, (unsigned char*)&input_buffer_clear[0], input_length, output_buffer, input_length);
#pragma endregion
#pragma region Store output buffer to external file
std::ofstream out_file(output_file, std::ios_base::binary);
std::ostream_iterator< char > _out_iterator(out_file);
std::copy(output_buffer, output_buffer + output_length, *_out_iterator);
#pragma endregion
#pragma region Close all handlers
free(output_buffer);
out_file.flush();
out_file.close();
#pragma endregion
}
//********************************************************************************************
int main(int argc, char** argv)
{
if (argc != 4)
{
usage();
return 0;
}
if (strcmp(argv[1], "-e") == 0)
encode_file(keyStr, argv[2], argv[3], false);
if (strcmp(argv[1], "-eu") == 0)
encode_file(keyStr_url, argv[2], argv[3], false);
if (strcmp(argv[1], "-ed") == 0)
encode_file(keyStr, argv[2], argv[3], true);
if (strcmp(argv[1], "-eud") == 0)
encode_file(keyStr_url, argv[2], argv[3], true);
if (strcmp(argv[1], "-d") == 0)
decode_file(keyStr, argv[2], argv[3]);
if (strcmp(argv[1], "-du") == 0)
decode_file(keyStr_url, argv[2], argv[3]);
return 0;
}
//********************************************************************************************