base64_rfc4648.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_BASE64_RFC4648
  24. #define CPPCODEC_BASE64_RFC4648
  25. #include "detail/codec.hpp"
  26. #include "detail/base64.hpp"
  27. namespace cppcodec {
  28. namespace detail {
  29. static constexpr const char base64_rfc4648_alphabet[] = {
  30. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  31. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  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',
  34. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
  35. };
  36. class base64_rfc4648
  37. {
  38. public:
  39. template <typename Codec> using codec_impl = stream_codec<Codec, base64_rfc4648>;
  40. static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size() {
  41. static_assert(sizeof(base64_rfc4648_alphabet) == 64, "base64 alphabet must have 64 values");
  42. return sizeof(base64_rfc4648_alphabet);
  43. }
  44. static CPPCODEC_ALWAYS_INLINE constexpr char symbol(alphabet_index_t idx)
  45. {
  46. return base64_rfc4648_alphabet[idx];
  47. }
  48. static CPPCODEC_ALWAYS_INLINE constexpr char normalized_symbol(char c) { return c; }
  49. static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return true; }
  50. static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return true; }
  51. static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; }
  52. static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(char c) { return c == '='; }
  53. static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof_symbol(char c) { return c == '\0'; }
  54. // RFC4648 does not specify any whitespace being allowed in base64 encodings.
  55. static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(char) { return false; }
  56. };
  57. } // namespace detail
  58. using base64_rfc4648 = detail::codec<detail::base64<detail::base64_rfc4648>>;
  59. } // namespace cppcodec
  60. #endif // CPPCODEC_BASE64_RFC4648