escaping.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2020 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/strings/internal/escaping.h"
  15. #include <limits>
  16. #include "absl/base/internal/endian.h"
  17. #include "absl/base/internal/raw_logging.h"
  18. namespace absl {
  19. ABSL_NAMESPACE_BEGIN
  20. namespace strings_internal {
  21. // The two strings below provide maps from normal 6-bit characters to their
  22. // base64-escaped equivalent.
  23. // For the inverse case, see kUn(WebSafe)Base64 in the external
  24. // escaping.cc.
  25. ABSL_CONST_INIT const char kBase64Chars[] =
  26. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  27. ABSL_CONST_INIT const char kWebSafeBase64Chars[] =
  28. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  29. size_t CalculateBase64EscapedLenInternal(size_t input_len, bool do_padding) {
  30. // Base64 encodes three bytes of input at a time. If the input is not
  31. // divisible by three, we pad as appropriate.
  32. //
  33. // Base64 encodes each three bytes of input into four bytes of output.
  34. constexpr size_t kMaxSize = (std::numeric_limits<size_t>::max() - 1) / 4 * 3;
  35. ABSL_INTERNAL_CHECK(input_len <= kMaxSize,
  36. "CalculateBase64EscapedLenInternal() overflow");
  37. size_t len = (input_len / 3) * 4;
  38. // Since all base 64 input is an integral number of octets, only the following
  39. // cases can arise:
  40. if (input_len % 3 == 0) {
  41. // (from https://tools.ietf.org/html/rfc3548)
  42. // (1) the final quantum of encoding input is an integral multiple of 24
  43. // bits; here, the final unit of encoded output will be an integral
  44. // multiple of 4 characters with no "=" padding,
  45. } else if (input_len % 3 == 1) {
  46. // (from https://tools.ietf.org/html/rfc3548)
  47. // (2) the final quantum of encoding input is exactly 8 bits; here, the
  48. // final unit of encoded output will be two characters followed by two
  49. // "=" padding characters, or
  50. len += 2;
  51. if (do_padding) {
  52. len += 2;
  53. }
  54. } else { // (input_len % 3 == 2)
  55. // (from https://tools.ietf.org/html/rfc3548)
  56. // (3) the final quantum of encoding input is exactly 16 bits; here, the
  57. // final unit of encoded output will be three characters followed by one
  58. // "=" padding character.
  59. len += 3;
  60. if (do_padding) {
  61. len += 1;
  62. }
  63. }
  64. return len;
  65. }
  66. // ----------------------------------------------------------------------
  67. // Take the input in groups of 4 characters and turn each
  68. // character into a code 0 to 63 thus:
  69. // A-Z map to 0 to 25
  70. // a-z map to 26 to 51
  71. // 0-9 map to 52 to 61
  72. // +(- for WebSafe) maps to 62
  73. // /(_ for WebSafe) maps to 63
  74. // There will be four numbers, all less than 64 which can be represented
  75. // by a 6 digit binary number (aaaaaa, bbbbbb, cccccc, dddddd respectively).
  76. // Arrange the 6 digit binary numbers into three bytes as such:
  77. // aaaaaabb bbbbcccc ccdddddd
  78. // Equals signs (one or two) are used at the end of the encoded block to
  79. // indicate that the text was not an integer multiple of three bytes long.
  80. // ----------------------------------------------------------------------
  81. size_t Base64EscapeInternal(const unsigned char* src, size_t szsrc, char* dest,
  82. size_t szdest, const char* base64,
  83. bool do_padding) {
  84. static const char kPad64 = '=';
  85. if (szsrc * 4 > szdest * 3) return 0;
  86. char* cur_dest = dest;
  87. const unsigned char* cur_src = src;
  88. char* const limit_dest = dest + szdest;
  89. const unsigned char* const limit_src = src + szsrc;
  90. // (from https://tools.ietf.org/html/rfc3548)
  91. // Special processing is performed if fewer than 24 bits are available
  92. // at the end of the data being encoded. A full encoding quantum is
  93. // always completed at the end of a quantity. When fewer than 24 input
  94. // bits are available in an input group, zero bits are added (on the
  95. // right) to form an integral number of 6-bit groups.
  96. //
  97. // If do_padding is true, padding at the end of the data is performed. This
  98. // output padding uses the '=' character.
  99. // Three bytes of data encodes to four characters of cyphertext.
  100. // So we can pump through three-byte chunks atomically.
  101. if (szsrc >= 3) { // "limit_src - 3" is UB if szsrc < 3.
  102. while (cur_src < limit_src - 3) { // While we have >= 32 bits.
  103. uint32_t in = absl::big_endian::Load32(cur_src) >> 8;
  104. cur_dest[0] = base64[in >> 18];
  105. in &= 0x3FFFF;
  106. cur_dest[1] = base64[in >> 12];
  107. in &= 0xFFF;
  108. cur_dest[2] = base64[in >> 6];
  109. in &= 0x3F;
  110. cur_dest[3] = base64[in];
  111. cur_dest += 4;
  112. cur_src += 3;
  113. }
  114. }
  115. // To save time, we didn't update szdest or szsrc in the loop. So do it now.
  116. szdest = static_cast<size_t>(limit_dest - cur_dest);
  117. szsrc = static_cast<size_t>(limit_src - cur_src);
  118. /* now deal with the tail (<=3 bytes) */
  119. switch (szsrc) {
  120. case 0:
  121. // Nothing left; nothing more to do.
  122. break;
  123. case 1: {
  124. // One byte left: this encodes to two characters, and (optionally)
  125. // two pad characters to round out the four-character cypherblock.
  126. if (szdest < 2) return 0;
  127. uint32_t in = cur_src[0];
  128. cur_dest[0] = base64[in >> 2];
  129. in &= 0x3;
  130. cur_dest[1] = base64[in << 4];
  131. cur_dest += 2;
  132. szdest -= 2;
  133. if (do_padding) {
  134. if (szdest < 2) return 0;
  135. cur_dest[0] = kPad64;
  136. cur_dest[1] = kPad64;
  137. cur_dest += 2;
  138. szdest -= 2;
  139. }
  140. break;
  141. }
  142. case 2: {
  143. // Two bytes left: this encodes to three characters, and (optionally)
  144. // one pad character to round out the four-character cypherblock.
  145. if (szdest < 3) return 0;
  146. uint32_t in = absl::big_endian::Load16(cur_src);
  147. cur_dest[0] = base64[in >> 10];
  148. in &= 0x3FF;
  149. cur_dest[1] = base64[in >> 4];
  150. in &= 0x00F;
  151. cur_dest[2] = base64[in << 2];
  152. cur_dest += 3;
  153. szdest -= 3;
  154. if (do_padding) {
  155. if (szdest < 1) return 0;
  156. cur_dest[0] = kPad64;
  157. cur_dest += 1;
  158. szdest -= 1;
  159. }
  160. break;
  161. }
  162. case 3: {
  163. // Three bytes left: same as in the big loop above. We can't do this in
  164. // the loop because the loop above always reads 4 bytes, and the fourth
  165. // byte is past the end of the input.
  166. if (szdest < 4) return 0;
  167. uint32_t in =
  168. (uint32_t{cur_src[0]} << 16) + absl::big_endian::Load16(cur_src + 1);
  169. cur_dest[0] = base64[in >> 18];
  170. in &= 0x3FFFF;
  171. cur_dest[1] = base64[in >> 12];
  172. in &= 0xFFF;
  173. cur_dest[2] = base64[in >> 6];
  174. in &= 0x3F;
  175. cur_dest[3] = base64[in];
  176. cur_dest += 4;
  177. szdest -= 4;
  178. break;
  179. }
  180. default:
  181. // Should not be reached: blocks of 4 bytes are handled
  182. // in the while loop before this switch statement.
  183. ABSL_RAW_LOG(FATAL, "Logic problem? szsrc = %zu", szsrc);
  184. break;
  185. }
  186. return static_cast<size_t>(cur_dest - dest);
  187. }
  188. } // namespace strings_internal
  189. ABSL_NAMESPACE_END
  190. } // namespace absl