Base64 library borrowed from HTTPServer implementation...

Dependents:   GSMandSdWork

Committer:
ansond
Date:
Thu Dec 18 03:59:35 2014 +0000
Revision:
0:687575a500e5
Child:
1:4920bc5699f3
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:687575a500e5 1 //
ansond 0:687575a500e5 2 // Base64 Encoding and Decoding
ansond 0:687575a500e5 3 //
ansond 0:687575a500e5 4 // @note Copyright © 2013 by Smartware Computing, all rights reserved.
ansond 0:687575a500e5 5 // Individuals may use this application for evaluation or non-commercial
ansond 0:687575a500e5 6 // purposes. Within this restriction, changes may be made to this application
ansond 0:687575a500e5 7 // as long as this copyright notice is retained. The user shall make
ansond 0:687575a500e5 8 // clear that their work is a derived work, and not the original.
ansond 0:687575a500e5 9 // Users of this application and sources accept this application "as is" and
ansond 0:687575a500e5 10 // shall hold harmless Smartware Computing, for any undesired results while
ansond 0:687575a500e5 11 // using this application - whether real or imagined.
ansond 0:687575a500e5 12 //
ansond 0:687575a500e5 13 // author David Smart, Smartware Computing
ansond 0:687575a500e5 14 //
ansond 0:687575a500e5 15 #ifndef WIN32
ansond 0:687575a500e5 16 #include "mbed.h"
ansond 0:687575a500e5 17 #else
ansond 0:687575a500e5 18 #include "windows.h"
ansond 0:687575a500e5 19 typedef unsigned int uint32_t;
ansond 0:687575a500e5 20 #endif
ansond 0:687575a500e5 21 #include "Base64.h"
ansond 0:687575a500e5 22
ansond 0:687575a500e5 23 static const char encoding_table[] = {
ansond 0:687575a500e5 24 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
ansond 0:687575a500e5 25 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
ansond 0:687575a500e5 26 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
ansond 0:687575a500e5 27 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
ansond 0:687575a500e5 28 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
ansond 0:687575a500e5 29 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
ansond 0:687575a500e5 30 'w', 'x', 'y', 'z', '0', '1', '2', '3',
ansond 0:687575a500e5 31 '4', '5', '6', '7', '8', '9', '+', '/'
ansond 0:687575a500e5 32 };
ansond 0:687575a500e5 33
ansond 0:687575a500e5 34 static const int mod_table[] = {0, 2, 1};
ansond 0:687575a500e5 35
ansond 0:687575a500e5 36 Base64::Base64()
ansond 0:687575a500e5 37 {
ansond 0:687575a500e5 38 decoding_table = NULL;
ansond 0:687575a500e5 39 }
ansond 0:687575a500e5 40
ansond 0:687575a500e5 41 Base64::~Base64()
ansond 0:687575a500e5 42 {
ansond 0:687575a500e5 43 if (decoding_table)
ansond 0:687575a500e5 44 free(decoding_table);
ansond 0:687575a500e5 45 }
ansond 0:687575a500e5 46
ansond 0:687575a500e5 47
ansond 0:687575a500e5 48 char * Base64::Encode(const char *data, size_t input_length, size_t *output_length)
ansond 0:687575a500e5 49 {
ansond 0:687575a500e5 50 *output_length = 4 * ((input_length + 2) / 3);
ansond 0:687575a500e5 51
ansond 0:687575a500e5 52 char *encoded_data = (char *)malloc(*output_length+1); // often used for text, so add room for NULL
ansond 0:687575a500e5 53 if (encoded_data == NULL) return NULL;
ansond 0:687575a500e5 54
ansond 0:687575a500e5 55 for (unsigned int i = 0, j = 0; i < input_length;) {
ansond 0:687575a500e5 56
ansond 0:687575a500e5 57 uint32_t octet_a = i < input_length ? data[i++] : 0;
ansond 0:687575a500e5 58 uint32_t octet_b = i < input_length ? data[i++] : 0;
ansond 0:687575a500e5 59 uint32_t octet_c = i < input_length ? data[i++] : 0;
ansond 0:687575a500e5 60
ansond 0:687575a500e5 61 uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
ansond 0:687575a500e5 62
ansond 0:687575a500e5 63 encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
ansond 0:687575a500e5 64 encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
ansond 0:687575a500e5 65 encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
ansond 0:687575a500e5 66 encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
ansond 0:687575a500e5 67 }
ansond 0:687575a500e5 68
ansond 0:687575a500e5 69 for (int i = 0; i < mod_table[input_length % 3]; i++)
ansond 0:687575a500e5 70 encoded_data[*output_length - 1 - i] = '=';
ansond 0:687575a500e5 71
ansond 0:687575a500e5 72 encoded_data[*output_length] = '\0'; // as a courtesy to text users
ansond 0:687575a500e5 73 return encoded_data;
ansond 0:687575a500e5 74 }
ansond 0:687575a500e5 75
ansond 0:687575a500e5 76
ansond 0:687575a500e5 77 char * Base64::Decode(const char *data, size_t input_length, size_t *output_length)
ansond 0:687575a500e5 78 {
ansond 0:687575a500e5 79 if (decoding_table == NULL)
ansond 0:687575a500e5 80 build_decoding_table();
ansond 0:687575a500e5 81
ansond 0:687575a500e5 82 if (input_length % 4 != 0)
ansond 0:687575a500e5 83 return NULL;
ansond 0:687575a500e5 84
ansond 0:687575a500e5 85 *output_length = input_length / 4 * 3;
ansond 0:687575a500e5 86 if (data[input_length - 1] == '=') (*output_length)--;
ansond 0:687575a500e5 87 if (data[input_length - 2] == '=') (*output_length)--;
ansond 0:687575a500e5 88
ansond 0:687575a500e5 89 char *decoded_data = (char *)malloc(*output_length+1); // often used for text, so add room for NULL
ansond 0:687575a500e5 90 if (decoded_data == NULL)
ansond 0:687575a500e5 91 return NULL;
ansond 0:687575a500e5 92
ansond 0:687575a500e5 93 for (unsigned int i = 0, j = 0; i < input_length;) {
ansond 0:687575a500e5 94
ansond 0:687575a500e5 95 uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
ansond 0:687575a500e5 96 uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
ansond 0:687575a500e5 97 uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
ansond 0:687575a500e5 98 uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
ansond 0:687575a500e5 99
ansond 0:687575a500e5 100 uint32_t triple = (sextet_a << 3 * 6)
ansond 0:687575a500e5 101 + (sextet_b << 2 * 6)
ansond 0:687575a500e5 102 + (sextet_c << 1 * 6)
ansond 0:687575a500e5 103 + (sextet_d << 0 * 6);
ansond 0:687575a500e5 104
ansond 0:687575a500e5 105 if (j < *output_length)
ansond 0:687575a500e5 106 decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
ansond 0:687575a500e5 107 if (j < *output_length)
ansond 0:687575a500e5 108 decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
ansond 0:687575a500e5 109 if (j < *output_length)
ansond 0:687575a500e5 110 decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
ansond 0:687575a500e5 111 }
ansond 0:687575a500e5 112 decoded_data[*output_length] = '\0'; // as a courtesy to text users
ansond 0:687575a500e5 113 return decoded_data;
ansond 0:687575a500e5 114 }
ansond 0:687575a500e5 115
ansond 0:687575a500e5 116
ansond 0:687575a500e5 117 void Base64::build_decoding_table()
ansond 0:687575a500e5 118 {
ansond 0:687575a500e5 119 decoding_table = (char *)malloc(256);
ansond 0:687575a500e5 120
ansond 0:687575a500e5 121 for (int i = 0; i < 64; i++)
ansond 0:687575a500e5 122 decoding_table[(unsigned char) encoding_table[i]] = i;
ansond 0:687575a500e5 123 }