diff options
author | Remi Collet <remi@remirepo.net> | 2019-10-23 13:29:12 +0200 |
---|---|---|
committer | Remi Collet <remi@remirepo.net> | 2019-10-23 13:29:12 +0200 |
commit | 261032f77820d0b620707ace75cfa82043558581 (patch) | |
tree | b94e3a12c0ed1af50793fe699eaa75fbbe87dc07 /preload-zstd.inc | |
parent | 2dfbea225f695ad6f66309b754e50b58eb6bde9f (diff) |
cleanup Zstd ex
Diffstat (limited to 'preload-zstd.inc')
-rw-r--r-- | preload-zstd.inc | 44 |
1 files changed, 8 insertions, 36 deletions
diff --git a/preload-zstd.inc b/preload-zstd.inc index f56c833..a42e5be 100644 --- a/preload-zstd.inc +++ b/preload-zstd.inc @@ -12,7 +12,7 @@ namespace Remi; class Zstd { static private $ffi = null; - public static function init() { + private static function init() { if (self::$ffi) { return; } @@ -22,70 +22,42 @@ class Zstd { echo "Using FFI::scope OK\n"; } catch (\FFI\Exception $e) { // Try direct load - if (PHP_SAPI === 'cli' || (int)ini_get("ffi.enable")) { - self::$ffi = \FFI::load(__DIR__ . '/preload-zstd.h'); - echo "Using FFI::load OK\n"; - } else { - throw $e; - } + self::$ffi = \FFI::load(__DIR__ . '/preload-zstd.h'); + echo "Using FFI::load OK\n"; } if (!self::$ffi) { throw new \RuntimeException("FFI parse fails"); } } - public static function compress($in, $out) { + public static function compress(string $src): string { self::init(); - $ret = []; - $src = file_get_contents($in); - if ($src === false) { - throw new \RuntimeException("Read fails"); - } $len = strlen($src); - $ret['in_len'] = $len; - $max = self::$ffi->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; + return substr($comp, 0, $clen); } - public static function decompress($in, $out) { + public static function decompress(string $comp): string { 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"); + throw new \RuntimeException("Decompression fails"); } - return $ret; + return substr($unco, 0, $ulen); } } |