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

Add support deserializing the bin format family #53

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 7 additions & 5 deletions msgpack/unpack_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ typedef enum {
//CS_ = 0x02, // false
//CS_ = 0x03, // true

//CS_ = 0x04,
//CS_ = 0x05,
//CS_ = 0x06,
//CS_ = 0x07,
CS_BIN_8 = 0x04,
CS_BIN_16 = 0x05,
CS_BIN_32 = 0x06,

//CS_ = 0x07,
//CS_ = 0x08,
//CS_ = 0x09,

CS_FLOAT = 0x0a,
CS_DOUBLE = 0x0b,
CS_UINT_8 = 0x0c,
Expand All @@ -65,7 +66,7 @@ typedef enum {
//CS_BIG_INT_32 = 0x17,
//CS_BIG_FLOAT_16 = 0x18,
//CS_BIG_FLOAT_32 = 0x19,
CS_RAW_8 = 0x19,
CS_RAW_8 = 0x19,
CS_RAW_16 = 0x1a,
CS_RAW_32 = 0x1b,
CS_ARRAY_16 = 0x1c,
Expand All @@ -75,6 +76,7 @@ typedef enum {

//ACS_BIG_INT_VALUE,
//ACS_BIG_FLOAT_VALUE,
ACS_BIN_VALUE,
ACS_RAW_VALUE,
} msgpack_unpack_state;

Expand Down
19 changes: 16 additions & 3 deletions msgpack/unpack_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,12 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
push_simple_value(_false);
case 0xc3: // true
push_simple_value(_true);
//case 0xc4:
//case 0xc5:
//case 0xc6:
case 0xc4: // bin 8
again_fixed_trail(NEXT_CS(p), 1);
case 0xc5: // bin 16
again_fixed_trail(NEXT_CS(p), 2);
case 0xc6: // bin 32
again_fixed_trail(NEXT_CS(p), 4);
//case 0xc7:
//case 0xc8:
//case 0xc9:
Expand Down Expand Up @@ -291,6 +294,16 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
case CS_INT_64:
push_fixed_value(_int64, _msgpack_load64(int64_t,n));

case CS_BIN_8:
again_fixed_trail_if_zero(ACS_BIN_VALUE, *(uint8_t*)n, _bin_zero);
case CS_BIN_16:
again_fixed_trail_if_zero(ACS_BIN_VALUE, _msgpack_load16(uint16_t,n), _bin_zero);
case CS_BIN_32:
again_fixed_trail_if_zero(ACS_BIN_VALUE, _msgpack_load32(uint32_t,n), _bin_zero);
case ACS_BIN_VALUE:
_bin_zero:
push_variable_value(_bin, data, n, trail);

//case CS_
//case CS_
//case CS_BIG_INT_16:
Expand Down
12 changes: 12 additions & 0 deletions msgpack_unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,18 @@ int msgpack_unserialize_raw(
return 0;
}

int msgpack_unserialize_bin(
msgpack_unserialize_data *unpack, const char* base,
const char* data, unsigned int len, zval **obj)
{
MSGPACK_UNSERIALIZE_ALLOC_STACK(unpack);

ZVAL_STRINGL(*obj, (char *)data, len, 1);

return 0;
}


int msgpack_unserialize_array(
msgpack_unserialize_data *unpack, unsigned int count, zval **obj)
{
Expand Down
5 changes: 5 additions & 0 deletions msgpack_unpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ int msgpack_unserialize_false(msgpack_unserialize_data *unpack, zval **obj);
int msgpack_unserialize_raw(
msgpack_unserialize_data *unpack, const char* base, const char* data,
unsigned int len, zval **obj);
int msgpack_unserialize_bin(
msgpack_unserialize_data *unpack, const char* base, const char* data,
unsigned int len, zval **obj);
int msgpack_unserialize_array(
msgpack_unserialize_data *unpack, unsigned int count, zval **obj);
int msgpack_unserialize_array_item(
Expand Down Expand Up @@ -120,6 +123,8 @@ static inline msgpack_unpack_object template_callback_root(unpack_user* user)
msgpack_unserialize_false(user, obj)
#define template_callback_raw(user, base, data, len, obj) \
msgpack_unserialize_raw(user, base, data, len, obj)
#define template_callback_bin(user, base, data, len, obj) \
msgpack_unserialize_bin(user, base, data, len, obj)
#define template_callback_array(user, count, obj) \
msgpack_unserialize_array(user, count, obj)
#define template_callback_array_item(user, container, obj) \
Expand Down
36 changes: 36 additions & 0 deletions tests/138.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
unpack bin format family
--SKIPIF--
<?php
if (version_compare(PHP_VERSION, '5.2.0') < 0) {
echo "skip tests in PHP 5.2 or newer";
}
--FILE--
<?php
if(!extension_loaded('msgpack')) {
dl('msgpack.' . PHP_SHLIB_SUFFIX);
}

$bin8_serialized = pack('H*', 'c40962696e382074657374');
$bin8_unserialized = msgpack_unpack($bin8_serialized);
var_dump($bin8_unserialized);
echo ($bin8_unserialized == "bin8 test") ? "OK" : "FAILED", PHP_EOL;

$bin16_serialized = pack('H*', 'c5000a62696e31362074657374');
$bin16_unserialized = msgpack_unpack($bin16_serialized);
var_dump($bin16_unserialized);
echo ($bin16_unserialized == "bin16 test") ? "OK" : "FAILED", PHP_EOL;


$bin32_serialized = pack('H*', 'c60000000a62696e33322074657374');
$bin32_unserialized = msgpack_unpack($bin32_serialized);
var_dump($bin32_unserialized);
echo ($bin32_unserialized == "bin32 test") ? "OK" : "FAILED", PHP_EOL;
?>
--EXPECTF--
string(9) "bin8 test"
OK
string(10) "bin16 test"
OK
string(10) "bin32 test"
OK