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