Skip to content

Commit

Permalink
Optimize EOS check
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Jul 25, 2019
1 parent a0617ee commit ec57850
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/HPack.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,28 +205,27 @@ public static function huffmanDecode(string $input) /* : ?string */
$length = \strlen($input);
$out = \str_repeat("\0", $length / 5 * 8 + 1); // max length

for ($bitCount = $off = $i = 0; $i < $length; $i++) {
// Fail if EOS symbol is found.
if ($input[$i] === "\x3f"
&& ($input[$i + 1] ?? null) === "\xff"
&& ($input[$i + 2] ?? null) === "\xff"
&& ($input[$i + 3] ?? null) === "\xff"
) {
return null;
}
// Fail if EOS symbol is found.
if (\strpos($input, "\x3f\xff\xff\xff") !== false) {
return null;
}

list($lookup, $chr) = $lookup[$input[$i]];
for ($bitCount = $off = $i = 0; $i < $length; $i++) {
$currentByte = $input[$i];

if ($chr == null) { // Loose type check intentional to match both null and empty string.
if ($chr === "") {
return null;
}
list($lookup, $chr) = $lookup[$currentByte];

if ($chr === null) {
continue;
}

if ($chr === "") {
return null;
}

$out[$off++] = $chr[0];
$bitCount += $lengths[$chr[0]];

if (isset($chr[1])) {
$out[$off++] = $chr[1];
$bitCount += $lengths[$chr[1]];
Expand Down

0 comments on commit ec57850

Please sign in to comment.