Skip to content

Instantly share code, notes, and snippets.

@jdelisle
Forked from vlakoff/convert_lz4_header.php
Last active May 16, 2020 11:57
Show Gist options
  • Save jdelisle/0d038ec255364fc2895950b61534b656 to your computer and use it in GitHub Desktop.
Save jdelisle/0d038ec255364fc2895950b61534b656 to your computer and use it in GitHub Desktop.
Convert from Mozilla's LZ4 format to LZ4 v1.3 (tested with FF51 profile & lz4 v1.7.5)
<?php
/**
* Convert from Mozilla's LZ4 format to LZ4 v1.3
*
* @link https://dxr.mozilla.org/mozilla-central/source/toolkit/components/lz4/lz4.js
* @link https://github.com/lz4/lz4
* @link https://github.com/lz4/lz4/blob/master/doc/lz4_Frame_format.md#legacy-frame
*
* @param string $input File content in Mozilla's LZ4 format
* @return string File content converted to LZ4 v1.3
*
* @throws \RuntimeException If input data appears not to be of Mozilla's LZ4 format
*/
function convert_lz4_header($input) {
$moz_header = substr($input, 0, 12);
$compressed_data = substr($input, 12);
if (substr($moz_header, 0, 8) !== "mozLz40\0") {
throw new \RuntimeException('Wrong magic number');
}
$lz4_header = pack('V', 0x184C2102) // legacy frame magic number LZ4 v1.0-1.3, uint32_t little endian
. pack('V', strlen($compressed_data)); // size of compressed data (without header), uint32_t little endian
return $lz4_header . $compressed_data;
}
@xeruf
Copy link

xeruf commented May 16, 2020

I can't decode the generator file with the current version of lz4 :/

Error 44 : Unrecognized header : file cannot be decoded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment