go to bug id or search bugs for
I have a VARIANT of type 8209 which is an array of bytes (VT_ARRAY | VT_UI1).
This array contains null byte in the middle.
When trying to convert it to a string only bytes before null byte are used. Others are ignored.
But they are present in the object. You can see it if you walk through the bytes of variant via foreach().
$string = "ab\0cd";
$variant = new VARIANT($string, VT_ARRAY | VT_UI1); // Array of bytes
$converted = (string) $variant;
// $converted variable should be equal to the original $string and should contain 5 characters.
Add a Patch
Add a Pull Request
I have the same problem when I want to extract the value of a LONGBINARY field from an MS-Access or MSSQL Database (OLE Field exactly), and the OLE embeeded header contains some zero bytes. I can obtain only 3 bytes when it converts to a string for save to a file. And when I loop with "foreach" the VARIANT ARRAY of UNSIGNED BYTES it loops correctly (except for the byte '\0' after every valid byte) and if I count the array with "count($field->Value)" for example, it results the double lenght that expects.
Why don't exists in PHP a way to obtain a simple array of bytes for a variable, or access the data like a "string" of bytes?
The problem is that php_com_olestring_to_string() is not binary
safe, because it passes -1 to the cchWideChar parameter of
WideCharToMultiByte(). That causes WideCharToMultiByte() to
stop at the first null character (actually, two null bytes), and
so the rest of the string is ignored.
As a BSTR has a prefix containing its length in bytes it
appears we could use this to calculate the proper cchWideChar
argument, see the attached patch.
The following patch has been added/updated:
Patch Name: olestring-binary-safe