Skip to content

Commit c8b7efb

Browse files
Merge pull request #58 from TimWolla/func-overload-is-no-more
Remove `mbstring.func_overload` compatibility layer
2 parents 5cba579 + 8380c62 commit c8b7efb

6 files changed

Lines changed: 23 additions & 35 deletions

File tree

src/Base32.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public static function decodeNoPadding(
221221
string $encodedString,
222222
bool $upper = false
223223
): string {
224-
$srcLen = Binary::safeStrlen($encodedString);
224+
$srcLen = \strlen($encodedString);
225225
if ($srcLen === 0) {
226226
return '';
227227
}
@@ -263,7 +263,7 @@ protected static function doDecode(
263263
: 'decode5Bits';
264264

265265
// Remove padding
266-
$srcLen = Binary::safeStrlen($src);
266+
$srcLen = \strlen($src);
267267
if ($srcLen === 0) {
268268
return '';
269269
}
@@ -284,15 +284,15 @@ protected static function doDecode(
284284
}
285285
} else {
286286
$src = \rtrim($src, '=');
287-
$srcLen = Binary::safeStrlen($src);
287+
$srcLen = \strlen($src);
288288
}
289289

290290
$err = 0;
291291
$dest = '';
292292
// Main loop (no padding):
293293
for ($i = 0; $i + 8 <= $srcLen; $i += 8) {
294294
/** @var array<int, int> $chunk */
295-
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 8));
295+
$chunk = \unpack('C*', \substr($src, $i, 8));
296296
/** @var int $c0 */
297297
$c0 = static::$method($chunk[1]);
298298
/** @var int $c1 */
@@ -323,7 +323,7 @@ protected static function doDecode(
323323
// The last chunk, which may have padding:
324324
if ($i < $srcLen) {
325325
/** @var array<int, int> $chunk */
326-
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
326+
$chunk = \unpack('C*', \substr($src, $i, $srcLen - $i));
327327
/** @var int $c0 */
328328
$c0 = static::$method($chunk[1]);
329329

@@ -474,12 +474,12 @@ protected static function doEncode(
474474
: 'encode5Bits';
475475

476476
$dest = '';
477-
$srcLen = Binary::safeStrlen($src);
477+
$srcLen = \strlen($src);
478478

479479
// Main loop (no padding):
480480
for ($i = 0; $i + 5 <= $srcLen; $i += 5) {
481481
/** @var array<int, int> $chunk */
482-
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 5));
482+
$chunk = \unpack('C*', \substr($src, $i, 5));
483483
$b0 = $chunk[1];
484484
$b1 = $chunk[2];
485485
$b2 = $chunk[3];
@@ -498,7 +498,7 @@ protected static function doEncode(
498498
// The last chunk, which may have padding:
499499
if ($i < $srcLen) {
500500
/** @var array<int, int> $chunk */
501-
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
501+
$chunk = \unpack('C*', \substr($src, $i, $srcLen - $i));
502502
$b0 = $chunk[1];
503503
if ($i + 3 < $srcLen) {
504504
$b1 = $chunk[2];

src/Base64.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ protected static function doEncode(
115115
bool $pad = true
116116
): string {
117117
$dest = '';
118-
$srcLen = Binary::safeStrlen($src);
118+
$srcLen = \strlen($src);
119119
// Main loop (no padding):
120120
for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
121121
/** @var array<int, int> $chunk */
122-
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 3));
122+
$chunk = \unpack('C*', \substr($src, $i, 3));
123123
$b0 = $chunk[1];
124124
$b1 = $chunk[2];
125125
$b2 = $chunk[3];
@@ -133,7 +133,7 @@ protected static function doEncode(
133133
// The last chunk, which may have padding:
134134
if ($i < $srcLen) {
135135
/** @var array<int, int> $chunk */
136-
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
136+
$chunk = \unpack('C*', \substr($src, $i, $srcLen - $i));
137137
$b0 = $chunk[1];
138138
if ($i + 1 < $srcLen) {
139139
$b1 = $chunk[2];
@@ -175,7 +175,7 @@ public static function decode(
175175
bool $strictPadding = false
176176
): string {
177177
// Remove padding
178-
$srcLen = Binary::safeStrlen($encodedString);
178+
$srcLen = \strlen($encodedString);
179179
if ($srcLen === 0) {
180180
return '';
181181
}
@@ -215,15 +215,15 @@ public static function decode(
215215
}
216216
} else {
217217
$encodedString = \rtrim($encodedString, '=');
218-
$srcLen = Binary::safeStrlen($encodedString);
218+
$srcLen = \strlen($encodedString);
219219
}
220220

221221
$err = 0;
222222
$dest = '';
223223
// Main loop (no padding):
224224
for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
225225
/** @var array<int, int> $chunk */
226-
$chunk = \unpack('C*', Binary::safeSubstr($encodedString, $i, 4));
226+
$chunk = \unpack('C*', \substr($encodedString, $i, 4));
227227
$c0 = static::decode6Bits($chunk[1]);
228228
$c1 = static::decode6Bits($chunk[2]);
229229
$c2 = static::decode6Bits($chunk[3]);
@@ -240,7 +240,7 @@ public static function decode(
240240
// The last chunk, which may have padding:
241241
if ($i < $srcLen) {
242242
/** @var array<int, int> $chunk */
243-
$chunk = \unpack('C*', Binary::safeSubstr($encodedString, $i, $srcLen - $i));
243+
$chunk = \unpack('C*', \substr($encodedString, $i, $srcLen - $i));
244244
$c0 = static::decode6Bits($chunk[1]);
245245

246246
if ($i + 2 < $srcLen) {
@@ -287,7 +287,7 @@ public static function decodeNoPadding(
287287
#[\SensitiveParameter]
288288
string $encodedString
289289
): string {
290-
$srcLen = Binary::safeStrlen($encodedString);
290+
$srcLen = \strlen($encodedString);
291291
if ($srcLen === 0) {
292292
return '';
293293
}

src/Binary.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,7 @@ public static function safeStrlen(
4949
#[\SensitiveParameter]
5050
string $str
5151
): int {
52-
if (\function_exists('mb_strlen')) {
53-
// mb_strlen in PHP 7.x can return false.
54-
/** @psalm-suppress RedundantCast */
55-
return (int) \mb_strlen($str, '8bit');
56-
} else {
57-
return \strlen($str);
58-
}
52+
return \strlen($str);
5953
}
6054

6155
/**
@@ -80,9 +74,6 @@ public static function safeSubstr(
8074
if ($length === 0) {
8175
return '';
8276
}
83-
if (\function_exists('mb_substr')) {
84-
return \mb_substr($str, $start, $length, '8bit');
85-
}
8677
// Unlike mb_substr(), substr() doesn't accept NULL for length
8778
if ($length !== null) {
8879
return \substr($str, $start, $length);

src/Hex.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static function encode(
5656
}
5757
}
5858
$hex = '';
59-
$len = Binary::safeStrlen($binString);
59+
$len = \strlen($binString);
6060
for ($i = 0; $i < $len; ++$i) {
6161
/** @var array<int, int> $chunk */
6262
$chunk = \unpack('C', $binString[$i]);
@@ -85,7 +85,7 @@ public static function encodeUpper(
8585
string $binString
8686
): string {
8787
$hex = '';
88-
$len = Binary::safeStrlen($binString);
88+
$len = \strlen($binString);
8989

9090
for ($i = 0; $i < $len; ++$i) {
9191
/** @var array<int, int> $chunk */
@@ -127,7 +127,7 @@ public static function decode(
127127
$hex_pos = 0;
128128
$bin = '';
129129
$c_acc = 0;
130-
$hex_len = Binary::safeStrlen($encodedString);
130+
$hex_len = \strlen($encodedString);
131131
$state = 0;
132132
if (($hex_len & 1) !== 0) {
133133
if ($strictPadding) {

tests/Base64UrlSafeTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPUnit\Framework\Attributes\DataProvider;
88
use PHPUnit\Framework\TestCase;
99
use ParagonIE\ConstantTime\Base64UrlSafe;
10-
use ParagonIE\ConstantTime\Binary;
1110
use RangeException;
1211
use TypeError;
1312

@@ -49,7 +48,7 @@ public function testRandom()
4948

5049
$random = \random_bytes(1 << 20);
5150
$enc = Base64UrlSafe::encode($random);
52-
$this->assertTrue(Binary::safeStrlen($enc) > 65536);
51+
$this->assertTrue(\strlen($enc) > 65536);
5352
$this->assertSame(
5453
$random,
5554
Base64UrlSafe::decode($enc)

tests/CanonicalTrait.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
declare(strict_types=1);
33
namespace ParagonIE\ConstantTime\Tests;
44

5-
use ParagonIE\ConstantTime\Binary;
6-
75
trait CanonicalTrait
86
{
97
public static function canonicalDataProvider(): array
@@ -22,8 +20,8 @@ abstract protected function getNextChar(string $c): string;
2220

2321
protected function increment(string $str): string
2422
{
25-
$i = Binary::safeStrlen($str) - 1;
23+
$i = \strlen($str) - 1;
2624
$c = $this->getNextChar($str[$i]);
27-
return Binary::safeSubstr($str, 0, $i) . $c;
25+
return \substr($str, 0, $i) . $c;
2826
}
2927
}

0 commit comments

Comments
 (0)