function base32_decode($input, $outputLength) { $inputLength = strlen($input); // echo "legnth is: $inputLength output:$outputLength\n$input\n"; $bytes = 0; $currentByte = 0; $output = ''; for ($offset = 0; $offset < $inputLength && $bytes < $outputLength; $offset += 8) { // print "offset: $offset\n"; $output[$bytes] = decode_bits($input[$offset + 0]) << 3; $currentByte = decode_bits($input[$offset + 1]); $output[$bytes] += $currentByte >> 2; $output[$bytes + 1] = ($currentByte & 0x3) << 6; if ($input[$offset + 2] == '=') { // print "Return + 2 bytes:$bytes\n"; return arrayToString($output, $bytes + 1); } else { $bytes++; } $output[$bytes] += decode_bits($input[$offset + 2]) << 1; $currentByte = decode_bits($input[$offset + 3]); $output[$bytes] += $currentByte >> 4; $output[$bytes + 1] = $currentByte << 4; $z = $input[$offset + 4]; // print "off4 = $z\n"; if ($input[$offset + 4] == '=') { // print "Return + 4 bytes:$bytes\n"; return arrayToString($output, $bytes + 1); } else { $bytes++; } $currentByte = decode_bits($input[$offset + 4]); $output[$bytes] += $currentByte >> 1; $output[$bytes + 1] = $currentByte << 7; if ($input[$offset + 5] == '=') { // print "Return + 5 bytes:$bytes\n"; return arrayToString($output, $bytes + 1); } else { $bytes++; } $output[$bytes] += decode_bits($input[$offset + 5]) << 2; $currentByte = decode_bits($input[$offset + 6]); $output[$bytes] += $currentByte >> 3; $output[$bytes + 1] = ($currentByte & 0x7) << 5; if ($input[$offset + 7] == '=') { // print "Return + 7 bytes:$bytes\n"; return arrayToString($output, $bytes + 1); } else { $bytes++; } $output[$bytes] += decode_bits($input[$offset + 7]) & 0x1f; $bytes++; } return arrayToString($output, $bytes); }
function base32_decode($input, $outputLength) { $inputLength = strlen($input); $bytes = 0; $currentByte = 0; $output = ''; for ($offset = 0; $offset < $inputLength && $bytes < $outputLength; $offset += 8) { $output[$bytes] = decode_bits($input[$offset + 0]) << 3; $currentByte = decode_bits($input[$offset + 1]); $output[$bytes] += $currentByte >> 2; $output[$bytes + 1] = ($currentByte & 0x3) << 6; if ($input[$offset + 2] == '=') { return arrayToString($output, $bytes + 1); } else { $bytes++; } $output[$bytes] += decode_bits($input[$offset + 2]) << 1; $currentByte = decode_bits($input[$offset + 3]); $output[$bytes] += $currentByte >> 4; $output[$bytes + 1] = $currentByte << 4; if ($input[$offset + 4] == '=') { return arrayToString($output, $bytes + 1); } else { $bytes++; } $currentByte = decode_bits($input[$offset + 4]); $output[$bytes] += $currentByte >> 1; $output[$bytes + 1] = $currentByte << 7; if ($input[$offset + 5] == '=') { return arrayToString($output, $bytes + 1); } else { $bytes++; } $output[$bytes] += decode_bits($input[$offset + 5]) << 2; $currentByte = decode_bits($input[$offset + 6]); $output[$bytes] += $currentByte >> 3; $output[$bytes + 1] = ($currentByte & 0x7) << 5; if ($input[$offset + 7] == '=') { return arrayToString($output, $bytes + 1); } else { $bytes++; } $output[$bytes] += decode_bits($input[$offset + 7]) & 0x1f; $bytes++; } return arrayToString($output, $bytes); }