A simple web server that can be bound to either the EthernetInterface or the WiflyInterface.

Dependents:   Smart-WiFly-WebServer WattEye X10Svr SSDP_Server

Committer:
WiredHome
Date:
Wed Feb 27 18:06:23 2019 +0000
Revision:
60:d49a346c386f
Parent:
44:71f09e4255f4
Enable header code 302

Who changed what in which revision?

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