Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Serving Gzip Zlib

From Leeroopedia
Knowledge Sources
Domains Compression, HTTP
Last Updated 2026-02-13 00:00 GMT

Overview

A C++ wrapper around the zlib library providing gzip compression and decompression with support for both one-shot and chunked (streaming) modes, including gzip header parsing and footer validation.

Description

The file provides two main classes: GZipHeader and ZLib. GZipHeader implements a state-machine parser for the gzip header format (RFC 1952), processing bytes incrementally and returning INCOMPLETE_HEADER, COMPLETE_HEADER, or INVALID_HEADER status. It handles all optional header fields including FEXTRA, FNAME, FCOMMENT, and FHCRC. The ZLib class wraps zlib's deflate/inflate APIs and adds gzip envelope handling. It supports one-shot compression/decompression (Compress/Uncompress), chunked streaming (CompressAtMost/UncompressAtMost with CompressChunkDone/UncompressChunkDone), and automatic allocation-based decompression (UncompressGzipAndAllocate). The class manages compression settings (level, window bits, memory level), CRC32 checksums, and gzip footer verification. It limits uncompressed data to 100MB by default (kMaxUncompressedBytes) as a safety measure. Internal state is reusable across multiple operations via Reset() (preserves settings) or Reinit() (full re-initialization).

Usage

Use this for compressing HTTP responses or decompressing gzip-encoded HTTP request bodies in the TensorFlow Serving HTTP server. It is used by EvHTTPRequest to automatically decompress incoming gzip content.

Code Reference

Source Location

  • Repository: Tensorflow_Serving
  • File: tensorflow_serving/util/net_http/compression/gzip_zlib.h (header), tensorflow_serving/util/net_http/compression/gzip_zlib.cc (implementation)
  • Lines: 1-338 (header), 1-821 (implementation)

Signature

class GZipHeader {
 public:
  enum Status { INCOMPLETE_HEADER, COMPLETE_HEADER, INVALID_HEADER };
  void Reset();
  Status ReadMore(const char* inbuf, int inbuf_len, const char** header_end);
};

class ZLib {
 public:
  static constexpr int64_t kMaxUncompressedBytes = 100 * 1024 * 1024;

  void Reinit();
  void Reset();
  void SetCompressionLevel(int level);

  int Compress(Bytef* dest, uLongf* destLen, const Bytef* source, uLong sourceLen);
  int Uncompress(Bytef* dest, uLongf* destLen, const Bytef* source, uLong sourceLen);
  int UncompressGzipAndAllocate(Bytef** dest, uLongf* destLen,
                                const Bytef* source, uLong sourceLen);

  int CompressAtMost(Bytef* dest, uLongf* destLen, const Bytef* source, uLong* sourceLen);
  int CompressChunkDone(Bytef* dest, uLongf* destLen);
  int UncompressAtMost(Bytef* dest, uLongf* destLen, const Bytef* source, uLong* sourceLen);
  bool UncompressChunkDone();

  static bool HasGzipHeader(const char* source, int sourceLen);
  bool IsGzipFooterValid() const;
};

Import

#include "tensorflow_serving/util/net_http/compression/gzip_zlib.h"

I/O Contract

Inputs

Name Type Required Description
source const Bytef* Yes Source data buffer to compress or decompress
sourceLen uLong Yes Length of the source data
dest Bytef* Yes Destination buffer for the output
destLen uLongf* Yes Capacity of the destination buffer; updated with actual output size

Outputs

Name Type Description
return int Z_OK on success, Z_BUF_ERROR if buffer too small, Z_MEM_ERROR on memory failure, Z_DATA_ERROR on corrupt data
destLen uLongf* Updated to reflect the actual number of bytes written

Usage Examples

One-Shot Compression

ZLib zlib;
const std::string input = "data to compress";
uLongf dest_len = ZLib::MinCompressbufSize(input.size());
std::vector<Bytef> compressed(dest_len);

int err = zlib.Compress(compressed.data(), &dest_len,
                        reinterpret_cast<const Bytef*>(input.data()),
                        input.size());
if (err == Z_OK) {
  compressed.resize(dest_len);
}

Decompressing Gzip Data with Auto-Allocation

ZLib zlib;
Bytef* uncompressed = nullptr;
uLongf uncompressed_len = ZLib::kMaxUncompressedBytes;

int err = zlib.UncompressGzipAndAllocate(
    &uncompressed, &uncompressed_len,
    compressed_data, compressed_size);

if (err == Z_OK && uncompressed != nullptr) {
  // Use uncompressed data
  std::allocator<Bytef>().deallocate(uncompressed, uncompressed_len);
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment