note 77073 deleted from function.base64-decode by cmb
| From: | cmb@php.net | Date: | Tue, 09 Jun 2015 17:11:26 +0000 |
| Subject: | note 77073 deleted from function.base64-decode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-202793@lists.php.net to get a copy of this message | ||
Note Submitter: morgangalpin att gmail dotty com
----
I was having trouble with base64_decode returning false if the data to be decoded wasn't
actually encoded. It turns out that it is a bug that exists in PHP version 5.1.2, which I'm
using, but it has been fixed in CVS. The relevant bug is: http://bugs.php.net/bug.php?id=37244
"base64_decode violates RFC 3548". The fix may become available in 5.2.4 or 6 or whatever
is coming next.
Since I'm not able to upgrade PHP to the latest version, I needed a way to check if some data
had actually been encoded before trying to decode it. Here is the function I've used; I hope it
helps someone.
<?php
/**
* Check a string of base64 encoded data to make sure it has actually
* been encoded.
*
* @param $encodedString string Base64 encoded string to validate.
* @return Boolean Returns true when the given string only contains
* base64 characters; returns false if there is even one non-base64 character.
*/
function checkBase64Encoded($encodedString) {
$length = strlen($encodedString);
// Check every character.
for ($i = 0; $i < $length; ++$i) {
$c = $encodedString[$i];
if (
($c < '0' || $c > '9')
&& ($c < 'a' || $c > 'z')
&& ($c < 'A' || $c > 'Z')
&& ($c != '+')
&& ($c != '/')
&& ($c != '=')
) {
// Bad character found.
return false;
}
}
// Only good characters found.
return true;
}
?>