Decoded a chunked body as per RFC 2616

@see http://tools.ietf.org/html/rfc2616#section-3.6.1
@param string $data Chunked body
@return string Decoded body
341845
protected static function decode_chunked($data) {
		if (!preg_match('/^([0-9a-f]+)[^\r\n]*\r\n/i', trim($data))) {
			return $data;
		}

		$decoded = '';
		$encoded = $data;

		while (true) {
			$is_chunked = (bool) preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', $encoded, $matches );
			if (!$is_chunked) {
				// Looks like it's not chunked after all
				return $data;
			}

			$length = hexdec(trim($matches[1]));
			if ($length === 0) {
				// Ignore trailer headers
				return $decoded;
			}

			$chunk_length = strlen($matches[0]);
			$decoded .= $part = substr($encoded, $chunk_length, $length);
			$encoded = substr($encoded, $chunk_length + $length + 2);

			if (trim($encoded) === '0' || empty($encoded)) {
				return $decoded;
			}
		}

		// We'll never actually get down here
		// @codeCoverageIgnoreStart
	}
957396
private function parseChunk($data)
    {
        $data = trim($data);
        if (strlen($data) === 0) {
            throw new SafeBrowsingException(
                'ERROR: Incorrect chunk data "' . $data . '"'
            );
        }

        $splitHead = explode("\n", $data, 2);
        $chunkInfo = explode(':', $splitHead[0]);
        list($type, $chunkNum, $hashLen, $chunkLen) = $chunkInfo;

        if ($chunkLen > 0) {
            //Convert to hex for easy processing
            //First get chunkData according to length
            $chunkData = bin2hex(substr($splitHead[1], 0, $chunkLen));
        } else {
            //No ChunkData, Still Parse
            $chunkData = '';
        }

        if ($type == 'a') {
            $prefixes = [];
            while (strlen($chunkData) > 0) {
                $prefixes[] = substr($chunkData, 0, 8);
                $count = hexdec(substr($chunkData, 8, 2));
                $chunkData = substr($chunkData, 10);
                for ($i = 0; $i < $count; $i++) {
                    $chunkData = substr($chunkData, (($hashLen * 2)));
                }
            }

            return [
                'type' => 'added',
                'chunk_num' => $chunkNum,
                'prefixes' => $prefixes
            ];
        } elseif ($type == 's') {
            $prefixes = [];
            while (strlen($chunkData) > 0) {
                $prefixes[] = substr($chunkData, 0, 8);
                $count = hexdec(substr($chunkData, 8, 2));
                $chunkData = substr($chunkData, 10);
                if ($count > 0) {
                    for ($i = 0; $i < $count; $i++) {
                        $chunkData = substr($chunkData, (($hashLen * 2) + 8));
                    }
                } else {
                    $chunkData = substr($chunkData, 8);
                }
            }

            return [
                'type' => 'removed',
                'chunk_num' => $chunkNum,
                'prefixes' => $prefixes
            ];
        } else {
            throw new SafeBrowsingException(
                'ERROR: In chunkNum "' . $chunkNum . '" incorrect type "' . $type . '"'
            );
        }
    }
1323724
public static function decode($data)
    {
        $dataLen = strlen($data);

        for ($return = '0', $i = 0; $i < $dataLen; $i++) {
            $current = strpos(self::BASE58_CHARS, $data[$i]);
            $return  = Math::mul($return, '58');
            $return  = Math::add($return, $current);
        }

        $return = Util::encodeHex($return);

        for ($i = 0; $i < $dataLen && substr($data, $i, 1) == '1'; $i++) {
            $return = '00'.$return;
        }

        if (strlen($return) % 2 != 0) {
            $return = '0'.$return;
        }

        return $return;
    }
352512
protected static function decode_chunked( $buffer )
    {
        $length = 0;
        $new = '';

        // read chunk-size, chunk-extension (if any) and crlf
        // get the position of the linebreak
        $chunkend = strpos($buffer,"\r\n") + 2;
        $temp = substr($buffer,0,$chunkend);
        $chunk_size = hexdec( trim($temp) );
        $chunkstart = $chunkend;
        while($chunk_size > 0)
        {
            $chunkend = strpos($buffer, "\r\n", $chunkstart + $chunk_size);

            // just in case we got a broken connection
            if($chunkend == false)
            {
                $chunk = substr($buffer,$chunkstart);
                // append chunk-data to entity-body
                $new .= $chunk;
                $length += strlen($chunk);
                break;
            }

            // read chunk-data and crlf
            $chunk = substr($buffer,$chunkstart,$chunkend-$chunkstart);
            // append chunk-data to entity-body
            $new .= $chunk;
            // length := length + chunk-size
            $length += strlen($chunk);
            // read chunk-size and crlf
            $chunkstart = $chunkend + 2;

            $chunkend = strpos($buffer,"\r\n",$chunkstart)+2;
            if($chunkend == false)
            {
                break; //just in case we got a broken connection
            }
            $temp = substr($buffer,$chunkstart,$chunkend-$chunkstart);
            $chunk_size = hexdec( trim($temp) );
            $chunkstart = $chunkend;
        }
        return $new;
    }
24034
public static function encode($data)
    {
        if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
            $json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        } else {
            $json = json_encode($data);
        }

        self::handleError(json_last_error());

        return $json;
    }
907691
public static function decodeUrlSafe($data) {
		$data = \strtr(
			$data,
			self::LAST_THREE_URL_SAFE,
			self::LAST_THREE_STANDARD
		);

		return self::decode($data);
	}
1135282
public static function decodeChunkedBody($body)
    {
        $decBody = '';

        // If mbstring overloads substr and strlen functions, we have to
        // override it's internal encoding
        if (function_exists('mb_internal_encoding') &&
           ((int) ini_get('mbstring.func_overload')) & 2) {

            $mbIntEnc = mb_internal_encoding();
            mb_internal_encoding('ASCII');
        }

        while (trim($body)) {
            if (! preg_match("/^([\da-fA-F]+)[^\r\n]*\r\n/sm", $body, $m)) {

                throw new Zend_Http_Exception("Error parsing body - doesn't seem to be a chunked message");
            }

            $length = hexdec(trim($m[1]));
            $cut = strlen($m[0]);
            $decBody .= substr($body, $cut, $length);
            $body = substr($body, $cut + $length + 2);
        }

        if (isset($mbIntEnc)) {
            mb_internal_encoding($mbIntEnc);
        }

        return $decBody;
    }
517702
public static function decode($data) {
		$decoded = \base64_decode($data, true);

		if ($decoded === false) {
			throw new DecodingError();
		}

		return $decoded;
	}
1159981
public function base64UrlDecode(string $data): string
    {
        return \base64_decode(\str_pad(\strtr($data, '-_', '+/'), \strlen($data)%4, '=', \STR_PAD_RIGHT));
    }
188431
private function unserialize($data)
    {
        if ($this->type == 'db') {
            return $this->db->decode($data, $this->packed);
        }

        return $this->packed ? @unserialize($data) : $data;
    }
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
            ]
        ];
    }
1027928
protected function decode($data)
    {
        if ($this->getEncoding() === self::ENCODING_BASE64) {
            return unserialize(base64_decode($data));

        } else {
            return unserialize($data);

        }
    }
172679
public function decode($data)
    {
        $decoded = parent::decode($data);

        return 1 === count($decoded) ? reset($decoded) : 0;
    }
118475
public static function decodeChunkedBody($body)
    {
        $decBody = '';
        
        while (trim($body)) {
            if (! preg_match("/^([\da-fA-F]+)[^\r\n]*\r\n/sm", $body, $m)) {
                require_once 'Zend/Http/Exception.php';
                throw new Zend_Http_Exception("Error parsing body - doesn't seem to be a chunked message");
            }

            $length = hexdec(trim($m[1]));
            $cut = strlen($m[0]);

            $decBody .= substr($body, $cut, $length);
            $body = substr($body, $cut + $length + 2);
        }

        return $decBody;
    }
583750
private function getQSEncode($data)
    {
        $req = null;
        foreach ($data as $key => $value) {
            $req .= $key . '=' . urlencode(stripslashes($value)) . '&';
        }

        // cut the last '&'
        $req = substr($req, 0, strlen($req) - 1);

        return $req;
    }
980921
public function decrypt($data){
        $data = base64_decode($data);
        $maxlength = $this->getMaxDecryptBlockSize($this->privateKey);
        $output='';
        while(strlen($data)){
            $input = substr($data, 0, $maxlength);
            $data = substr($data, $maxlength);
            openssl_private_decrypt($input, $out, $this->privateKey);
            $output .= $out;
        }
        return $output;
    }
667337
public function data($data)
    {
        if($this->state == Connection::READ_HEADERS) {
            if(!$this->start_time) $this->start_time = microtime(true);
            $this->header_buffer .= $data;

            $end = strpos($this->header_buffer, "\r\n\r\n", 4);
            if($end === false) return;

            // parse HTTP request
            $this->parse_request(substr($this->header_buffer, 0, $end));

            // TODO: check for Transfer-Encoding: chunked

            $this->content_length = (int)$this->get_header('Content-Length');

            $body_start = $end + 4;
            $data = substr($this->header_buffer, $body_start);

            $this->state = Connection::READ_CONTENT;
        }

        if($this->state == Connection::READ_CONTENT) {
            if($this->is_chunked) {
                // TODO: Handle chunked data
            } else {
                fwrite($this->body, $data);
                $this->bytes_read += strlen($data);

                if($this->content_length - $this->bytes_read <= 0) $this->state = Connection::READ_COMPLETE;
            }
        }

        if($this->state == Connection::READ_COMPLETE) fseek($this->body, 0);
    }