Read a 'key' or 'value' component from a hstore's string representation

@param string $str
@param int $p
@return string
164203
private function readKeyValue($str, &$p)
    {

        $c = substr($str, $p, 1);

        // Unquoted string.
        if ($c != '"') {
            $len = strcspn($str, " \r\n\t,=>", $p);
            $value = substr($str, $p, $len);
            $p += $len;
            // $value = stripcslashes($value)
            $value = str_replace( '\\"', '"', $value );
            $value = str_replace( '\\\\', '\\', $value );
            return $value;
        }

        // Quoted string.
        $m = null;
        if (preg_match('/" ((?' . '>[^"\\\\]+|\\\\.)*) "/Asx', $str, $m, 0, $p)) {
            $p += strlen( $m[0] );
            // $value = stripcslashes($m[1]);
            $value = str_replace( '\\"', '"', $m[1] );
            $value = str_replace( '\\\\', '\\', $value );
            return $value;
        }

        // Error.
        throw new \Exception( "quoted or unquoted string `{$str}` only" );

    }
311461
public static PublicKey stringToRSAPublicKey (String str)
    {
        try {
            BigInteger mod = new BigInteger(str.substring(0, str.indexOf(SPLIT)), 16);
            BigInteger exp = new BigInteger(str.substring(str.indexOf(SPLIT) + 1), 16);
            RSAPublicKeySpec keySpec = new RSAPublicKeySpec(mod, exp);
            KeyFactory kf = KeyFactory.getInstance("RSA");
            return kf.generatePublic(keySpec);
        } catch (NumberFormatException nfe) {
            log.warning("Failed to read key from string.", "str", str, nfe);
        } catch (GeneralSecurityException gse) {
            log.warning("Failed to read key from string.", "str", str, gse);
        }
        return null;
    }
835641
static public function fromCamel($str) {
      $str[0] = strtolower($str[0]);
      return preg_replace_callback('/([A-Z])/', function($char) {return "_" . strtolower($char[1]);}, $str);
  }
737362
private function readZipCode($pointer) {
    if (false === $pointer) {
      // Deal with invalid IPs
      $zipCode = self::INVALID_IP_ADDRESS;
    } elseif (0 === self::$columns[self::ZIP_CODE][$this->type]) {
      // If the field is not suported, return accordingly
      $zipCode = self::FIELD_NOT_SUPPORTED;
    } else {
      // Read the zip code
      $zipCode = $this->readString($pointer + self::$columns[self::ZIP_CODE][$this->type]);
    }
    return $zipCode;
  }
530310
public function parseEncodedString(&$s, &$p)
    {
        $l = $this->parseEncodedBinary($s, $p);

        if ($l === null || $l === false) {
            return $l;
        }

        $o = $p;
        $p += $l;

        return mb_orig_substr($s, $o, $l);
    }
674940
private function getEncryptionKey($str = null)
    {
        $key        = $this->getSalt();
        $key_crypt  = md5($key);
        $count      = 0;
        $tmp        = '';
        for ($ctr=0;$ctr<strlen($str);$ctr++) {
            if ($count==strlen($key_crypt)) {
                $count=0;
            }
            $tmp.= substr($str,$ctr,1) ^ substr($key_crypt,$count,1);
            $count++;
        }
        return $tmp;
    }
1188854
public static function unpack_reference($str, $cleanparams = false) {
        $decoded = base64_decode($str, true);
        if ($decoded === false) {
            throw new file_reference_exception(null, $str, null, null, 'Invalid base64 format');
        }
        $params = @unserialize($decoded); // hide E_NOTICE
        if ($params === false) {
            throw new file_reference_exception(null, $decoded, null, null, 'Not an unserializeable value');
        }
        if (is_array($params) && $cleanparams) {
            $params = array(
                'component' => is_null($params['component']) ? ''   : clean_param($params['component'], PARAM_COMPONENT),
                'filearea'  => is_null($params['filearea'])  ? ''   : clean_param($params['filearea'], PARAM_AREA),
                'itemid'    => is_null($params['itemid'])    ? 0    : clean_param($params['itemid'], PARAM_INT),
                'filename'  => is_null($params['filename'])  ? null : clean_param($params['filename'], PARAM_FILE),
                'filepath'  => is_null($params['filepath'])  ? null : clean_param($params['filepath'], PARAM_PATH),
                'contextid' => is_null($params['contextid']) ? null : clean_param($params['contextid'], PARAM_INT)
            );
        }
        return $params;
    }
162011
public function parseComponent($str)
	{
		if (! is_string($str)) {
			throw new InvalidUrlException('Unexpected type.');
		}
		if (strpos($str, ' ') !== false) {
			throw new InvalidUrlException('Path contains whitespace.');
		}
		$p = explode('/', $str);
		foreach ($p as $i => $v) {
			$p[$i] = rawurldecode($v);
		}
		$decodedPath = join('/', $p);
		$this->path->set($decodedPath);
	}
367949
public static function formataCPFCNPJ($str)
    {
        $str = preg_replace('![^\d]!', '', $str);
        if (11 == strlen($str) || 14 > strlen($str)) {
            $str = CPF::formatar($str);
        } elseif (14 <= strlen($str)) {
            $str = CNPJ::formatar($str);
        }

        return $str;
    }
932937
public function string($str = null)
    {
        if ($str !== null) {
            foreach ($this->childScanners as $s) {
                $s->string($str);
            }
        }
        return parent::string($str);
    }
568872
public function decrypt($str=null, $key=null)
    {
        if (!is_null($key)) {
            $this->setSalt($key);
        }
        $str = self::getEncryptionKey(base64_decode($str));
        $tmp = '';
        for ($ctr=0;$ctr<strlen($str);$ctr++) {
            $md5 = substr($str,$ctr,1);
            $ctr++;
            $tmp.= (substr($str,$ctr,1) ^ $md5);
        }
        return $tmp;
    }
759216
function parse_plain(&$p, $body = null)
    {
        $part = $p['structure'];

        // Get message body from IMAP server
        if ($body === null) {
            $body = $this->get_part_body($p['object'], $part);
        }

        // In this way we can use fgets on string as on file handle
        // Don't use php://temp for security (body may come from an encrypted part)
        $fd = fopen('php://memory', 'r+');
        if (!$fd) {
            return;
        }

        fwrite($fd, $body);
        rewind($fd);

        $body   = '';
        $prefix = '';
        $mode   = '';
        $tokens = array(
            'BEGIN PGP SIGNED MESSAGE' => 'signed-start',
            'END PGP SIGNATURE'        => 'signed-end',
            'BEGIN PGP MESSAGE'        => 'encrypted-start',
            'END PGP MESSAGE'          => 'encrypted-end',
        );
        $regexp = '/^-----(' . implode('|', array_keys($tokens)) . ')-----[\r\n]*/';

        while (($line = fgets($fd)) !== false) {
            if ($line[0] === '-' && $line[4] === '-' && preg_match($regexp, $line, $m)) {
                switch ($tokens[$m[1]]) {
                case 'signed-start':
                    $body = $line;
                    $mode = 'signed';
                    break;

                case 'signed-end':
                    if ($mode === 'signed') {
                        $body .= $line;
                    }
                    break 2; // ignore anything after this line

                case 'encrypted-start':
                    $body = $line;
                    $mode = 'encrypted';
                    break;

                case 'encrypted-end':
                    if ($mode === 'encrypted') {
                        $body .= $line;
                    }
                    break 2; // ignore anything after this line
                }

                continue;
            }

            if ($mode === 'signed') {
                $body .= $line;
            }
            else if ($mode === 'encrypted') {
                $body .= $line;
            }
            else {
                $prefix .= $line;
            }
        }

        fclose($fd);

        if ($mode === 'signed') {
            $this->parse_plain_signed($p, $body, $prefix);
        }
        else if ($mode === 'encrypted') {
            $this->parse_plain_encrypted($p, $body, $prefix);
        }
    }
1310170
public static function fromName($str){
		
		$strName = \strtoupper(\trim($str));
		
		foreach(static::getConstants() as $name => $value){
			if($strName === \strtoupper($name)){
				return new static($value);
			}
		}
		
		return null;
		
	}
745939
public static function fromString(string $str) : Cookie
    {
        $err = false;
        $name = "";
        $value = "";
        $expires = null;
        $domain = null;
        $path = "/";
        $secure = false;
        $httpOnly = false;


        if ($str === "") {
            $err = "The string is not a valid representation of a cookie.";
        }


        if ($err === false) {
            $parts = array_map("trim", explode(";", $str));

            $nameValue = explode("=", $parts[0]);

            $name = $nameValue[0];
            $value = ((count($nameValue) === 2) ? $nameValue[1] : "");

            if (count($parts) > 1) {
                array_shift($parts);

                foreach ($parts as $p) {
                    $keyPair = array_map("trim", explode("=", $p));
                    if (count($keyPair) === 1) {
                        $keyPair[] = null;
                    }

                    $val = $keyPair[1];

                    switch (strtolower($keyPair[0])) {
                        case "expires":
                            if ($val !== null) {
                                if (is_numeric($val) === true) {
                                    $expires = new \DateTime();
                                    $expires->setTimestamp((int)$val);
                                } else {
                                    if (strpos(strtolower($val), " utc") !== false ||
                                        strpos(strtolower($val), " gmt") !== false) {
                                        $val = substr($val, 0, strlen($val) - 4);
                                    }
                                    $expires = \DateTime::createFromFormat("D, d M Y H:i:s", $val);
                                }
                            }
                            break;

                        case "domain":
                            $domain = $val;
                            break;

                        case "path":
                            $path = $val;
                            break;

                        case "secure":
                            $secure = true;
                            break;

                        case "httponly":
                            $httpOnly = true;
                            break;
                    }
                }
            }
        }


        if ($err === false) {
            return new Cookie($name, $value, $expires, $domain, $path, $secure, $httpOnly);
        } else {
            throw new \InvalidArgumentException($err);
        }
    }
789120
public static function parse_str($str, &$result) {
		if (\function_exists('mb_parse_str')) {
			return \mb_parse_str($str, $result);
		}
		else {
			return \parse_str($str, $result);
		}
	}
849315
public function decode($str)
    {
        $ret = mcrypt_decrypt($this->cipher, $this->key, $str, $this->mode, $this->iv);
        //去除补码
        if ($this->padding)
        {
            $ret =  self::PADDING_PKCS5 == $this->padding ? $this->stripPKCS5Padding($ret) : $this->stripPKCS7Padding($ret);
        }
        //是否使用BASE64编码
        if ($this->base64Encoding)
        {
            return base64_decode($ret);
        }
        else
        {
            return $ret;
        }
    }
299312
public static function decode(string $str, \Exception &$err = null): TagContext
    {
        $tagContext = TagContext::empty();
        $strLen = strlen($str);
        if ($strLen == 0) {
            return $tagContext;
        }
        if ($str[0] > self::TAGS_VERSION_ID) {
            $err = new \Exception("cannot decode: unsupported version: " . ord($str[0]) .
                "; supports only up to: " . ord(self::TAGS_VERSION_ID));
            return $tagContext;
        };

        $idx = 1;

        $tagsLen = 0;
        while ($idx < $strLen) {
            // read keytype
            if ($str[$idx] != self::KEYTYPE_STRING) {
                $err = new \Exception("cannot decode: invalid key type: " . ord($str[$idx]));
                return TagContext::empty();
            }
            $idx++;

            if ($idx >= $strLen) {
                $err = new \Exception("unexpected end: index: " . $idx . " length: " . $strLen);
            }

            // read varint (key length)
            $keyLen = 0;
            $cnt = self::decodeUnsigned(substr($str, $idx), $keyLen);
            if ($cnt <= 0 || $keyLen == 0) {
                $err = new \Exception("cannot decode: key length varint");
                return TagContext::empty();
            }
            $idx += $cnt;

            // read key payload of size $keylen
            $valueEnd = $idx + $keyLen;
            if ($valueEnd > $strLen) {
                $err = new \Exception("malformed encoding: length: $keyLen, upper: $valueEnd, maxLength: $strLen");
                return TagContext::empty();
            }
            try {
                $key = TagKey::create(substr($str, $idx, $keyLen));
            } catch (\Exception $e) {
                $err = $e;
                return TagContext::empty();
            }
            $idx = $valueEnd;

            // read varint (value length)
            $valLen = 0;
            $cnt = self::decodeUnsigned(substr($str, $idx), $valLen);
            if ($cnt <= 0 || $keyLen == 0) {
                $err = new \Exception("cannot decode: value length varint");
                return TagContext::empty();
            }

            // Total size of all Tag key + value strings excluding serialization
            // details should not be greater than TagContext::MAX_LENGTH
            // Since TagContext extraction is an all or nothing operation, we
            // fail the decode step if overstepping TagContext::MAX_LENGTH.
            $tagsLen += $keyLen + $valLen;
            if ($tagsLen > TagContext::MAX_LENGTH) {
                $err = new \Exception(TagContext::EX_INVALID_CONTEXT);
                return TagContext::empty();
            }

            $idx += $cnt;

            // read value payload of size $valLen
            $valueEnd = $idx + $valLen;
            if ($valueEnd > $strLen) {
                $err = new \Exception("malformed encoding: length: $valLen, upper: $valueEnd, maxLength: $strLen");
                return TagContext::empty();
            }
            try {
                $value = TagValue::create(substr($str, $idx, $valLen));
            } catch (\Exception $e) {
                $err = $e;
                return TagContext::empty();
            }
            $idx = $valueEnd;

            $tagContext->upsert($key, $value);
        }


        return $tagContext;
    }