base32_rfc4648.hpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // Edited by acetone, 2024
  24. #ifndef CPPCODEC_BASE32_RFC4648
  25. #define CPPCODEC_BASE32_RFC4648
  26. #include "detail/codec.hpp"
  27. #include "detail/base32.hpp"
  28. namespace cppcodec_samty {
  29. namespace detail {
  30. // RFC 4648 uses a simple alphabet: A-Z starting at index 0, then 2-7 starting at index 26.
  31. static constexpr const char base32_rfc4648_alphabet[] = {
  32. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  33. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', // at index 26
  34. '2', '3', '4', '5', '6', '7'
  35. };
  36. class base32_rfc4648
  37. {
  38. public:
  39. template <typename Codec> using codec_impl = stream_codec<Codec, base32_rfc4648>;
  40. static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size() {
  41. static_assert(sizeof(base32_rfc4648_alphabet) == 32, "base32 alphabet must have 32 values");
  42. return sizeof(base32_rfc4648_alphabet);
  43. }
  44. static CPPCODEC_ALWAYS_INLINE constexpr char symbol(alphabet_index_t idx)
  45. {
  46. return base32_rfc4648_alphabet[idx];
  47. }
  48. static CPPCODEC_ALWAYS_INLINE constexpr char normalized_symbol(char c)
  49. {
  50. // Lower-case letters are accepted, though not generally expected.
  51. return (c >= 'a' && c <= 'z') ? (c - 'a' + 'A') : c;
  52. }
  53. static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return false; }
  54. static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return false; }
  55. static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; }
  56. static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(char c) { return c == '='; }
  57. static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof_symbol(char c) { return c == '\0'; }
  58. // RFC4648 does not specify any whitespace being allowed in base32 encodings.
  59. static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(char) { return false; }
  60. };
  61. } // namespace detail
  62. using base32_rfc4648 = detail::codec<detail::base32<detail::base32_rfc4648>>;
  63. } // namespace cppcodec
  64. #endif // CPPCODEC_BASE32_RFC4648