Last active
August 13, 2021 23:17
-
-
Save vlakoff/3139e310664285c6c83b to your computer and use it in GitHub Desktop.
Convert from Mozilla's LZ4 format to LZ4 v1.3
This file contains hidden or 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 | |
/** | |
* Convert from Mozilla's LZ4 format to LZ4 v1.3 | |
* | |
* @link https://searchfox.org/mozilla-central/source/toolkit/components/lz4/lz4.js | |
* @link https://lz4.software.informer.com/1.3/ | |
* | |
* @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'); | |
} | |
$new_header = pack('V', 0x184C2103) // magic number LZ4 v1.0-1.3, little endian | |
. substr($moz_header, 8, 4) // size of decompressed file, uint32 little endian | |
. hex2bin('0000004D') // I don't know what this is, but it never changes | |
. pack('V', strlen($compressed_data)); // size of compressed data (without header), uint32 little endian | |
return $new_header . $compressed_data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See: https://bugzilla.mozilla.org/show_bug.cgi?id=1209390