src/site/php/zlib fix.php

Constants

ZLIB_ENCODING_RAW

ZLIB_ENCODING_RAW = -15

ZLIB_ENCODING_DEFLATE

ZLIB_ENCODING_DEFLATE = 15

ZLIB_ENCODING_GZIP

ZLIB_ENCODING_GZIP = 31

FORCE_DEFLATE

FORCE_DEFLATE = 15

FORCE_GZIP

FORCE_GZIP = 31

Functions

cfw_zlib()

cfw_zlib(string  $op, string  $data, int  $level = -1, string  $dict = '') : array{ok: bool, data?: string, error?: string}

Runs one op over the bridge.

Parameters

string $op

gzip, gunzip, zlib, unzlib, deflate or inflate.

string $data

The bytes to transform.

int $level

A zlib level, or -1 for the default.

string $dict

A preset dictionary, or an empty string for none.

Returns

array{ok: bool, data?: string, error?: string} —

['ok' => true, 'data' => string] or ['ok' => false, 'error' => string].

cfw_zlib_fail()

cfw_zlib_fail(string  $name, string  $reason) : false

Raises the diagnostic ext-zlib raises, then answers FALSE like it does.

Parameters

string $name

The PHP function that failed.

string $reason

What ext-zlib would have said.

Returns

false —

cfw_zlib_dict()

cfw_zlib_dict(string  $op, string  $data, string  $dict, int  $level = -1) : string|false

Compresses or decompresses against a preset dictionary.

NOT shaped like a gz* function: PHP has never had a dictionary parameter on gzcompress(), so widening one of those signatures would make a host-only argument look like part of the language. function_exists('cfw_zlib_dict') is the feature test a caller uses.

$op is 'zlib' to compress and 'unzlib' to decompress; the output is an ordinary zlib stream with FDICT set. gzip and the raw pair are refused, because neither has anywhere to record the dictionary's checksum -- so a wrong dictionary would decode to plausible garbage instead of failing.

Parameters

string $op

zlib or unzlib.

string $data

The bytes to transform.

string $dict

The preset dictionary.

int $level

A zlib level, or -1 for the default.

Returns

string|false —

The bytes, or FALSE with an E_USER_WARNING, matching the gz* functions.

cfw_zlib_installed()

cfw_zlib_installed() : bool

Returns

bool —

cfw_zlib_level()

cfw_zlib_level(string  $name, int  $level) : int

Refuses a level outside -1..9, with the ValueError ext-zlib throws.

Parameters

string $name

The PHP function being called.

int $level

The level the caller passed.

Returns

int —

The level, unchanged.

cfw_zlib_encoding()

cfw_zlib_encoding(string  $name, int  $encoding) : string

The container an encoding names.

All three encoders accept all three encodings and emit that container -- measured on 8.5.7, gzcompress(x, 9, ZLIB_ENCODING_GZIP) is byte-identical to gzencode(x, 9, FORCE_GZIP) -- so the three share one mapping.

Parameters

string $name

The PHP function being called.

int $encoding

A ZLIB_ENCODING_* value.

Returns

string —

cfw_zlib_cap()

cfw_zlib_cap(string  $name, string  $data, int  $max_length) : string|false

Applies $max_length the way zlib does, which is NOT a truncation.

Measured: gzuncompress(gzcompress('hello world'), 5) is FALSE, not 'hello'. zlib fails the inflate when its output buffer is too small, so a cap below the payload is a data error rather than a short read. 0 means no cap.

Parameters

string $name

The PHP function being called.

string $data

The inflated bytes.

int $max_length

The cap, or 0 for none.

Returns

string|false —

The bytes, or FALSE with an E_USER_WARNING when the cap was exceeded.

gzencode()

gzencode(string  $data, int  $level = -1, int  $encoding = 31) : string|false

Parameters

string $data

The bytes to compress.

int $level

A zlib level, or -1 for the default.

int $encoding

A ZLIB_ENCODING_* value.

Returns

string|false —

gzcompress()

gzcompress(string  $data, int  $level = -1, int  $encoding = 15) : string|false

Parameters

string $data

The bytes to compress.

int $level

A zlib level, or -1 for the default.

int $encoding

A ZLIB_ENCODING_* value.

Returns

string|false —

gzdeflate()

gzdeflate(string  $data, int  $level = -1, int  $encoding = -15) : string|false

Parameters

string $data

The bytes to compress.

int $level

A zlib level, or -1 for the default.

int $encoding

A ZLIB_ENCODING_* value.

Returns

string|false —

gzdecode()

gzdecode(string  $data, int  $max_length = 0) : string|false

Parameters

string $data

The gzip stream.

int $max_length

The cap on the inflated size, or 0 for none.

Returns

string|false —

gzuncompress()

gzuncompress(string  $data, int  $max_length = 0) : string|false

Parameters

string $data

The zlib stream.

int $max_length

The cap on the inflated size, or 0 for none.

Returns

string|false —

gzinflate()

gzinflate(string  $data, int  $max_length = 0) : string|false

Parameters

string $data

The raw deflate stream.

int $max_length

The cap on the inflated size, or 0 for none.

Returns

string|false —