Skip to content

Commit ac29067

Browse files
committed
GH Issue #49, implement the 'compatibility mode' mentioned in msgpack implementation guidelines. When msgpack.use_str8_serialization is set to 0 str serialization will never serialize a string using str8
1 parent 5b8fac8 commit ac29067

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

msgpack.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ STD_PHP_INI_BOOLEAN(
4545
STD_PHP_INI_BOOLEAN(
4646
"msgpack.illegal_key_insert", "0", PHP_INI_ALL, OnUpdateBool,
4747
illegal_key_insert, zend_msgpack_globals, msgpack_globals)
48+
STD_PHP_INI_BOOLEAN(
49+
"msgpack.use_str8_serialization", "1", PHP_INI_ALL, OnUpdateBool,
50+
use_str8_serialization, zend_msgpack_globals, msgpack_globals)
4851
PHP_INI_END()
4952

5053
#if HAVE_PHP_SESSION
@@ -70,6 +73,7 @@ static void msgpack_init_globals(zend_msgpack_globals *msgpack_globals) /* {{{ *
7073
msgpack_globals->php_only = 1;
7174

7275
msgpack_globals->illegal_key_insert = 0;
76+
msgpack_globals->use_str8_serialization = 1;
7377
msgpack_globals->serialize.var_hash = NULL;
7478
msgpack_globals->serialize.level = 0;
7579
}

msgpack/pack_template.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ msgpack_pack_inline_func(_raw)(msgpack_pack_user x, size_t l)
730730
if(l < 32) {
731731
unsigned char d = 0xa0 | l;
732732
msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
733-
} else if (l < 256) {
733+
} else if (l < 256 && MSGPACK_G(use_str8_serialization)) {
734734
unsigned char buf[2];
735735
buf[0] = 0xd9; buf[1] = (uint8_t)l;
736736
msgpack_pack_append_buffer(x, buf, 2);

php_msgpack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ZEND_BEGIN_MODULE_GLOBALS(msgpack)
2424
zend_bool error_display;
2525
zend_bool php_only;
2626
zend_bool illegal_key_insert;
27+
zend_bool use_str8_serialization;
2728
struct {
2829
void *var_hash;
2930
unsigned level;

tests/029.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (!extension_loaded("session")) {
88
}
99
?>
1010
--FILE--
11-
<?php
11+
<?php
1212
ob_start();
1313
phpinfo(INFO_MODULES);
1414
$str = ob_get_clean();
@@ -51,3 +51,4 @@ Directive => Local Value => Master Value
5151
msgpack.error_display => On => On
5252
msgpack.illegal_key_insert => Off => Off
5353
msgpack.php_only => On => On
54+
msgpack.use_str8_serialization => On => On

tests/137.phpt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,28 @@ unpack/pack str8
77
}
88
--FILE--
99
<?php
10-
function test() {
11-
if(!extension_loaded('msgpack'))
12-
{
13-
dl('msgpack.' . PHP_SHLIB_SUFFIX);
14-
}
15-
16-
$str = "Simple test for short string - type str8";
17-
$str8 = chr(0xD9) . chr(strlen($str)) . $str;
18-
echo msgpack_unpack($str8) . "\n";
19-
$data = msgpack_pack($str);
20-
echo ($data[0] == chr(0xD9) && $data[1] == chr(strlen($str)) ? "OK" : "FAILED"), PHP_EOL;
10+
if(!extension_loaded('msgpack')) {
11+
dl('msgpack.' . PHP_SHLIB_SUFFIX);
2112
}
2213

23-
test();
14+
$str = "Simple test for short string - type str8";
15+
$str8 = chr(0xD9) . chr(strlen($str)) . $str;
16+
echo msgpack_unpack($str8) . "\n";
17+
18+
//assert str8 serialization works, and default for use use_str8_serialization is 1
19+
$data = msgpack_pack($str);
20+
var_dump(bin2hex($data));
21+
echo ($data[0] == chr(0xD9) && $data[1] == chr(strlen($str)) ? "OK" : "FAILED"), PHP_EOL;
22+
23+
ini_set('msgpack.use_str8_serialization', 0);
24+
$data = msgpack_pack($str);
25+
var_dump(bin2hex($data));
26+
echo ($data[0] == chr(0xDA) && $data[2] == chr(strlen($str)) ? "OK" : "FAILED"), PHP_EOL;
27+
2428
?>
2529
--EXPECTF--
2630
Simple test for short string - type str8
31+
string(84) "d92853696d706c65207465737420666f722073686f727420737472696e67202d20747970652073747238"
32+
OK
33+
string(86) "da002853696d706c65207465737420666f722073686f727420737472696e67202d20747970652073747238"
2734
OK

0 commit comments

Comments
 (0)