Skip to content

Instantly share code, notes, and snippets.

@moriyoshi
Created May 20, 2019 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moriyoshi/1eef3efb8ed609e3ef141cbbd2b1f09c to your computer and use it in GitHub Desktop.
Save moriyoshi/1eef3efb8ed609e3ef141cbbd2b1f09c to your computer and use it in GitHub Desktop.
<?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