Skip to content

Commit d514428

Browse files
authored
Merge pull request #178 from jackprice/jackprice/msgpack-f32
Allow forcing floats to be encoded as F32
2 parents ac2add8 + d33a7fb commit d514428

File tree

9 files changed

+96
-1
lines changed

9 files changed

+96
-1
lines changed

msgpack.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ STD_PHP_INI_BOOLEAN(
5555
STD_PHP_INI_BOOLEAN(
5656
"msgpack.use_str8_serialization", "1", PHP_INI_ALL, OnUpdateBool,
5757
use_str8_serialization, zend_msgpack_globals, msgpack_globals)
58+
STD_PHP_INI_BOOLEAN(
59+
"msgpack.force_f32", "0", PHP_INI_ALL, OnUpdateBool,
60+
force_f32, zend_msgpack_globals, msgpack_globals)
5861
PHP_INI_END()
5962

6063
#if HAVE_PHP_SESSION
@@ -115,6 +118,8 @@ static ZEND_MINIT_FUNCTION(msgpack) /* {{{ */ {
115118
MSGPACK_CLASS_OPT_PHPONLY, CONST_CS | CONST_PERSISTENT);
116119
REGISTER_LONG_CONSTANT("MESSAGEPACK_OPT_ASSOC",
117120
MSGPACK_CLASS_OPT_ASSOC, CONST_CS | CONST_PERSISTENT);
121+
REGISTER_LONG_CONSTANT("MESSAGEPACK_OPT_FORCE_F32",
122+
MSGPACK_CLASS_OPT_FORCE_F32, CONST_CS | CONST_PERSISTENT);
118123

119124
return SUCCESS;
120125
}

msgpack_class.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ typedef struct {
1111
long php_only;
1212
zend_bool assoc;
1313
zend_object object;
14+
zend_bool force_f32;
1415
} php_msgpack_base_t;
1516

1617
typedef struct {
@@ -178,6 +179,7 @@ static void php_msgpack_unpacker_free(zend_object *object) /* {{{ */ {
178179
static ZEND_METHOD(msgpack, __construct) /* {{{ */ {
179180
zend_bool php_only = MSGPACK_G(php_only);
180181
zend_bool assoc = MSGPACK_G(assoc);
182+
zend_bool force_f32 = MSGPACK_G(force_f32);
181183
php_msgpack_base_t *base = Z_MSGPACK_BASE_P(getThis());
182184

183185
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &php_only) == FAILURE) {
@@ -186,6 +188,7 @@ static ZEND_METHOD(msgpack, __construct) /* {{{ */ {
186188

187189
base->php_only = php_only;
188190
base->assoc = assoc;
191+
base->force_f32 = force_f32;
189192
}
190193
/* }}} */
191194

@@ -205,6 +208,9 @@ static ZEND_METHOD(msgpack, setOption) /* {{{ */ {
205208
case MSGPACK_CLASS_OPT_ASSOC:
206209
base->assoc = i_zend_is_true(value);
207210
break;
211+
case MSGPACK_CLASS_OPT_FORCE_F32:
212+
base->force_f32 = i_zend_is_true(value);
213+
break;
208214
default:
209215
MSGPACK_WARNING("[msgpack] (MessagePack::setOption) "
210216
"error setting msgpack option");
@@ -221,6 +227,7 @@ static ZEND_METHOD(msgpack, pack) /* {{{ */ {
221227
smart_str buf = {0};
222228
int php_only = MSGPACK_G(php_only);
223229
zend_bool assoc = MSGPACK_G(assoc);
230+
zend_bool force_f32 = MSGPACK_G(force_f32);
224231
php_msgpack_base_t *base = Z_MSGPACK_BASE_P(getThis());
225232

226233
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &parameter) == FAILURE) {
@@ -229,11 +236,13 @@ static ZEND_METHOD(msgpack, pack) /* {{{ */ {
229236

230237
MSGPACK_G(php_only) = base->php_only;
231238
MSGPACK_G(assoc) = base->assoc;
239+
MSGPACK_G(force_f32) = base->force_f32;
232240

233241
php_msgpack_serialize(&buf, parameter);
234242

235243
MSGPACK_G(php_only) = php_only;
236244
MSGPACK_G(assoc) = assoc;
245+
MSGPACK_G(force_f32) = force_f32;
237246
if (buf.s) {
238247
smart_str_0(&buf);
239248
ZVAL_STR(return_value, buf.s);
@@ -512,6 +521,7 @@ void msgpack_init_class() /* {{{ */ {
512521

513522
zend_declare_class_constant_long(msgpack_ce, ZEND_STRS("OPT_PHPONLY") - 1, MSGPACK_CLASS_OPT_PHPONLY);
514523
zend_declare_class_constant_long(msgpack_ce, ZEND_STRS("OPT_ASSOC") - 1, MSGPACK_CLASS_OPT_ASSOC);
524+
zend_declare_class_constant_long(msgpack_ce, ZEND_STRS("OPT_FORCE_F32") - 1, MSGPACK_CLASS_OPT_FORCE_F32);
515525

516526
/* unpacker */
517527
INIT_CLASS_ENTRY(ce, "MessagePackUnpacker", msgpack_unpacker_methods);

msgpack_class.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#define MSGPACK_CLASS_OPT_PHPONLY -1001
66
#define MSGPACK_CLASS_OPT_ASSOC -1002
7+
#define MSGPACK_CLASS_OPT_FORCE_F32 -1003
78

89
void msgpack_init_class();
910

msgpack_pack.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,11 @@ void msgpack_serialize_zval(smart_str *buf, zval *val, HashTable *var_hash) /* {
589589
msgpack_pack_long(buf, zval_get_long(val_noref));
590590
break;
591591
case IS_DOUBLE:
592-
msgpack_pack_double(buf, Z_DVAL_P(val_noref));
592+
if (MSGPACK_G(force_f32)) {
593+
msgpack_pack_float(buf, (float) Z_DVAL_P(val_noref));
594+
} else {
595+
msgpack_pack_double(buf, Z_DVAL_P(val_noref));
596+
}
593597
break;
594598
case IS_STRING:
595599
msgpack_serialize_string(buf, Z_STRVAL_P(val_noref), Z_STRLEN_P(val_noref));

php_msgpack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ZEND_BEGIN_MODULE_GLOBALS(msgpack)
2626
zend_bool assoc;
2727
zend_bool illegal_key_insert;
2828
zend_bool use_str8_serialization;
29+
zend_bool force_f32;
2930
struct {
3031
void *var_hash;
3132
unsigned level;

tests/029.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ header Version => %s
5151
Directive => Local Value => Master Value
5252
msgpack.assoc => %s => %s
5353
msgpack.error_display => %s => %s
54+
msgpack.force_f32 => Off => Off
5455
msgpack.illegal_key_insert => %s => %s
5556
msgpack.php_only => %s => %s
5657
msgpack.use_str8_serialization => %s => %s

tests/141.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Check for f32 serialisation
3+
--SKIPIF--
4+
--FILE--
5+
<?php
6+
if(!extension_loaded('msgpack')) {
7+
dl('msgpack.' . PHP_SHLIB_SUFFIX);
8+
}
9+
10+
function test($type, $variable) {
11+
$packer = new \MessagePack(false);
12+
13+
$packer->setOption(\MessagePack::OPT_FORCE_F32, true);
14+
15+
$serialized = $packer->pack($variable);
16+
17+
echo $type, PHP_EOL;
18+
echo bin2hex($serialized), PHP_EOL;
19+
}
20+
21+
test('double: 123.456', 123.456);
22+
?>
23+
--EXPECT--
24+
double: 123.456
25+
ca42f6e979

tests/142.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Check for f32 serialisation
3+
--SKIPIF--
4+
--INI--
5+
msgpack.force_f32 = 1
6+
--FILE--
7+
<?php
8+
if(!extension_loaded('msgpack')) {
9+
dl('msgpack.' . PHP_SHLIB_SUFFIX);
10+
}
11+
12+
function test($type, $variable) {
13+
$serialized = msgpack_pack($variable);
14+
15+
echo $type, PHP_EOL;
16+
echo bin2hex($serialized), PHP_EOL;
17+
}
18+
19+
test('double: 123.456', 123.456);
20+
?>
21+
--EXPECT--
22+
double: 123.456
23+
ca42f6e979

tests/143.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Check for f32 serialisation
3+
--SKIPIF--
4+
--INI--
5+
msgpack.force_f32 = 1
6+
--FILE--
7+
<?php
8+
if(!extension_loaded('msgpack')) {
9+
dl('msgpack.' . PHP_SHLIB_SUFFIX);
10+
}
11+
12+
function test($type, $variable) {
13+
$packer = new \MessagePack(false);
14+
15+
$serialized = $packer->pack($variable);
16+
17+
echo $type, PHP_EOL;
18+
echo bin2hex($serialized), PHP_EOL;
19+
}
20+
21+
test('double: 123.456', 123.456);
22+
?>
23+
--EXPECT--
24+
double: 123.456
25+
ca42f6e979

0 commit comments

Comments
 (0)