Base64 library borrowed from HTTPServer implementation...

Dependents:   GSMandSdWork

Committer:
ansond
Date:
Tue Dec 30 03:17:43 2014 +0000
Revision:
1:4920bc5699f3
Parent:
0:687575a500e5
updated header

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 1:4920bc5699f3 1 /**
ansond 1:4920bc5699f3 2 * @file Base64.cpp
ansond 1:4920bc5699f3 3 * @brief Base64 encoding and decoding (DERIVED WORK)
ansond 1:4920bc5699f3 4 * @author David Smart, Smartware Computing, Doug Anson ARM
ansond 1:4920bc5699f3 5 * @version 1.0
ansond 1:4920bc5699f3 6 * @see
ansond 1:4920bc5699f3 7 *
ansond 1:4920bc5699f3 8 * Copyright (c) 2014
ansond 1:4920bc5699f3 9 *
ansond 1:4920bc5699f3 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 1:4920bc5699f3 11 * you may not use this file except in compliance with the License.
ansond 1:4920bc5699f3 12 * You may obtain a copy of the License at
ansond 1:4920bc5699f3 13 *
ansond 1:4920bc5699f3 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 1:4920bc5699f3 15 *
ansond 1:4920bc5699f3 16 * Unless required by applicable law or agreed to in writing, software
ansond 1:4920bc5699f3 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 1:4920bc5699f3 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 1:4920bc5699f3 19 * See the License for the specific language governing permissions and
ansond 1:4920bc5699f3 20 * limitations under the License.
ansond 1:4920bc5699f3 21 *
ansond 1:4920bc5699f3 22 * @note Copyright © 2013 by Smartware Computing, all rights reserved.
ansond 1:4920bc5699f3 23 * Individuals may use this application for evaluation or non-commercial
ansond 1:4920bc5699f3 24 * purposes. Within this restriction, changes may be made to this application
ansond 1:4920bc5699f3 25 * as long as this copyright notice is retained. The user shall make
ansond 1:4920bc5699f3 26 * clear that their work is a derived work, and not the original.
ansond 1:4920bc5699f3 27 * Users of this application and sources accept this application "as is" and
ansond 1:4920bc5699f3 28 * shall hold harmless Smartware Computing, for any undesired results while
ansond 1:4920bc5699f3 29 * using this application - whether real or imagined.
ansond 1:4920bc5699f3 30 *
ansond 1:4920bc5699f3 31 * author David Smart, Smartware Computing
ansond 1:4920bc5699f3 32 */
ansond 1:4920bc5699f3 33
ansond 0:687575a500e5 34 #ifndef WIN32
ansond 0:687575a500e5 35 #include "mbed.h"
ansond 0:687575a500e5 36 #else
ansond 0:687575a500e5 37 #include "windows.h"
ansond 0:687575a500e5 38 typedef unsigned int uint32_t;
ansond 0:687575a500e5 39 #endif
ansond 0:687575a500e5 40 #include "Base64.h"
ansond 0:687575a500e5 41
ansond 0:687575a500e5 42 static const char encoding_table[] = {
ansond 0:687575a500e5 43 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
ansond 0:687575a500e5 44 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
ansond 0:687575a500e5 45 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
ansond 0:687575a500e5 46 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
ansond 0:687575a500e5 47 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
ansond 0:687575a500e5 48 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
ansond 0:687575a500e5 49 'w', 'x', 'y', 'z', '0', '1', '2', '3',
ansond 0:687575a500e5 50 '4', '5', '6', '7', '8', '9', '+', '/'
ansond 0:687575a500e5 51 };
ansond 0:687575a500e5 52
ansond 0:687575a500e5 53 static const int mod_table[] = {0, 2, 1};
ansond 0:687575a500e5 54
ansond 0:687575a500e5 55 Base64::Base64()
ansond 0:687575a500e5 56 {
ansond 0:687575a500e5 57 decoding_table = NULL;
ansond 0:687575a500e5 58 }
ansond 0:687575a500e5 59
ansond 0:687575a500e5 60 Base64::~Base64()
ansond 0:687575a500e5 61 {
ansond 0:687575a500e5 62 if (decoding_table)
ansond 0:687575a500e5 63 free(decoding_table);
ansond 0:687575a500e5 64 }
ansond 0:687575a500e5 65
ansond 0:687575a500e5 66
ansond 0:687575a500e5 67 char * Base64::Encode(const char *data, size_t input_length, size_t *output_length)
ansond 0:687575a500e5 68 {
ansond 0:687575a500e5 69 *output_length = 4 * ((input_length + 2) / 3);
ansond 0:687575a500e5 70
ansond 0:687575a500e5 71 char *encoded_data = (char *)malloc(*output_length+1); // often used for text, so add room for NULL
ansond 0:687575a500e5 72 if (encoded_data == NULL) return NULL;
ansond 0:687575a500e5 73
ansond 0:687575a500e5 74 for (unsigned int i = 0, j = 0; i < input_length;) {
ansond 0:687575a500e5 75
ansond 0:687575a500e5 76 uint32_t octet_a = i < input_length ? data[i++] : 0;
ansond 0:687575a500e5 77 uint32_t octet_b = i < input_length ? data[i++] : 0;
ansond 0:687575a500e5 78 uint32_t octet_c = i < input_length ? data[i++] : 0;
ansond 0:687575a500e5 79
ansond 0:687575a500e5 80 uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
ansond 0:687575a500e5 81
ansond 0:687575a500e5 82 encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
ansond 0:687575a500e5 83 encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
ansond 0:687575a500e5 84 encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
ansond 0:687575a500e5 85 encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
ansond 0:687575a500e5 86 }
ansond 0:687575a500e5 87
ansond 0:687575a500e5 88 for (int i = 0; i < mod_table[input_length % 3]; i++)
ansond 0:687575a500e5 89 encoded_data[*output_length - 1 - i] = '=';
ansond 0:687575a500e5 90
ansond 0:687575a500e5 91 encoded_data[*output_length] = '\0'; // as a courtesy to text users
ansond 0:687575a500e5 92 return encoded_data;
ansond 0:687575a500e5 93 }
ansond 0:687575a500e5 94
ansond 0:687575a500e5 95
ansond 0:687575a500e5 96 char * Base64::Decode(const char *data, size_t input_length, size_t *output_length)
ansond 0:687575a500e5 97 {
ansond 0:687575a500e5 98 if (decoding_table == NULL)
ansond 0:687575a500e5 99 build_decoding_table();
ansond 0:687575a500e5 100
ansond 0:687575a500e5 101 if (input_length % 4 != 0)
ansond 0:687575a500e5 102 return NULL;
ansond 0:687575a500e5 103
ansond 0:687575a500e5 104 *output_length = input_length / 4 * 3;
ansond 0:687575a500e5 105 if (data[input_length - 1] == '=') (*output_length)--;
ansond 0:687575a500e5 106 if (data[input_length - 2] == '=') (*output_length)--;
ansond 0:687575a500e5 107
ansond 0:687575a500e5 108 char *decoded_data = (char *)malloc(*output_length+1); // often used for text, so add room for NULL
ansond 0:687575a500e5 109 if (decoded_data == NULL)
ansond 0:687575a500e5 110 return NULL;
ansond 0:687575a500e5 111
ansond 0:687575a500e5 112 for (unsigned int i = 0, j = 0; i < input_length;) {
ansond 0:687575a500e5 113
ansond 0:687575a500e5 114 uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
ansond 0:687575a500e5 115 uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
ansond 0:687575a500e5 116 uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
ansond 0:687575a500e5 117 uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
ansond 0:687575a500e5 118
ansond 0:687575a500e5 119 uint32_t triple = (sextet_a << 3 * 6)
ansond 0:687575a500e5 120 + (sextet_b << 2 * 6)
ansond 0:687575a500e5 121 + (sextet_c << 1 * 6)
ansond 0:687575a500e5 122 + (sextet_d << 0 * 6);
ansond 0:687575a500e5 123
ansond 0:687575a500e5 124 if (j < *output_length)
ansond 0:687575a500e5 125 decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
ansond 0:687575a500e5 126 if (j < *output_length)
ansond 0:687575a500e5 127 decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
ansond 0:687575a500e5 128 if (j < *output_length)
ansond 0:687575a500e5 129 decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
ansond 0:687575a500e5 130 }
ansond 0:687575a500e5 131 decoded_data[*output_length] = '\0'; // as a courtesy to text users
ansond 0:687575a500e5 132 return decoded_data;
ansond 0:687575a500e5 133 }
ansond 0:687575a500e5 134
ansond 0:687575a500e5 135
ansond 0:687575a500e5 136 void Base64::build_decoding_table()
ansond 0:687575a500e5 137 {
ansond 0:687575a500e5 138 decoding_table = (char *)malloc(256);
ansond 0:687575a500e5 139
ansond 0:687575a500e5 140 for (int i = 0; i < 64; i++)
ansond 0:687575a500e5 141 decoding_table[(unsigned char) encoding_table[i]] = i;
ansond 0:687575a500e5 142 }