]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/ParagonIE/ConstantTime/Base64.php
Merge branch 'nightly' into 'master'
[quix0rs-gnu-social.git] / extlib / ParagonIE / ConstantTime / Base64.php
1 <?php
2 namespace ParagonIE\ConstantTime;
3
4 /**
5  *  Copyright (c) 2016 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 /**
28  * Class Base64
29  * [A-Z][a-z][0-9]+/
30  *
31  * @package ParagonIE\ConstantTime
32  */
33 abstract class Base64 implements EncoderInterface
34 {
35     /**
36      * Encode into Base64
37      *
38      * Base64 character set "[A-Z][a-z][0-9]+/"
39      *
40      * @param string $src
41      * @return string
42      */
43     public static function encode($src)
44     {
45         $dest = '';
46         $srcLen = Binary::safeStrlen($src);
47         // Main loop (no padding):
48         for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
49             $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 3));
50             $b0 = $chunk[1];
51             $b1 = $chunk[2];
52             $b2 = $chunk[3];
53
54             $dest .=
55                 static::encode6Bits(               $b0 >> 2       ) .
56                 static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
57                 static::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
58                 static::encode6Bits(  $b2                     & 63);
59         }
60         // The last chunk, which may have padding:
61         if ($i < $srcLen) {
62             $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
63             $b0 = $chunk[1];
64             if ($i + 1 < $srcLen) {
65                 $b1 = $chunk[2];
66                 $dest .=
67                     static::encode6Bits(               $b0 >> 2       ) .
68                     static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
69                     static::encode6Bits( ($b1 << 2)               & 63) . '=';
70             } else {
71                 $dest .=
72                     static::encode6Bits( $b0 >> 2) .
73                     static::encode6Bits(($b0 << 4) & 63) . '==';
74             }
75         }
76         return $dest;
77     }
78
79     /**
80      * decode from base64 into binary
81      *
82      * Base64 character set "./[A-Z][a-z][0-9]"
83      *
84      * @param string $src
85      * @return string|bool
86      * @throws \RangeException
87      */
88     public static function decode($src, $strictPadding = false)
89     {
90         // Remove padding
91         $srcLen = Binary::safeStrlen($src);
92         if ($srcLen === 0) {
93             return '';
94         }
95         if ($strictPadding) {
96             if (($srcLen & 3) === 0) {
97                 if ($src[$srcLen - 1] === '=') {
98                     $srcLen--;
99                     if ($src[$srcLen - 1] === '=') {
100                         $srcLen--;
101                     }
102                 }
103             }
104             if (($srcLen & 3) === 1) {
105                 throw new \RangeException(
106                     'Incorrect padding'
107                 );
108             }
109         } else {
110             $src = \rtrim($src, '=');
111             $srcLen = Binary::safeStrlen($src);
112         }
113
114         $err = 0;
115         $dest = '';
116         // Main loop (no padding):
117         for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
118             $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 4));
119             $c0 = static::decode6Bits($chunk[1]);
120             $c1 = static::decode6Bits($chunk[2]);
121             $c2 = static::decode6Bits($chunk[3]);
122             $c3 = static::decode6Bits($chunk[4]);
123
124             $dest .= \pack(
125                 'CCC',
126                 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
127                 ((($c1 << 4) | ($c2 >> 2)) & 0xff),
128                 ((($c2 << 6) |  $c3      ) & 0xff)
129             );
130             $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
131         }
132         // The last chunk, which may have padding:
133         if ($i < $srcLen) {
134             $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
135             $c0 = static::decode6Bits($chunk[1]);
136             if ($i + 2 < $srcLen) {
137                 $c1 = static::decode6Bits($chunk[2]);
138                 $c2 = static::decode6Bits($chunk[3]);
139                 $dest .= \pack(
140                     'CC',
141                     ((($c0 << 2) | ($c1 >> 4)) & 0xff),
142                     ((($c1 << 4) | ($c2 >> 2)) & 0xff)
143                 );
144                 $err |= ($c0 | $c1 | $c2) >> 8;
145             } elseif($i + 1 < $srcLen) {
146                 $c1 = static::decode6Bits($chunk[2]);
147                 $dest .= \pack(
148                     'C',
149                     ((($c0 << 2) | ($c1 >> 4)) & 0xff)
150                 );
151                 $err |= ($c0 | $c1) >> 8;
152             } elseif ($i < $srcLen && $strictPadding) {
153                 $err |= 1;
154             }
155         }
156         if ($err !== 0) {
157             return false;
158         }
159         return $dest;
160     }
161
162     /**
163      * Uses bitwise operators instead of table-lookups to turn 6-bit integers
164      * into 8-bit integers.
165      *
166      * Base64 character set:
167      * [A-Z]      [a-z]      [0-9]      +     /
168      * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
169      *
170      * @param int $src
171      * @return int
172      */
173     protected static function decode6Bits($src)
174     {
175         $ret = -1;
176
177         // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
178         $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
179
180         // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
181         $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
182
183         // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
184         $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
185
186         // if ($src == 0x2b) $ret += 62 + 1;
187         $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63;
188
189         // if ($src == 0x2f) ret += 63 + 1;
190         $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64;
191
192         return $ret;
193     }
194
195     /**
196      * Uses bitwise operators instead of table-lookups to turn 8-bit integers
197      * into 6-bit integers.
198      *
199      * @param int $src
200      * @return string
201      */
202     protected static function encode6Bits($src)
203     {
204         $diff = 0x41;
205
206         // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
207         $diff += ((25 - $src) >> 8) & 6;
208
209         // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
210         $diff -= ((51 - $src) >> 8) & 75;
211
212         // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15
213         $diff -= ((61 - $src) >> 8) & 15;
214
215         // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3
216         $diff += ((62 - $src) >> 8) & 3;
217
218         return \pack('C', $src + $diff);
219     }
220 }