Decode single 6-bit digit from base64

@param string $char
@return number
438994
public static function base64Decode($char)
    {
        if (!array_key_exists($char, self::$CHAR_TO_INT)) {
            throw new Exception("Not a valid base 64 digit: ".$char);
        }
        return self::$CHAR_TO_INT[$char];
    }
457075
protected function decodePunycode($encoded)
    {
        $prefix = 'xn--';
        $safe_char = 0xFFFC;
        $base = 36;
        $tmin = 1;
        $tmax = 26;
        $skew = 38;
        $damp = 700;

        if (strpos($encoded, $prefix) !== 0 || strlen(trim(str_replace($prefix, '', $encoded))) == 0) {
            return $encoded;
        }

        $is_first = true;
        $bias = 72;
        $idx = 0;
        $char = 0x80;
        $decoded = array();
        $output = '';

        $delim_pos = strrpos($encoded, '-');
        if ($delim_pos > strlen($prefix)) {
            for ($k = strlen($prefix); $k < $delim_pos; ++$k) {
                $decoded[] = ord($encoded{$k});
            }
        }
        $deco_len = count($decoded);
        $enco_len = strlen($encoded);

        for ($enco_idx = $delim_pos ? ($delim_pos + 1) : 0; $enco_idx < $enco_len; ++$deco_len) {
            for ($old_idx = $idx, $w = 1, $k = $base; 1; $k += $base) {
                $cp = ord($encoded{$enco_idx++});
                $digit = ($cp - 48 < 10) ? $cp - 22 : (($cp - 65 < 26) ? $cp - 65 : (($cp - 97 < 26) ? $cp - 97 : $base));
                $idx += $digit * $w;
                $t = ($k <= $bias) ? $tmin : (($k >= $bias + $tmax) ? $tmax : ($k - $bias));
                if ($digit < $t) {
                    break;
                }
                $w = (int)($w * ($base - $t));
            }
            $delta = $idx - $old_idx;
            $delta = intval($is_first ? ($delta / $damp) : ($delta / 2));
            $delta += intval($delta / ($deco_len + 1));
            for ($k = 0; $delta > (($base - $tmin) * $tmax) / 2; $k += $base) {
                $delta = intval($delta / ($base - $tmin));
            }
            $bias = intval($k + ($base - $tmin + 1) * $delta / ($delta + $skew));
            $is_first = false;
            $char += (int)($idx / ($deco_len + 1));
            $idx %= ($deco_len + 1);
            if ($deco_len > 0) {
                for ($i = $deco_len; $i > $idx; $i--) {
                    $decoded[$i] = $decoded[($i - 1)];
                }
            }
            $decoded[$idx++] = $char;
        }

        foreach ($decoded as $k => $v) {
            if ($v < 128) {
                $output .= chr($v);
            } // 7bit are transferred literally
            elseif ($v < (1 << 11)) {
                $output .= chr(192 + ($v >> 6)) . chr(128 + ($v & 63));
            } // 2 bytes
            elseif ($v < (1 << 16)) {
                $output .= chr(224 + ($v >> 12)) . chr(128 + (($v >> 6) & 63)) . chr(128 + ($v & 63));
            } // 3 bytes
            elseif ($v < (1 << 21)) {
                $output .= chr(240 + ($v >> 18)) . chr(128 + (($v >> 12) & 63)) . chr(128 + (($v >> 6) & 63)) . chr(128 + ($v & 63));
            } // 4 bytes
            else {
                $output .= $safe_char;
            } //  'Conversion from UCS-4 to UTF-8 failed: malformed input at byte '.$k
        }
        return $output;
    }
1338812
private function getCharIndex(string $char): int {
        if (\strlen($char) > 1) $char = \substr($char, 0, 1);
        return \ord($char) - \ord('a');
    }
631982
public function decode($encoded)
    {
        $vlq = 0;
        $i = 0;

        do {
            $digit = $this->base64Decode($encoded[$i]);
            $vlq |= ($digit & $this->mask) << ($i * $this->shift);
            $i++;
        } while ($digit & $this->continuationBit);

        return $this->fromVLQSigned($vlq);
    }
730283
public static function random62($length)
    {
        $str = self::DIGIT.self::ALPHABET;
        
        $strLength = 62;
        while($length > $strLength){
            $str .= $str;
            $strLength += 60;
        }
        
        $str = str_shuffle($str);
        return substr($str, 0,$length);
    }
9846
protected function isLetter($char)
    {
        if ($char === null) {
            return false;
        }

        // Notice: ord(null) will return 0.
        // ord() does not work with utf-8 characters.
        $ascii = ord($char);

        // ASCII codes: 65 = 'A', 90 = 'Z', 97 = 'a', 122 = 'z'
        return (($ascii >= 65 and $ascii <= 90) or ($ascii >= 97 and $ascii <= 122));
    }
325412
public static function char2Unicode($char)
    {
        switch (strlen($char)) {
            case 1 :
                return ord($char);
            case 2 :
                return (ord($char{1}) & 63) |
                    ((ord($char{0}) & 31) << 6);
            case 3 :
                return (ord($char{2}) & 63) |
                    ((ord($char{1}) & 63) << 6) |
                    ((ord($char{0}) & 15) << 12);
            case 4 :
                return (ord($char{3}) & 63) |
                    ((ord($char{2}) & 63) << 6) |
                    ((ord($char{1}) & 63) << 12) |
                    ((ord($char{0}) & 7) << 18);
            default :
                trigger_error('Character is not UTF-8!', E_USER_WARNING);
                return false;
        }
    }
54059
private function baseDecode($string)
    {
        $b      = 61;
        $base   = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $limit  = strlen($string);
        $result = strpos($base, $string[0]);
        for ($i = 1; $i < $limit; ++$i) {
            $result = $b * $result + strpos($base, $string[$i]);
        }

        return $result;
    }
444164
private function baseEncode($integer)
    {
        $b      = 61;
        $base   = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $r      = $integer % $b;
        $result = $base[$r];
        $q      = floor($integer / $b);
        while ($q) {
            $r      = $q % $b;
            $q      = floor($q / $b);
            $result = $base[$r].$result;
        }

        return $result;
    }
248899
public function base64Decode($char)
    {
        if (! array_key_exists($char, $this->charToIntMap)) {
            throw new \Exception(sprintf('Invalid base 64 digit "%s" given.', $char));
        }

        return $this->charToIntMap[$char];
    }
409936
function decode_base64(salt) {
        var decoded = new Array(16);
        var i = 0, j = 0, c1, c2;
        while(true) {
            c1 = index_64[salt.charCodeAt(i++)-46];
            c2 = index_64[salt.charCodeAt(i++)-46];
            decoded[j++] = (c1 << 2 | c2 >> 4) & 0xff;
            if (i === 22) break;
            
            c1 = c2 << 4;
            c2 = index_64[salt.charCodeAt(i++)-46];
            decoded[j++] = (c1 | c2 >> 2) & 0xff;

            c1 = c2 << 6;
            c2 = index_64[salt.charCodeAt(i++)-46];
            decoded[j++] = (c1 | c2) & 0xff;
        }
        return decoded;
    }
489810
protected function isDigit($char)
    {
        if ($char === null) {
            return false;
        }

        // Notice: ord(null) will return 0.
        // ord() does not work with utf-8 characters.
        $ascii = ord($char);

        // ASCII codes: 48 = '0', 57 = '9'
        return ($ascii >= 48 and $ascii <= 57);
    }
622139
public static function randomChar( $char )
	{
		if ( strlen( $char ) == 0 )
			return '';

		$char = ord( $char );

		if ( $char > 64 && $char < 91 ) // Uppercase
			return static::randomCharRange( 65, 90 );

		if ( $char > 96 && $char < 123 ) // Lowercase
			return static::randomCharRange( 97, 122 );

		if ( $char > 47 && $char < 58 ) // Numeric
			return static::randomCharRange( 48, 57 );

		return static::$randomCharMap[random_int( 0, count( static::$randomCharMap ) )];
	}
4053
public function base64Encode($number)
    {
        if ($number < 0 || $number > 63) {
            throw new \Exception(sprintf('Invalid number "%s" given. Must be between 0 and 63.', $number));
        }

        return $this->intToCharMap[$number];
    }
712983
public static long decodeBase62(final String str) {
		long result = 0;
		int base = BASE62.length();
		for (int i = 0; i < str.length(); i++) {
			result += BASE62.indexOf(str.charAt(i)) * Math.pow(base, i);
		}

		return result;
	}
459689
public static function isBase64($string)
    {
        if(!is_string($string)) return false;

        // Check if there is no invalid character in string
        if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) return false;

        // Decode the string in strict mode and send the response
        if (!base64_decode($string, true)) return false;

        // Encode and compare it to original one
        if (base64_encode(base64_decode($string, true)) !== $string) return false;

        // http://www.albertmartin.de/blog/code.php/19/base64-detection
        // check for bad character when decoding the base64 string
        $check = str_split(base64_decode($string, true));
        $x = 0;
        array_walk($check, function($char) use (&$x){
            if (ord($char) > 126) $x++;
        });
        //var_dump("Count: ". $x . " " . ($x/count($check)*100) . "%" . PHP_EOL);
        if ($x/count($check)*100 > self::$base64BadCharacterThreshold) return false;

        return true;

    }
649642
public static function urlsafe_b64decode($string) {
		$data = str_replace(array('-','_','@'),array('+','/','='),$string);
		$mod4 = strlen($data) % 4;
		if ($mod4) {
			$data .= substr('====', $mod4);
		}
		return base64_decode($data);
	}