note 77073 added to function.base64-decode

From: Date: Tue, 14 Aug 2007 00:25:26 +0000
Subject: note 77073 added to function.base64-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-129459@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 216.235.15.211 Probable Submitter: 207.194.109.185 ---- Manual Page -- http://www.php.net/manual/en/function.base64-decode.php Edit -- https://master.php.net/note/edit/77073 Del: integrated -- https://master.php.net/note/delete/77073/integrated Del: useless -- https://master.php.net/note/delete/77073/useless Del: bad code -- https://master.php.net/note/delete/77073/bad+code Del: spam -- https://master.php.net/note/delete/77073/spam Del: non-english -- https://master.php.net/note/delete/77073/non-english Del: in docs -- https://master.php.net/note/delete/77073/in+docs Del: other reasons-- https://master.php.net/note/delete/77073 Reject -- https://master.php.net/note/reject/77073 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129459) next »