forked from irwir/eMule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBase64Coding.cpp
287 lines (208 loc) · 8.18 KB
/
CBase64Coding.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//this file is part of eMule
//Copyright (C)2002-2008 Merkur ( strEmail.Format("%s@%s", "devteam", "emule-project.net") / http://www.emule-project.net )
//
//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., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "stdafx.h"
#include "CBase64Coding.hpp"
#pragma hdrstop
#define CARRIAGE_RETURN (13)
#define LINE_FEED (10)
/*
** Author: Samuel R. Blackburn
** Internet: wfc@pobox.com
**
** You can use it any way you like as long as you don't try to sell it.
**
** Any attempt to sell WFC in source code form must have the permission
** of the original author. You can produce commercial executables with
** WFC but you can't sell WFC.
**
** Copyright, 2000, Samuel R. Blackburn
**
** $Workfile: CBase64Coding.cpp $
** $Revision: 14 $
** $Modtime: 5/12/00 3:39p $
** $Reuse Tracing Code: 1 $
*/
//Modified for use with CAsyncProxySocket, removed tracing code
#if defined( _DEBUG ) && ! defined( WFC_STL )
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define END_OF_BASE64_ENCODED_DATA ('=')
#define BASE64_END_OF_BUFFER (0xFD)
#define BASE64_IGNORABLE_CHARACTER (0xFE)
#define BASE64_UNKNOWN_VALUE (0xFF)
#define BASE64_NUMBER_OF_CHARACTERS_PER_LINE (72)
static inline BYTE __get_character( const BYTE * buffer, const BYTE * decoder_table, int& index, int size_of_buffer )
{
BYTE return_value = 0;
do
{
if ( index >= size_of_buffer )
{
return( BASE64_END_OF_BUFFER );
}
return_value = buffer[ index ];
index++;
}
while( return_value != END_OF_BASE64_ENCODED_DATA &&
decoder_table[ return_value ] == BASE64_IGNORABLE_CHARACTER );
return( return_value );
}
CBase64Coding::CBase64Coding()
{
}
CBase64Coding::~CBase64Coding()
{
}
BOOL CBase64Coding::Encode( const char * source, int len, char * destination_string )
{
const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int loop_index = 0;
int number_of_bytes_to_encode = len;
BYTE byte_to_add = 0;
BYTE byte_1 = 0;
BYTE byte_2 = 0;
BYTE byte_3 = 0;
DWORD number_of_bytes_encoded = (DWORD) ( (double) number_of_bytes_to_encode / (double) 0.75 ) + 1;
// Now add in the CR/LF pairs, each line is truncated at 72 characters
// 2000-05-12
// Thanks go to Ilia Golubev (ilia@varicom.co.il) for finding a bug here.
// I was using number_of_bytes_to_encode rather than number_of_bytes_encoded.
number_of_bytes_encoded += (DWORD)( ( ( number_of_bytes_encoded / BASE64_NUMBER_OF_CHARACTERS_PER_LINE ) + 1 ) * 2 );
char * destination = destination_string;
number_of_bytes_encoded = 0;
while( loop_index < number_of_bytes_to_encode )
{
// Output the first byte
byte_1 = source[ loop_index ];
byte_to_add = alphabet[ ( byte_1 >> 2 ) ];
destination[ number_of_bytes_encoded ] = static_cast< char >( byte_to_add );
number_of_bytes_encoded++;
loop_index++;
if ( loop_index >= number_of_bytes_to_encode )
{
// We're at the end of the data to encode
byte_2 = 0;
byte_to_add = alphabet[ ( ( ( byte_1 & 0x03 ) << 4 ) | ( ( byte_2 & 0xF0 ) >> 4 ) ) ];
destination[ number_of_bytes_encoded ] = byte_to_add;
number_of_bytes_encoded++;
destination[ number_of_bytes_encoded ] = END_OF_BASE64_ENCODED_DATA;
number_of_bytes_encoded++;
destination[ number_of_bytes_encoded ] = END_OF_BASE64_ENCODED_DATA;
// 1999-09-01
// Thanks go to Yurong Lin (ylin@dial.pipex.com) for finding a bug here.
// We must NULL terminate the string before letting CString have the buffer back.
destination[ number_of_bytes_encoded + 1 ] = 0;
return( TRUE );
}
else
{
byte_2 = source[ loop_index ];
}
byte_to_add = alphabet[ ( ( ( byte_1 & 0x03 ) << 4 ) | ( ( byte_2 & 0xF0 ) >> 4 ) ) ];
destination[ number_of_bytes_encoded ] = byte_to_add;
number_of_bytes_encoded++;
loop_index++;
if ( loop_index >= number_of_bytes_to_encode )
{
// We ran out of bytes, we need to add the last half of byte_2 and pad
byte_3 = 0;
byte_to_add = alphabet[ ( ( ( byte_2 & 0x0F ) << 2 ) | ( ( byte_3 & 0xC0 ) >> 6 ) ) ];
destination[ number_of_bytes_encoded ] = byte_to_add;
number_of_bytes_encoded++;
destination[ number_of_bytes_encoded ] = END_OF_BASE64_ENCODED_DATA;
// 1999-09-01
// Thanks go to Yurong Lin (ylin@dial.pipex.com) for finding a bug here.
// We must NULL terminate the string before letting CString have the buffer back.
destination[ number_of_bytes_encoded + 1 ] = 0;
return( TRUE );
}
else
{
byte_3 = source[ loop_index ];
}
loop_index++;
byte_to_add = alphabet[ ( ( ( byte_2 & 0x0F ) << 2 ) | ( ( byte_3 & 0xC0 ) >> 6 ) ) ];
destination[ number_of_bytes_encoded ] = byte_to_add;
number_of_bytes_encoded++;
byte_to_add = alphabet[ ( byte_3 & 0x3F ) ];
destination[ number_of_bytes_encoded ] = byte_to_add;
number_of_bytes_encoded++;
if ( ( number_of_bytes_encoded % BASE64_NUMBER_OF_CHARACTERS_PER_LINE ) == 0 )
{
destination[ number_of_bytes_encoded ] = CARRIAGE_RETURN;
number_of_bytes_encoded++;
destination[ number_of_bytes_encoded ] = LINE_FEED;
number_of_bytes_encoded++;
}
}
destination[ number_of_bytes_encoded ] = END_OF_BASE64_ENCODED_DATA;
// 1999-09-01
// Thanks go to Yurong Lin (ylin@dial.pipex.com) for finding a bug here.
// We must NULL terminate the string before letting CString have the buffer back.
destination[ number_of_bytes_encoded + 1 ] = 0;
return( TRUE );
}
// End of source
#if 0
<HTML>
<HEAD>
<TITLE>WFC - CBase64Coding</TITLE>
<META name="keywords" content="WFC, MFC extension library, freeware class library, Win32, MIME encoding, base 64, source code">
<META name="description" content="This C++ class let's you MIME encode bytes to text using base64.">
</HEAD>
<BODY>
<H1>CBase64Coding</H1>
$Revision: 14 $<BR><HR>
<H2>Description</H2>
This class gives you the ability to encode/decode data using base64.
<H2>Constructors</H2>
<DL COMPACT>
<DT><PRE><B>CBase64Coding</B>()<DD>
Constructs this object.
</DL>
<H2>Methods</H2>
<DL COMPACT>
<DT><PRE>BOOL <B><A NAME="Decode">Decode</A></B>( const CByteArray& source, CByteArray& destination )
BOOL <B>Decode</B>( const CString& source, CByteArray& destination )</PRE><DD>
This method takes base64 encoded text and produces the bytes. It decodes
the base64 encoding.
<DT><PRE>BOOL <B><A NAME="Encode">Encode</A></B>( const CByteArray& source, CByteArray& destination )
BOOL <B>Encode</B>( const CByteArray& source, CString& destination )</PRE><DD>
This method takes bytes and turns them into base64 text.
</DL>
<H2>Example</H2>
<PRE><CODE>#include <wfc.h>
int _tmain( int number_of_command_line_arguments, LPCTSTR command_line_arguments[] )
{
<A HREF="WfcTrace.htm">WFCTRACEINIT</A>( TEXT( "_tmain()" ) );
CByteArray bytes;
get_file_contents( command_line_arguments[ 0 ], bytes );
<B>CBase64Coding</B> encoder;
CString encoded_data;
if ( encoder.Encode( bytes, encoded_data ) != FALSE )
{
_tprintf( TEXT( "%s\n", (LPCTSTR) encoded_data );
}
}</CODE></PRE>
<HR><I>Copyright, 2000, <A HREF="mailto:wfc@pobox.com">Samuel R. Blackburn</A></I><BR>
$Workfile: CBase64Coding.cpp $<BR>
$Modtime: 5/12/00 3:39p $
</BODY>
</HTML>
#endif