Skip to content

Commit 550563e

Browse files
committed
Add constants to handle options and add test case for options
1 parent a4dfd2b commit 550563e

4 files changed

Lines changed: 96 additions & 19 deletions

File tree

README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,22 @@ LZ4 compression to frame.
130130
* _max\_block\_size_
131131

132132
Maximum uncompressed size of each block.
133+
Pass any of the following values:
133134

134-
* 4 = 64 KB
135-
* 5 = 256 KB
136-
* 6 = 1 MB
137-
* 7 = 4 MB
138-
* any other value: 64 KB
135+
* _LZ4\_BLOCK\_SIZE\_64KB_
136+
* _LZ4\_BLOCK\_SIZE\_256KB_
137+
* _LZ4\_BLOCK\_SIZE\_1MB_
138+
* _LZ4\_BLOCK\_SIZE\_4MB_
139+
140+
Any other value will be treated as _LZ4\_BLOCK\_SIZE\_64KB_.
139141

140142
* _checksums_
141143

142144
Enable/disable frame level and block level checksums.
143-
The value is a bitmask using this bit interpretation:
145+
Pass a bitwise combination of the following constants:
144146

145-
* bit 0: frame level checksum
146-
* bit 1: block level checksum
147-
148-
Which leads to the following:
149-
150-
* 0 = no checksums
151-
* 1 = frame level checksum
152-
* 2 = block level checksum
153-
* 3 = both checksums
147+
* _LZ4\_CHECKSUM\_FRAME_: frame level checksum
148+
* _LZ4\_CHECKSUM\_BLOCK_: block level checksum
154149

155150
#### Return Values
156151

lz4.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
#define PHP_LZ4_CLEVEL_MIN 3
6868
#endif
6969

70+
#define PHP_LZ4_CHECKSUM_FRAME (1<<0)
71+
#define PHP_LZ4_CHECKSUM_BLOCK (1<<1)
72+
7073
static ZEND_FUNCTION(lz4_compress);
7174
static ZEND_FUNCTION(lz4_uncompress);
7275
static ZEND_FUNCTION(lz4_compress_frame);
@@ -115,6 +118,12 @@ static PHP_MINIT_FUNCTION(lz4)
115118
REGISTER_LONG_CONSTANT("LZ4_CLEVEL_MAX", PHP_LZ4_CLEVEL_MAX, CONST_CS | CONST_PERSISTENT);
116119
REGISTER_LONG_CONSTANT("LZ4_VERSION_NUMBER", LZ4_versionNumber(), CONST_CS | CONST_PERSISTENT);
117120
REGISTER_STRING_CONSTANT("LZ4_VERSION_TEXT", (char *)LZ4_versionString(), CONST_CS | CONST_PERSISTENT);
121+
REGISTER_LONG_CONSTANT("LZ4_CHECKSUM_FRAME", PHP_LZ4_CHECKSUM_FRAME, CONST_CS | CONST_PERSISTENT);
122+
REGISTER_LONG_CONSTANT("LZ4_CHECKSUM_BLOCK", PHP_LZ4_CHECKSUM_BLOCK, CONST_CS | CONST_PERSISTENT);
123+
REGISTER_LONG_CONSTANT("LZ4_BLOCK_SIZE_64KB", LZ4F_max64KB, CONST_CS | CONST_PERSISTENT);
124+
REGISTER_LONG_CONSTANT("LZ4_BLOCK_SIZE_256KB", LZ4F_max256KB, CONST_CS | CONST_PERSISTENT);
125+
REGISTER_LONG_CONSTANT("LZ4_BLOCK_SIZE_1MB", LZ4F_max1MB, CONST_CS | CONST_PERSISTENT);
126+
REGISTER_LONG_CONSTANT("LZ4_BLOCK_SIZE_4MB", LZ4F_max4MB, CONST_CS | CONST_PERSISTENT);
118127

119128
#if PHP_MAJOR_VERSION >= 7 && defined(HAVE_APCU_SUPPORT)
120129
apc_register_serializer("lz4",
@@ -294,8 +303,8 @@ static int php_lz4_compress_frame(char* in, const int in_len,
294303
}
295304
preferences.frameInfo.blockSizeID = max_block_size;
296305
preferences.frameInfo.contentSize = in_len;
297-
preferences.frameInfo.contentChecksumFlag = checksums & 0x01;
298-
preferences.frameInfo.blockChecksumFlag = (checksums & 0x02) >> 1;
306+
preferences.frameInfo.contentChecksumFlag = (checksums & PHP_LZ4_CHECKSUM_FRAME) > 0;
307+
preferences.frameInfo.blockChecksumFlag = (checksums & PHP_LZ4_CHECKSUM_BLOCK) > 0;
299308
preferences.compressionLevel = level;
300309

301310
var_len = LZ4F_compressFrameBound(in_len, &preferences);

lz4.stub.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,51 @@
2626
*/
2727
const LZ4_VERSION_NUMBER = UNKNOWN;
2828

29+
/**
30+
* @var int
31+
* @cvalue LZ4_CHECKSUM_FRAME
32+
*/
33+
const LZ4_CHECKSUM_FRAME = UNKNOWN;
34+
35+
/**
36+
* @var int
37+
* @cvalue LZ4_CHECKSUM_BLOCK
38+
*/
39+
const LZ4_CHECKSUM_BLOCK = UNKNOWN;
40+
41+
/**
42+
* @var int
43+
* @cvalue LZ4_BLOCK_SIZE_64KB
44+
*/
45+
const LZ4_BLOCK_SIZE_64KB = UNKNOWN;
46+
47+
/**
48+
* @var int
49+
* @cvalue LZ4_BLOCK_SIZE_256KB
50+
*/
51+
const LZ4_BLOCK_SIZE_256KB = UNKNOWN;
52+
53+
/**
54+
* @var int
55+
* @cvalue LZ4_BLOCK_SIZE_1MB
56+
*/
57+
const LZ4_BLOCK_SIZE_1MB = UNKNOWN;
58+
59+
/**
60+
* @var int
61+
* @cvalue LZ4_BLOCK_SIZE_4MB
62+
*/
63+
const LZ4_BLOCK_SIZE_4MB = UNKNOWN;
64+
2965

3066
function lz4_compress(string $data, int $level = 0, string $extra = NULL): string|false {}
3167

3268

3369
function lz4_uncompress(string $data, int $maxsize = -1, int $offset = -1): string|false {}
3470

3571
/**
36-
* @param int $max_block_size 4: 64KB, 5: 256KB, 6: 1MB, 7: 4MB, all other values: 64KB
37-
* @param int $checksums 0: none, 1: frame content, 2: each block, 3: frame content + each block
72+
* @param int $max_block_size any of the LZ4_BLOCK_SIZE_* constants
73+
* @param int $checksums bitmask of the LZ4_CHECKSUM_* constants
3874
*/
3975
function lz4_compress_frame(string $data, int $level = 0, int $max_block_size = 0, int $checksums = 0): string|false {}
4076

tests/frame_002.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
Test lz4_compress_frame() function : with all parameters
3+
--SKIPIF--
4+
--FILE--
5+
<?php
6+
if (!extension_loaded('lz4')) {
7+
dl('lz4.' . PHP_SHLIB_SUFFIX);
8+
}
9+
10+
include(dirname(__FILE__) . '/data.inc');
11+
12+
echo "*** Testing lz4_compress_frame() : with all parameters ***\n";
13+
14+
// Initialise all required variables
15+
16+
$smallstring = "A small string to compress\n";
17+
18+
// Compressing a big string
19+
echo "-- Compression --\n";
20+
$output = lz4_compress_frame($data, 6, LZ4_BLOCK_SIZE_256KB, LZ4_CHECKSUM_FRAME | LZ4_CHECKSUM_BLOCK);
21+
var_dump(strcmp(lz4_uncompress_frame($output), $data));
22+
23+
// Compressing a smaller string
24+
echo "-- Compression --\n";
25+
$output = lz4_compress_frame($smallstring, 6, LZ4_BLOCK_SIZE_256KB, LZ4_CHECKSUM_FRAME | LZ4_CHECKSUM_BLOCK);
26+
var_dump(bin2hex($output));
27+
var_dump(strcmp(lz4_uncompress_frame($output), $smallstring));
28+
?>
29+
===Done===
30+
--EXPECT--
31+
*** Testing lz4_compress_frame() : with all parameters ***
32+
-- Compression --
33+
int(0)
34+
-- Compression --
35+
string(116) "04224d187c401b00000000000000981b0000804120736d616c6c20737472696e6720746f20636f6d70726573730a2f4da318000000002f4da318"
36+
int(0)
37+
===Done===

0 commit comments

Comments
 (0)