Skip to content

Commit

Permalink
GH Issue #49, implement the 'compatibility mode' mentioned in msgpack…
Browse files Browse the repository at this point in the history
… implementation guidelines. When msgpack.use_str8_serialization is set to 0 str serialization will never serialize a string using str8
  • Loading branch information
Sean-Der committed Jul 2, 2015
1 parent b922c27 commit 66f1e04
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
4 changes: 4 additions & 0 deletions msgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ STD_PHP_INI_BOOLEAN(
STD_PHP_INI_BOOLEAN(
"msgpack.illegal_key_insert", "0", PHP_INI_ALL, OnUpdateBool,
illegal_key_insert, zend_msgpack_globals, msgpack_globals)
STD_PHP_INI_BOOLEAN(
"msgpack.use_str8_serialization", "1", PHP_INI_ALL, OnUpdateBool,
use_str8_serialization, zend_msgpack_globals, msgpack_globals)
PHP_INI_END()

#if HAVE_PHP_SESSION
Expand Down Expand Up @@ -77,6 +80,7 @@ static void msgpack_init_globals(zend_msgpack_globals *msgpack_globals)
msgpack_globals->illegal_key_insert = 0;
msgpack_globals->serialize.var_hash = NULL;
msgpack_globals->serialize.level = 0;
msgpack_globals->use_str8_serialization = 1;
}

static ZEND_MINIT_FUNCTION(msgpack)
Expand Down
2 changes: 1 addition & 1 deletion msgpack/pack_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ msgpack_pack_inline_func(_raw)(msgpack_pack_user x, size_t l)
if(l < 32) {
unsigned char d = 0xa0 | l;
msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
} else if (l < 256) {
} else if (l < 256 && MSGPACK_G(use_str8_serialization)) {
unsigned char buf[2];
buf[0] = 0xd9; buf[1] = (uint8_t)l;
msgpack_pack_append_buffer(x, buf, 2);
Expand Down
1 change: 1 addition & 0 deletions php_msgpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ZEND_BEGIN_MODULE_GLOBALS(msgpack)
zend_bool error_display;
zend_bool php_only;
zend_bool illegal_key_insert;
zend_bool use_str8_serialization;
struct {
void *var_hash;
unsigned level;
Expand Down
3 changes: 2 additions & 1 deletion tests/029.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (!extension_loaded("session")) {
}
?>
--FILE--
<?php
<?php
ob_start();
phpinfo(INFO_MODULES);
$str = ob_get_clean();
Expand Down Expand Up @@ -51,3 +51,4 @@ Directive => Local Value => Master Value
msgpack.error_display => On => On
msgpack.illegal_key_insert => Off => Off
msgpack.php_only => On => On
msgpack.use_str8_serialization => On => On
31 changes: 19 additions & 12 deletions tests/137.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@ unpack/pack str8
}
--FILE--
<?php
function test() {
if(!extension_loaded('msgpack'))
{
dl('msgpack.' . PHP_SHLIB_SUFFIX);
}

$str = "Simple test for short string - type str8";
$str8 = chr(0xD9) . chr(strlen($str)) . $str;
echo msgpack_unpack($str8) . "\n";
$data = msgpack_pack($str);
echo ($data[0] == chr(0xD9) && $data[1] == chr(strlen($str)) ? "OK" : "FAILED"), PHP_EOL;
if(!extension_loaded('msgpack')) {
dl('msgpack.' . PHP_SHLIB_SUFFIX);
}

test();
$str = "Simple test for short string - type str8";
$str8 = chr(0xD9) . chr(strlen($str)) . $str;
echo msgpack_unpack($str8) . "\n";

//assert str8 serialization works, and default for use use_str8_serialization is 1
$data = msgpack_pack($str);
var_dump(bin2hex($data));
echo ($data[0] == chr(0xD9) && $data[1] == chr(strlen($str)) ? "OK" : "FAILED"), PHP_EOL;

ini_set('msgpack.use_str8_serialization', 0);
$data = msgpack_pack($str);
var_dump(bin2hex($data));
echo ($data[0] == chr(0xDA) && $data[2] == chr(strlen($str)) ? "OK" : "FAILED"), PHP_EOL;

?>
--EXPECTF--
Simple test for short string - type str8
string(84) "d92853696d706c65207465737420666f722073686f727420737472696e67202d20747970652073747238"
OK
string(86) "da002853696d706c65207465737420666f722073686f727420737472696e67202d20747970652073747238"
OK

0 comments on commit 66f1e04

Please sign in to comment.