Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Core/Util/Base64UrlSafe.php

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