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 #ifndef BASE64_H
ansond 0:687575a500e5 2 #define BASE64_H
ansond 0:687575a500e5 3
ansond 0:687575a500e5 4 #include "mbed.h"
ansond 0:687575a500e5 5
ansond 0:687575a500e5 6 /** Base64 encoder and decoder
ansond 0:687575a500e5 7 *
ansond 0:687575a500e5 8 * This class provided both encoding and decoding functions. These functions
ansond 0:687575a500e5 9 * perform dynamic memory allocations to create space for the translated
ansond 0:687575a500e5 10 * response. It is up to the calling function to free the space when
ansond 0:687575a500e5 11 * done with the translation.
ansond 0:687575a500e5 12 *
ansond 0:687575a500e5 13 * This code was derived from code found online that did not have any
ansond 0:687575a500e5 14 * copyright or reference to its work.
ansond 0:687575a500e5 15 *
ansond 0:687575a500e5 16 * @code
ansond 0:687575a500e5 17 * Base64 n;
ansond 0:687575a500e5 18 *
ansond 0:687575a500e5 19 * size_t encodedLen;
ansond 0:687575a500e5 20 * char *encoded = n.Encode("This is the message", 20, &encodedLen);
ansond 0:687575a500e5 21 * printf("Encoded message is {%s}\r\n", encoded);
ansond 0:687575a500e5 22 * @endcode
ansond 0:687575a500e5 23 */
ansond 0:687575a500e5 24 class Base64
ansond 0:687575a500e5 25 {
ansond 0:687575a500e5 26 public:
ansond 0:687575a500e5 27 /** Constructor
ansond 0:687575a500e5 28 *
ansond 0:687575a500e5 29 */
ansond 0:687575a500e5 30 Base64();
ansond 0:687575a500e5 31
ansond 0:687575a500e5 32 /** Destructor
ansond 0:687575a500e5 33 *
ansond 0:687575a500e5 34 * This will release memory that may have been allocated (when the Decode
ansond 0:687575a500e5 35 * function was called).
ansond 0:687575a500e5 36 */
ansond 0:687575a500e5 37 ~Base64();
ansond 0:687575a500e5 38
ansond 0:687575a500e5 39 /** Encodes a string of information of a defined length
ansond 0:687575a500e5 40 *
ansond 0:687575a500e5 41 * The encoded information is considered a binary stream, therefore a length is provided
ansond 0:687575a500e5 42 * which is used to compute the amount of memory to allocate for the conversion.
ansond 0:687575a500e5 43 *
ansond 0:687575a500e5 44 * @note The Decode method does not know how you are using it - if it is for text,
ansond 0:687575a500e5 45 * it will not apply any null termination, unless that was part of the input.
ansond 0:687575a500e5 46 *
ansond 0:687575a500e5 47 * @param data is a pointer to the input binary stream.
ansond 0:687575a500e5 48 * @param input_length is the number of bytes to process.
ansond 0:687575a500e5 49 * @param output_length is a pointer to a size_t value into which is written the
ansond 0:687575a500e5 50 * number of bytes in the output.
ansond 0:687575a500e5 51 *
ansond 0:687575a500e5 52 * @returns a pointer to the allocated block of memory holding the converted results.
ansond 0:687575a500e5 53 * @returns NULL if something went very wrong.
ansond 0:687575a500e5 54 */
ansond 0:687575a500e5 55 char *Encode(const char *data, size_t input_length, size_t *output_length);
ansond 0:687575a500e5 56
ansond 0:687575a500e5 57 /** Decodes a base64 encoded stream back into the original information.
ansond 0:687575a500e5 58 *
ansond 0:687575a500e5 59 * The information to decode is considered a base64 encoded stream. A length is
ansond 0:687575a500e5 60 * provided which is used to compute the amount of memory to allocate for the conversion.
ansond 0:687575a500e5 61 *
ansond 0:687575a500e5 62 * @note The Decode method does not know how you are using it - if it is for text,
ansond 0:687575a500e5 63 * it will not apply any null termination, unless that was part of the input.
ansond 0:687575a500e5 64 *
ansond 0:687575a500e5 65 * @param data is a pointer to the encoded data to decode.
ansond 0:687575a500e5 66 * @param input_length is the number of bytes to process.
ansond 0:687575a500e5 67 * @param output_length is a pointer to a size_t value into which is written the
ansond 0:687575a500e5 68 * number of bytes in the output.
ansond 0:687575a500e5 69 *
ansond 0:687575a500e5 70 * @returns a pointer to the allocated block of memory holding the converted results.
ansond 0:687575a500e5 71 * @returns NULL if something went very wrong.
ansond 0:687575a500e5 72 */
ansond 0:687575a500e5 73 char *Decode(const char *data, size_t input_length, size_t *output_length);
ansond 0:687575a500e5 74
ansond 0:687575a500e5 75 private:
ansond 0:687575a500e5 76 void build_decoding_table();
ansond 0:687575a500e5 77 char *decoding_table;
ansond 0:687575a500e5 78 };
ansond 0:687575a500e5 79
ansond 0:687575a500e5 80 #endif // BASE64_H