Skip to content
Merged
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

Comment on lines +217 to +224
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update return type and wording for zstd_compress_init.
The docs still say resource **zstd_compress_init**, but the implementation now returns a ZstdContext object—not a resource. Also consider using the noun “compression” instead of the verb “compress.”

Apply this diff:

- resource **zstd_compress_init** ( [ int _$level_ = ZSTD_COMPRESS_LEVEL_DEFAULT ] )
- Initialize an incremental compress context
+ ZstdContext **zstd_compress_init** ( [ int _$level_ = ZSTD_COMPRESS_LEVEL_DEFAULT ] )
+ Initialize an incremental compression context
📝 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\_init — Initialize an incremental compress context
#### Description
resource **zstd\_compress\_init** ( [ int _$level_ = ZSTD_COMPRESS_LEVEL_DEFAULT ] )
Initialize an incremental compress context
### zstd\_compress\_init — Initialize an incremental compress context
#### Description
ZstdContext **zstd\_compress\_init** ( [ int _$level_ = ZSTD_COMPRESS_LEVEL_DEFAULT ] )
Initialize an incremental compression context

#### Parameters

* _level_

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

#### Return Values

Returns a zstd context instance on success, or FALSE on failure


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

#### Description

string **zstd\_compress\_add** ( ZstdContext _$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 instance on success, or FALSE on failure

Comment on lines +263 to +274
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update return type for zstd_uncompress_init.
Similar to zstd_compress_init, this function returns a ZstdContext object. Update the docs accordingly:

- resource **zstd_uncompress_init** ( void )
- Initialize an incremental uncompress context
+ ZstdContext **zstd_uncompress_init** ( void )
+ Initialize an incremental uncompress context
📝 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\_uncompress\_init — Initialize an incremental uncompress context
#### Description
resource **zstd\_uncompress\_init** ( void )
Initialize an incremental uncompress context
#### Return Values
Returns a zstd context instance on success, or FALSE on failure
### zstd\_uncompress\_init — Initialize an incremental uncompress context
#### Description
ZstdContext **zstd_uncompress_init** ( void )
Initialize an incremental uncompress context
#### Return Values
Returns a zstd context instance on success, or FALSE on failure


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

#### Description

string **zstd\_uncompress\_add** ( ZstdContext _$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

Align namespace alias default parameter.
The namespaced compress_init alias currently defaults to 3, but the core docs and implementation use ZSTD_COMPRESS_LEVEL_DEFAULT. For consistency, reference the constant here as well.

- 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 )
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 aliases.

## 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===
45 changes: 45 additions & 0 deletions tests/inc_comp.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--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)
object(ZstdContext)#%d (0) {
}
int(%d)
bool(true)
bool(true)
int(512)
object(ZstdContext)#%d (0) {
}
int(%d)
bool(true)
bool(true)
int(1024)
object(ZstdContext)#%d (0) {
}
int(%d)
bool(true)
bool(true)
===Done===
39 changes: 39 additions & 0 deletions tests/inc_decomp.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--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)
object(ZstdContext)#%d (0) {
}
bool(true)
int(512)
object(ZstdContext)#%d (0) {
}
bool(true)
int(1024)
object(ZstdContext)#%d (0) {
}
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