| OLD | NEW |
| 1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */ | 1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */ |
| 2 /* vi: set expandtab shiftwidth=4 tabstop=4: */ | 2 /* vi: set expandtab shiftwidth=4 tabstop=4: */ |
| 3 | 3 |
| 4 /** | 4 /** |
| 5 * \file | 5 * \file |
| 6 * <PRE> | 6 * <PRE> |
| 7 * High performance base64 encoder / decoder | 7 * High performance base64 encoder / decoder |
| 8 * Version 1.3 -- 17-Mar-2006 | 8 * Version 1.3 -- 17-Mar-2006 |
| 9 * | 9 * |
| 10 * Copyright © 2005, 2006, Nick Galbreath -- nickg [at] modp [dot] com | 10 * Copyright © 2005, 2006, Nick Galbreath -- nickg [at] modp [dot] com |
| 11 * All rights reserved. | 11 * All rights reserved. |
| 12 * | 12 * |
| 13 * http://modp.com/release/base64 | 13 * http://modp.com/release/base64 |
| 14 * | 14 * |
| 15 * Released under bsd license. See modp_b64.c for details. | 15 * Released under bsd license. See modp_b64.c for details. |
| 16 * </pre> | 16 * </pre> |
| 17 * | 17 * |
| 18 * The default implementation is the standard b64 encoding with padding. | 18 * The default implementation is the standard b64 encoding with padding. |
| 19 * It's easy to change this to use "URL safe" characters and to remove | 19 * It's easy to change this to use "URL safe" characters and to remove |
| 20 * padding. See the modp_b64.c source code for details. | 20 * padding. See the modp_b64.c source code for details. |
| 21 * | 21 * |
| 22 */ | 22 */ |
| 23 | 23 |
| 24 #ifndef MODP_B64 | 24 #ifndef MODP_B64 |
| 25 #define MODP_B64 | 25 #define MODP_B64 |
| 26 | 26 |
| 27 #include <stddef.h> |
| 28 |
| 27 #ifdef __cplusplus | 29 #ifdef __cplusplus |
| 28 extern "C" { | 30 extern "C" { |
| 29 #endif | 31 #endif |
| 30 | 32 |
| 31 /** | 33 /** |
| 32 * Encode a raw binary string into base 64. | 34 * Encode a raw binary string into base 64. |
| 33 * src contains the bytes | 35 * src contains the bytes |
| 34 * len contains the number of bytes in the src | 36 * len contains the number of bytes in the src |
| 35 * dest should be allocated by the caller to contain | 37 * dest should be allocated by the caller to contain |
| 36 * at least modp_b64_encode_len(len) bytes (see below) | 38 * at least modp_b64_encode_len(len) bytes (see below) |
| 37 * This will contain the null-terminated b64 encoded result | 39 * This will contain the null-terminated b64 encoded result |
| 38 * returns length of the destination string plus the ending null byte | 40 * returns length of the destination string plus the ending null byte |
| 39 * i.e. the result will be equal to strlen(dest) + 1 | 41 * i.e. the result will be equal to strlen(dest) + 1 |
| 40 * | 42 * |
| 41 * Example | 43 * Example |
| 42 * | 44 * |
| 43 * \code | 45 * \code |
| 44 * char* src = ...; | 46 * char* src = ...; |
| 45 * int srclen = ...; //the length of number of bytes in src | 47 * int srclen = ...; //the length of number of bytes in src |
| 46 * char* dest = (char*) malloc(modp_b64_encode_len); | 48 * char* dest = (char*) malloc(modp_b64_encode_len); |
| 47 * int len = modp_b64_encode(dest, src, sourcelen); | 49 * int len = modp_b64_encode(dest, src, sourcelen); |
| 48 * if (len == -1) { | 50 * if (len == -1) { |
| 49 * printf("Error\n"); | 51 * printf("Error\n"); |
| 50 * } else { | 52 * } else { |
| 51 * printf("b64 = %s\n", dest); | 53 * printf("b64 = %s\n", dest); |
| 52 * } | 54 * } |
| 53 * \endcode | 55 * \endcode |
| 54 * | 56 * |
| 55 */ | 57 */ |
| 56 int modp_b64_encode(char* dest, const char* str, int len); | 58 size_t modp_b64_encode(char* dest, const char* str, size_t len); |
| 57 | 59 |
| 58 /** | 60 /** |
| 59 * Decode a base64 encoded string | 61 * Decode a base64 encoded string |
| 60 * | 62 * |
| 61 * src should contain exactly len bytes of b64 characters. | 63 * src should contain exactly len bytes of b64 characters. |
| 62 * if src contains -any- non-base characters (such as white | 64 * if src contains -any- non-base characters (such as white |
| 63 * space, -1 is returned. | 65 * space, -1 is returned. |
| 64 * | 66 * |
| 65 * dest should be allocated by the caller to contain at least | 67 * dest should be allocated by the caller to contain at least |
| 66 * len * 3 / 4 bytes. | 68 * len * 3 / 4 bytes. |
| 67 * | 69 * |
| 68 * Returns the length (strlen) of the output, or -1 if unable to | 70 * Returns the length (strlen) of the output, or -1 if unable to |
| 69 * decode | 71 * decode |
| 70 * | 72 * |
| 71 * \code | 73 * \code |
| 72 * char* src = ...; | 74 * char* src = ...; |
| 73 * int srclen = ...; // or if you don't know use strlen(src) | 75 * int srclen = ...; // or if you don't know use strlen(src) |
| 74 * char* dest = (char*) malloc(modp_b64_decode_len(srclen)); | 76 * char* dest = (char*) malloc(modp_b64_decode_len(srclen)); |
| 75 * int len = modp_b64_decode(dest, src, sourcelen); | 77 * int len = modp_b64_decode(dest, src, sourcelen); |
| 76 * if (len == -1) { error } | 78 * if (len == -1) { error } |
| 77 * \endcode | 79 * \endcode |
| 78 */ | 80 */ |
| 79 int modp_b64_decode(char* dest, const char* src, int len); | 81 size_t modp_b64_decode(char* dest, const char* src, size_t len); |
| 80 | 82 |
| 81 /** | 83 /** |
| 82 * Given a source string of length len, this returns the amount of | 84 * Given a source string of length len, this returns the amount of |
| 83 * memory the destination string should have. | 85 * memory the destination string should have. |
| 84 * | 86 * |
| 85 * remember, this is integer math | 87 * remember, this is integer math |
| 86 * 3 bytes turn into 4 chars | 88 * 3 bytes turn into 4 chars |
| 87 * ceiling[len / 3] * 4 + 1 | 89 * ceiling[len / 3] * 4 + 1 |
| 88 * | 90 * |
| 89 * +1 is for any extra null. | 91 * +1 is for any extra null. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 119 * if (modp_b64_decode((char*) &foo, b64encoded, len) == -1) { | 121 * if (modp_b64_decode((char*) &foo, b64encoded, len) == -1) { |
| 120 * // bad characters | 122 * // bad characters |
| 121 * return false; | 123 * return false; |
| 122 * } | 124 * } |
| 123 * } | 125 * } |
| 124 * // foo is filled out now | 126 * // foo is filled out now |
| 125 * \endcode | 127 * \endcode |
| 126 */ | 128 */ |
| 127 #define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4) | 129 #define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4) |
| 128 | 130 |
| 131 #define MODP_B64_ERROR ((size_t)-1) |
| 132 |
| 129 #ifdef __cplusplus | 133 #ifdef __cplusplus |
| 130 } | 134 } |
| 131 | 135 |
| 132 #include <string> | 136 #include <string> |
| 133 | 137 |
| 134 inline std::string& modp_b64_encode(std::string& s) | 138 inline std::string& modp_b64_encode(std::string& s) |
| 135 { | 139 { |
| 136 std::string x(modp_b64_encode_len(s.size()), '\0'); | 140 std::string x(modp_b64_encode_len(s.size()), '\0'); |
| 137 int d = modp_b64_encode(const_cast<char*>(x.data()), s.data(), s.size()); | 141 size_t d = modp_b64_encode(const_cast<char*>(x.data()), s.data(), (int)s.siz
e()); |
| 138 x.erase(d, std::string::npos); | 142 x.erase(d, std::string::npos); |
| 139 s.swap(x); | 143 s.swap(x); |
| 140 return s; | 144 return s; |
| 141 } | 145 } |
| 142 | 146 |
| 143 /** | 147 /** |
| 144 * base 64 decode a string (self-modifing) | 148 * base 64 decode a string (self-modifing) |
| 145 * On failure, the string is empty. | 149 * On failure, the string is empty. |
| 146 * | 150 * |
| 147 * This function is for C++ only (duh) | 151 * This function is for C++ only (duh) |
| 148 * | 152 * |
| 149 * \param[in,out] s the string to be decoded | 153 * \param[in,out] s the string to be decoded |
| 150 * \return a reference to the input string | 154 * \return a reference to the input string |
| 151 */ | 155 */ |
| 152 inline std::string& modp_b64_decode(std::string& s) | 156 inline std::string& modp_b64_decode(std::string& s) |
| 153 { | 157 { |
| 154 std::string x(modp_b64_decode_len(s.size()), '\0'); | 158 std::string x(modp_b64_decode_len(s.size()), '\0'); |
| 155 int d = modp_b64_decode(const_cast<char*>(x.data()), s.data(), s.size()); | 159 size_t d = modp_b64_decode(const_cast<char*>(x.data()), s.data(), (int)s.siz
e()); |
| 156 if (d < 0) { | 160 if (d == MODP_B64_ERROR) { |
| 157 x.clear(); | 161 x.clear(); |
| 158 } else { | 162 } else { |
| 159 x.erase(d, std::string::npos); | 163 x.erase(d, std::string::npos); |
| 160 } | 164 } |
| 161 s.swap(x); | 165 s.swap(x); |
| 162 return s; | 166 return s; |
| 163 } | 167 } |
| 164 | 168 |
| 165 #endif /* __cplusplus */ | 169 #endif /* __cplusplus */ |
| 166 | 170 |
| 167 #endif /* MODP_B64 */ | 171 #endif /* MODP_B64 */ |
| OLD | NEW |