escaping.cc 7.3 KB

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