php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62875 base64_encode broken with binary string
Submitted: 2012-08-20 14:29 UTC Modified: 2021-09-09 10:25 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:0 of 0 (0.0%)
From: robertleglu at msn dot com Assigned: cmb (profile)
Status: Wont fix Package: URL related
PHP Version: Irrelevant OS: z/OS
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: robertleglu at msn dot com
New email:
PHP Version: OS:

 

 [2012-08-20 14:29 UTC] robertleglu at msn dot com
Description:
------------
problem occurs on z/OS 1.11 (OS of IBM mainframe) with latest version of php for 
that system.

binary strings are badly encoded with base64_encode.
rewriting my own (portable) version bypass the problem.


php version : (php_cli --version)
PHP 5.1.2 (cli) (built: Sep 28 2010 16:33:24)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies

Note that z/OS works in EBCDIC, so any code that assume ascii code, ascii order 
and ascii continuity is broken 






Test script:
---------------
<?php
$str = chr(0x47).chr(0x49).chr(46);
echo base64_encode($str).PHP_EOL;
?>

Expected result:
----------------
R0ku


Actual result:
--------------
5fEG



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-20 15:23 UTC] reeze dot xia at gmail dot com
I can't reproduce it.

http://3v4l.org/GIL8C

It seems a OS specific bug. and I can't get a z/OS :(
 [2012-08-20 15:25 UTC] rasmus@php.net
reeze, it isn't really OS-specific but rather EBCDIC-specific. We have EBCDIC 
hacks in a few places, but I guess nobody bothered to make base64_encode EBCDIC-
safe.
 [2012-08-21 07:56 UTC] robertleglu at msn dot com
I continue searching and i am pretty sure that the internal routines is written 
with an ascii table somewhere.

I was testing my routine called portable_base64_encode on z/OS and was confused 
because it seems to be wrong.
Here is the result :

str : A base64_encode : QQ== portable_base64_encode : wQ==
str : ABC base64_encode : QUJD portable_base64_encode : wcLD

But, although the built-in function may seem right and mine seems wrong,
the fact is that the binary code of 'A' 'B' and 'C' are not 65, 66 and 67, but 
193, 194 and 195 in EBCDIC.

So, even for a human-readable string, the built-in routine is wrong because 
base64 is intended to encode a binary flow...
 [2021-09-09 10:25 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-09-09 10:25 UTC] cmb@php.net
As of PHP 8.0.0, EBCDIC is no longer supported at all:
<https://github.com/php/php-src/pull/5390>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 10 14:01:29 2024 UTC