root/capture-mod/trunk/b64.h
| Revision 1779, 16.7 kB (checked in by xkovah, 3 months ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | /* ///////////////////////////////////////////////////////////////////////////// |
| 2 | * File: b64.h |
| 3 | * |
| 4 | * Purpose: Header file for the b64 library |
| 5 | * |
| 6 | * Created: 18th October 2004 |
| 7 | * Updated: 3rd August 2008 |
| 8 | * |
| 9 | * Thanks: To Adam McLaurin, for ideas regarding the b64_decode2() and |
| 10 | * b64_encode2(). |
| 11 | * |
| 12 | * Home: http://synesis.com.au/software/ |
| 13 | * |
| 14 | * Copyright (c) 2004-2008, Matthew Wilson and Synesis Software |
| 15 | * All rights reserved. |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions are met: |
| 19 | * |
| 20 | * - Redistributions of source code must retain the above copyright notice, this |
| 21 | * list of conditions and the following disclaimer. |
| 22 | * - Redistributions in binary form must reproduce the above copyright notice, |
| 23 | * this list of conditions and the following disclaimer in the documentation |
| 24 | * and/or other materials provided with the distribution. |
| 25 | * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of |
| 26 | * any contributors may be used to endorse or promote products derived from |
| 27 | * this software without specific prior written permission. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 30 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 33 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 34 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 35 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 37 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 38 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 39 | * POSSIBILITY OF SUCH DAMAGE. |
| 40 | * |
| 41 | * ////////////////////////////////////////////////////////////////////////// */ |
| 42 | |
| 43 | |
| 44 | /** \file b64.h |
| 45 | * |
| 46 | * \brief [C/C++] Header file for the b64 library. |
| 47 | */ |
| 48 | |
| 49 | #ifndef B64_INCL_B64_H_B64 |
| 50 | #define B64_INCL_B64_H_B64 |
| 51 | |
| 52 | /* ///////////////////////////////////////////////////////////////////////////// |
| 53 | * Version information |
| 54 | */ |
| 55 | |
| 56 | #ifndef B64_DOCUMENTATION_SKIP_SECTION |
| 57 | # define B64_VER_B64_H_B64_MAJOR 1 |
| 58 | # define B64_VER_B64_H_B64_MINOR 5 |
| 59 | # define B64_VER_B64_H_B64_REVISION 3 |
| 60 | # define B64_VER_B64_H_B64_EDIT 27 |
| 61 | #endif /* !B64_DOCUMENTATION_SKIP_SECTION */ |
| 62 | |
| 63 | /** \def B64_VER_MAJOR |
| 64 | * The major version number of b64 |
| 65 | */ |
| 66 | |
| 67 | /** \def B64_VER_MINOR |
| 68 | * The minor version number of b64 |
| 69 | */ |
| 70 | |
| 71 | /** \def B64_VER_REVISION |
| 72 | * The revision version number of b64 |
| 73 | */ |
| 74 | |
| 75 | /** \def B64_VER |
| 76 | * The current composite version number of b64 |
| 77 | */ |
| 78 | |
| 79 | #ifndef B64_DOCUMENTATION_SKIP_SECTION |
| 80 | # define B64_VER_1_0_1 0x01000100 |
| 81 | # define B64_VER_1_0_2 0x01000200 |
| 82 | # define B64_VER_1_0_3 0x01000300 |
| 83 | # define B64_VER_1_1_1 0x01010100 |
| 84 | # define B64_VER_1_1_2 0x01010200 |
| 85 | # define B64_VER_1_1_3 0x01010300 |
| 86 | # define B64_VER_1_2_1 0x01020100 |
| 87 | # define B64_VER_1_2_2 0x01020200 |
| 88 | # define B64_VER_1_2_3 0x01020300 |
| 89 | # define B64_VER_1_2_4 0x01020400 |
| 90 | # define B64_VER_1_2_5 0x01020500 |
| 91 | # define B64_VER_1_2_6 0x01020600 |
| 92 | # define B64_VER_1_2_7 0x01020700 |
| 93 | # define B64_VER_1_3_1 0x010301ff |
| 94 | # define B64_VER_1_3_2 0x010302ff |
| 95 | # define B64_VER_1_3_3 0x010303ff |
| 96 | |
| 97 | # define B64_VER B64_VER_1_3_3 |
| 98 | #else /* ? B64_DOCUMENTATION_SKIP_SECTION */ |
| 99 | # define B64_VER 0x010303ff |
| 100 | #endif /* !B64_DOCUMENTATION_SKIP_SECTION */ |
| 101 | |
| 102 | #define B64_VER_MAJOR 1 |
| 103 | #define B64_VER_MINOR 3 |
| 104 | #define B64_VER_REVISION 3 |
| 105 | |
| 106 | /* ///////////////////////////////////////////////////////////////////////////// |
| 107 | * Includes |
| 108 | */ |
| 109 | |
| 110 | #include <stddef.h> |
| 111 | |
| 112 | /* ///////////////////////////////////////////////////////////////////////////// |
| 113 | * Namespace |
| 114 | */ |
| 115 | |
| 116 | #if !defined(B64_NO_NAMESPACE) && !defined(__cplusplus) |
| 117 | #define B64_NO_NAMESPACE |
| 118 | #endif /* !B64_NO_NAMESPACE && !__cplusplus */ |
| 119 | |
| 120 | #ifdef B64_NAMESPACE |
| 121 | #undef B64_NAMESPACE |
| 122 | #endif /* B64_NAMESPACE */ |
| 123 | |
| 124 | #ifdef B64_NAMESPACE_QUALIFIER |
| 125 | #undef B64_NAMESPACE_QUALIFIER |
| 126 | #endif /* B64_NAMESPACE_QUALIFIER */ |
| 127 | |
| 128 | |
| 129 | #ifndef B64_NO_NAMESPACE |
| 130 | |
| 131 | # ifdef B64_CUSTOM_NAMESPACE |
| 132 | # define B64_NAMESPACE B64_CUSTOM_NAMESPACE |
| 133 | # else /* ? B64_CUSTOM_NAMESPACE */ |
| 134 | # define B64_NAMESPACE b64 |
| 135 | # endif /* B64_CUSTOM_NAMESPACE */ |
| 136 | |
| 137 | # if defined(B64_CUSTOM_NAMESPACE) && \ |
| 138 | defined(B64_CUSTOM_NAMESPACE_QUALIFIER) |
| 139 | # define B64_NAMESPACE_QUALIFIER B64_CUSTOM_NAMESPACE_QUALIFIER |
| 140 | # else /* B64_CUSTOM_NAMESPACE && B64_CUSTOM_NAMESPACE_QUALIFIER */ |
| 141 | # define B64_NAMESPACE_QUALIFIER ::B64_NAMESPACE |
| 142 | # endif /* B64_CUSTOM_NAMESPACE && B64_CUSTOM_NAMESPACE_QUALIFIER */ |
| 143 | |
| 144 | |
| 145 | /** \brief [C/C++] The b64 namespace, within which the core library types and functions |
| 146 | * reside in C++ compilation. In C compilation, they all reside in the global |
| 147 | * namespace. |
| 148 | * |
| 149 | * \htmlonly |
| 150 | * <hr> |
| 151 | * \endhtmlonly |
| 152 | */ |
| 153 | namespace B64_NAMESPACE |
| 154 | { |
| 155 | #endif /* !B64_NO_NAMESPACE */ |
| 156 | |
| 157 | /* ///////////////////////////////////////////////////////////////////////////// |
| 158 | * Enumerations |
| 159 | */ |
| 160 | |
| 161 | /** \brief Return codes (from b64_encode2() / b64_decode2()) |
| 162 | */ |
| 163 | enum B64_RC |
| 164 | { |
| 165 | B64_RC_OK = 0 /*!< Operation was successful. */ |
| 166 | , B64_RC_INSUFFICIENT_BUFFER = 1 /*!< The given translation buffer was not of sufficient size. */ |
| 167 | , B64_RC_TRUNCATED_INPUT = 2 /*!< The input did not represent a fully formed stream of octet couplings. */ |
| 168 | , B64_RC_DATA_ERROR = 3 /*!< Invalid data. */ |
| 169 | #ifndef B64_DOCUMENTATION_SKIP_SECTION |
| 170 | , B64_max_RC_value |
| 171 | #endif /* !B64_DOCUMENTATION_SKIP_SECTION */ |
| 172 | }; |
| 173 | |
| 174 | #ifndef __cplusplus |
| 175 | typedef enum B64_RC B64_RC; |
| 176 | #endif /* !__cplusplus */ |
| 177 | |
| 178 | /** \brief Coding behaviour modification flags (for b64_encode2() / b64_decode2()) |
| 179 | */ |
| 180 | enum B64_FLAGS |
| 181 | { |
| 182 | B64_F_LINE_LEN_USE_PARAM = 0x0000 /*!< Uses the lineLen parameter to b64_encode2(). Ignored by b64_decode2(). */ |
| 183 | , B64_F_LINE_LEN_INFINITE = 0x0001 /*!< Ignores the lineLen parameter to b64_encode2(). Line length is infinite. Ignored by b64_decode2(). */ |
| 184 | , B64_F_LINE_LEN_64 = 0x0002 /*!< Ignores the lineLen parameter to b64_encode2(). Line length is 64. Ignored by b64_decode2(). */ |
| 185 | , B64_F_LINE_LEN_76 = 0x0003 /*!< Ignores the lineLen parameter to b64_encode2(). Line length is 76. Ignored by b64_decode2(). */ |
| 186 | , B64_F_LINE_LEN_MASK = 0x000f /*!< Mask for testing line length flags to b64_encode2(). Ignored by b64_encode2(). */ |
| 187 | , B64_F_STOP_ON_NOTHING = 0x0000 /*!< Decoding ignores all invalid characters in the input data. Ignored by b64_encode2(). */ |
| 188 | , B64_F_STOP_ON_UNKNOWN_CHAR = 0x0100 /*!< Causes decoding to break if any non-Base-64 [a-zA-Z0-9=+/], non-whitespace character is encountered. Ignored by b64_encode2(). */ |
| 189 | , B64_F_STOP_ON_UNEXPECTED_WS = 0x0200 /*!< Causes decoding to break if any unexpected whitespace is encountered. Ignored by b64_encode2(). */ |
| 190 | , B64_F_STOP_ON_BAD_CHAR = 0x0300 /*!< Causes decoding to break if any non-Base-64 [a-zA-Z0-9=+/] character is encountered. Ignored by b64_encode2(). */ |
| 191 | }; |
| 192 | |
| 193 | #ifndef __cplusplus |
| 194 | typedef enum B64_FLAGS B64_FLAGS; |
| 195 | #endif /* !__cplusplus */ |
| 196 | |
| 197 | /* ///////////////////////////////////////////////////////////////////////////// |
| 198 | * Functions |
| 199 | */ |
| 200 | |
| 201 | #ifdef __cplusplus |
| 202 | extern "C" { |
| 203 | #endif /* __cplusplus */ |
| 204 | |
| 205 | /** \brief Encodes a block of binary data into Base-64 |
| 206 | * |
| 207 | * \param src Pointer to the block to be encoded. May not be NULL, except when |
| 208 | * \c dest is NULL, in which case it is ignored. |
| 209 | * \param srcSize Length of block to be encoded |
| 210 | * \param dest Pointer to the buffer into which the result is to be written. May |
| 211 | * be NULL, in which case the function returns the required length |
| 212 | * \param destLen Length of the buffer into which the result is to be written. Must |
| 213 | * be at least as large as that indicated by the return value from |
| 214 | * \link b64::b64_encode b64_encode(NULL, srcSize, NULL, 0)\endlink. |
| 215 | * |
| 216 | * \return 0 if the size of the buffer was insufficient, or the length of the |
| 217 | * converted buffer was longer than \c destLen |
| 218 | * |
| 219 | * \note The function returns the required length if \c dest is NULL |
| 220 | * |
| 221 | * \note The function returns the required length if \c dest is NULL. The returned size |
| 222 | * might be larger than the actual required size, but will never be smaller. |
| 223 | * |
| 224 | * \note Threading: The function is fully re-entrant. |
| 225 | * |
| 226 | * \see b64::encode() |
| 227 | */ |
| 228 | size_t b64_encode(void const *src, size_t srcSize, char *dest, size_t destLen); |
| 229 | |
| 230 | /** \brief Encodes a block of binary data into Base-64 |
| 231 | * |
| 232 | * \param src Pointer to the block to be encoded. May not be NULL, except when |
| 233 | * \c dest is NULL, in which case it is ignored. |
| 234 | * \param srcSize Length of block to be encoded |
| 235 | * \param dest Pointer to the buffer into which the result is to be written. May |
| 236 | * be NULL, in which case the function returns the required length |
| 237 | * \param destLen Length of the buffer into which the result is to be written. Must |
| 238 | * be at least as large as that indicated by the return value from |
| 239 | * \link b64::b64_encode2 b64_encode2(NULL, srcSize, NULL, 0, flags, lineLen, rc)\endlink. |
| 240 | * \param flags A combination of the B64_FLAGS enumeration, that moderate the |
| 241 | * behaviour of the function |
| 242 | * \param lineLen If the flags parameter contains B64_F_LINE_LEN_USE_PARAM, then |
| 243 | * this parameter represents the length of the lines into which the encoded form is split, |
| 244 | * with a hard line break ('\\r\\n'). If this value is 0, then the line is not |
| 245 | * split. If it is <0, then the RFC-1113 recommended line length of 64 is used |
| 246 | * \param rc The return code representing the status of the operation. May be NULL. |
| 247 | * |
| 248 | * \return 0 if the size of the buffer was insufficient, or the length of the |
| 249 | * converted buffer was longer than \c destLen |
| 250 | * |
| 251 | * \note The function returns the required length if \c dest is NULL. The returned size |
| 252 | * might be larger than the actual required size, but will never be smaller. |
| 253 | * |
| 254 | * \note Threading: The function is fully re-entrant. |
| 255 | * |
| 256 | * \see b64::encode() |
| 257 | */ |
| 258 | size_t b64_encode2( void const *src |
| 259 | , size_t srcSize |
| 260 | , char *dest |
| 261 | , size_t destLen |
| 262 | , unsigned flags |
| 263 | , int lineLen /* = 0 */ |
| 264 | , B64_RC *rc /* = NULL */); |
| 265 | |
| 266 | /** \brief Decodes a sequence of Base-64 into a block of binary data |
| 267 | * |
| 268 | * \param src Pointer to the Base-64 block to be decoded. May not be NULL, except when |
| 269 | * \c dest is NULL, in which case it is ignored. If \c dest is NULL, and \c src is |
| 270 | * <b>not</b> NULL, then the returned value is calculated exactly, otherwise a value |
| 271 | * is returned that is guaranteed to be large enough to hold the decoded block. |
| 272 | * |
| 273 | * \param srcLen Length of block to be encoded. Must be an integral of 4, the Base-64 |
| 274 | * encoding quantum, otherwise the Base-64 block is assumed to be invalid |
| 275 | * \param dest Pointer to the buffer into which the result is to be written. May |
| 276 | * be NULL, in which case the function returns the required length |
| 277 | * \param destSize Length of the buffer into which the result is to be written. Must |
| 278 | * be at least as large as that indicated by the return value from |
| 279 | * \c b64_decode(src, srcSize, NULL, 0), even in the case where the encoded form |
| 280 | * contains a number of characters that will be ignored, resulting in a lower total |
| 281 | * length of converted form. |
| 282 | * |
| 283 | * \return 0 if the size of the buffer was insufficient, or the length of the |
| 284 | * converted buffer was longer than \c destSize |
| 285 | * |
| 286 | * \note The function returns the required length if \c dest is NULL. The returned size |
| 287 | * might be larger than the actual required size, but will never be smaller. |
| 288 | * |
| 289 | * \note \anchor anchor__4_characters The behaviour of both |
| 290 | * \link b64::b64_encode2 b64_encode2()\endlink |
| 291 | * and |
| 292 | * \link b64::b64_decode2 b64_decode2()\endlink |
| 293 | * are undefined if the line length is not a multiple of 4. |
| 294 | * |
| 295 | * \note Threading: The function is fully re-entrant. |
| 296 | * |
| 297 | * \see b64::decode() |
| 298 | */ |
| 299 | size_t b64_decode(char const *src, size_t srcLen, void *dest, size_t destSize); |
| 300 | |
| 301 | /** \brief Decodes a sequence of Base-64 into a block of binary data |
| 302 | * |
| 303 | * \param src Pointer to the Base-64 block to be decoded. May not be NULL, except when |
| 304 | * \c dest is NULL, in which case it is ignored. If \c dest is NULL, and \c src is |
| 305 | * <b>not</b> NULL, then the returned value is calculated exactly, otherwise a value |
| 306 | * is returned that is guaranteed to be large enough to hold the decoded block. |
| 307 | * |
| 308 | * \param srcLen Length of block to be encoded. Must be an integral of 4, the Base-64 |
| 309 | * encoding quantum, otherwise the Base-64 block is assumed to be invalid |
| 310 | * \param dest Pointer to the buffer into which the result is to be written. May |
| 311 | * be NULL, in which case the function returns the required length |
| 312 | * \param destSize Length of the buffer into which the result is to be written. Must |
| 313 | * be at least as large as that indicated by the return value from |
| 314 | * \c b64_decode(src, srcSize, NULL, 0), even in the case where the encoded form |
| 315 | * contains a number of characters that will be ignored, resulting in a lower total |
| 316 | * length of converted form. |
| 317 | * \param flags A combination of the B64_FLAGS enumeration, that moderate the |
| 318 | * behaviour of the function. |
| 319 | * \param rc The return code representing the status of the operation. May be NULL. |
| 320 | * \param badChar If the flags parameter does not contain B64_F_STOP_ON_NOTHING, this |
| 321 | * parameter specifies the address of a pointer that will be set to point to any |
| 322 | * character in the sequence that stops the parsing, as dictated by the flags |
| 323 | * parameter. May be NULL. |
| 324 | * |
| 325 | * \return 0 if the size of the buffer was insufficient, or the length of the |
| 326 | * converted buffer was longer than \c destSize, or a bad character stopped parsing. |
| 327 | * |
| 328 | * \note The function returns the required length if \c dest is NULL. The returned size |
| 329 | * might be larger than the actual required size, but will never be smaller. |
| 330 | * |
| 331 | * \note The behaviour of both |
| 332 | * \link b64::b64_encode2 b64_encode2()\endlink |
| 333 | * and |
| 334 | * \link b64::b64_decode2 b64_decode2()\endlink |
| 335 | * are undefined if the line length is not a multiple of 4. |
| 336 | * |
| 337 | * \note Threading: The function is fully re-entrant. |
| 338 | * |
| 339 | * \see b64::decode() |
| 340 | */ |
| 341 | size_t b64_decode2( char const *src |
| 342 | , size_t srcLen |
| 343 | , void *dest |
| 344 | , size_t destSize |
| 345 | , unsigned flags |
| 346 | , char const **badChar /* = NULL */ |
| 347 | , B64_RC *rc /* = NULL */); |
| 348 | |
| 349 | |
| 350 | /** \brief Returns the textual description of the error |
| 351 | * |
| 352 | * \param code The \link b64::B64_RC error code\endlink |
| 353 | */ |
| 354 | char const *b64_getErrorString(B64_RC code); |
| 355 | |
| 356 | /** \brief Returns the length of the textual description of the error |
| 357 | * |
| 358 | * \see b64_getErrorString() |
| 359 | * |
| 360 | * \param code The \link b64::B64_RC error code\endlink |
| 361 | */ |
| 362 | size_t b64_getErrorStringLength(B64_RC code); |
| 363 | |
| 364 | |
| 365 | #ifdef __cplusplus |
| 366 | } /* extern "C" */ |
| 367 | #endif /* __cplusplus */ |
| 368 | |
| 369 | /* ///////////////////////////////////////////////////////////////////////////// |
| 370 | * Namespace |
| 371 | */ |
| 372 | |
| 373 | #ifndef B64_NO_NAMESPACE |
| 374 | } /* namespace B64_NAMESPACE */ |
| 375 | |
| 376 | # ifndef B64_DOCUMENTATION_SKIP_SECTION |
| 377 | |
| 378 | namespace stlsoft |
| 379 | { |
| 380 | |
| 381 | inline char const *c_str_data_a( B64_NAMESPACE_QUALIFIER::B64_RC code) |
| 382 | { |
| 383 | return B64_NAMESPACE_QUALIFIER::b64_getErrorString(code); |
| 384 | } |
| 385 | inline char const *c_str_data( B64_NAMESPACE_QUALIFIER::B64_RC code) |
| 386 | { |
| 387 | return B64_NAMESPACE_QUALIFIER::b64_getErrorString(code); |
| 388 | } |
| 389 | |
| 390 | inline size_t c_str_len_a( B64_NAMESPACE_QUALIFIER::B64_RC code) |
| 391 | { |
| 392 | return B64_NAMESPACE_QUALIFIER::b64_getErrorStringLength(code); |
| 393 | } |
| 394 | inline size_t c_str_len( B64_NAMESPACE_QUALIFIER::B64_RC code) |
| 395 | { |
| 396 | return B64_NAMESPACE_QUALIFIER::b64_getErrorStringLength(code); |
| 397 | } |
| 398 | |
| 399 | inline char const *c_str_ptr_a( B64_NAMESPACE_QUALIFIER::B64_RC code) |
| 400 | { |
| 401 | return B64_NAMESPACE_QUALIFIER::b64_getErrorString(code); |
| 402 | } |
| 403 | inline char const *c_str_ptr( B64_NAMESPACE_QUALIFIER::B64_RC code) |
| 404 | { |
| 405 | return B64_NAMESPACE_QUALIFIER::b64_getErrorString(code); |
| 406 | } |
| 407 | |
| 408 | } /* namespace stlsoft */ |
| 409 | |
| 410 | # endif /* !B64_DOCUMENTATION_SKIP_SECTION */ |
| 411 | |
| 412 | #endif /* !B64_NO_NAMESPACE */ |
| 413 | |
| 414 | /* ////////////////////////////////////////////////////////////////////////// */ |
| 415 | |
| 416 | #endif /* B64_INCL_B64_H_B64 */ |
| 417 | |
| 418 | /* ////////////////////////////////////////////////////////////////////////// */ |
Note: See TracBrowser for help on using the browser.
