1 | /********** |
2 | This library is free software; you can redistribute it and/or modify it under |
3 | the terms of the GNU Lesser General Public License as published by the |
4 | Free Software Foundation; either version 3 of the License, or (at your |
5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
6 | |
7 | This library is distributed in the hope that it will be useful, but WITHOUT |
8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
10 | more details. |
11 | |
12 | You should have received a copy of the GNU Lesser General Public License |
13 | along with this library; if not, write to the Free Software Foundation, Inc., |
14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
15 | **********/ |
16 | // "liveMedia" |
17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
18 | // Base64 encoding and decoding |
19 | // implementation |
20 | |
21 | #include "Base64.hh" |
22 | #include <strDup.hh> |
23 | #include <string.h> |
24 | |
25 | static char base64DecodeTable[256]; |
26 | |
27 | static void initBase64DecodeTable() { |
28 | int i; |
29 | for (i = 0; i < 256; ++i) base64DecodeTable[i] = (char)0x80; |
30 | // default value: invalid |
31 | |
32 | for (i = 'A'; i <= 'Z'; ++i) base64DecodeTable[i] = 0 + (i - 'A'); |
33 | for (i = 'a'; i <= 'z'; ++i) base64DecodeTable[i] = 26 + (i - 'a'); |
34 | for (i = '0'; i <= '9'; ++i) base64DecodeTable[i] = 52 + (i - '0'); |
35 | base64DecodeTable[(unsigned char)'+'] = 62; |
36 | base64DecodeTable[(unsigned char)'/'] = 63; |
37 | base64DecodeTable[(unsigned char)'='] = 0; |
38 | } |
39 | |
40 | unsigned char* base64Decode(char const* in, unsigned& resultSize, |
41 | Boolean trimTrailingZeros) { |
42 | if (in == NULL) return NULL; // sanity check |
43 | return base64Decode(in, strlen(in), resultSize, trimTrailingZeros); |
44 | } |
45 | |
46 | unsigned char* base64Decode(char const* in, unsigned inSize, |
47 | unsigned& resultSize, |
48 | Boolean trimTrailingZeros) { |
49 | static Boolean haveInitializedBase64DecodeTable = False; |
50 | if (!haveInitializedBase64DecodeTable) { |
51 | initBase64DecodeTable(); |
52 | haveInitializedBase64DecodeTable = True; |
53 | } |
54 | |
55 | unsigned char* out = new unsigned char[inSize+1]; // ensures we have enough space |
56 | int k = 0; |
57 | int paddingCount = 0; |
58 | int const jMax = inSize - 3; |
59 | // in case "inSize" is not a multiple of 4 (although it should be) |
60 | for (int j = 0; j < jMax; j += 4) { |
61 | char inTmp[4], outTmp[4]; |
62 | for (int i = 0; i < 4; ++i) { |
63 | inTmp[i] = in[i+j]; |
64 | if (inTmp[i] == '=') ++paddingCount; |
65 | outTmp[i] = base64DecodeTable[(unsigned char)inTmp[i]]; |
66 | if ((outTmp[i]&0x80) != 0) outTmp[i] = 0; // this happens only if there was an invalid character; pretend that it was 'A' |
67 | } |
68 | |
69 | out[k++] = (outTmp[0]<<2) | (outTmp[1]>>4); |
70 | out[k++] = (outTmp[1]<<4) | (outTmp[2]>>2); |
71 | out[k++] = (outTmp[2]<<6) | outTmp[3]; |
72 | } |
73 | |
74 | if (trimTrailingZeros) { |
75 | while (paddingCount > 0 && k > 0 && out[k-1] == '\0') { --k; --paddingCount; } |
76 | } |
77 | resultSize = k; |
78 | unsigned char* result = new unsigned char[resultSize]; |
79 | memmove(result, out, resultSize); |
80 | delete[] out; |
81 | |
82 | return result; |
83 | } |
84 | |
85 | static const char base64Char[] = |
86 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ; |
87 | |
88 | char* base64Encode(char const* origSigned, unsigned origLength) { |
89 | unsigned char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB set |
90 | if (orig == NULL) return NULL; |
91 | |
92 | unsigned const numOrig24BitValues = origLength/3; |
93 | Boolean havePadding = origLength > numOrig24BitValues*3; |
94 | Boolean havePadding2 = origLength == numOrig24BitValues*3 + 2; |
95 | unsigned const numResultBytes = 4*(numOrig24BitValues + havePadding); |
96 | char* result = new char[numResultBytes+1]; // allow for trailing '\0' |
97 | |
98 | // Map each full group of 3 input bytes into 4 output base-64 characters: |
99 | unsigned i; |
100 | for (i = 0; i < numOrig24BitValues; ++i) { |
101 | result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F]; |
102 | result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F]; |
103 | result[4*i+2] = base64Char[((orig[3*i+1]<<2) | (orig[3*i+2]>>6))&0x3F]; |
104 | result[4*i+3] = base64Char[orig[3*i+2]&0x3F]; |
105 | } |
106 | |
107 | // Now, take padding into account. (Note: i == numOrig24BitValues) |
108 | if (havePadding) { |
109 | result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F]; |
110 | if (havePadding2) { |
111 | result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F]; |
112 | result[4*i+2] = base64Char[(orig[3*i+1]<<2)&0x3F]; |
113 | } else { |
114 | result[4*i+1] = base64Char[((orig[3*i]&0x3)<<4)&0x3F]; |
115 | result[4*i+2] = '='; |
116 | } |
117 | result[4*i+3] = '='; |
118 | } |
119 | |
120 | result[numResultBytes] = '\0'; |
121 | return result; |
122 | } |
123 | |