Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Library:Zlib

From ReverseEngineering.wiki
Library
Developer Jean-loup Gailly and Mark Adler
Platforms any (portable C89; DLL zlib1.dll on Windows, libz.so / libz.dylib elsewhere)
Versions 0.71 (1995) to 1.3.2 (2026); develop branch v1.3.2.1 at commit 767c4c9
License Zlib
Website https://zlib.net/
On this wiki Facts · Code · Notes · Topics

Mark Adler and Jean-loup Gailly's general-purpose compression library (deflate/inflate, zlib and gzip wrappers, CRC-32 and Adler-32), statically linked into a large share of all native programs. Its constant tables survive in every build and name the exact version even when the copyright strings were stripped.

Overview

zlib implements DEFLATE compression (Format:DEFLATE) with three framings: the zlib stream (Format:Zlib stream, RFC 1950), gzip (Format:Gzip, RFC 1952) and raw deflate. Programs embed it to read Format:PNG (through Library:Libpng), ZIP/JAR/APK, PDF streams, HTTP Content-Encoding, Git objects, game archives (for example sector method 0x02 of Format:MPQ (MoPaQ), via Library:StormLib) and many save formats, so it is one of the libraries you most often meet unnamed in a statically linked binary. Forks and re-implementations (zlib-ng, Chromium's third_party/zlib, miniz, libdeflate) share the API or the formats but not always the tables below.

Identification

What survives in a build, from most to least reliable:

  1. Inflate tables (inftrees.c, inflate_table()): lbase[31], lext[31], dbase[32], dext[32] as unsigned short. They are needed at run time, so no linker can drop them. The last two entries of lext are changed by hand for every version (64 distinct pairs from 1.2.0 to the current develop branch) and give the exact version: see #Versions.
  2. Fixed-code table lenfix[512] (inffixed.h), included by inflate.c and infback.c, so up to two copies; each copy also matches its own first six entries at +0x400.
  3. CRC-32 table crc_table[256] (crc32.h) starting 00 00 00 00 96 30 07 77 2C 61 0E EE BA 51 09 99. Any reflected CRC-32 implementation has this table, so it alone does not prove zlib; builds with DYNAMIC_CRC_TABLE compute it at run time and have no copy.
  4. Deflate tables (trees.h, trees.c, deflate.c): _dist_code[512], _length_code[256], int extra_lbits[29] and the configuration_table[10] of (good, lazy, nice, chain, function pointer) rows per level. Present only if deflate is linked.
  5. Code constants: Adler-32's BASE 65521 (0xFFF1) and NMAX 5552 (0x15B0); compilers turn % 65521 into a multiply by 0x80078071 followed by a right shift of 15 (seen in MSVC x86/x64 and MinGW GCC builds). inflate_state.mode values 0x3F34-0x3F53 (16180-16211, since 1.2.9) appear as comparison immediates in inflate(); deflate's status uses 42, 57, 69, 73, 91, 103, 113 and 666.
  6. Strings: messages such as incorrect header check, invalid distance too far back, incorrect data check, the z_errmsg[] table (need dictionary, stream end, file error, stream error, data error, insufficient memory, buffer error, incompatible version) and the version string (1.3.1) that every inflateInit/deflateInit caller passes. The banners " inflate 1.3.1 Copyright 1995-2024 Mark Adler " and " deflate 1.3.1 Copyright 1995-2024 Jean-loup Gailly and Mark Adler " are present in GCC and clang builds but were missing from every MSVC-linked binary checked (see #Pitfalls).
  7. Exports (DLL builds): zlibVersion, inflateInit_, inflateInit2_, deflateInit_, deflateInit2_, inflate, deflate, crc32, adler32, compress2, uncompress, gzopen...

The wiki's zlibscan script searches a file for all of these tables in both byte orders and prints the version; export_symbols with subject Library:Zlib gives a Ghidra/IDA script that finds the signature facts in your binary.

Versions

The pair (lext[29], lext[30]) of inftrees.c, read at every commit of the official repository that touched the file (development versions written with the git tag prefix v): 1.2.0 (73, 194), 1.2.1 (76, 66), 1.2.2 (199, 198), 1.2.3 (201, 196), v1.2.3.4 (72, 200), 1.2.4 (64, 195), 1.2.5 (73, 195), 1.2.6 (203, 69), 1.2.7 (78, 68), 1.2.8 (72, 78), 1.2.9 (192, 79), 1.2.10 (192, 202), 1.2.11 (77, 202), 1.2.12 (199, 202), 1.2.13 (194, 65), 1.3 (198, 203), v1.3.0.1 (70, 200), 1.3.1 (203, 77), v1.3.1.2 (64, 204), 1.3.2 (199, 75), develop v1.3.2.1 (68, 193). The full table (including all development versions, which Chromium and others ship) is in the identification fact and in zlibscan. The other entries of the four tables have not changed since 1.2.0.

zlib 0.99 to 1.1.4 used a different inflate (cplens/cplext as 32-bit uInt arrays); there the last two cplext entries are 192, 192 (0.99-1.0.4) or 112, 112 (1.0.5-1.1.4). Seen in a real binary: a standalone FreeType 2.6.2 freetype.dll (MSVC x64) carries FreeType's bundled zlib 1.1.4 inflate, with the 31-entry cplens (3, 4, ... 258, 0, 0) directly followed by cplext ending 112, 112, and no inflate 1.1.4 Copyright banner. The 192, 192 form (0.99-1.0.4) is still unverified.

API overview

inflateInit(strm) and deflateInit(strm, level) are macros for inflateInit_(strm, ZLIB_VERSION, (int)sizeof(z_stream)) and deflateInit_(strm, level, ZLIB_VERSION, (int)sizeof(z_stream)); the *Init2_ forms add windowBits (8..15 zlib stream, -8..-15 raw deflate, +16 gzip, +32 on inflate for zlib-or-gzip auto-detection, 0 on inflate for the window size from the header), and deflate also method (8), memLevel (1-9, default 8) and strategy (0 default, 1 filtered, 2 Huffman only, 3 RLE, 4 fixed). Because the version string and the structure size are passed at every call site, a call to an unnamed function with a pointer to "1.x.y" and the immediate 0x38 (32-bit), 0x58 (Win64) or 0x70 (64-bit Unix) is an init call. One-shot helpers: compress2(dst, &dstLen, src, srcLen, level), uncompress, crc32(crc, buf, len) (start with 0), adler32(adler, buf, len) (start with 1).

Return codes and flush values are in the enum facts; zlibCompileFlags() reports the build's type sizes and options (Win64 MinGW build of 1.3.1: 0x65, i.e. 32-bit uInt and uLong, 64-bit pointers, 32-bit z_off_t).

Core data structures

  • z_stream (public, zlib.h): next_in, avail_in, total_in, next_out, avail_out, total_out, msg, state, zalloc, zfree, opaque, data_type, adler, reserved; 56 bytes on 32-bit, 88 on Win64 (LLP64, 32-bit uLong), 112 on 64-bit Unix (LP64).
  • struct inflate_state and deflate_state (private) both start with a back pointer strm to the owning z_stream, followed by the mode (inflate) or status (deflate) integer. Since 1.2.9 every API call checks state->strm == strm and that the mode is in range before doing anything (inflateStateCheck, deflateStateCheck), so a recovered state pointer can be confirmed by following it back.

See also

Code & repositories

  • {{Repo|url=https://github.com/madler/zlib%7Ccommit=767c4c947852e143f582c85f14cf573411df1b35%7Clicense=Zlib (LICENSE file; GitHub's detector reports none)|description=The official zlib source (develop branch, after release 1.3.2): inflate/deflate, the gzip file functions, CRC-32 and Adler-32, plus contrib/ (minizip, infback9 for Deflate64, puff, a small reference inflater). Release tags v1.0.x to v1.3.2 let you diff any version that turns up in a binary.|tested_on=Read at this commit and at every release tag (inftrees.c lext pairs, inflate.h modes, deflate.h states, zlib.h API); behaviour checked with Python 3.11 (zlib 1.3.2) and ctypes calls into a MinGW Win64 zlib1.dll 1.3.1; not built (no C compiler on the test machine).|checked=checked 2026-10-17; last activity 2026-09-21; Note: the latest release ships binaries; the wiki links only the source, never the binaries}}
  • https://github.com/zlib-ng/zlib-ng @ 1727258b764262c73149014cdf6a39bd551b3bd5 · license: Zlib
    zlib-ng source: zlib-ng.h.in and zlib.h.in carry the version macros, inftrees.c the unchanged inflate tables; arch/ has the SIMD code.
    Tested on: source read, not run: source read at the pinned commit; nothing built or run · checked 2026-10-18; last activity 2026-10-17; Note: the latest release ships binaries; the wiki links only the source, never the binaries

Pitfalls

  • The copyright banners are global const char[] arrays that nothing references. Every MSVC-linked DLL checked (python311.dll and python312.dll of CPython for Windows, TortoiseGit's zlib DLLs, linker 14.4x) lacked them, while MinGW GCC, MSYS2 GCC and clang/lld (node.exe) builds kept them. Unverified explanation: MSVC's /OPT:REF discards the unreferenced data when it is placed in its own COMDAT (/Gw). Use the lext pair instead of searching for Copyright.
  • One binary can contain several zlibs: node.exe v24.13.0 (Windows x64) carries zlib 1.3.1 and Chromium's development version v1.3.0.1 (version string with the suffix -motley), each with its own tables.
  • deflateInit2 silently turns windowBits 8 into 9 for zlib streams ("until 256-byte window bug fixed"), so a stream requested with an 8-bit window starts with 0x18, not 0x08; raw deflate with 8 is rejected.
  • Library:FreeType compiles its own inflate-only zlib copy (src/gzip) unless built with FT_CONFIG_OPTION_SYSTEM_ZLIB: zlib 1.1.4 up to FreeType 2.11.1, 1.2.11 to 1.3.1 from 2.12.0 (see Fact:FreeType/behavior/Bundled zlib inflate copy). An old 1.1.4-style inflate (cplext ending 112, 112) inside a modern program is most likely FreeType's copy, not the program's zlib. When searching for the 1.1.4 cplext, check its last two entries: its first 29 uInt values (0 x8, 1 x4, ... 5 x4, 0) are also deflate's extra_lbits[29] in every zlib version.
  • zlib-ng, libdeflate and miniz are different code: do not expect lext, lenfix or the copyright strings there (zlib-ng in compat mode still exports the zlib API names).

Open questions

Evidence & references

  • Source read at https://github.com/madler/zlib commit 767c4c947852e143f582c85f14cf573411df1b35 (develop branch) and at every release tag; version pairs collected with git show <commit>:inftrees.c over git log --all -- inftrees.c.
  • Runtime checks with Python 3.11 (zlib 1.3.2) and ctypes on MinGW zlib1.dll 1.3.1 (Win64), and table scans of the DLLs named above.