Created
May 20, 2019 02:43
-
-
Save moriyoshi/1eef3efb8ed609e3ef141cbbd2b1f09c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function lz4decodeframes($in) { | |
if (substr($in, 0, 4) !== "\x04\x22\x4d\x18") { | |
throw new Exception(); | |
} | |
$o = 6; | |
$f = ord($in[4]); | |
if ($f & 8) { | |
$o += 8; | |
} | |
if ($f & 1) { | |
$o += 4; | |
} | |
$o += 1; // HC | |
for (;;) { | |
$blockSize = unpack('V', substr($in, $o, 4))[1]; | |
$o += 4; | |
if (!$blockSize) { | |
break; | |
} | |
if ($blockSize & 0x80000000) { | |
yield substr($in, $o, $blockSize & 0x7fffffff); | |
} else { | |
yield lz4decode($in, $o, $o + $blockSize + 4); | |
} | |
$o += $blockSize; | |
if ($f & 16) { | |
$o += 4; | |
} | |
} | |
} | |
foreach (lz4decodeframes(file_get_contents('php://stdin')) as $block) { | |
echo $block; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment