base64_url.hpp 3.1 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. #ifndef CPPCODEC_BASE64_URL
  24. #define CPPCODEC_BASE64_URL
  25. #include "detail/codec.hpp"
  26. #include "detail/base64.hpp"
  27. namespace cppcodec {
  28. namespace detail {
  29. // The URL and filename safe alphabet is also specified by RFC4648, named "base64url".
  30. // We keep the underscore ("base64_url") for consistency with the other codec variants.
  31. static constexpr const char base64_url_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',
  34. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  35. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  36. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
  37. };
  38. class base64_url
  39. {
  40. public:
  41. template <typename Codec> using codec_impl = stream_codec<Codec, base64_url>;
  42. static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size() {
  43. static_assert(sizeof(base64_url_alphabet) == 64, "base64 alphabet must have 64 values");
  44. return sizeof(base64_url_alphabet);
  45. }
  46. static CPPCODEC_ALWAYS_INLINE constexpr char symbol(alphabet_index_t idx)
  47. {
  48. return base64_url_alphabet[idx];
  49. }
  50. static CPPCODEC_ALWAYS_INLINE constexpr char normalized_symbol(char c) { return c; }
  51. static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return true; }
  52. static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return true; }
  53. static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; }
  54. static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(char c) { return c == '='; }
  55. static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof_symbol(char c) { return c == '\0'; }
  56. // RFC4648 does not specify any whitespace being allowed in base64 encodings.
  57. static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(char) { return false; }
  58. };
  59. } // namespace detail
  60. using base64_url = detail::codec<detail::base64<detail::base64_url>>;
  61. } // namespace cppcodec
  62. #endif // CPPCODEC_BASE64_URL