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