php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64131 rawurlencode appends char in conj.w. base64_decode when url-len isn't div. by 3
Submitted: 2013-02-02 11:18 UTC Modified: 2013-02-02 12:52 UTC
From: post at kira-bianca dot eu Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4.11 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: post at kira-bianca dot eu
New email:
PHP Version: OS:

 

 [2013-02-02 11:18 UTC] post at kira-bianca dot eu
Description:
------------
If I encode a URL with first the function base64_encode() and then with rawurlencode() are appended in the URL may 1 or 2 characters "%3D".

If the length of the URL is divisible by 3, the result is okay.
If the division of the URL length by 3 a remainder of 1, to the url "%3D%3D" attached
With a remainder of 2, "%3D" attached.

Test script:
---------------
$dom = "domain";
for ($i=0; $i<10; $dom.='x', $i++){
	$url = "$dom.ldk";
	$url_e = rawurlencode(base64_encode("$url"));
	printf("%20s (len div 3 = %1d); encoded:%32s; decoded:%s\n",
		$url,
		strlen($url)%3,
		$url_e,
		rawurldecode(base64_decode($url_e)));
}

Expected result:
----------------
          domain.ldk (len div 3 = 1); encoded:            ZG9tYWluLmxkaw%3D%3D; decoded:domain.ldk\nÃÜ
         domainx.ldk (len div 3 = 2); encoded:              ZG9tYWlueC5sZGs%3D; decoded:domainx.ldk7
        domainxx.ldk (len div 3 = 0); encoded:                ZG9tYWlueHgubGRr; decoded:domainxx.ldk
       domainxxx.ldk (len div 3 = 1); encoded:        ZG9tYWlueHh4Lmxkaw%3D%3D; decoded:domainxxx.ldk\nÃÜ
      domainxxxx.ldk (len div 3 = 2); encoded:          ZG9tYWlueHh4eC5sZGs%3D; decoded:domainxxxx.ldk7
     domainxxxxx.ldk (len div 3 = 0); encoded:            ZG9tYWlueHh4eHgubGRr; decoded:domainxxxxx.ldk
    domainxxxxxx.ldk (len div 3 = 1); encoded:    ZG9tYWlueHh4eHh4Lmxkaw%3D%3D; decoded:domainxxxxxx.ldk\nÃÜ
   domainxxxxxxx.ldk (len div 3 = 2); encoded:      ZG9tYWlueHh4eHh4eC5sZGs%3D; decoded:domainxxxxxxx.ldk7
  domainxxxxxxxx.ldk (len div 3 = 0); encoded:        ZG9tYWlueHh4eHh4eHgubGRr; decoded:domainxxxxxxxx.ldk
 domainxxxxxxxxx.ldk (len div 3 = 1); encoded:ZG9tYWlueHh4eHh4eHh4Lmxkaw%3D%3D; decoded:domainxxxxxxxxx.ldk\nÃÜ


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-02 12:52 UTC] cataphract@php.net
you should be decoding with

base64_decode(rawurldecode($url_e))

that is, you should apply the functions in the reverse order.

See http://en.wikipedia.org/wiki/Inverse_function#Symmetry
 [2013-02-02 12:52 UTC] cataphract@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 16:01:34 2025 UTC