Base64 library borrowed from HTTPServer implementation...
Base64.h@1:4920bc5699f3, 2014-12-30 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 1:4920bc5699f3 | 1 | /** |
ansond | 1:4920bc5699f3 | 2 | * @file Base64.h |
ansond | 1:4920bc5699f3 | 3 | * @brief Base64 encoding and decoding header (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 BASE64_H |
ansond | 0:687575a500e5 | 35 | #define BASE64_H |
ansond | 0:687575a500e5 | 36 | |
ansond | 0:687575a500e5 | 37 | #include "mbed.h" |
ansond | 0:687575a500e5 | 38 | |
ansond | 0:687575a500e5 | 39 | /** Base64 encoder and decoder |
ansond | 0:687575a500e5 | 40 | * |
ansond | 0:687575a500e5 | 41 | * This class provided both encoding and decoding functions. These functions |
ansond | 0:687575a500e5 | 42 | * perform dynamic memory allocations to create space for the translated |
ansond | 0:687575a500e5 | 43 | * response. It is up to the calling function to free the space when |
ansond | 0:687575a500e5 | 44 | * done with the translation. |
ansond | 0:687575a500e5 | 45 | * |
ansond | 0:687575a500e5 | 46 | * This code was derived from code found online that did not have any |
ansond | 0:687575a500e5 | 47 | * copyright or reference to its work. |
ansond | 0:687575a500e5 | 48 | * |
ansond | 0:687575a500e5 | 49 | * @code |
ansond | 0:687575a500e5 | 50 | * Base64 n; |
ansond | 0:687575a500e5 | 51 | * |
ansond | 0:687575a500e5 | 52 | * size_t encodedLen; |
ansond | 0:687575a500e5 | 53 | * char *encoded = n.Encode("This is the message", 20, &encodedLen); |
ansond | 0:687575a500e5 | 54 | * printf("Encoded message is {%s}\r\n", encoded); |
ansond | 0:687575a500e5 | 55 | * @endcode |
ansond | 0:687575a500e5 | 56 | */ |
ansond | 0:687575a500e5 | 57 | class Base64 |
ansond | 0:687575a500e5 | 58 | { |
ansond | 0:687575a500e5 | 59 | public: |
ansond | 0:687575a500e5 | 60 | /** Constructor |
ansond | 0:687575a500e5 | 61 | * |
ansond | 0:687575a500e5 | 62 | */ |
ansond | 0:687575a500e5 | 63 | Base64(); |
ansond | 0:687575a500e5 | 64 | |
ansond | 0:687575a500e5 | 65 | /** Destructor |
ansond | 0:687575a500e5 | 66 | * |
ansond | 0:687575a500e5 | 67 | * This will release memory that may have been allocated (when the Decode |
ansond | 0:687575a500e5 | 68 | * function was called). |
ansond | 0:687575a500e5 | 69 | */ |
ansond | 0:687575a500e5 | 70 | ~Base64(); |
ansond | 0:687575a500e5 | 71 | |
ansond | 0:687575a500e5 | 72 | /** Encodes a string of information of a defined length |
ansond | 0:687575a500e5 | 73 | * |
ansond | 0:687575a500e5 | 74 | * The encoded information is considered a binary stream, therefore a length is provided |
ansond | 0:687575a500e5 | 75 | * which is used to compute the amount of memory to allocate for the conversion. |
ansond | 0:687575a500e5 | 76 | * |
ansond | 0:687575a500e5 | 77 | * @note The Decode method does not know how you are using it - if it is for text, |
ansond | 0:687575a500e5 | 78 | * it will not apply any null termination, unless that was part of the input. |
ansond | 0:687575a500e5 | 79 | * |
ansond | 0:687575a500e5 | 80 | * @param data is a pointer to the input binary stream. |
ansond | 0:687575a500e5 | 81 | * @param input_length is the number of bytes to process. |
ansond | 0:687575a500e5 | 82 | * @param output_length is a pointer to a size_t value into which is written the |
ansond | 0:687575a500e5 | 83 | * number of bytes in the output. |
ansond | 0:687575a500e5 | 84 | * |
ansond | 0:687575a500e5 | 85 | * @returns a pointer to the allocated block of memory holding the converted results. |
ansond | 0:687575a500e5 | 86 | * @returns NULL if something went very wrong. |
ansond | 0:687575a500e5 | 87 | */ |
ansond | 0:687575a500e5 | 88 | char *Encode(const char *data, size_t input_length, size_t *output_length); |
ansond | 0:687575a500e5 | 89 | |
ansond | 0:687575a500e5 | 90 | /** Decodes a base64 encoded stream back into the original information. |
ansond | 0:687575a500e5 | 91 | * |
ansond | 0:687575a500e5 | 92 | * The information to decode is considered a base64 encoded stream. A length is |
ansond | 0:687575a500e5 | 93 | * provided which is used to compute the amount of memory to allocate for the conversion. |
ansond | 0:687575a500e5 | 94 | * |
ansond | 0:687575a500e5 | 95 | * @note The Decode method does not know how you are using it - if it is for text, |
ansond | 0:687575a500e5 | 96 | * it will not apply any null termination, unless that was part of the input. |
ansond | 0:687575a500e5 | 97 | * |
ansond | 0:687575a500e5 | 98 | * @param data is a pointer to the encoded data to decode. |
ansond | 0:687575a500e5 | 99 | * @param input_length is the number of bytes to process. |
ansond | 0:687575a500e5 | 100 | * @param output_length is a pointer to a size_t value into which is written the |
ansond | 0:687575a500e5 | 101 | * number of bytes in the output. |
ansond | 0:687575a500e5 | 102 | * |
ansond | 0:687575a500e5 | 103 | * @returns a pointer to the allocated block of memory holding the converted results. |
ansond | 0:687575a500e5 | 104 | * @returns NULL if something went very wrong. |
ansond | 0:687575a500e5 | 105 | */ |
ansond | 0:687575a500e5 | 106 | char *Decode(const char *data, size_t input_length, size_t *output_length); |
ansond | 0:687575a500e5 | 107 | |
ansond | 0:687575a500e5 | 108 | private: |
ansond | 0:687575a500e5 | 109 | void build_decoding_table(); |
ansond | 0:687575a500e5 | 110 | char *decoding_table; |
ansond | 0:687575a500e5 | 111 | }; |
ansond | 0:687575a500e5 | 112 | |
ansond | 0:687575a500e5 | 113 | #endif // BASE64_H |