Skip to content

Commit 5e0ab06

Browse files
authored
PHPC-1988: base64 encode binary data when debugging (#1960)
2 parents bff1a60 + ca106e1 commit 5e0ab06

24 files changed

Lines changed: 99 additions & 74 deletions

src/BSON/Binary.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static bool phongo_binary_init_from_hash(phongo_binary_t* intern, HashTable* pro
7474
return false;
7575
}
7676

77-
static HashTable* phongo_binary_get_properties_hash(zend_object* object, bool is_temp)
77+
static HashTable* phongo_binary_get_properties_hash(zend_object* object, bool is_temp, bool is_debug)
7878
{
7979
phongo_binary_t* intern;
8080
HashTable* props;
@@ -90,7 +90,12 @@ static HashTable* phongo_binary_get_properties_hash(zend_object* object, bool is
9090
{
9191
zval data, type;
9292

93-
ZVAL_STRINGL(&data, intern->data, intern->data_len);
93+
if (is_debug) {
94+
ZVAL_NEW_STR(&data, php_base64_encode((unsigned char*) intern->data, intern->data_len));
95+
} else {
96+
ZVAL_STRINGL(&data, intern->data, intern->data_len);
97+
}
98+
9499
zend_hash_str_update(props, "data", sizeof("data") - 1, &data);
95100

96101
ZVAL_LONG(&type, intern->type);
@@ -197,7 +202,7 @@ static PHP_METHOD(MongoDB_BSON_Binary, __serialize)
197202
{
198203
PHONGO_PARSE_PARAMETERS_NONE();
199204

200-
RETURN_ARR(phongo_binary_get_properties_hash(Z_OBJ_P(getThis()), true));
205+
RETURN_ARR(phongo_binary_get_properties_hash(Z_OBJ_P(getThis()), true, false));
201206
}
202207

203208
static PHP_METHOD(MongoDB_BSON_Binary, __unserialize)
@@ -284,7 +289,7 @@ static int phongo_binary_compare_objects(zval* o1, zval* o2)
284289
static HashTable* phongo_binary_get_debug_info(zend_object* object, int* is_temp)
285290
{
286291
*is_temp = 1;
287-
HashTable* props = phongo_binary_get_properties_hash(object, true);
292+
HashTable* props = phongo_binary_get_properties_hash(object, true, true);
288293

289294
phongo_binary_t* intern = Z_OBJ_BINARY(object);
290295

@@ -317,7 +322,7 @@ static HashTable* phongo_binary_get_debug_info(zend_object* object, int* is_temp
317322

318323
static HashTable* phongo_binary_get_properties(zend_object* object)
319324
{
320-
return phongo_binary_get_properties_hash(object, false);
325+
return phongo_binary_get_properties_hash(object, false, false);
321326
}
322327

323328
void phongo_binary_init_ce(INIT_FUNC_ARGS)

tests/bson/bson-binary-clone-001.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,63 +39,63 @@ bool(true)
3939
bool(false)
4040
object(MongoDB\BSON\Binary)#%d (2) {
4141
["data"]=>
42-
string(16) "randomBinaryData"
42+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
4343
["type"]=>
4444
int(0)
4545
}
4646
bool(true)
4747
bool(false)
4848
object(MongoDB\BSON\Binary)#%d (2) {
4949
["data"]=>
50-
string(16) "randomBinaryData"
50+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
5151
["type"]=>
5252
int(1)
5353
}
5454
bool(true)
5555
bool(false)
5656
object(MongoDB\BSON\Binary)#%d (2) {
5757
["data"]=>
58-
string(16) "randomBinaryData"
58+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
5959
["type"]=>
6060
int(2)
6161
}
6262
bool(true)
6363
bool(false)
6464
object(MongoDB\BSON\Binary)#%d (2) {
6565
["data"]=>
66-
string(16) "randomBinaryData"
66+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
6767
["type"]=>
6868
int(3)
6969
}
7070
bool(true)
7171
bool(false)
7272
object(MongoDB\BSON\Binary)#%d (2) {
7373
["data"]=>
74-
string(16) "randomBinaryData"
74+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
7575
["type"]=>
7676
int(4)
7777
}
7878
bool(true)
7979
bool(false)
8080
object(MongoDB\BSON\Binary)#%d (2) {
8181
["data"]=>
82-
string(16) "randomBinaryData"
82+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
8383
["type"]=>
8484
int(5)
8585
}
8686
bool(true)
8787
bool(false)
8888
object(MongoDB\BSON\Binary)#%d (2) {
8989
["data"]=>
90-
string(16) "randomBinaryData"
90+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
9191
["type"]=>
9292
int(128)
9393
}
9494
bool(true)
9595
bool(false)
9696
object(MongoDB\BSON\Binary)#%d (2) {
9797
["data"]=>
98-
string(16) "randomBinaryData"
98+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
9999
["type"]=>
100100
int(133)
101101
}

tests/bson/bson-binary-clone-002.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,63 +39,63 @@ bool(true)
3939
bool(false)
4040
object(MongoDB\BSON\Binary)#%d (2) {
4141
["data"]=>
42-
string(16) "randomBinaryData"
42+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
4343
["type"]=>
4444
int(0)
4545
}
4646
bool(true)
4747
bool(false)
4848
object(MongoDB\BSON\Binary)#%d (2) {
4949
["data"]=>
50-
string(16) "randomBinaryData"
50+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
5151
["type"]=>
5252
int(1)
5353
}
5454
bool(true)
5555
bool(false)
5656
object(MongoDB\BSON\Binary)#%d (2) {
5757
["data"]=>
58-
string(16) "randomBinaryData"
58+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
5959
["type"]=>
6060
int(2)
6161
}
6262
bool(true)
6363
bool(false)
6464
object(MongoDB\BSON\Binary)#%d (2) {
6565
["data"]=>
66-
string(16) "randomBinaryData"
66+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
6767
["type"]=>
6868
int(3)
6969
}
7070
bool(true)
7171
bool(false)
7272
object(MongoDB\BSON\Binary)#%d (2) {
7373
["data"]=>
74-
string(16) "randomBinaryData"
74+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
7575
["type"]=>
7676
int(4)
7777
}
7878
bool(true)
7979
bool(false)
8080
object(MongoDB\BSON\Binary)#%d (2) {
8181
["data"]=>
82-
string(16) "randomBinaryData"
82+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
8383
["type"]=>
8484
int(5)
8585
}
8686
bool(true)
8787
bool(false)
8888
object(MongoDB\BSON\Binary)#%d (2) {
8989
["data"]=>
90-
string(16) "randomBinaryData"
90+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
9191
["type"]=>
9292
int(128)
9393
}
9494
bool(true)
9595
bool(false)
9696
object(MongoDB\BSON\Binary)#%d (2) {
9797
["data"]=>
98-
string(16) "randomBinaryData"
98+
string(24) "cmFuZG9tQmluYXJ5RGF0YQ=="
9999
["type"]=>
100100
int(133)
101101
}

tests/bson/bson-binary-fromVector-001.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var_dump(MongoDB\BSON\Binary::fromVector([1, 0, true, false], MongoDB\BSON\Vecto
1313
--EXPECTF--
1414
object(MongoDB\BSON\Binary)#%d (%d) {
1515
["data"]=>
16-
string(18) "%a"
16+
string(%d) "%a"
1717
["type"]=>
1818
int(9)
1919
["vector"]=>
@@ -32,7 +32,7 @@ object(MongoDB\BSON\Binary)#%d (%d) {
3232
}
3333
object(MongoDB\BSON\Binary)#%d (%d) {
3434
["data"]=>
35-
string(6) "%a"
35+
string(%d) "%a"
3636
["type"]=>
3737
int(9)
3838
["vector"]=>
@@ -51,7 +51,7 @@ object(MongoDB\BSON\Binary)#%d (%d) {
5151
}
5252
object(MongoDB\BSON\Binary)#%d (%d) {
5353
["data"]=>
54-
string(3) "%a"
54+
string(%d) "%a"
5555
["type"]=>
5656
int(9)
5757
["vector"]=>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
MongoDB\BSON\Binary debug output
3+
--FILE--
4+
<?php
5+
6+
$binary = new MongoDB\BSON\Binary('foobar', MongoDB\BSON\Binary::TYPE_GENERIC);
7+
8+
var_dump($binary);
9+
10+
?>
11+
===DONE===
12+
<?php exit(0); ?>
13+
--EXPECTF--
14+
object(MongoDB\BSON\Binary)#%d (%d) {
15+
["data"]=>
16+
string(8) "Zm9vYmFy"
17+
["type"]=>
18+
int(0)
19+
}
20+
===DONE===

tests/bson/bson-binary-jsonserialize-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object(stdClass)#%d (%d) {
2222
["foo"]=>
2323
object(MongoDB\BSON\Binary)#%d (%d) {
2424
["data"]=>
25-
string(13) "gargleblaster"
25+
string(%d) "%s"
2626
["type"]=>
2727
int(24)
2828
}

tests/bson/bson-binary-serialization-002.phpt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ foreach ($tests as $test) {
2727
--EXPECTF--
2828
object(MongoDB\BSON\Binary)#%d (%d) {
2929
["data"]=>
30-
string(6) "foobar"
30+
string(8) "Zm9vYmFy"
3131
["type"]=>
3232
int(0)
3333
}
3434
string(70) "O:19:"MongoDB\BSON\Binary":2:{s:4:"data";s:6:"foobar";s:4:"type";i:0;}"
3535
object(MongoDB\BSON\Binary)#%d (%d) {
3636
["data"]=>
37-
string(6) "foobar"
37+
string(8) "Zm9vYmFy"
3838
["type"]=>
3939
int(0)
4040
}
@@ -55,49 +55,49 @@ object(MongoDB\BSON\Binary)#%d (%d) {
5555

5656
object(MongoDB\BSON\Binary)#%d (%d) {
5757
["data"]=>
58-
string(4) "%sfoo"
58+
string(8) "AGZvbw=="
5959
["type"]=>
6060
int(0)
6161
}
6262
string(68) "O:19:"MongoDB\BSON\Binary":2:{s:4:"data";s:4:"%sfoo";s:4:"type";i:0;}"
6363
object(MongoDB\BSON\Binary)#%d (%d) {
6464
["data"]=>
65-
string(4) "%sfoo"
65+
string(8) "AGZvbw=="
6666
["type"]=>
6767
int(0)
6868
}
6969

7070
object(MongoDB\BSON\Binary)#%d (%d) {
7171
["data"]=>
72-
string(16) "%s"
72+
string(24) "Ej5FZ+ibEtOkVkJmVUQAAA=="
7373
["type"]=>
7474
int(4)
7575
}
7676
string(81) "O:19:"MongoDB\BSON\Binary":2:{s:4:"data";s:16:"%s";s:4:"type";i:4;}"
7777
object(MongoDB\BSON\Binary)#%d (%d) {
7878
["data"]=>
79-
string(16) "%s"
79+
string(24) "Ej5FZ+ibEtOkVkJmVUQAAA=="
8080
["type"]=>
8181
int(4)
8282
}
8383

8484
object(MongoDB\BSON\Binary)#%d (%d) {
8585
["data"]=>
86-
string(16) "%s"
86+
string(24) "OFj2IjCsPJFfMAxmQxLGPw=="
8787
["type"]=>
8888
int(5)
8989
}
9090
string(81) "O:19:"MongoDB\BSON\Binary":2:{s:4:"data";s:16:"%s";s:4:"type";i:5;}"
9191
object(MongoDB\BSON\Binary)#%d (%d) {
9292
["data"]=>
93-
string(16) "%s"
93+
string(24) "OFj2IjCsPJFfMAxmQxLGPw=="
9494
["type"]=>
9595
int(5)
9696
}
9797

9898
object(MongoDB\BSON\Binary)#%d (%d) {
9999
["data"]=>
100-
string(6) "%a"
100+
string(8) "AwABAgME"
101101
["type"]=>
102102
int(9)
103103
["vector"]=>
@@ -117,7 +117,7 @@ object(MongoDB\BSON\Binary)#%d (%d) {
117117
string(70) "O:19:"MongoDB\BSON\Binary":2:{s:4:"data";s:6:"%a";s:4:"type";i:9;}"
118118
object(MongoDB\BSON\Binary)#%d (%d) {
119119
["data"]=>
120-
string(6) "%a"
120+
string(8) "AwABAgME"
121121
["type"]=>
122122
int(9)
123123
["vector"]=>

tests/bson/bson-document-toPHP-001.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ object(stdClass)#%d (2) {
4747
["__pclass"]=>
4848
object(MongoDB\BSON\Binary)#%d (2) {
4949
["data"]=>
50-
string(18) "MyAbstractDocument"
50+
string(24) "TXlBYnN0cmFjdERvY3VtZW50"
5151
["type"]=>
5252
int(128)
5353
}
@@ -60,7 +60,7 @@ object(stdClass)#%d (2) {
6060
["__pclass"]=>
6161
object(MongoDB\BSON\Binary)#%d (2) {
6262
["data"]=>
63-
string(10) "MyDocument"
63+
string(16) "TXlEb2N1bWVudA=="
6464
["type"]=>
6565
int(128)
6666
}
@@ -73,7 +73,7 @@ object(stdClass)#%d (2) {
7373
["__pclass"]=>
7474
object(MongoDB\BSON\Binary)#%d (2) {
7575
["data"]=>
76-
string(27) "MongoDB\BSON\Unserializable"
76+
string(36) "TW9uZ29EQlxCU09OXFVuc2VyaWFsaXphYmxl"
7777
["type"]=>
7878
int(128)
7979
}
@@ -86,7 +86,7 @@ object(stdClass)#%d (2) {
8686
["__pclass"]=>
8787
object(MongoDB\BSON\Binary)#%d (2) {
8888
["data"]=>
89-
string(24) "MongoDB\BSON\Persistable"
89+
string(32) "TW9uZ29EQlxCU09OXFBlcnNpc3RhYmxl"
9090
["type"]=>
9191
int(68)
9292
}

0 commit comments

Comments
 (0)