wppaste
WordPress

Requests::compatible_gzinflate( string $gz_data ): string|bool

Since
1.6.0
Source
wp-includes/Requests/src/Requests.php:988
Decompression of deflated string while staying compatible with the majority of servers.

Description

Certain Servers will return deflated data with headers which PHP's gzinflate() function cannot handle out of the box. The following function has been created from various snippets on the gzinflate() PHP documentation.

Warning: Magic numbers within. Due to the potential different formats that the compressed data may be returned in, some "magic offsets" are needed to ensure proper decompression takes place. For a simple progmatic way to determine the magic offset in use, see: https://core.trac.wordpress.org/ticket/18273

Compatibility

WordPress
since 1.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$gz_datastring
String to decompress.

Return value

string|bool
False on failure.

Performance profile

How much work a call to Requests::compatible_gzinflate() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8–115

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 148.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 25 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost Requests::compatible_gzinflate() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8none
always12::InvalidArgument()
!$flg && $decompressed !== false27–42ord(), ::compatible_gzinflate()
$decompressed !== false37–44unpack(), unpack(), gzinflate()
always43–47unpack(), unpack(), unpack()
$flg && $decompressed !== false45–52ord(), unpack(), ::compatible_gzinflate()
$decompressed === false49–53unpack(), unpack(), gzinflate(), gzinflate()
$decompressed === false52–56unpack(), unpack(), gzinflate(), unpack()
!$flg52–74ord(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate()
$decompressed === false58–62unpack(), unpack(), gzinflate(), gzinflate(), gzinflate()
!$flg && $decompressed === false58–77ord(), ::compatible_gzinflate(), unpack(), unpack(), unpack()
always62–66unpack(), unpack(), unpack(), unpack(), array_sum(), gzinflate()
13 further outcomes, up to 115 instructions
!$flg && $decompressed === false64–83ord(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), gzinflate()
!$flg && $decompressed === false67–86ord(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), unpack()
$flg70–84ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate()
$decompressed === false71–75unpack(), unpack(), gzinflate(), unpack(), unpack(), array_sum(), gzinflate()
!$flg && $decompressed === false73–92ord(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), gzinflate(), gzinflate()
$flg && $decompressed === false76–87ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), unpack()
!$flg && $decompressed === false77–96ord(), ::compatible_gzinflate(), unpack(), unpack(), unpack(), unpack(), array_sum(), gzinflate()
$flg && $decompressed === false82–93ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), gzinflate()
$flg && $decompressed === false85–96ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), unpack()
!$flg && $decompressed === false86–105ord(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), unpack(), unpack(), array_sum(), gzinflate()
$flg && $decompressed === false91–102ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), gzinflate(), gzinflate()
$flg && $decompressed === false95–106ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), unpack(), unpack(), array_sum(), gzinflate()
$flg && $decompressed === false104–115ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), unpack(), unpack(), array_sum(), gzinflate()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1488–11518
8.51488–11518
8.41488–1151838 fewer instructions than PHP 8.3
8.318610–15018
8.218610–15018
8.118610–15018
7.418610–15018

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

  • \WpOrg\Requests\Exception\InvalidArgument::create()
  • \WpOrg\Requests\Requests::compatible_gzinflate()

Source code

	public static function compatible_gzinflate($gz_data) {		if (is_string($gz_data) === false) {			throw InvalidArgument::create(1, '$gz_data', 'string', gettype($gz_data));		} 		if (trim($gz_data) === '') {			return false;		} 		// Compressed data might contain a full zlib header, if so strip it for		// gzinflate()		if (substr($gz_data, 0, 3) === "\x1f\x8b\x08") {			$i   = 10;			$flg = ord(substr($gz_data, 3, 1));			if ($flg > 0) {				if ($flg & 4) {					list($xlen) = unpack('v', substr($gz_data, $i, 2));					$i         += 2 + $xlen;				} 				if ($flg & 8) {					$i = strpos($gz_data, "\0", $i) + 1;				} 				if ($flg & 16) {					$i = strpos($gz_data, "\0", $i) + 1;				} 				if ($flg & 2) {					$i += 2;				}			} 			$decompressed = self::compatible_gzinflate(substr($gz_data, $i));			if ($decompressed !== false) {				return $decompressed;			}		} 		// If the data is Huffman Encoded, we must first strip the leading 2		// byte Huffman marker for gzinflate()		// The response is Huffman coded by many compressors such as		// java.util.zip.Deflater, Ruby's Zlib::Deflate, and .NET's		// System.IO.Compression.DeflateStream.		//		// See https://decompres.blogspot.com/ for a quick explanation of this		// data type		$huffman_encoded = false; 		// low nibble of first byte should be 0x08		list(, $first_nibble) = unpack('h', $gz_data); 		// First 2 bytes should be divisible by 0x1F		list(, $first_two_bytes) = unpack('n', $gz_data); 		if ($first_nibble === 0x08 && ($first_two_bytes % 0x1F) === 0) {			$huffman_encoded = true;		} 		if ($huffman_encoded) {			$decompressed = @gzinflate(substr($gz_data, 2));			if ($decompressed !== false) {				return $decompressed;			}		} 		if (substr($gz_data, 0, 4) === "\x50\x4b\x03\x04") {			// ZIP file format header			// Offset 6: 2 bytes, General-purpose field			// Offset 26: 2 bytes, filename length			// Offset 28: 2 bytes, optional field length			// Offset 30: Filename field, followed by optional field, followed			// immediately by data			list(, $general_purpose_flag) = unpack('v', substr($gz_data, 6, 2)); 			// If the file has been compressed on the fly, 0x08 bit is set of			// the general purpose field. We can use this to differentiate			// between a compressed document, and a ZIP file			$zip_compressed_on_the_fly = ((0x08 & $general_purpose_flag) === 0x08);

Changelog

Introduced in 1.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/Requests/src/Requests.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.