From 68bb93d4659f1665719af2a284b672d0d80adf01 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Fri, 18 Oct 2019 15:00:46 +0200 Subject: initial work --- preload-zstd.inc | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 preload-zstd.inc (limited to 'preload-zstd.inc') diff --git a/preload-zstd.inc b/preload-zstd.inc new file mode 100644 index 0000000..de8abd0 --- /dev/null +++ b/preload-zstd.inc @@ -0,0 +1,89 @@ +ZSTD_compressBound($len); + $ret['max_len'] = $max; + + $comp = str_repeat(' ', $max); + $clen = self::$ffi->ZSTD_compress($comp, $max, $src, $len, 6); + if (self::$ffi->ZSTD_isError($clen)) { + throw new \RuntimeException("Compression fails"); + } + $ret['out_len'] = $clen; + if (file_put_contents($out, substr($comp, 0, $clen)) !== $clen) { + throw new \RuntimeException("Save fails"); + } + + return $ret; + } + + public static function decompress($in, $out) { + self::init(); + + $ret = []; + $comp = file_get_contents($in); + if ($comp === false) { + throw new \RuntimeException("Read fails"); + } + $clen = strlen($comp); + $ret['in_len'] = $clen; + + $max = self::$ffi->ZSTD_decompressBound($comp, $clen); + $ret['max_len'] = $max; + + $unco = str_repeat(' ', $max); + $ulen = self::$ffi->ZSTD_decompress($unco, $max, $comp, $clen); + if (self::$ffi->ZSTD_isError($clen)) { + throw new \RuntimeException("Compression fails"); + } + $ret['out_len'] = $ulen; + if (file_put_contents($out, substr($unco, 0, $ulen)) !== $ulen) { + throw new \RuntimeException("Save fails"); + } + + return $ret; + } +} + -- cgit