PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/google-listings-and-ads/vendor/paragonie/constant_time_encoding/src/Base64.php

https://gitlab.com/remyvianne/krowkaramel
PHP | 270 lines | 147 code | 23 blank | 100 comment | 18 complexity | d335c9fe154405a23c7c331deb6e628a MD5 | raw file
  1. <?php
  2. declare(strict_types=1);
  3. namespace ParagonIE\ConstantTime;
  4. /**
  5. * Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
  6. * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. /**
  27. * Class Base64
  28. * [A-Z][a-z][0-9]+/
  29. *
  30. * @package ParagonIE\ConstantTime
  31. */
  32. abstract class Base64 implements EncoderInterface
  33. {
  34. /**
  35. * Encode into Base64
  36. *
  37. * Base64 character set "[A-Z][a-z][0-9]+/"
  38. *
  39. * @param string $binString
  40. * @return string
  41. * @throws \TypeError
  42. */
  43. public static function encode(string $binString): string
  44. {
  45. return static::doEncode($binString, true);
  46. }
  47. /**
  48. * Encode into Base64, no = padding
  49. *
  50. * Base64 character set "[A-Z][a-z][0-9]+/"
  51. *
  52. * @param string $src
  53. * @return string
  54. * @throws \TypeError
  55. */
  56. public static function encodeUnpadded(string $src): string
  57. {
  58. return static::doEncode($src, false);
  59. }
  60. /**
  61. * @param string $src
  62. * @param bool $pad Include = padding?
  63. * @return string
  64. * @throws \TypeError
  65. */
  66. protected static function doEncode(string $src, bool $pad = true): string
  67. {
  68. $dest = '';
  69. $srcLen = Binary::safeStrlen($src);
  70. // Main loop (no padding):
  71. for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
  72. /** @var array<int, int> $chunk */
  73. $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 3));
  74. $b0 = $chunk[1];
  75. $b1 = $chunk[2];
  76. $b2 = $chunk[3];
  77. $dest .=
  78. static::encode6Bits( $b0 >> 2 ) .
  79. static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
  80. static::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
  81. static::encode6Bits( $b2 & 63);
  82. }
  83. // The last chunk, which may have padding:
  84. if ($i < $srcLen) {
  85. /** @var array<int, int> $chunk */
  86. $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
  87. $b0 = $chunk[1];
  88. if ($i + 1 < $srcLen) {
  89. $b1 = $chunk[2];
  90. $dest .=
  91. static::encode6Bits($b0 >> 2) .
  92. static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
  93. static::encode6Bits(($b1 << 2) & 63);
  94. if ($pad) {
  95. $dest .= '=';
  96. }
  97. } else {
  98. $dest .=
  99. static::encode6Bits( $b0 >> 2) .
  100. static::encode6Bits(($b0 << 4) & 63);
  101. if ($pad) {
  102. $dest .= '==';
  103. }
  104. }
  105. }
  106. return $dest;
  107. }
  108. /**
  109. * decode from base64 into binary
  110. *
  111. * Base64 character set "./[A-Z][a-z][0-9]"
  112. *
  113. * @param string $encodedString
  114. * @param bool $strictPadding
  115. * @return string
  116. * @throws \RangeException
  117. * @throws \TypeError
  118. * @psalm-suppress RedundantCondition
  119. */
  120. public static function decode(string $encodedString, bool $strictPadding = false): string
  121. {
  122. // Remove padding
  123. $srcLen = Binary::safeStrlen($encodedString);
  124. if ($srcLen === 0) {
  125. return '';
  126. }
  127. if ($strictPadding) {
  128. if (($srcLen & 3) === 0) {
  129. if ($encodedString[$srcLen - 1] === '=') {
  130. $srcLen--;
  131. if ($encodedString[$srcLen - 1] === '=') {
  132. $srcLen--;
  133. }
  134. }
  135. }
  136. if (($srcLen & 3) === 1) {
  137. throw new \RangeException(
  138. 'Incorrect padding'
  139. );
  140. }
  141. if ($encodedString[$srcLen - 1] === '=') {
  142. throw new \RangeException(
  143. 'Incorrect padding'
  144. );
  145. }
  146. } else {
  147. $encodedString = \rtrim($encodedString, '=');
  148. $srcLen = Binary::safeStrlen($encodedString);
  149. }
  150. $err = 0;
  151. $dest = '';
  152. // Main loop (no padding):
  153. for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
  154. /** @var array<int, int> $chunk */
  155. $chunk = \unpack('C*', Binary::safeSubstr($encodedString, $i, 4));
  156. $c0 = static::decode6Bits($chunk[1]);
  157. $c1 = static::decode6Bits($chunk[2]);
  158. $c2 = static::decode6Bits($chunk[3]);
  159. $c3 = static::decode6Bits($chunk[4]);
  160. $dest .= \pack(
  161. 'CCC',
  162. ((($c0 << 2) | ($c1 >> 4)) & 0xff),
  163. ((($c1 << 4) | ($c2 >> 2)) & 0xff),
  164. ((($c2 << 6) | $c3 ) & 0xff)
  165. );
  166. $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
  167. }
  168. // The last chunk, which may have padding:
  169. if ($i < $srcLen) {
  170. /** @var array<int, int> $chunk */
  171. $chunk = \unpack('C*', Binary::safeSubstr($encodedString, $i, $srcLen - $i));
  172. $c0 = static::decode6Bits($chunk[1]);
  173. if ($i + 2 < $srcLen) {
  174. $c1 = static::decode6Bits($chunk[2]);
  175. $c2 = static::decode6Bits($chunk[3]);
  176. $dest .= \pack(
  177. 'CC',
  178. ((($c0 << 2) | ($c1 >> 4)) & 0xff),
  179. ((($c1 << 4) | ($c2 >> 2)) & 0xff)
  180. );
  181. $err |= ($c0 | $c1 | $c2) >> 8;
  182. } elseif ($i + 1 < $srcLen) {
  183. $c1 = static::decode6Bits($chunk[2]);
  184. $dest .= \pack(
  185. 'C',
  186. ((($c0 << 2) | ($c1 >> 4)) & 0xff)
  187. );
  188. $err |= ($c0 | $c1) >> 8;
  189. } elseif ($strictPadding) {
  190. $err |= 1;
  191. }
  192. }
  193. $check = ($err === 0);
  194. if (!$check) {
  195. throw new \RangeException(
  196. 'Base64::decode() only expects characters in the correct base64 alphabet'
  197. );
  198. }
  199. return $dest;
  200. }
  201. /**
  202. * Uses bitwise operators instead of table-lookups to turn 6-bit integers
  203. * into 8-bit integers.
  204. *
  205. * Base64 character set:
  206. * [A-Z] [a-z] [0-9] + /
  207. * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
  208. *
  209. * @param int $src
  210. * @return int
  211. */
  212. protected static function decode6Bits(int $src): int
  213. {
  214. $ret = -1;
  215. // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
  216. $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
  217. // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
  218. $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
  219. // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
  220. $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
  221. // if ($src == 0x2b) $ret += 62 + 1;
  222. $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63;
  223. // if ($src == 0x2f) ret += 63 + 1;
  224. $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64;
  225. return $ret;
  226. }
  227. /**
  228. * Uses bitwise operators instead of table-lookups to turn 8-bit integers
  229. * into 6-bit integers.
  230. *
  231. * @param int $src
  232. * @return string
  233. */
  234. protected static function encode6Bits(int $src): string
  235. {
  236. $diff = 0x41;
  237. // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
  238. $diff += ((25 - $src) >> 8) & 6;
  239. // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
  240. $diff -= ((51 - $src) >> 8) & 75;
  241. // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15
  242. $diff -= ((61 - $src) >> 8) & 15;
  243. // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3
  244. $diff += ((62 - $src) >> 8) & 3;
  245. return \pack('C', $src + $diff);
  246. }
  247. }