zlib compression for C++ macros. More...
| int | compressText(string src, int level) |
| string | uncompressText(int maxOut) |
| z_stream | z_stream |
| int | deflateInit(z_stream *strm, int level) |
| int | deflate(z_stream *strm, int flush) |
| int | deflateEnd(z_stream *strm) |
| int | inflateInit(z_stream *strm) |
| int | inflate(z_stream *strm, int flush) |
| int | inflateEnd(z_stream *strm) |
| int | Z_OK |
| int | Z_STREAM_END |
| int | Z_NEED_DICT |
| int | Z_ERRNO |
| int | Z_STREAM_ERROR |
| int | Z_DATA_ERROR |
| int | Z_MEM_ERROR |
| int | Z_BUF_ERROR |
| int | Z_VERSION_ERROR |
| int | Z_NO_FLUSH |
| int | Z_PARTIAL_FLUSH |
| int | Z_SYNC_FLUSH |
| int | Z_FULL_FLUSH |
| int | Z_FINISH |
| int | Z_NO_COMPRESSION |
| int | Z_BEST_SPEED |
| int | Z_BEST_COMPRESSION |
| int | Z_DEFAULT_COMPRESSION |
| int | Z_DEFAULT_STRATEGY |
| int | Z_NULL |
zlib compression for C++ macros. Load with #include <zlib.h> (also accepts <zconf.h>). Available from LayoutEditor 20260918.
The plugin links zlib. LayoutEditor itself is not linked against zlib for macros.
Compression means: take a block of bytes and store them in fewer bytes (a zip-like format). Decompression (inflate) restores the original bytes. Typical uses are smaller files, smaller network payloads, or packing a large string.
For text in a macro, use the helpers compressText and uncompressText. They keep the compressed bytes inside the plugin. The raw stream fields z_stream.next_in / next_out cannot hold a LayoutEditor string pointer across calls (the host string buffer is cleared after each invoke).
The stream functions (deflate / inflate) are the native zlib API. They are for advanced macros that already manage memory buffers. Beginners should stay with the helpers.
#include <zlib.h>
int main(){
string src = "";
int i;
for (i = 0; i < 24; ++i)
src = src + "The LayoutEditor macro language can call native zlib. ";
int inSize = src.length();
int compressedSize = compressText(src, Z_DEFAULT_COMPRESSION);
if (compressedSize < 0){
cout("compressText failed\n");
return 1;
}
string restored = uncompressText(inSize);
cout("input: ", inSize, " compressed: ", compressedSize, "\n");
if (restored == src)
cout("round-trip: ok\n");
}
Python: import zlib. This include is for C++ macros only.
Compresses the UTF-8 bytes of src and stores the result in the plugin. The next uncompressText call reads that stored buffer.
Parameters:
src (string) — the original text (or any byte string).level (int) — how hard to squeeze:
Z_DEFAULT_COMPRESSION — usual choice (−1, zlib picks a default)Z_NO_COMPRESSION (0) — store without compressingZ_BEST_SPEED (1) — fastest, larger resultZ_BEST_COMPRESSION (9) — smallest result, slowerReturns: int — size of the compressed data in bytes, or −1 on error. A smaller number than src.length() means compression helped (repetitive text compresses well).
Call uncompressText afterwards in the same macro run. The stored blob is per thread; a second compressText overwrites it.
Inflates the last compressText result back into a string.
Parameters:
maxOut (int) — maximum number of bytes in the restored text. Pass at least the original length (src.length()). If you pass less than 1, 1 is used. If the buffer is too small, inflation fails and you get an empty string.Returns: string — the restored text. Empty string on error (wrong data, maxOut too small, or no previous compressText).
A z_stream is a structure zlib uses while it compresses or inflates in chunks. You declare z_stream strm;, call deflateInit / inflateInit, then deflate / inflate until the data is finished, then deflateEnd / inflateEnd.
Because next_in / next_out are raw pointers, they must point at memory that still exists for the whole sequence of calls. A temporary LayoutEditor string does not stay valid. Prefer compressText unless you know how to keep a buffer alive.
Declare z_stream s;. Fields you can read and write:
| Field | Meaning |
|---|---|
next_in |
pointer to the next input byte |
avail_in |
how many input bytes remain |
next_out |
pointer to the next output byte |
avail_out |
how many output bytes still fit |
zalloc |
allocator (usually leave as 0 / Z_NULL) |
zfree |
free function (usually Z_NULL) |
opaque |
extra pointer for the allocator (usually Z_NULL) |
Prepares strm for compression. Call this once before deflate.
Parameters:
strm (z_stream *) — address of your stream (&s).level (int) — same values as compressText (Z_DEFAULT_COMPRESSION, 0 … 9).Returns: Z_OK on success, or a negative error code (Z_MEM_ERROR, Z_STREAM_ERROR, …).
Compresses more input into the output buffer. Update next_in / avail_in / next_out / avail_out before each call.
Parameters:
strm (z_stream *).flush (int) — usually Z_NO_FLUSH while more input remains, and Z_FINISH when all input has been given.Returns: Z_OK if more work remains, Z_STREAM_END when everything is flushed, or a negative error code.
Frees zlib’s internal state for this stream. Always call this after deflateInit, even on error.
Parameters:
strm (z_stream *).Returns: Z_OK on success, or an error code.
Prepares strm for decompression.
Parameters:
strm (z_stream *) — &s.Returns: Z_OK on success, or a negative error code.
Decompresses more input into the output buffer.
Parameters:
strm (z_stream *).flush (int) — typically Z_NO_FLUSH or Z_FINISH.Returns: Z_OK, Z_STREAM_END when the compressed stream is complete, or a negative error code (Z_DATA_ERROR if the bytes are not valid zlib data).
Frees zlib’s internal inflate state. Always pair with inflateInit.
Parameters:
strm (z_stream *).Returns: Z_OK on success, or an error code.
Returns: the named constant. success; more work may remain
Returns: the named constant. the stream is finished
Returns: the named constant. a preset dictionary is required (rare)
Returns: the named constant. file / system error
Returns: the named constant. the z_stream was used incorrectly
Returns: the named constant. the compressed data is damaged or not zlib
Returns: the named constant. not enough memory
Returns: the named constant. the output buffer is full (not always fatal)
Returns: the named constant. zlib version mismatch
Returns: the named constant. compress as much as convenient
Returns: the named constant. flush some output
Returns: the named constant. flush so a decompressor can catch up
Returns: the named constant. reset compression state (larger output)
Returns: the named constant. no more input; emit everything
Returns: the named constant. 0 — store only
Returns: the named constant. 1
Returns: the named constant. 9
Returns: the named constant. default level
Returns: the named constant. default algorithm strategy
Returns: the named constant. empty pointer (0)