#35817 [Opn->Csd]: unpack do not work as expected
ID: 35817
Updated by: iliaa@php.net
Reported By: lynjwxm at hotmail dot com
-Status: Open
+Status: Closed
Bug Type: Strings related
Operating System: rhel as 3
PHP Version: 5.1.1
New Comment:
This bug has been fixed in CVS.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
Thank you for the report, and for helping us make PHP better.
Previous Comments:
------------------------------------------------------------------------
[2005-12-28 09:58:38] lynjwxm at hotmail dot com
and it seems php has the diffrent behavior with perl's:
perl's behavior for 3 bytes(odd bytes)
$out=pack "H3","181";
$out1=unpack "H3",$out;
print $out1;
---------------
expect result:
181
---------------
actual result:
181
perl's behavior for one byte(odd bytes)
$out=pack "H","8";
$out1=unpack "H",$out;
print $out1;
---------------
expect result:
8
---------------
actual result:
8
perl's behavior for 4 byte(even bytes)
$out=pack "H4","1818";
$out1=unpack "H4",$out;
print $out1;
---------------
expect result:
1818
---------------
actual result:
1818
So, can I say it is a bug?
------------------------------------------------------------------------
[2005-12-28 04:12:44] lynjwxm at hotmail dot com
simplest:
<?php
$a=pack("H","8");
var_dump($a);
var_dump(bin2hex($a));
$b=unpack("H",$a);
var_dump($b);
?>
---------------
string(1) " "
string(2) "80"
array(1) {
[1]=>
string(0) ""
}
---------------
Do you mean I can not pack hex string "8"? I should only pack hex
string "88" or so?
------------------------------------------------------------------------
[2005-12-28 02:30:28] lynjwxm at hotmail dot com
in source code:
in func: PHP_FUNCTION(pack)
switch ((int) code) {
case 'h':
case 'H':
INC_OUTPUTPOS((arg + 1) / 2,1) /* 4 bit per arg */
break;
in func: PHP_FUNCTION(unpack)
case 'h':
case 'H':
size = (arg > 0) ? arg / 2 : arg;
arg = 1;
break;
we can find that when we pack an odd number bytes hexstring in function
pack(),it add an extra byte of '0'.But it did not do so in function
unpack().
<?php
$a = pack("H3i", "1810", 513);
var_dump(bin2hex($a));
var_dump(unpack("H3/iabc", $a));
?>
before change code, result:
string(12) "181001020000"
array(2) {
[1]=>
string(2) "18"
["abc"]=>
int(131344)
}
-- size = (arg > 0) ? arg / 2 : arg;
++ size = (arg > 0) ? (arg + 1) / 2 : arg;
after change code, result:
string(12) "181001020000"
array(2) {
[1]=>
string(4) "1810"
["abc"]=>
int(513)
}
------------------------------------------------------------------------
[2005-12-27 10:40:49] lynjwxm at hotmail dot com
Description:
------------
(sorry for my poor english.....)
It seems unpack can not deal with an odd number of hex string args.
Under a xterm, it will cause "xterm" displayed on term.
Reproduce code:
---------------
<?php
$a=pack("H3i","181",5);
$b=unpack("H3/ias",$a);
var_dump($b);
?>
Expected result:
----------------
array(2){
[1]=>
string(3)"181"
["as"]=>
int(5)
}
Actual result:
--------------
array(2){
[1]=>
string(2)"18"
["as"]=>
int(1296)
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=35817&edit=1
Thread (8 messages)