Skip to content

Instantly share code, notes, and snippets.

@vlakoff
Last active August 13, 2021 23:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vlakoff/3139e310664285c6c83b to your computer and use it in GitHub Desktop.
Save vlakoff/3139e310664285c6c83b to your computer and use it in GitHub Desktop.
Convert from Mozilla's LZ4 format to LZ4 v1.3
<?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;
}
@vlakoff
Copy link
Author

vlakoff commented Feb 17, 2018

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