WP_Http_Encoding::compatible_gzinflate() WordPress Method

The WP_Http_Encoding::compatible_gzinflate() method is used to decompress data encoded with gzip, just like the gzinflate() function. However, this method is more compatible with a wider range of servers and PHP versions.

WP_Http_Encoding::compatible_gzinflate( string $gz_data ) #

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 pragmatic way to determine the magic offset in use, see: https://core.trac.wordpress.org/ticket/18273


Top ↑

Parameters

$gz_data

(string)(Required)String to decompress.


Top ↑

Return

(string|false) Decompressed string on success, false on failure.


Top ↑

Source

File: wp-includes/class-wp-http-encoding.php

	public static function compatible_gzinflate( $gz_data ) {

		// Compressed data might contain a full header, if so strip it for gzinflate().
		if ( "\x1f\x8b\x08" === substr( $gz_data, 0, 3 ) ) {
			$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          = $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 = $i + 2;
				}
			}
			$decompressed = @gzinflate( substr( $gz_data, $i, -8 ) );
			if ( false !== $decompressed ) {
				return $decompressed;
			}
		}

		// Compressed data from java.util.zip.Deflater amongst others.
		$decompressed = @gzinflate( substr( $gz_data, 2 ) );
		if ( false !== $decompressed ) {
			return $decompressed;
		}

		return false;
	}


Top ↑

Changelog

Changelog
VersionDescription
2.8.1Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.