-
Notifications
You must be signed in to change notification settings - Fork 30
Add incremental zstd_(un)compress_init() and zstd_(un)compress_add() #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ef61a70
901c587
ce0145f
473b548
cef80f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update return type for - resource **zstd_uncompress_init** ( void )
- Initialize an incremental uncompress context
+ ZstdContext **zstd_uncompress_init** ( void )
+ Initialize an incremental uncompress context 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
### 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 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Align namespace alias default parameter. - function compress_init ( [ $level = 3 ] )
+ function compress_init ( [ $level = ZSTD_COMPRESS_LEVEL_DEFAULT ] ) 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
`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 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
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=== |
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=== |
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=== |
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=== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update return type and wording for
zstd_compress_init
.The docs still say
resource **zstd_compress_init**
, but the implementation now returns aZstdContext
object—not a resource. Also consider using the noun “compression” instead of the verb “compress.”Apply this diff:
📝 Committable suggestion