Parse BER-encoding

Serves a similar purpose to openssl's asn1parse

@param string $encoded
@return array
@access public
18143
function decodeBER($encoded)
    {
        if (is_object($encoded) && strtolower(get_class($encoded)) == 'file_asn1_element') {
            $encoded = $encoded->element;
        }

        $this->encoded = $encoded;
        // encapsulate in an array for BC with the old decodeBER
        return array($this->_decode_ber($encoded));
    }
1300568
public static byte[] decodeString(String encoded) {
        return encoded == null ? null : Base64.getDecoder().decode(encoded);
    }
805359
public static function decodeMimeStringPart(&$data)
    {
        array_shift($data);
        $encode = strtoupper(array_shift($data));
        $type = strtoupper(array_shift($data));
        if ($type === '') {
            return [];
        }

        $str = array_shift($data);

        return [
            $encode => [
                $type => $str
            ]
        ];
    }
519597
public function decode($data)
    {
        $parser = new Parser();
        $this->arrayData = (array) $parser->parse($data);
        return $this->arrayData;
    }
698081
public function decodePair(string $keyValuePair): array
    {
        # more than a handful of clients like to use '+' characters
        # when encoding data that is (almost) compliant with rfc 3986
        # this is a hack that allows for it
        if (strpos($keyValuePair, "+") !== false) {
            $keyValuePair = str_replace("+", " ", $keyValuePair);
        }

        $parts = explode("=", $keyValuePair, 2);
        $key = rawurldecode($parts[0]);

        # interpret a key with an empty value as `null`
        $value = (count($parts) === 1) ? null : rawurldecode($parts[1]);
        return [$key, $value];
    }
947551
private static function parseArray($val)
    {
        $result = array();
        $openBrackets = 0;
        $openString = false;
        $openLString = false;
        $buffer = '';

        $strLen = strlen($val);
        for($i = 0; $i < $strLen; $i++)
        {
            if($val[$i] == '[' && !$openString && !$openLString)
            {
                $openBrackets++;

                if($openBrackets == 1)
                {
                    // Skip first and last brackets.
                    continue;
                }
            }
            elseif($val[$i] == ']' && !$openString && !$openLString)
            {
                $openBrackets--;

                if($openBrackets == 0)
                {
                    // Allow terminating commas before the closing bracket
                    if(trim($buffer) != '')
                    {
                        $result[] = self::parseValue( trim($buffer) );
                    }

                    if (!self::checkDataType($result))
                    {
                        throw new Exception('Data types cannot be mixed in an array: ' . $buffer);
                    }
                    // Skip first and last brackets. We're finish.
                    return $result;
                }
            }
            elseif($val[$i] == '"' && $val[$i - 1] != "\\" && !$openLString)
            {
                $openString = !$openString;
            }
            elseif($val[$i] == "'"  && !$openString) {
                $openLString = !$openLString;
            }

            if( $val[$i] == ',' && !$openString && !$openLString && $openBrackets == 1)
            {
                $result[] = self::parseValue( trim($buffer) );

                if (!self::checkDataType($result))
                {
                    throw new Exception('Data types cannot be mixed in an array: ' . $buffer);
                }
                $buffer = '';
            }
            else
            {
                $buffer .= $val[$i];
            }
        }

        // If we're here, something went wrong.
        throw new Exception('Wrong array definition: ' . $val);
    }
603025
public function parse($payload)
    {
        if ($payload) {
            $json = json_decode(trim($payload), true);
            if ( ! $json) {
                throw new ParserException('Failed To Parse JSON');
            }
            return $json;
        }

        return [];
    }
1328004
public function parseCode($code)
    {
        $segments = (new NamespacedItemResolver)->parseKey($code);

        if (is_null($segments[0])) {
            $segments[0] = '*';
        }

        return $segments;
    }
272535
protected static function decode($body, $encoding)
    {
        if (ENCQUOTEDPRINTABLE === $encoding) {
            return quoted_printable_decode($body);
        }

        if (ENCBASE64 === $encoding) {
            return base64_decode($body);
        }

        return $body;
    }
655217
public static function get($encoding) {
        if (isset(self::$aliases[strtolower($encoding)])) {
            return self::$aliases[strtolower($encoding)];
        } else {
            return $encoding;
        }
    }
301355
function CharConvert(&$array, $encoding) {

		// identical encoding - end here
		if ($encoding == $this->encoding) {
			return;
		}

		// loop thru array
		foreach ($array as $key => $value) {

			// go recursive
			if (is_array($value)) {
				$this->CharConvert($array[$key], $encoding);
			}

			// convert string
			elseif (is_string($value)) {
				$array[$key] = trim(getid3_lib::iconv_fallback($encoding, $this->encoding, $value));
			}
		}
	}
179398
public static String decode(String encoded) {
        // speedily leave if we're not needed
        if (encoded == null)
            return null;
        if (encoded.indexOf('%') == -1 && encoded.indexOf('+') == -1)
            return encoded;

        //allocate the buffer - use byte[] to avoid calls to new.
        byte holdbuffer[] = new byte[encoded.length()];

        char holdchar;
        int bufcount = 0;

        for (int count = 0; count < encoded.length(); count++) {
            char cur = encoded.charAt(count);
            if (cur == '%') {
                holdbuffer[bufcount++] = (byte) Integer.parseInt(encoded.substring(count + 1, count + 3), 16);
                if (count + 2 >= encoded.length())
                    count = encoded.length();
                else
                    count += 2;
            }
            else if (cur == '+') {
                holdbuffer[bufcount++] = (byte) ' ';
            }
            else {
                holdbuffer[bufcount++] = (byte) cur;
            }
        }
        // REVISIT -- remedy for Deprecated warning.
        //return new String(holdbuffer,0,0,bufcount);
        return new String(holdbuffer, 0, bufcount);
    }
1143345
public static function decodeKey($key, $encoding)
    {
        if ($encoding == "") {
            return $key;
        }

        if ($encoding == "url") {
            return rawurldecode($key);
        } else {
            throw new OssException("Unrecognized encoding type: " . $encoding);
        }
    }
98298
private function parseByteArray()
    {
        list(, $length) = unpack('N', $this->buffer);

        try {
            return substr($this->buffer, 4, $length);
        } finally {
            $this->buffer = substr($this->buffer, $length + 4);
        }
    }
948208
public function decode($data): array
    {
        // Try native PECL YAML PHP extension first if available.
        if (\function_exists('yaml_parse') && $this->useNativeDecoder()) {
            // Safely decode YAML.
            $saved = @ini_get('yaml.decode_php');
            @ini_set('yaml.decode_php', '0');
            $decoded = @yaml_parse($data);
            @ini_set('yaml.decode_php', $saved);

            if ($decoded !== false) {
                return $decoded;
            }
        }

        try {
            return YamlParser::parse($data);
        } catch (ParseException $e) {
            if ($this->useCompatibleDecoder()) {
                return (array) FallbackYamlParser::parse($data);
            }

            throw new \RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e);
        }
    }
51684
public static function encode($input, $encoding)
    {
        switch (strtolower($encoding)) {
            case 'utf-8':
                $output = $input;
                break;
            
            case 'x-user-defined':
                $output = preg_replace_callback('/[^\\x00-\\x7F]/u', function ($matches) {
                    $codePoint = self::getCodePoint($matches[0]);
                    return $codePoint <= 0xF7FF ? chr($codePoint - 0xF780 + 0x80) : '&#' . $codePoint . ';';
                }, $input);
                break;
            
            default:
                $output = self::convertEncoding($input, $encoding);
        }
        
        return $output;
    }
410209
public static String decodeString(String encoded)
    {
        return decodeString(encoded,0,encoded.length(),StringUtil.__ISO_8859_1);
    }