Base64.php 7.2 KB

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