|
| 1 | +/** |
| 2 | + * Copyright (C) 2015 Topology LP |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to |
| 7 | + * deal in the Software without restriction, including without limitation the |
| 8 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | + * IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +#ifndef CPPCODEC_BASE32_CROCKFORD |
| 25 | +#define CPPCODEC_BASE32_CROCKFORD |
| 26 | + |
| 27 | +#include "detail/codec.hpp" |
| 28 | +#include "detail/base32.hpp" |
| 29 | + |
| 30 | +namespace cppcodec { |
| 31 | + |
| 32 | +namespace detail { |
| 33 | + |
| 34 | +static constexpr const char base32_crockford_alphabet[] = { |
| 35 | + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', // at index 10 |
| 36 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 18 - no I |
| 37 | + 'J', 'K', // 20 - no L |
| 38 | + 'M', 'N', // 22 - no O |
| 39 | + 'P', 'Q', 'R', 'S', 'T', // 27 - no U |
| 40 | + 'V', 'W', 'X', 'Y', 'Z' // 32 |
| 41 | +}; |
| 42 | + |
| 43 | +class base32_crockford_base |
| 44 | +{ |
| 45 | +public: |
| 46 | + static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size() { |
| 47 | + static_assert(sizeof(base32_crockford_alphabet) == 32, "base32 alphabet must have 32 values"); |
| 48 | + return sizeof(base32_crockford_alphabet); |
| 49 | + } |
| 50 | + static CPPCODEC_ALWAYS_INLINE constexpr char symbol(alphabet_index_t idx) |
| 51 | + { |
| 52 | + return base32_crockford_alphabet[idx]; |
| 53 | + } |
| 54 | + static CPPCODEC_ALWAYS_INLINE constexpr char normalized_symbol(char c) |
| 55 | + { |
| 56 | + // Hex decoding is always case-insensitive (even in RFC 4648), the question |
| 57 | + // is only for encoding whether to use upper-case or lower-case letters. |
| 58 | + return (c == 'O' || c == 'o') ? '0' |
| 59 | + : (c == 'I' || c == 'i' || c == 'L' || c == 'l') ? '1' |
| 60 | + : (c >= 'a' && c <= 'z') ? (c - 'a' + 'A') |
| 61 | + : c; |
| 62 | + } |
| 63 | + |
| 64 | + static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return false; } |
| 65 | + static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return false; } |
| 66 | + static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(char) { return false; } |
| 67 | + static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof_symbol(char c) { return c == '\0'; } |
| 68 | + |
| 69 | + static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(char c) { |
| 70 | + return c == '-'; // "Hyphens (-) can be inserted into strings [for readability]." |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +// base32_crockford is a concatenative iterative (i.e. streaming) interpretation of Crockford base32. |
| 75 | +// It interprets the statement "zero-extend the number to make its bit-length a multiple of 5" |
| 76 | +// to mean zero-extending it on the right. |
| 77 | +// (The other possible interpretation is base32_crockford_num, a place-based single number encoding system. |
| 78 | +// See http://merrigrove.blogspot.ca/2014/04/what-heck-is-base64-encoding-really.html for more info.) |
| 79 | +class base32_crockford : public base32_crockford_base |
| 80 | +{ |
| 81 | +public: |
| 82 | + template <typename Codec> using codec_impl = stream_codec<Codec, base32_crockford>; |
| 83 | +}; |
| 84 | + |
| 85 | +} // namespace detail |
| 86 | + |
| 87 | +using base32_crockford = detail::codec<detail::base32<detail::base32_crockford>>; |
| 88 | + |
| 89 | +} // namespace cppcodec |
| 90 | + |
| 91 | +#endif // CPPCODEC_BASE32_CROCKFORD |
0 commit comments