base32_rfc4648.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #ifndef CPPCODEC_BASE32_RFC4648
  24. #define CPPCODEC_BASE32_RFC4648
  25. #include "detail/codec.hpp"
  26. #include "detail/base32.hpp"
  27. namespace cppcodec {
  28. namespace detail {
  29. // RFC 4648 uses a simple alphabet: A-Z starting at index 0, then 2-7 starting at index 26.
  30. static constexpr const char base32_rfc4648_alphabet[] = {
  31. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  32. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', // at index 26
  33. '2', '3', '4', '5', '6', '7'
  34. };
  35. class base32_rfc4648
  36. {
  37. public:
  38. template <typename Codec> using codec_impl = stream_codec<Codec, base32_rfc4648>;
  39. static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size() {
  40. static_assert(sizeof(base32_rfc4648_alphabet) == 32, "base32 alphabet must have 32 values");
  41. return sizeof(base32_rfc4648_alphabet);
  42. }
  43. static CPPCODEC_ALWAYS_INLINE constexpr char symbol(alphabet_index_t idx)
  44. {
  45. return base32_rfc4648_alphabet[idx];
  46. }
  47. static CPPCODEC_ALWAYS_INLINE constexpr char normalized_symbol(char c)
  48. {
  49. // Lower-case letters are accepted, though not generally expected.
  50. return (c >= 'a' && c <= 'z') ? (c - 'a' + 'A') : c;
  51. }
  52. static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return true; }
  53. static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return true; }
  54. static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; }
  55. static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(char c) { return c == '='; }
  56. static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof_symbol(char c) { return c == '\0'; }
  57. // RFC4648 does not specify any whitespace being allowed in base32 encodings.
  58. static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(char) { return false; }
  59. };
  60. } // namespace detail
  61. using base32_rfc4648 = detail::codec<detail::base32<detail::base32_rfc4648>>;
  62. } // namespace cppcodec
  63. #endif // CPPCODEC_BASE32_RFC4648