base32.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #include <util/generic/string.h>
  3. #include <string>
  4. #include <string_view>
  5. // Base32 encoding based on RFC 4648 alphabet (incompatible with Crockford and Geohash alphabet)
  6. // https://en.wikipedia.org/wiki/Base32#RFC_4648_Base32_alphabet
  7. ///
  8. /// @return Size of the buffer required to decode Base32 encoded data of size `len`.
  9. ///
  10. constexpr size_t Base32DecodeBufSize(size_t len) noexcept {
  11. return (len * 5 + 7) / 8;
  12. }
  13. ///
  14. /// @brief Decodes only valid Base32 string, behaviour for invalid data is unspecified.
  15. ///
  16. /// @param src a base32 encoded string.
  17. /// @param dst an pointer to allocated memory for writing result.
  18. ///
  19. /// @return Count of written bytes.
  20. ///
  21. size_t Base32Decode(std::string_view src, char* dst);
  22. ///
  23. /// @param src a base32 encoded string.
  24. /// @param dst a decoded string.
  25. ///
  26. inline void Base32Decode(std::string_view src, std::string& dst)
  27. {
  28. ::ResizeUninitialized(dst, Base32DecodeBufSize(src.size()));
  29. dst.resize(Base32Decode(src, dst.data()));
  30. }
  31. ///
  32. /// @param s a base32 encoded string.
  33. ///
  34. /// @returns a decoded string.
  35. ///
  36. inline std::string Base32Decode(std::string_view s)
  37. {
  38. std::string ret;
  39. Base32Decode(s, ret);
  40. return ret;
  41. }
  42. ///
  43. /// @brief Decodes Base32 string with strict verification of invalid symbols,
  44. /// also tries to decode Base32 string with padding inside.
  45. ///
  46. /// @throws Throws exceptions on inputs which contain invalid symbols or incorrect padding.
  47. ///
  48. /// @param src a base32 encoded string.
  49. /// @param dst an pointer to allocated memory for writing result.
  50. ///
  51. /// @return Count of written bytes.
  52. ///
  53. size_t Base32StrictDecode(std::string_view src, char* dst);
  54. ///
  55. /// @param src a base32 encoded string.
  56. /// @param dst a decoded string.
  57. ///
  58. inline void Base32StrictDecode(std::string_view src, std::string& dst)
  59. {
  60. ::ResizeUninitialized(dst, Base32DecodeBufSize(src.size()));
  61. dst.resize(Base32StrictDecode(src, dst.data()));
  62. }
  63. ///
  64. /// @param s a base32 encoded string.
  65. ///
  66. /// @returns a decoded string.
  67. ///
  68. inline std::string Base32StrictDecode(std::string_view s)
  69. {
  70. std::string ret;
  71. Base32StrictDecode(s, ret);
  72. return ret;
  73. }
  74. ///
  75. /// @return Size of the buffer required to encode Base32 decoded data of size `len`.
  76. ///
  77. constexpr size_t Base32EncodeBufSize(size_t len) noexcept {
  78. return ((len * 8 + 4) / 5 + 7) / 8 * 8;
  79. }
  80. ///
  81. /// @param src a base32 decoded string.
  82. /// @param dst an pointer to allocated memory for writing result.
  83. ///
  84. /// @return Count of written bytes.
  85. ///
  86. size_t Base32Encode(std::string_view src, char* dst);
  87. ///
  88. /// @param src a base32 decoded string.
  89. /// @param dst a encoded string.
  90. ///
  91. inline void Base32Encode(std::string_view src, std::string& dst)
  92. {
  93. ::ResizeUninitialized(dst, Base32EncodeBufSize(src.size()));
  94. dst.resize(Base32Encode(src, dst.data()));
  95. }
  96. ///
  97. /// @param s a base32 decoded string.
  98. ///
  99. /// @returns a encoded string.
  100. ///
  101. inline std::string Base32Encode(std::string_view s)
  102. {
  103. std::string ret;
  104. Base32Encode(s, ret);
  105. return ret;
  106. }