base64_i2p.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_BASE64_I2P
  25. #define CPPCODEC_BASE64_I2P
  26. #include "detail/codec.hpp"
  27. #include "detail/base64.hpp"
  28. namespace cppcodec_samty {
  29. namespace detail {
  30. static constexpr const char base64_i2p_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',
  33. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  34. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  35. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '~'
  36. };
  37. class base64_i2p
  38. {
  39. public:
  40. template <typename Codec> using codec_impl = stream_codec<Codec, base64_i2p>;
  41. static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size() {
  42. static_assert(sizeof(base64_i2p_alphabet) == 64, "base64 alphabet must have 64 values");
  43. return sizeof(base64_i2p_alphabet);
  44. }
  45. static CPPCODEC_ALWAYS_INLINE constexpr char symbol(alphabet_index_t idx)
  46. {
  47. return base64_i2p_alphabet[idx];
  48. }
  49. static CPPCODEC_ALWAYS_INLINE constexpr char normalized_symbol(char c) { return c; }
  50. static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return true; }
  51. static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return true; }
  52. static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; }
  53. static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(char c) { return c == '='; }
  54. static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof_symbol(char c) { return c == '\0'; }
  55. // RFC4648 does not specify any whitespace being allowed in base64 encodings.
  56. static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(char) { return false; }
  57. };
  58. } // namespace detail
  59. using base64_i2p = detail::codec<detail::base64<detail::base64_i2p>>;
  60. } // namespace cppcodec
  61. #endif // CPPCODEC_BASE64_I2P