Skip to content

Commit

Permalink
Update Base2n.php
Browse files Browse the repository at this point in the history
Fixes from source analyzer (security and speed optimization's)
  • Loading branch information
rusxakep committed Feb 26, 2015
1 parent f21290e commit df52398
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions Base2n.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public function encode($rawString)
$encodedString = '';
$byte = array_shift($bytes);
$bitsRead = 0;

$oldBits = 0;

$chars = $this->_chars;
$bitsPerCharacter = $this->_bitsPerCharacter;
$rightPadFinalBits = $this->_rightPadFinalBits;
Expand Down Expand Up @@ -233,7 +234,7 @@ public function decode($encodedString, $strict = FALSE)

// Remove trailing padding characters
if ($padFinalGroup) {
while ($encodedString[$lastNotatedIndex] == $padCharacter) {
while ($encodedString[$lastNotatedIndex] === $padCharacter) {
$encodedString = substr($encodedString, 0, $lastNotatedIndex);
$lastNotatedIndex--;
}
Expand All @@ -246,7 +247,7 @@ public function decode($encodedString, $strict = FALSE)
// Convert each encoded character to a series of unencoded bits
for ($c = 0; $c <= $lastNotatedIndex; $c++) {

if (!isset($charmap[$encodedString[$c]]) && !$caseSensitive) {
if (!$caseSensitive && !isset($charmap[$encodedString[$c]])) {
// Encoded character was not found; try other case
if (isset($charmap[$cUpper = strtoupper($encodedString[$c])])) {
$charmap[$encodedString[$c]] = $charmap[$cUpper];
Expand All @@ -266,7 +267,7 @@ public function decode($encodedString, $strict = FALSE)
$newBits = $charmap[$encodedString[$c]] << $bitsNeeded - $bitsPerCharacter;
$bitsWritten += $bitsPerCharacter;

} elseif ($c != $lastNotatedIndex || $rightPadFinalBits) {
} elseif ($c !== $lastNotatedIndex || $rightPadFinalBits) {
// Zero or more too many bits to complete a byte; shift right
$newBits = $charmap[$encodedString[$c]] >> $unusedBitCount;
$bitsWritten = 8; //$bitsWritten += $bitsNeeded;
Expand All @@ -279,11 +280,11 @@ public function decode($encodedString, $strict = FALSE)

$byte |= $newBits;

if ($bitsWritten == 8 || $c == $lastNotatedIndex) {
if ($bitsWritten === 8 || $c === $lastNotatedIndex) {
// Byte is ready to be written
$rawString .= pack('C', $byte);

if ($c != $lastNotatedIndex) {
if ($c !== $lastNotatedIndex) {
// Start the next byte
$bitsWritten = $unusedBitCount;
$byte = ($charmap[$encodedString[$c]] ^ ($newBits << $unusedBitCount)) << 8 - $bitsWritten;
Expand Down

0 comments on commit df52398

Please sign in to comment.