summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--preload-redis.inc6
-rw-r--r--preload-zstd.inc44
-rw-r--r--zstd.php26
3 files changed, 25 insertions, 51 deletions
diff --git a/preload-redis.inc b/preload-redis.inc
index 199b0fd..cc8f9dd 100644
--- a/preload-redis.inc
+++ b/preload-redis.inc
@@ -47,11 +47,7 @@ class Redis {
self::$ffi = \FFI::scope("_REMI_REDIS_");
} catch (\FFI\Exception $e) {
// Try direct load
- if (PHP_SAPI === 'cli' || (int)ini_get("ffi.enable")) {
- self::$ffi = \FFI::load(__DIR__ . '/preload-redis.h');
- } else {
- throw $e;
- }
+ self::$ffi = \FFI::load(__DIR__ . '/preload-redis.h');
}
if (!self::$ffi) {
throw new \RuntimeException("FFI parse fails");
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);
}
}
diff --git a/zstd.php b/zstd.php
index 7faebea..0ec22d7 100644
--- a/zstd.php
+++ b/zstd.php
@@ -1,4 +1,5 @@
-<?php
+<?php declare(strict_types=1);
+
/**
* ZSTD compressor using FFI and libzstd
* PoC, only for documentation purpose
@@ -22,15 +23,19 @@ if (PHP_SAPI == "cli" && !class_exists("\\Remi\\Zstd")) {
if (class_exists("\\Remi\\Zstd")) {
$t = microtime(true);
- $ret = \Remi\Zstd::compress(PHP_BINARY, "testffi.zstd");
- printf("\nSrc length = %d\n", $ret['in_len']);
- printf("ZSTD_compressBound = %d\n", $ret['max_len']);
- printf("ZSTD_compress = %d\n", $ret['out_len']);
+ $src = file_get_contents(PHP_BINARY);
+ $comp = \Remi\Zstd::compress($src);
+ printf("\nSrc length = %d\n", \strlen($src));
+ printf("ZSTD_compress = %d\n", \strlen($comp));
+ file_put_contents("testffi.zstd", $comp);
+
+ $comp = file_get_contents("testffi.zstd");
+ $out = \Remi\Zstd::decompress($comp);
+ printf("Src length = %d\n", \strlen($comp));
+ printf("ZSTD_decompress = %d\n", \strlen($out));
+ file_put_contents("testffi.orig", $out);
- $ret = \Remi\Zstd::decompress("testffi.zstd", "testffi.orig");
- printf("Src length = %d\n", $ret['in_len']);
- printf("ZSTD_decompressBound = %d\n", $ret['max_len']);
- printf("ZSTD_decompress = %d\n", $ret['out_len']);
+ printf("Check = %s\n", $src === $out ? "OK" : "KO");
$t = microtime(true) - $t;
printf("Using FFI extension = %.3f\"\n\n", $t);
@@ -51,12 +56,13 @@ if (extension_loaded("zstd")) {
file_put_contents("testzstd.zstd", $comp);
$comp = file_get_contents("testzstd.zstd");
+ printf("Src length = %d\n", \strlen($comp));
$unco = zstd_uncompress($comp);
$ulen = strlen($unco);
printf("ZSTD_decompress = %d\n", $ulen);
file_put_contents("testzstd.orig", $unco);
- var_dump($src === $unco);
+ printf("Check = %s\n", $src === $unco ? "OK" : "KO");
$t = microtime(true) - $t;
printf("Using ZSTD extension = %.3f\"\n\n", $t);
} else {