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