#47151 [Opn->Csd]: PHP 6.0 decodes incorrect base64 uft8 data
| From: | lunter at interia dot pl | Date: | Mon, 19 Jan 2009 22:47:52 +0000 |
| Subject: | #47151 [Opn->Csd]: PHP 6.0 decodes incorrect base64 uft8 data | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-133034@lists.php.net to get a copy of this message | ||
ID: 47151
User updated by: lunter at interia dot pl
Reported By: lunter at interia dot pl
-Status: Open
+Status: Closed
Bug Type: Unicode Engine related
Operating System: all
PHP Version: 6CVS-2009-01-19 (CVS)
New Comment:
new version, better examples
http://bugs.php.net/bug.php?id=47155
Previous Comments:
------------------------------------------------------------------------
[2009-01-19 17:53:20] lunter at interia dot pl
Description:
------------
Problem:
--------
PHP 6.0 decodes incorrect base64 uft8 data.
If it is bogus, show the way to encode 'zrEgKyDOsiA9IM6z' to
(unicode)string 'α + β = γ'.
--------------------------------------------------------------------------------------------
PHP 6.0 example (example.php):
------------------------------
<?
// unicode.semantics = off
// unicode.runtime_encoding = iso-8859-1
// unicode.script_encoding = utf-8
// unicode.output_encoding = utf-8
// unicode.from_error_mode = U_INVALID_SUBSTITUTE
// unicode.from_error_subst_char = 3f
$base64='zrEgKyDOsiA9IM6z'; // this is utf-8 based64 text
$binary=base64_decode($base64); // binary utf-8 bytes
$text=unicode_decode($binary,'iso-8859-1'); // why iso-8859-x is only
supported, where is raw binary option ?
// $text=bin2uni($binary); // needed
header('Content-Type: text/plain; charset=utf-8');
print($text); // SHOULD BE (utf-8): α + β = γ
?>
--------------------------------------------------------------------------------------------
Solution:
---------
We can not get (unicode)string from (binary)string consists utf-8
bytes.
Imagine: converting (unicode)<->(binary unicode bytes string) newer
need charset infomation.
C#: System.Text.Encoding.UTF8.GetString()
Decodes a sequence of bytes from the specified byte array into a
string.
PHP equivalent needed: unicode bin2uni( binary $b )
Decodes a sequence of bytes from the specified binary string into an
unicode string.
--------------------------------------------------------------------------------------------
C# working equivalent (example.ashx):
-------------------------------------
<%@ WebHandler Language="C#" Class="example_handler" %>
using System;
using System.Data;
using System.Web;
public class example_handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string base64 = "zrEgKyDOsiA9IM6z"; // this is utf-8 based64
text
byte[] binary = Convert.FromBase64String(base64); // binary
utf-8 bytes
string text = System.Text.Encoding.UTF8.GetString(binary); //
raw binary supported
context.Response.ContentType = "text/plain; charset=utf-8";
context.Response.Write(text); // very good (utf-8): α + β
= γ
}
public bool IsReusable {
get {
return false;
}
}
}
Reproduce code:
---------------
above
Expected result:
----------------
above
Actual result:
--------------
above
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=47151&edit=1