Core\Compress::decompress
Core\Compress::decompress(
Core\Bytes::Source|Core\Bytes::Chunks $data,
Core\Compress::Codec $codec,
uint $maxBytes = 67108864,
uint $maxRatio = 1000,
): bytesReads compressed data and gives you the original bytes back.
The compressed data can be bytes, an open file, an uploaded file, Core\Request::bodyStream(), or a list or loop that gives bytes pieces. A file or a stream is read one piece at a time. You pass the same Core\Compress::Codec the data was compressed with. Data that is not in that format throws a ParseError. If the data arrived from outside your program, the result is tainted too.
Every call has a limit on how much it may produce. $maxBytes is the largest result you accept, and $maxRatio is the most bytes of result per byte of input. A call that would pass either limit throws a ParseError and returns nothing. You can set smaller limits than the server allows. You can never set larger ones, and there is no way to switch the limit off.
In plain words: a tiny compressed file can unpack into a huge one and fill up a server. These two limits stop a file that is made to do that.
Parameters
$dataCore\Bytes::Source|Core\Bytes::ChunksThe compressed data:bytes, an openCore\IO\File, an uploaded file, the request body, or the pieces of a loop. If$dataistainted, the result istaintedtoo. Decompressing does not make data safe to use. A source that is notbytesis checked against$maxRatiowhile it is read, so a small upload that would decompress to a lot stops early.$codecCore\Compress::CodecThe format$datais in. The method does not detect the format. Data in another format throws an error.$maxBytesuintdefault 67108864The most output this call produces, in bytes. It can be lower than[limits] max_decompressed, but not higher.0means zero bytes. It does not mean no limit.$maxRatiouintdefault 1000The most bytes of output for each byte of input. This is the second part of the size limit. A compression bomb is small data that decompresses to a huge size. With a limit in bytes alone, a small request can still reach that limit.
Return value
Returns bytes. The decompressed bytes. The result is never cut short. If the output would go over either limit, the method throws an error and returns no partial result.
Errors
ParseError$datais not valid data in the format$codec, or the output would go over either limit. It is never anIOError. A hostile archive and a full disk are different problems.
Examples
Read compressed bytes back
<?nvs// Shows how to read compressed bytes back. You pass the same format the data// was compressed with.
bytes $notes = Core\Str::repeat("meeting notes for 4 March\n", 40) as bytes;bytes $frame = Core\Compress::compress($notes, Core\Compress::Codec::Zstd);bytes $back = Core\Compress::decompress($frame, Core\Compress::Codec::Zstd);
echo 'compressed: ', Core\Bytes::length($frame), " bytes\n";echo 'read back: ', Core\Bytes::length($back), " bytes\n";echo 'same as before: ', Core\Hash::equals($back, $notes) ? 'yes' : 'no', "\n";
// The format is not guessed. Reading the same bytes as gzip throws a// `ParseError`.try { Core\Compress::decompress($frame, Core\Compress::Codec::Gzip); echo "gzip read it\n";} catch (ParseError $wrongFormat) { echo "gzip could not read it\n";}compressed: 79 bytesread back: 1040 bytessame as before: yesgzip could not read itThe limit on the result
<?nvs// Shows the limit every call has. `$maxBytes` is the largest result you accept,// and a result that would be larger throws a `ParseError`.
bytes $page = Core\Str::repeat("<p>the same line again</p>\n", 400) as bytes;bytes $frame = Core\Compress::compress($page, Core\Compress::Codec::Gzip);
// The page is 10800 bytes, so a limit of 20000 accepts it.bytes $whole = Core\Compress::decompress($frame, Core\Compress::Codec::Gzip, 20000);echo 'read ', Core\Bytes::length($whole), " bytes\n";
// The same data under a limit of 1000 throws. You get nothing back, not the// first 1000 bytes.try { Core\Compress::decompress($frame, Core\Compress::Codec::Gzip, 1000); echo "the limit did not hold\n";} catch (ParseError $tooLarge) { echo "1000 bytes was not enough room\n";}read 10800 bytes1000 bytes was not enough roomAn upload that arrived compressed
<?nvs// Shows what a program does with an upload that arrived compressed. Whoever// sent it chose its size, so the call says how much it is willing to unpack.
// These bytes replace the upload. In a web request, you pass// `Core\Request::bodyStream()` in their place, and it is read in pieces.string $rows = Core\Str::repeat("1042,Berlin,39.90\n", 200);bytes $upload = Core\Compress::compress($rows, Core\Compress::Codec::Gzip);
// At most one mebibyte of result, and at most 200 bytes of result for each// byte that was sent. Both limits are checked while the result grows.try { bytes $csv = Core\Compress::decompress($upload, Core\Compress::Codec::Gzip, 1048576, 200); echo 'accepted ', Core\Bytes::length($csv), " bytes\n";} catch (ParseError $refused) { echo "the upload was refused\n";}
// The same call with a limit of 5 bytes of result per byte sent. This upload// packs much tighter than that, so it is refused.try { Core\Compress::decompress($upload, Core\Compress::Codec::Gzip, 1048576, 5); echo "the second limit did not hold\n";} catch (ParseError $tooTight) { echo "refused: it unpacks to more than 5 times its size\n";}accepted 3600 bytesrefused: it unpacks to more than 5 times its size