File: encodingUtils.cpp

package info (click to toggle)
primrose 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,304 kB
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 92; perl: 67
file content (453 lines) | stat: -rw-r--r-- 13,085 bytes parent folder | download | duplicates (30)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * Modification History
 *
 * 2003-August-22   Jason Rohrer
 * Created.
 *
 * 2003-September-22   Jason Rohrer
 * Added base64 encoding.
 *
 * 2004-March-21   Jason Rohrer
 * Fixed a variable scoping and redefinition bug pointed out by Benjamin Meyer.
 */


#include "encodingUtils.h"


#include "minorGems/util/SimpleVector.h"


#include <stdio.h>
#include <string.h>



char fourBitIntToHex( int inInt ) {
    char outChar[2];

    if( inInt < 10 ) {
        sprintf( outChar, "%d", inInt );
        }
    else {
        switch( inInt ) {
            case 10:
                outChar[0] = 'A';
                break;
            case 11:
                outChar[0] = 'B';
                break;
            case 12:
                outChar[0] = 'C';
                break;
            case 13:
                outChar[0] = 'D';
                break;
            case 14:
                outChar[0] = 'E';
                break;
            case 15:
                outChar[0] = 'F';
                break;
            default:
                outChar[0] = '0';
                break;
            }
        }

    return outChar[0];
    }



// returns -1 if inHex is not a valid hex character
int hexToFourBitInt( char inHex ) {
    int returnInt;

    switch( inHex ) {
        case '0':
            returnInt = 0;
            break;
        case '1':
            returnInt = 1;
            break;
        case '2':
            returnInt = 2;
            break;
        case '3':
            returnInt = 3;
            break;
        case '4':
            returnInt = 4;
            break;
        case '5':
            returnInt = 5;
            break;
        case '6':
            returnInt = 6;
            break;
        case '7':
            returnInt = 7;
            break;
        case '8':
            returnInt = 8;
            break;
        case '9':
            returnInt = 9;
            break;
        case 'A':
        case 'a':
            returnInt = 10;
            break;
        case 'B':
        case 'b':
            returnInt = 11;
            break;
        case 'C':
        case 'c':
            returnInt = 12;
            break;
        case 'D':
        case 'd':
            returnInt = 13;
            break;
        case 'E':
        case 'e':
            returnInt = 14;
            break;
        case 'F':
        case 'f':
            returnInt = 15;
            break;
        default:
            returnInt = -1;
            break;
        }

    return returnInt;
    }



char *hexEncode( unsigned char *inData, int inDataLength ) {

    char *resultHexString = new char[ inDataLength * 2 + 1 ];
    int hexStringIndex = 0;
    
    for( int i=0; i<inDataLength; i++ ) {

        unsigned char currentByte = inData[ i ];

        int highBits = 0xF & ( currentByte >> 4 );
        int lowBits = 0xF & ( currentByte );

        resultHexString[ hexStringIndex ] = fourBitIntToHex( highBits );
        hexStringIndex++;

        resultHexString[ hexStringIndex ] = fourBitIntToHex( lowBits );
        hexStringIndex++;
        }

    resultHexString[ hexStringIndex ] = '\0';
    
    return resultHexString;
    }



unsigned char *hexDecode( char *inHexString ) {

    int hexLength = strlen( inHexString );
    
    if( hexLength % 2 != 0 ) {
        // hex strings must be even in length
        return NULL;
        }

    int dataLength = hexLength / 2;
    
    unsigned char *rawData = new unsigned char[ dataLength ];


    for( int i=0; i<dataLength; i++ ) {

        int highBits = hexToFourBitInt( inHexString[ 2 * i ] );
        int lowBits = hexToFourBitInt( inHexString[ 2 * i + 1 ] );

        if( highBits == -1 || lowBits == -1 ) {
            delete [] rawData;
            return NULL;
            }
        
        rawData[i] = (unsigned char)( highBits << 4 | lowBits );
        }

    return rawData;
    }



/*
 * These tables were taken from the GNU Privacy Guard source code.
 *
 * Wow... writing base64 functions would have been much more difficult
 * without these tables, especially the reverse table.
 */



// The base-64 character list
// Maps base64 binary numbers to ascii characters
static const char *binaryToAscii =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz"
    "0123456789+/";

// The reverse base-64 list
// Maps ascii characters to base64 binary numbers
static unsigned char asciiToBinary[256] = {
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
    0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
    0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
    0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
    0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
    0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff };


char *base64Encode( unsigned char *inData, int inDataLength,
                    char inBreakLines ) {

    SimpleVector<char> *encodingVector = new SimpleVector<char>();

    int numInLine = 0;
    
    // take groups of 3 data bytes and map them to 4 base64 digits
    for( int i=0; i<inDataLength; i=i+3 ) {

        if( i+2 < inDataLength ) {
            // not at end yet

            unsigned int block =
                inData[i]   << 16 |
                inData[i+1] << 8 |
                inData[i+2];

            // base64 digits, with digitA at left
            unsigned int digitA = 0x3F & ( block >> 18 );
            unsigned int digitB = 0x3F & ( block >> 12 );
            unsigned int digitC = 0x3F & ( block >> 6 );
            unsigned int digitD = 0x3F & ( block );

            encodingVector->push_back( binaryToAscii[ digitA ] );
            encodingVector->push_back( binaryToAscii[ digitB ] );
            encodingVector->push_back( binaryToAscii[ digitC ] );
            encodingVector->push_back( binaryToAscii[ digitD ] );
            numInLine += 4;

            if( inBreakLines && numInLine == 76 ) {
                // break the line
                encodingVector->push_back( '\r' );
                encodingVector->push_back( '\n' );
                numInLine = 0;
                }
            
            }
        else {
            // at end
            int numLeft = inDataLength - i;

            switch( numLeft ) {
                case 0:
                    // no padding
                    break;
                case 1: {
                    // two digits, two pads
                    unsigned int block =
                        inData[i]   << 16 |
                        0;
                    unsigned int digitA = 0x3F & ( block >> 18 );
                    unsigned int digitB = 0x3F & ( block >> 12 );
            
                    encodingVector->push_back( binaryToAscii[ digitA ] );
                    encodingVector->push_back( binaryToAscii[ digitB ] );

                    encodingVector->push_back( '=' );
                    encodingVector->push_back( '=' );
                    break;
                    }
                case 2: {
                    // three digits, one pad
                    unsigned int block =
                        inData[i]   << 16 |
                        inData[i+1] << 8 |
                        0;

                    // base64 digits, with digitA at left
                    unsigned int digitA = 0x3F & ( block >> 18 );
                    unsigned int digitB = 0x3F & ( block >> 12 );
                    unsigned int digitC = 0x3F & ( block >> 6 );
                    
                    encodingVector->push_back( binaryToAscii[ digitA ] );
                    encodingVector->push_back( binaryToAscii[ digitB ] );
                    encodingVector->push_back( binaryToAscii[ digitC ] );
                    encodingVector->push_back( '=' );
                    break;
                    }
                default:
                    break;
                }
            // done with all data
            i = inDataLength;
            }
        }

    char *returnString = encodingVector->getElementString();

    delete encodingVector;

    return returnString;
    }



unsigned char *base64Decode( char *inBase64String,
                             int *outDataLength ) {

    SimpleVector<unsigned char> *decodedVector =
        new SimpleVector<unsigned char>();


    
    int encodingLength = strlen( inBase64String );

    SimpleVector<unsigned char> *binaryEncodingVector =
        new SimpleVector<unsigned char>();

    int i;
    for( i=0; i<encodingLength; i++ ) {
        unsigned char currentChar = (unsigned char)( inBase64String[i] );

        unsigned char currentBinary = asciiToBinary[ currentChar ]; 
        
        if( currentBinary != 0xFF ) {
            // in range
            binaryEncodingVector->push_back( currentBinary );
            }
        }

    int binaryEncodingLength = binaryEncodingVector->size();

    unsigned char *binaryEncoding = binaryEncodingVector->getElementArray();
    delete binaryEncodingVector;

    int blockCount = binaryEncodingLength / 4;

    if( binaryEncodingLength % 4 != 0 ) {
        // extra, 0-padded block
        blockCount += 1;
        }



    // take groups of 4 encoded digits and map them to 3 data bytes
    for( i=0; i<binaryEncodingLength; i=i+4 ) {

        if( i+3 < binaryEncodingLength ) {
            // not at end yet

            unsigned int block =
                binaryEncoding[i]   << 18 |
                binaryEncoding[i+1] << 12 |
                binaryEncoding[i+2] << 6 |
                binaryEncoding[i+3];

            // data byte digits, with digitA at left
            unsigned int digitA = 0xFF & ( block >> 16 );
            unsigned int digitB = 0xFF & ( block >> 8 );
            unsigned int digitC = 0xFF & ( block );
            
            decodedVector->push_back( digitA );
            decodedVector->push_back( digitB );
            decodedVector->push_back( digitC );            
            }
        else {
            // at end
            int numLeft = binaryEncodingLength - i;

            switch( numLeft ) {
                case 0:
                    // no padding
                    break;
                case 1: {
                    // impossible
                    break;
                    }
                case 2: {
                    // two base64 digits, one data byte
                    unsigned int block =
                        binaryEncoding[i]   << 18 |
                        binaryEncoding[i+1] << 12 |
                        0;
                    
                    // data byte digits, with digitA at left
                    unsigned int digitA = 0xFF & ( block >> 16 );
                    
                    decodedVector->push_back( digitA );
                    break;
                    }
                case 3: {
                    // three base64 digits, two data bytes
                    unsigned int block =
                        binaryEncoding[i]   << 18 |
                        binaryEncoding[i+1] << 12 |
                        binaryEncoding[i+2] << 6 |
                        0;

                    // data byte digits, with digitA at left
                    unsigned int digitA = 0xFF & ( block >> 16 );
                    unsigned int digitB = 0xFF & ( block >> 8 );
                    
            
                    decodedVector->push_back( digitA );
                    decodedVector->push_back( digitB );
                    break;
                    }
                default:
                    break;
                }
            // done with all data
            i = binaryEncodingLength;
            }
        }    

    delete [] binaryEncoding;
    

    *outDataLength = decodedVector->size();
    unsigned char* returnData = decodedVector->getElementArray();

    delete decodedVector;

    return returnData;
    }