The ZLib module

introduction

ZLib is an abstraction of the DEFLATE compression algorithm. It can (losslessly) compress and decompress data. DEFLATE uses a combination of the LZ77 sliding window and Huffmann coding to achieve compression.

ZLib is a third party module, and as such, the finer details of the zlib implementation are sometimes not known. Thus, the documentation of the zlib module is at times sparse.

Supported formats

ZLib can compress data to and extract data from the zlib format. This format is somewhat a de facto standard.

API Documentation

The API Documentation is generated by Doxygen.

Memory usage

Deflation (compression)

Taken from zconf.h:

The memory requirements for deflate are (in bytes):
(1 << (windowBits+2)) + (1 << (memLevel+9))
that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) plus a few kilobytes for small objects.

Inflation (decompression)

Taken from zconf.h:

The memory requirements for inflate are (in bytes):
1 << windowBits
that is, 32K for windowBits=15 (default value) plus a few kilobytes for small objects.

Stack

ZLib has a load of static const int data on the stack. Most of it is static huffmann trees, lengths and distances.

Globals

ZLib has three OperaGlobals, m_static_l_desc, m_static_d_desc and m_static_bl_desc. All have the type static_tree_desc_s.

OOM

ZLib functions that may run out of memory will return Z_MEM_ERROR if an allocation fails. (Notable exception is updatewindow, that returns 1. However, ZLib functions that call updatewindow will still return Z_MEM_ERROR if this happens.) Handling OOM is left to caller.

Implementation

Both inflation and deflation is performed by first creating and initializing a z_streamp stream, and then call deflate/inflate until all data is processed. The caller is responsible for filling the input buffer with data and make sure there's room in the output buffer before each call. Both deflate and inflate will process as much data as possible, not stopping until the input buffer is empty or the output buffer is full.