1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2021 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
14 https://github.com/superwills/NibbleAndAHalf
15 base64.h -- Fast base64 encoding and decoding.
16 version 1.0.0, April 17, 2013 143a
18 Copyright (C) 2013 William Sherif
20 This software is provided 'as-is', without any express or implied
21 warranty. In no event will the authors be held liable for any damages
22 arising from the use of this software.
24 Permission is granted to anyone to use this software for any purpose,
25 including commercial applications, and to alter it and redistribute it
26 freely, subject to the following restrictions:
28 1. The origin of this software must not be misrepresented; you must not
29 claim that you wrote the original software. If you use this software
30 in a product, an acknowledgment in the product documentation would be
31 appreciated but is not required.
32 2. Altered source versions must be plainly marked as such, and must not be
33 misrepresented as being the original software.
34 3. This notice may not be removed or altered from any source distribution.
39 YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz
43 // ----------------------------------------------------------------------------
44 // Base64 encoder/decoder code was altered for integration in Rocks'n'Diamonds
45 // ----------------------------------------------------------------------------
53 const static char *b64encode =
54 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
56 int base64_encoded_size(int unencoded_size)
58 int mod = unencoded_size % 3;
59 int pad = (mod > 0 ? 3 - mod : 0);
61 return 4 * (unencoded_size + pad) / 3 + 1;
64 void base64_encode(char *encoded_data,
65 const void *unencoded_ptr, int unencoded_size)
67 const byte *unencoded_data = (const byte *)unencoded_ptr;
68 char *ptr = encoded_data;
71 int mod = unencoded_size % 3;
72 int pad = (mod > 0 ? 3 - mod : 0);
74 for (i = 0; i <= unencoded_size - 3; i += 3)
76 byte byte0 = unencoded_data[i];
77 byte byte1 = unencoded_data[i + 1];
78 byte byte2 = unencoded_data[i + 2];
80 *ptr++ = b64encode[byte0 >> 2];
81 *ptr++ = b64encode[((byte0 & 0x03) << 4) + (byte1 >> 4)];
82 *ptr++ = b64encode[((byte1 & 0x0f) << 2) + (byte2 >> 6)];
83 *ptr++ = b64encode[byte2 & 0x3f];
88 byte byte0 = unencoded_data[i];
89 byte byte1 = unencoded_data[i + 1];
91 *ptr++ = b64encode[byte0 >> 2];
92 *ptr++ = b64encode[((byte0 & 0x03) << 4) + (byte1 >> 4)];
93 *ptr++ = b64encode[((byte1 & 0x0f) << 2)];
98 byte byte0 = unencoded_data[i];
100 *ptr++ = b64encode[byte0 >> 2];
101 *ptr++ = b64encode[(byte0 & 0x03) << 4];
109 const static byte b64decode[] =
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0
112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16
113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, // 32
114 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, // 48
116 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64
117 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, // 80
118 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96
119 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, // 112
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128
122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 144
123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 160
124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 176
126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 192
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 208
128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 224
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 240
132 int base64_decoded_size(const char *encoded_data)
134 int encoded_size = strlen(encoded_data);
136 if (encoded_size < 2)
141 if (encoded_data[encoded_size - 1] == '=')
143 if (encoded_data[encoded_size - 2] == '=')
146 return 3 * encoded_size / 4 - pad;
149 void base64_decode(byte *decoded_data, const char *encoded_ptr)
151 const byte *encoded_data = (const byte *)encoded_ptr;
152 byte *ptr = decoded_data;
153 int encoded_size = strlen(encoded_ptr);
156 if (encoded_size < 2)
161 if (encoded_data[encoded_size - 1] == '=')
163 if (encoded_data[encoded_size - 2] == '=')
166 for (i = 0; i <= encoded_size - 4 - pad; i += 4)
168 byte byte0 = b64decode[encoded_data[i]];
169 byte byte1 = b64decode[encoded_data[i + 1]];
170 byte byte2 = b64decode[encoded_data[i + 2]];
171 byte byte3 = b64decode[encoded_data[i + 3]];
173 *ptr++ = (byte0 << 2) | (byte1 >> 4);
174 *ptr++ = (byte1 << 4) | (byte2 >> 2);
175 *ptr++ = (byte2 << 6) | (byte3);
180 byte byte0 = b64decode[encoded_data[i]];
181 byte byte1 = b64decode[encoded_data[i + 1]];
182 byte byte2 = b64decode[encoded_data[i + 2]];
184 *ptr++ = (byte0 << 2) | (byte1 >> 4);
185 *ptr++ = (byte1 << 4) | (byte2 >> 2);
189 byte byte0 = b64decode[encoded_data[i]];
190 byte byte1 = b64decode[encoded_data[i + 1]];
192 *ptr++ = (byte0 << 2) | (byte1 >> 4);