A simple web server that can be bound to either the EthernetInterface or the WiflyInterface.
Dependents: Smart-WiFly-WebServer WattEye X10Svr SSDP_Server
Base64.cpp@13:8975d7928678, 2013-08-12 (annotated)
- Committer:
- WiredHome
- Date:
- Mon Aug 12 11:26:59 2013 +0000
- Revision:
- 13:8975d7928678
- Parent:
- 12:109bf1558300
- Child:
- 14:19c5f6151319
Added Base64 Encode/Decode for simple password management
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WiredHome | 13:8975d7928678 | 1 | #ifndef WIN32 |
WiredHome | 12:109bf1558300 | 2 | #include "mbed.h" |
WiredHome | 13:8975d7928678 | 3 | #else |
WiredHome | 13:8975d7928678 | 4 | #include "windows.h" |
WiredHome | 13:8975d7928678 | 5 | typedef unsigned int uint32_t; |
WiredHome | 13:8975d7928678 | 6 | #endif |
WiredHome | 12:109bf1558300 | 7 | #include "Base64.h" |
WiredHome | 12:109bf1558300 | 8 | |
WiredHome | 12:109bf1558300 | 9 | static const char encoding_table[] = { |
WiredHome | 12:109bf1558300 | 10 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
WiredHome | 12:109bf1558300 | 11 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
WiredHome | 12:109bf1558300 | 12 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
WiredHome | 12:109bf1558300 | 13 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
WiredHome | 12:109bf1558300 | 14 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
WiredHome | 12:109bf1558300 | 15 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
WiredHome | 12:109bf1558300 | 16 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
WiredHome | 12:109bf1558300 | 17 | '4', '5', '6', '7', '8', '9', '+', '/' |
WiredHome | 12:109bf1558300 | 18 | }; |
WiredHome | 12:109bf1558300 | 19 | |
WiredHome | 12:109bf1558300 | 20 | static const int mod_table[] = {0, 2, 1}; |
WiredHome | 12:109bf1558300 | 21 | |
WiredHome | 12:109bf1558300 | 22 | Base64::Base64() |
WiredHome | 12:109bf1558300 | 23 | { |
WiredHome | 12:109bf1558300 | 24 | decoding_table = NULL; |
WiredHome | 12:109bf1558300 | 25 | } |
WiredHome | 12:109bf1558300 | 26 | |
WiredHome | 12:109bf1558300 | 27 | Base64::~Base64() |
WiredHome | 12:109bf1558300 | 28 | { |
WiredHome | 12:109bf1558300 | 29 | if (decoding_table) |
WiredHome | 12:109bf1558300 | 30 | free(decoding_table); |
WiredHome | 12:109bf1558300 | 31 | } |
WiredHome | 12:109bf1558300 | 32 | |
WiredHome | 12:109bf1558300 | 33 | |
WiredHome | 13:8975d7928678 | 34 | char * Base64::Encode(const char *data, size_t input_length, size_t *output_length) |
WiredHome | 12:109bf1558300 | 35 | { |
WiredHome | 12:109bf1558300 | 36 | *output_length = 4 * ((input_length + 2) / 3); |
WiredHome | 12:109bf1558300 | 37 | |
WiredHome | 13:8975d7928678 | 38 | char *encoded_data = (char *)malloc(*output_length+1); // often used for text, so add room for NULL |
WiredHome | 12:109bf1558300 | 39 | if (encoded_data == NULL) return NULL; |
WiredHome | 12:109bf1558300 | 40 | |
WiredHome | 13:8975d7928678 | 41 | for (unsigned int i = 0, j = 0; i < input_length;) { |
WiredHome | 12:109bf1558300 | 42 | |
WiredHome | 12:109bf1558300 | 43 | uint32_t octet_a = i < input_length ? data[i++] : 0; |
WiredHome | 12:109bf1558300 | 44 | uint32_t octet_b = i < input_length ? data[i++] : 0; |
WiredHome | 12:109bf1558300 | 45 | uint32_t octet_c = i < input_length ? data[i++] : 0; |
WiredHome | 12:109bf1558300 | 46 | |
WiredHome | 12:109bf1558300 | 47 | uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; |
WiredHome | 12:109bf1558300 | 48 | |
WiredHome | 12:109bf1558300 | 49 | encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F]; |
WiredHome | 12:109bf1558300 | 50 | encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F]; |
WiredHome | 12:109bf1558300 | 51 | encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F]; |
WiredHome | 12:109bf1558300 | 52 | encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F]; |
WiredHome | 12:109bf1558300 | 53 | } |
WiredHome | 12:109bf1558300 | 54 | |
WiredHome | 12:109bf1558300 | 55 | for (int i = 0; i < mod_table[input_length % 3]; i++) |
WiredHome | 12:109bf1558300 | 56 | encoded_data[*output_length - 1 - i] = '='; |
WiredHome | 12:109bf1558300 | 57 | |
WiredHome | 13:8975d7928678 | 58 | encoded_data[*output_length] = '\0'; // as a courtesy to text users |
WiredHome | 12:109bf1558300 | 59 | return encoded_data; |
WiredHome | 12:109bf1558300 | 60 | } |
WiredHome | 12:109bf1558300 | 61 | |
WiredHome | 12:109bf1558300 | 62 | |
WiredHome | 13:8975d7928678 | 63 | char * Base64::Decode(const char *data, size_t input_length, size_t *output_length) |
WiredHome | 12:109bf1558300 | 64 | { |
WiredHome | 12:109bf1558300 | 65 | if (decoding_table == NULL) |
WiredHome | 12:109bf1558300 | 66 | build_decoding_table(); |
WiredHome | 12:109bf1558300 | 67 | |
WiredHome | 12:109bf1558300 | 68 | if (input_length % 4 != 0) |
WiredHome | 12:109bf1558300 | 69 | return NULL; |
WiredHome | 12:109bf1558300 | 70 | |
WiredHome | 12:109bf1558300 | 71 | *output_length = input_length / 4 * 3; |
WiredHome | 12:109bf1558300 | 72 | if (data[input_length - 1] == '=') (*output_length)--; |
WiredHome | 12:109bf1558300 | 73 | if (data[input_length - 2] == '=') (*output_length)--; |
WiredHome | 12:109bf1558300 | 74 | |
WiredHome | 13:8975d7928678 | 75 | char *decoded_data = (char *)malloc(*output_length+1); // often used for text, so add room for NULL |
WiredHome | 12:109bf1558300 | 76 | if (decoded_data == NULL) |
WiredHome | 12:109bf1558300 | 77 | return NULL; |
WiredHome | 12:109bf1558300 | 78 | |
WiredHome | 13:8975d7928678 | 79 | for (unsigned int i = 0, j = 0; i < input_length;) { |
WiredHome | 12:109bf1558300 | 80 | |
WiredHome | 12:109bf1558300 | 81 | uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]]; |
WiredHome | 12:109bf1558300 | 82 | uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]]; |
WiredHome | 12:109bf1558300 | 83 | uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]]; |
WiredHome | 12:109bf1558300 | 84 | uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]]; |
WiredHome | 12:109bf1558300 | 85 | |
WiredHome | 12:109bf1558300 | 86 | uint32_t triple = (sextet_a << 3 * 6) |
WiredHome | 12:109bf1558300 | 87 | + (sextet_b << 2 * 6) |
WiredHome | 12:109bf1558300 | 88 | + (sextet_c << 1 * 6) |
WiredHome | 12:109bf1558300 | 89 | + (sextet_d << 0 * 6); |
WiredHome | 12:109bf1558300 | 90 | |
WiredHome | 12:109bf1558300 | 91 | if (j < *output_length) |
WiredHome | 12:109bf1558300 | 92 | decoded_data[j++] = (triple >> 2 * 8) & 0xFF; |
WiredHome | 12:109bf1558300 | 93 | if (j < *output_length) |
WiredHome | 12:109bf1558300 | 94 | decoded_data[j++] = (triple >> 1 * 8) & 0xFF; |
WiredHome | 12:109bf1558300 | 95 | if (j < *output_length) |
WiredHome | 12:109bf1558300 | 96 | decoded_data[j++] = (triple >> 0 * 8) & 0xFF; |
WiredHome | 12:109bf1558300 | 97 | } |
WiredHome | 13:8975d7928678 | 98 | decoded_data[*output_length] = '\0'; // as a courtesy to text users |
WiredHome | 12:109bf1558300 | 99 | return decoded_data; |
WiredHome | 12:109bf1558300 | 100 | } |
WiredHome | 12:109bf1558300 | 101 | |
WiredHome | 12:109bf1558300 | 102 | |
WiredHome | 12:109bf1558300 | 103 | void Base64::build_decoding_table() |
WiredHome | 12:109bf1558300 | 104 | { |
WiredHome | 12:109bf1558300 | 105 | decoding_table = (char *)malloc(256); |
WiredHome | 12:109bf1558300 | 106 | |
WiredHome | 12:109bf1558300 | 107 | for (int i = 0; i < 64; i++) |
WiredHome | 12:109bf1558300 | 108 | decoding_table[(unsigned char) encoding_table[i]] = i; |
WiredHome | 12:109bf1558300 | 109 | } |