Requests::compatible_gzinflate( string $gz_data ): string|bool
- Since
- 1.6.0
- Source
wp-includes/Requests/src/Requests.php:988
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
- Scaling
- Constant
- Instructions
- 8–115
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 148.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8 | none |
| always | 12 | ::InvalidArgument() |
!$flg && $decompressed !== false | 27–42 | ord(), ::compatible_gzinflate() |
$decompressed !== false | 37–44 | unpack(), unpack(), gzinflate() |
| always | 43–47 | unpack(), unpack(), unpack() |
$flg && $decompressed !== false | 45–52 | ord(), unpack(), ::compatible_gzinflate() |
$decompressed === false | 49–53 | unpack(), unpack(), gzinflate(), gzinflate() |
$decompressed === false | 52–56 | unpack(), unpack(), gzinflate(), unpack() |
!$flg | 52–74 | ord(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate() |
$decompressed === false | 58–62 | unpack(), unpack(), gzinflate(), gzinflate(), gzinflate() |
!$flg && $decompressed === false | 58–77 | ord(), ::compatible_gzinflate(), unpack(), unpack(), unpack() |
| always | 62–66 | unpack(), unpack(), unpack(), unpack(), array_sum(), gzinflate() |
13 further outcomes, up to 115 instructions
!$flg && $decompressed === false | 64–83 | ord(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), gzinflate() |
!$flg && $decompressed === false | 67–86 | ord(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), unpack() |
$flg | 70–84 | ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate() |
$decompressed === false | 71–75 | unpack(), unpack(), gzinflate(), unpack(), unpack(), array_sum(), gzinflate() |
!$flg && $decompressed === false | 73–92 | ord(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), gzinflate(), gzinflate() |
$flg && $decompressed === false | 76–87 | ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), unpack() |
!$flg && $decompressed === false | 77–96 | ord(), ::compatible_gzinflate(), unpack(), unpack(), unpack(), unpack(), array_sum(), gzinflate() |
$flg && $decompressed === false | 82–93 | ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), gzinflate() |
$flg && $decompressed === false | 85–96 | ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), unpack() |
!$flg && $decompressed === false | 86–105 | ord(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), unpack(), unpack(), array_sum(), gzinflate() |
$flg && $decompressed === false | 91–102 | ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), gzinflate(), gzinflate() |
$flg && $decompressed === false | 95–106 | ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), unpack(), unpack(), array_sum(), gzinflate() |
$flg && $decompressed === false | 104–115 | ord(), unpack(), ::compatible_gzinflate(), unpack(), unpack(), gzinflate(), unpack(), unpack(), array_sum(), gzinflate() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 148 | 8–115 | 18 | |
| 8.5 | 148 | 8–115 | 18 | |
| 8.4 | 148 | 8–115 | 18 | 38 fewer instructions than PHP 8.3 |
| 8.3 | 186 | 10–150 | 18 | |
| 8.2 | 186 | 10–150 | 18 | |
| 8.1 | 186 | 10–150 | 18 | |
| 7.4 | 186 | 10–150 | 18 |
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.