| 1 | /* |
| 2 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * "Daniel Kopecek" <[email protected]> |
| 21 | */ |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include "xbase64.h" |
| 29 | |
| 30 | #if defined(WANT_XBASE64) |
| 31 | static const char xb64_enc_alphabet[65] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:_"; |
| 32 | static const char xb64_dec_alphabet[75] = { |
| 33 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 62, 0, 0, 0, 0, 0, 0, 36, 37, 38, |
| 34 | 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, |
| 35 | 59, 60, 61, 0, 0, 0, 0, 63, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
| 36 | 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 |
| 37 | }; |
| 38 | |
| 39 | #define B(n) (*(in+(n))) |
| 40 | static inline void __xb64_enc3 (const uint8_t in[3], char out[4]) { |
| 41 | out[0] = xb64_enc_alphabet[ B(0) & 0x3f]; |
| 42 | out[1] = xb64_enc_alphabet[(B(0) & 0xc0) >> 2 | (B(1) & 0x0f)]; |
| 43 | out[2] = xb64_enc_alphabet[(B(1) & 0xf0) >> 2 | (B(2) & 0x03)]; |
| 44 | out[3] = xb64_enc_alphabet[(B(2) & 0xfc) >> 2]; |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | static inline void __xb64_enc2 (const uint8_t in[2], char out[4]) { |
| 49 | out[0] = xb64_enc_alphabet[ B(0) & 0x3f]; |
| 50 | out[1] = xb64_enc_alphabet[(B(0) & 0xc0) >> 2 | (B(1) & 0x0f)]; |
| 51 | out[2] = xb64_enc_alphabet[(B(1) & 0xf0) >> 2]; |
| 52 | out[3] = XBASE64_PADDING_CHAR; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | static inline void __xb64_enc1 (const uint8_t in, char out[4]) { |
| 57 | out[0] = xb64_enc_alphabet[ in & 0x3f]; |
| 58 | out[1] = xb64_enc_alphabet[(in & 0xc0) >> 2]; |
| 59 | out[2] = XBASE64_PADDING_CHAR; |
| 60 | out[3] = XBASE64_PADDING_CHAR; |
| 61 | return; |
| 62 | } |
| 63 | #undef B |
| 64 | |
| 65 | #define B(n) ((*(in+(n)) - 48) % 75) |
| 66 | static inline void __xb64_dec4 (const char in[4], uint8_t out[3]) { |
| 67 | out[0] = xb64_dec_alphabet[B(0)] | ((xb64_dec_alphabet[B(1)] & 0x30) << 2); |
| 68 | out[1] = (xb64_dec_alphabet[B(1)] & 0x0f) | ((xb64_dec_alphabet[B(2)] & 0x3c) << 2); |
| 69 | out[2] = (xb64_dec_alphabet[B(2)] & 0x03) | ((xb64_dec_alphabet[B(3)] & 0x3f) << 2); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | static inline void __xb64_dec3 (const char in[3], uint8_t out[2]) { |
| 74 | out[0] = xb64_dec_alphabet[B(0)] | ((xb64_dec_alphabet[B(1)] & 0x30) << 2); |
| 75 | out[1] = (xb64_dec_alphabet[B(1)] & 0x0f) | ((xb64_dec_alphabet[B(2)] & 0x3c) << 2); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | static inline void __xb64_dec2 (const char in[2], uint8_t out[1]) { |
| 80 | out[0] = xb64_dec_alphabet[B(0)] | ((xb64_dec_alphabet[B(1)] & 0x30) << 2); |
| 81 | return; |
| 82 | } |
| 83 | #undef B |
| 84 | |
| 85 | size_t xbase64_encode (const uint8_t *data, size_t size, char **buffer) { |
| 86 | register uint8_t d = size % 3; |
| 87 | register size_t i, s = size - d; |
| 88 | |
| 89 | *buffer = malloc(sizeof(char) * (1 + (s/3 * 4) + (d != 0 ? 4 : 0))); |
| 90 | |
| 91 | for (i = 0; i < s/3; ++i) { |
| 92 | __xb64_enc3 (data + (i * 3), (*buffer) + (i * 4)); |
| 93 | } |
| 94 | |
| 95 | switch (d) { |
| 96 | case 2: |
| 97 | __xb64_enc2 (data + (i * 3), (*buffer) + (i * 4)); |
| 98 | ++i; |
| 99 | break; |
| 100 | case 1: |
| 101 | __xb64_enc1 (*(data + (i * 3)), (*buffer) + (i * 4)); |
| 102 | ++i; |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | (*buffer)[(i * 4)] = '\0'; |
| 107 | return (i * 4); |
| 108 | } |
| 109 | |
| 110 | size_t xbase64_decode (const char *data, size_t size, uint8_t **buffer) { |
| 111 | register size_t i, s = size; |
| 112 | register uint8_t d = 0; |
| 113 | |
| 114 | d = 4 - (size % 4); |
| 115 | |
| 116 | if (d == 4) { |
| 117 | if (*(data + s - 1) == XBASE64_PADDING_CHAR) { |
| 118 | if (*(data + s - 2) == XBASE64_PADDING_CHAR) { |
| 119 | d = 2; |
| 120 | } else { |
| 121 | d = 1; |
| 122 | } |
| 123 | |
| 124 | s -= 4; |
| 125 | } |
| 126 | } else { |
| 127 | if (d == 3) { |
| 128 | *buffer = NULL; |
| 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | fprintf(stderr, "d = %u\n", d); |
| 134 | |
| 135 | *buffer = malloc(sizeof(uint8_t) * ((s / 4 * 3) + d)); |
| 136 | |
| 137 | for (i = 0; i < s/4; ++i) { |
| 138 | __xb64_dec4 (data + (i * 4), (*buffer) + (i * 3)); |
| 139 | } |
| 140 | |
| 141 | switch (d) { |
| 142 | case 2: |
| 143 | __xb64_dec2 (data + (i * 4), (*buffer) + (i * 3)); |
| 144 | return (i * 3) + 1; |
| 145 | case 1: |
| 146 | __xb64_dec3 (data + (i * 4), (*buffer) + (i * 3)); |
| 147 | return (i * 3) + 2; |
| 148 | default: |
| 149 | return (i * 3); |
| 150 | } |
| 151 | } |
| 152 | #endif /* WANT_XBASE64 */ |
| 153 | |
| 154 | #if defined(WANT_BASE64) |
| 155 | static const char b64_enc_alphabet[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 156 | static const char b64_dec_alphabet[75] = { |
| 157 | /* 0 */ 52, |
| 158 | /* 1 */ 53, |
| 159 | /* 2 */ 54, |
| 160 | /* 3 */ 55, |
| 161 | /* 4 */ 56, |
| 162 | /* 5 */ 57, |
| 163 | /* 6 */ 58, |
| 164 | /* 7 */ 59, |
| 165 | /* 8 */ 60, |
| 166 | /* 9 */ 61, |
| 167 | /* : */ 0, |
| 168 | /* ; */ 0, |
| 169 | /* < */ 0, |
| 170 | /* = */ 0, |
| 171 | /* > */ 0, |
| 172 | /* ? */ 0, |
| 173 | /* @ */ 0, |
| 174 | /* A */ 0, |
| 175 | /* B */ 1, |
| 176 | /* C */ 2, |
| 177 | /* D */ 3, |
| 178 | /* E */ 4, |
| 179 | /* F */ 5, |
| 180 | /* G */ 6, |
| 181 | /* H */ 7, |
| 182 | /* I */ 8, |
| 183 | /* J */ 9, |
| 184 | /* K */ 10, |
| 185 | /* L */ 11, |
| 186 | /* M */ 12, |
| 187 | /* N */ 13, |
| 188 | /* O */ 14, |
| 189 | /* P */ 15, |
| 190 | /* Q */ 16, |
| 191 | /* R */ 17, |
| 192 | /* S */ 18, |
| 193 | /* T */ 19, |
| 194 | /* U */ 20, |
| 195 | /* V */ 21, |
| 196 | /* W */ 22, |
| 197 | /* X */ 23, |
| 198 | /* Y */ 24, |
| 199 | /* Z */ 25, |
| 200 | /* [ */ 0, |
| 201 | /* \ */ 0, |
| 202 | /* ] */ 0, |
| 203 | /* ^ */ 0, |
| 204 | /* _ */ 0, |
| 205 | /* ` */ 0, |
| 206 | /* a */ 26, |
| 207 | /* b */ 27, |
| 208 | /* c */ 28, |
| 209 | /* d */ 29, |
| 210 | /* e */ 30, |
| 211 | /* f */ 31, |
| 212 | /* g */ 32, |
| 213 | /* h */ 33, |
| 214 | /* i */ 34, |
| 215 | /* j */ 35, |
| 216 | /* k */ 36, |
| 217 | /* l */ 37, |
| 218 | /* m */ 38, |
| 219 | /* n */ 39, |
| 220 | /* o */ 40, |
| 221 | /* p */ 41, |
| 222 | /* q */ 42, |
| 223 | /* r */ 43, |
| 224 | /* s */ 44, |
| 225 | /* t */ 45, |
| 226 | /* u */ 46, |
| 227 | /* v */ 47, |
| 228 | /* w */ 48, |
| 229 | /* x */ 49, |
| 230 | /* y */ 50, |
| 231 | /* z */ 51 |
| 232 | }; |
| 233 | |
| 234 | #define B(n) (*(in+(n))) |
| 235 | static inline void __b64_enc3 (const uint8_t in[3], char out[4]) { |
| 236 | out[0] = b64_enc_alphabet[(B(0) & 0xfc) >> 2]; |
| 237 | out[1] = b64_enc_alphabet[(B(0) & 0x03) << 4 | (B(1) & 0xf0) >> 4]; |
| 238 | out[2] = b64_enc_alphabet[(B(1) & 0x0f) << 2 | (B(2) & 0xc0) >> 6]; |
| 239 | out[3] = b64_enc_alphabet[(B(2) & 0x3f)]; |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | static inline void __b64_enc2 (const uint8_t in[2], char out[4]) { |
| 244 | out[0] = b64_enc_alphabet[(B(0) & 0xfc) >> 2]; |
| 245 | out[1] = b64_enc_alphabet[(B(0) & 0x03) << 4 | (B(1) & 0xf0) >> 4]; |
| 246 | out[2] = b64_enc_alphabet[(B(1) & 0x0f) << 2]; |
| 247 | out[3] = BASE64_PADDING_CHAR; |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | static inline void __b64_enc1 (const uint8_t in, char out[4]) { |
| 252 | out[0] = b64_enc_alphabet[(in & 0xfc) >> 2]; |
| 253 | out[1] = b64_enc_alphabet[(in & 0x03) << 4]; |
| 254 | out[2] = BASE64_PADDING_CHAR; |
| 255 | out[3] = BASE64_PADDING_CHAR; |
| 256 | return; |
| 257 | } |
| 258 | #undef B |
| 259 | |
| 260 | #define B(n) ((*(in+(n)) - 48) % 75) |
| 261 | static inline void __b64_dec4 (const char in[4], uint8_t out[3]) { |
| 262 | out[0] = b64_dec_alphabet[B(0)] << 2 | ((b64_dec_alphabet[B(1)] & 0x30) >> 4); |
| 263 | out[1] = ((b64_dec_alphabet[B(1)] & 0x0f) << 4) | ((b64_dec_alphabet[B(2)] & 0x3c) >> 2); |
| 264 | out[2] = ((b64_dec_alphabet[B(2)] & 0x03) << 6) | b64_dec_alphabet[B(3)]; |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | static inline void __b64_dec3 (const char in[3], uint8_t out[2]) { |
| 269 | out[0] = b64_dec_alphabet[B(0)] << 2 | ((b64_dec_alphabet[B(1)] & 0x30) >> 4); |
| 270 | out[1] = ((b64_dec_alphabet[B(1)] & 0x0f) << 4) | ((b64_dec_alphabet[B(2)] & 0x3c) >> 2); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | static inline void __b64_dec2 (const char in[2], uint8_t out[1]) { |
| 275 | out[0] = ((b64_dec_alphabet[B(0)] & 0x03) << 6) | b64_dec_alphabet[B(1)]; |
| 276 | return; |
| 277 | } |
| 278 | #undef B |
| 279 | |
| 280 | size_t base64_encode (const uint8_t *data, size_t size, char **buffer) { |
| 281 | register uint8_t d = size % 3; |
| 282 | register size_t i, s = size - d; |
| 283 | |
| 284 | *buffer = malloc(sizeof(char) * (1 + (s / 3 * 4) + (d != 0 ? 4 : 0))); |
| 285 | |
| 286 | for (i = 0; i < s/3; ++i) { |
| 287 | __b64_enc3 (data + (i * 3), (*buffer) + (i * 4)); |
| 288 | } |
| 289 | |
| 290 | switch (d) { |
| 291 | case 2: |
| 292 | __b64_enc2 (data + (i * 3), (*buffer) + (i * 4)); |
| 293 | ++i; |
| 294 | break; |
| 295 | case 1: |
| 296 | __b64_enc1 (*(data + (i * 3)), (*buffer) + (i * 4)); |
| 297 | ++i; |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | (*buffer)[(i * 4)] = '\0'; |
| 302 | return (i * 4); |
| 303 | } |
| 304 | |
| 305 | size_t base64_decode (const char *data, size_t size, uint8_t **buffer) { |
| 306 | register size_t i, s = size; |
| 307 | register uint8_t d = 0; |
| 308 | |
| 309 | d = size % 4; |
| 310 | |
| 311 | if (d == 0) { |
| 312 | if (*(data + s - 1) == BASE64_PADDING_CHAR) { |
| 313 | if (*(data + s - 2) == BASE64_PADDING_CHAR) { |
| 314 | d = 2; |
| 315 | } else { |
| 316 | d = 1; |
| 317 | } |
| 318 | |
| 319 | s -= 4; |
| 320 | } |
| 321 | } else { |
| 322 | if (d == 1) { |
| 323 | *buffer = NULL; |
| 324 | return 0; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | *buffer = malloc(sizeof(uint8_t) * ((s / 4 * 3) + d)); |
| 329 | |
| 330 | for (i = 0; i < s/4; ++i) { |
| 331 | __b64_dec4 (data + (i * 4), (*buffer) + (i * 3)); |
| 332 | } |
| 333 | |
| 334 | switch (d) { |
| 335 | case 2: |
| 336 | __b64_dec2 (data + (i * 4), (*buffer) + (i * 3)); |
| 337 | return (i * 3) + 1; |
| 338 | case 1: |
| 339 | __b64_dec3 (data + (i * 4), (*buffer) + (i * 3)); |
| 340 | return (i * 3) + 2; |
| 341 | default: |
| 342 | return (i * 3); |
| 343 | } |
| 344 | } |
| 345 | #endif /* WANT_BASE64 */ |