Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 95 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ LIBZSTD\_VERSION\_STRING | libzstd version string
* zstd\_uncompress — Zstandard decompression
* zstd\_compress\_dict — Zstandard compression using a digested dictionary
* zstd\_uncompress\_dict — Zstandard decompression using a digested dictionary
* zstd\_compress_\init — Initialize an incremental compress context
* zstd\_compress\_add — Incrementally compress data
* zstd\_uncompress_\init — Initialize an incremental uncompress context
* zstd\_uncompress\_add — Incrementally uncompress data


### zstd\_compress — Zstandard compression

Expand Down Expand Up @@ -209,6 +214,88 @@ Zstandard decompression using a digested dictionary.
Returns the decompressed data or FALSE if an error occurred.


### zstd\_compress\_init — Initialize an incremental compress context

#### Description

resource **zstd\_compress\_init** ( [ int _$level_ = ZSTD_COMPRESS_LEVEL_DEFAULT ] )

Initialize an incremental compress context

#### Parameters

* _level_

The higher the level, the slower the compression. (Defaults to `ZSTD_COMPRESS_LEVEL_DEFAULT`)

#### Return Values

Returns a zstd context resource (zstd.state) on success, or FALSE on failure


### zstd\_compress\_add — Incrementally compress data

#### Description

string **zstd\_compress\_add** ( resource _$context_, string _$data_ [, bool _$end_ = false ] )

Incrementally compress data

#### Parameters

* _context_

A context created with `zstd_compress_init()`.

* _data_

A chunk of data to compress.

* _end_

Set to true to terminate with the last chunk of data.

#### Return Values

Returns a chunk of compressed data, or FALSE on failure.


### zstd\_uncompress\_init — Initialize an incremental uncompress context

#### Description

resource **zstd\_uncompress\_init** ( void )

Initialize an incremental uncompress context

#### Return Values

Returns a zstd context resource (zstd.state) on success, or FALSE on failure


### zstd\_uncompress\_add — Incrementally uncompress data

#### Description

string **zstd\_uncompress\_add** ( resource _$context_, string _$data_ )

Incrementally uncompress data

#### Parameters

* _context_

A context created with `zstd_uncompress_init()`.

* _data_

A chunk of compressed data.

#### Return Values

Returns a chunk of uncompressed data, or FALSE on failure.


## Namespace

```
Expand All @@ -218,10 +305,16 @@ function compress( $data [, $level = 3 ] )
function uncompress( $data )
function compress_dict ( $data, $dict )
function uncompress_dict ( $data, $dict )
function compress_init ( [ $level = 3 ] )
function compress_add ( $context, $data [, $end = false ] )
function uncompress_init ()
function uncompress_add ( $context, $data )

Comment on lines +308 to +312
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use the constant for the default compression level in namespace docs.
The implementation uses ZSTD_COMPRESS_LEVEL_DEFAULT, so the default should match:

- function compress_init ( [ $level = 3 ] )
+ function compress_init ( [ $level = ZSTD_COMPRESS_LEVEL_DEFAULT ] )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function compress_init ( [ $level = 3 ] )
function compress_add ( $context, $data [, $end = false ] )
function uncompress_init ()
function uncompress_add ( $context, $data )
--- a/README.md
+++ b/README.md
@@ Lines 308-312
- function compress_init ( [ $level = 3 ] )
+ function compress_init ( [ $level = ZSTD_COMPRESS_LEVEL_DEFAULT ] )
function compress_add ( $context, $data [, $end = false ] )
function uncompress_init ()
function uncompress_add ( $context, $data )

```

`zstd_compress`, `zstd_uncompress`, `zstd_compress_dict` and
`zstd_uncompress_dict` function alias.
`zstd_compress`, `zstd_uncompress`, `zstd_compress_dict`,
`zstd_uncompress_dict`, `zstd_compress_init`, `zstd_compress_add`,
`zstd_uncompress_init` and `zstd_uncompress_add` function alias.
Comment on lines +315 to +317
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix alias list grammar and punctuation.
Change "function alias" to "function aliases." and add a terminal period:

- `zstd_uncompress_init` and `zstd_uncompress_add` function alias.
+ `zstd_uncompress_init` and `zstd_uncompress_add` function aliases.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
`zstd_compress`, `zstd_uncompress`, `zstd_compress_dict`,
`zstd_uncompress_dict`, `zstd_compress_init`, `zstd_compress_add`,
`zstd_uncompress_init` and `zstd_uncompress_add` function alias.
`zstd_compress`, `zstd_uncompress`, `zstd_compress_dict`,
`zstd_uncompress_dict`, `zstd_compress_init`, `zstd_compress_add`,
`zstd_uncompress_init` and `zstd_uncompress_add` function aliases.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~315-~315: Loose punctuation mark.
Context: ... $context, $data ) ``` zstd_compress, `zstd_uncompress`, `zstd_compress_dict`...

(UNLIKELY_OPENING_PUNCTUATION)


## Streams

Expand Down
27 changes: 27 additions & 0 deletions tests/inc.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Incremental compression and decompression
--FILE--
<?php

// compression
$resource = zstd_compress_init();
$compressed = '';
$compressed .= zstd_compress_add($resource, 'Hello, ', false);
$compressed .= zstd_compress_add($resource, 'World!', false);
$compressed .= zstd_compress_add($resource, '', true);

echo zstd_uncompress($compressed), PHP_EOL;

// uncompression
$resource = zstd_uncompress_init();
$uncompressed = '';
$uncompressed .= zstd_uncompress_add($resource, substr($compressed, 0, 5));
$uncompressed .= zstd_uncompress_add($resource, substr($compressed, 5));

echo $uncompressed, PHP_EOL;
?>
===Done===
--EXPECTF--
Hello, World!
Hello, World!
===Done===
42 changes: 42 additions & 0 deletions tests/inc_comp.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
Incremental compression
--FILE--
<?php
include(dirname(__FILE__) . '/data.inc');

foreach ([128, 512, 1024] as $size) {
var_dump($size);
$handle = zstd_compress_init();
var_dump($handle);

$pos= 0;
$compressed = '';
while ($pos < strlen($data)) {
$chunk = substr($data, $pos, $size);
$compressed .= zstd_compress_add($handle, $chunk, false);
$pos += strlen($chunk);
}
$compressed .= zstd_compress_add($handle, '', true);
var_dump(strlen($compressed), strlen($compressed) < strlen($data));

var_dump($data === zstd_uncompress($compressed));
}
?>
===Done===
--EXPECTF--
int(128)
resource(%d) of type (zstd.state)
int(%d)
bool(true)
bool(true)
int(512)
resource(%d) of type (zstd.state)
int(%d)
bool(true)
bool(true)
int(1024)
resource(%d) of type (zstd.state)
int(%d)
bool(true)
bool(true)
===Done===
36 changes: 36 additions & 0 deletions tests/inc_decomp.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Incremental decompression
--FILE--
<?php
include(dirname(__FILE__) . '/data.inc');

$compressed = zstd_compress($data);

foreach ([128, 512, 1024] as $size) {
var_dump($size);
$handle = zstd_uncompress_init();
var_dump($handle);

$pos= 0;
$uncompressed = '';
while ($pos < strlen($compressed)) {
$chunk = substr($compressed, $pos, $size);
$uncompressed .= zstd_uncompress_add($handle, $chunk);
$pos += strlen($chunk);
}

var_dump($data === $uncompressed);
}
?>
===Done===
--EXPECTF--
int(128)
resource(%d) of type (zstd.state)
bool(true)
int(512)
resource(%d) of type (zstd.state)
bool(true)
int(1024)
resource(%d) of type (zstd.state)
bool(true)
===Done===
27 changes: 27 additions & 0 deletions tests/inc_ns.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Incremental compression and decompression (namespaces)
--FILE--
<?php

// compression
$resource = \Zstd\compress_init();
$compressed = '';
$compressed .= \Zstd\compress_add($resource, 'Hello, ', false);
$compressed .= \Zstd\compress_add($resource, 'World!', false);
$compressed .= \Zstd\compress_add($resource, '', true);

echo \Zstd\uncompress($compressed), PHP_EOL;

// uncompression
$resource = \Zstd\uncompress_init();
$uncompressed = '';
$uncompressed .= \Zstd\uncompress_add($resource, substr($compressed, 0, 5));
$uncompressed .= \Zstd\uncompress_add($resource, substr($compressed, 5));

echo $uncompressed, PHP_EOL;
?>
===Done===
--EXPECTF--
Hello, World!
Hello, World!
===Done===
Loading