base64.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. #include <util/generic/strbuf.h>
  4. #include <util/generic/string.h>
  5. /* @return Size of the buffer required to decode Base64 encoded data of size `len`.
  6. */
  7. constexpr size_t Base64DecodeBufSize(const size_t len) noexcept {
  8. return (len + 3) / 4 * 3;
  9. }
  10. /* Decode Base64 encoded data. Can decode both regular Base64 and Base64URL encoded data. Can decode
  11. * only valid Base64[URL] data, behaviour for invalid data is unspecified.
  12. *
  13. * @throws Throws exception in case of incorrect padding.
  14. *
  15. * @param dst memory for writing output.
  16. * @param b pointer to the beginning of base64 encoded string.
  17. * @param a pointer to the end of base64 encoded string
  18. *
  19. * @return Return number of bytes decoded.
  20. */
  21. size_t Base64Decode(void* dst, const char* b, const char* e);
  22. inline TStringBuf Base64Decode(const TStringBuf src, void* dst) {
  23. return TStringBuf(static_cast<const char*>(dst), Base64Decode(dst, src.begin(), src.end()));
  24. }
  25. inline void Base64Decode(const TStringBuf src, TString& dst) {
  26. dst.ReserveAndResize(Base64DecodeBufSize(src.size()));
  27. dst.resize(Base64Decode(src, dst.begin()).size());
  28. }
  29. //WARNING: can process not whole input silently, use Base64StrictDecode instead of this function
  30. inline TString Base64Decode(const TStringBuf s) {
  31. TString ret;
  32. Base64Decode(s, ret);
  33. return ret;
  34. }
  35. ///
  36. /// @brief Decodes Base64 string with strict verification
  37. /// of invalid symbols, also tries to decode Base64 string with padding
  38. /// inside.
  39. //
  40. /// @throws Throws exceptions on inputs which contain invalid symbols
  41. /// or incorrect padding.
  42. /// @{
  43. ///
  44. /// @param b a pointer to the beginning of base64 encoded string.
  45. /// @param e a pointer to the end of base64 encoded string.
  46. /// @param dst memory for writing output.
  47. ///
  48. /// @return Returns number of bytes decoded.
  49. ///
  50. size_t Base64StrictDecode(void* dst, const char* b, const char* e);
  51. ///
  52. /// @param src a base64 encoded string.
  53. /// @param dst an pointer to allocated memory
  54. /// for writing result.
  55. ///
  56. /// @return Returns dst wrapped into TStringBuf.
  57. ///
  58. inline TStringBuf Base64StrictDecode(const TStringBuf src, void* dst) {
  59. return TStringBuf(static_cast<const char*>(dst), Base64StrictDecode(dst, src.begin(), src.end()));
  60. }
  61. ///
  62. /// @param src a base64 encoded string.
  63. /// @param dst a decoded string.
  64. ///
  65. inline void Base64StrictDecode(const TStringBuf src, TString& dst) {
  66. dst.ReserveAndResize(Base64DecodeBufSize(src.size()));
  67. dst.resize(Base64StrictDecode(src, dst.begin()).size());
  68. }
  69. ///
  70. /// @param src a base64 encoded string.
  71. ///
  72. /// @returns a decoded string.
  73. ///
  74. inline TString Base64StrictDecode(const TStringBuf src) {
  75. TString ret;
  76. Base64StrictDecode(src, ret);
  77. return ret;
  78. }
  79. /// @}
  80. /// Works with strings which length is not divisible by 4.
  81. TString Base64DecodeUneven(const TStringBuf s);
  82. size_t Base64DecodeUneven(void* dst, const TStringBuf s);
  83. //encode
  84. constexpr size_t Base64EncodeBufSize(const size_t len) noexcept {
  85. return (len + 2) / 3 * 4 + 1;
  86. }
  87. char* Base64Encode(char* outstr, const unsigned char* instr, size_t len);
  88. char* Base64EncodeUrl(char* outstr, const unsigned char* instr, size_t len);
  89. /// Make base64 string which stay unchaged after applying 'urlencode' function
  90. /// as it doesn't contain character, which cannot be used in urls
  91. /// @param outstr a pointer to allocated memory for writing result.
  92. /// @param instr a to buffer to encode
  93. /// @param len size of instr buffer
  94. ///
  95. /// @return Returns pointer to last symbol in outstr buffer.
  96. ///
  97. char* Base64EncodeUrlNoPadding(char* outstr, const unsigned char* instr, size_t len);
  98. inline TStringBuf Base64Encode(const TStringBuf src, void* output) {
  99. return TStringBuf(static_cast<const char*>(output), Base64Encode(static_cast<char*>(output), reinterpret_cast<const unsigned char*>(src.data()), src.size()));
  100. }
  101. inline TStringBuf Base64EncodeUrl(const TStringBuf src, void* output) {
  102. return TStringBuf(static_cast<const char*>(output), Base64EncodeUrl(static_cast<char*>(output), reinterpret_cast<const unsigned char*>(src.data()), src.size()));
  103. }
  104. inline TStringBuf Base64EncodeUrlNoPadding(const TStringBuf src, void* output) {
  105. return TStringBuf(static_cast<const char*>(output), Base64EncodeUrlNoPadding(static_cast<char*>(output), reinterpret_cast<const unsigned char*>(src.data()), src.size()));
  106. }
  107. inline void Base64Encode(const TStringBuf src, TString& dst) {
  108. dst.ReserveAndResize(Base64EncodeBufSize(src.size()));
  109. dst.resize(Base64Encode(src, dst.begin()).size());
  110. }
  111. inline void Base64EncodeUrl(const TStringBuf src, TString& dst) {
  112. dst.ReserveAndResize(Base64EncodeBufSize(src.size()));
  113. dst.resize(Base64EncodeUrl(src, dst.begin()).size());
  114. }
  115. inline void Base64EncodeUrlNoPadding(const TStringBuf src, TString& dst) {
  116. dst.ReserveAndResize(Base64EncodeBufSize(src.size()));
  117. dst.resize(Base64EncodeUrlNoPadding(src, dst.begin()).size());
  118. }
  119. inline TString Base64Encode(const TStringBuf s) {
  120. TString ret;
  121. Base64Encode(s, ret);
  122. return ret;
  123. }
  124. inline TString Base64EncodeUrl(const TStringBuf s) {
  125. TString ret;
  126. Base64EncodeUrl(s, ret);
  127. return ret;
  128. }
  129. inline TString Base64EncodeUrlNoPadding(const TStringBuf s) {
  130. TString ret;
  131. Base64EncodeUrlNoPadding(s, ret);
  132. return ret;
  133. }