-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZLibCommon.pas
358 lines (289 loc) · 13.2 KB
/
ZLibCommon.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
zlib bindings - common types, constants and functions
These units provides plain (no wrappers or helpers) bindings for zlib
library. Most comments were copied directly from zlib.h header without
any change.
This binding is distributed with all necessary binaries (object files,
DLLs) precompiled. For details please refer to file bin_readme.txt.
Version 1.1.7 (2024-10-14)
Build against zlib version 1.3.1
Last change 2024-10-14
©2017-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Bnd.ZLib
Dependencies:
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
DynLibUtils - github.com/TheLazyTomcat/Lib.DynLibUtils
Library AuxExceptions is required only when rebasing local exception classes
(see symbol ZLib_UseAuxExceptions for details).
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
InterlockedOps - github.com/TheLazyTomcat/Lib.InterlockedOps
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WindowsVersion - github.com/TheLazyTomcat/Lib.WindowsVersion
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit ZLibCommon;
{$INCLUDE '.\ZLib_defs.inc'}
interface
uses
SysUtils,
AuxTypes{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Basic constants and types
===============================================================================}
const
{$IFDEF Windows}
LibName = 'zlib1.dll';
{$ELSE}
LibName = 'libz.so.1.3.1';
{$ENDIF}
type
int = Int32; pint = ^int;
off64_t = Int64;
size_t = PtrUInt;
uInt = UInt32; puInt = ^uInt;
{$IF Defined(Linux) and Defined(x64)}
off_t = Int64;
long = Int64;
uLong = UInt64; puLong = ^uLong;
{$ELSE}
off_t = Int32;
long = Int32;
uLong = UInt32; puLong = ^uLong;
{$IFEND}
unsigned = UInt32; punsigned = ^unsigned;
PPByte = ^PByte;
{===============================================================================
Zlib constants and types
===============================================================================}
const
z_errmsg: array[0..9] of PAnsiChar = (
'need dictionary', // Z_NEED_DICT 2
'stream end', // Z_STREAM_END 1
'', // Z_OK 0
'file error', // Z_ERRNO (-1)
'stream error', // Z_STREAM_ERROR (-2)
'data error', // Z_DATA_ERROR (-3)
'insufficient memory', // Z_MEM_ERROR (-4)
'buffer error', // Z_BUF_ERROR (-5)
'incompatible version', // Z_VERSION_ERROR (-6)
'');
type
z_size_t = size_t;
z_off_t = off_t;
z_off64_t = off64_t;
z_crc_t = UInt32; pz_crc_t = ^z_crc_t;
const
MAX_MEM_LEVEL = 9;
MAX_WBITS = 15;
DEF_MEM_LEVEL = 8;
DEF_WBITS = MAX_WBITS;
SEEK_SET = 0; (* Seek from beginning of file. *)
SEEK_CUR = 1; (* Seek from current position. *)
SEEK_END = 2; (* Set file pointer to EOF plus "offset" *)
WBITS_RAW = -15;
WBITS_ZLIB = 15;
WBITS_GZIP = 31;
const
ZLIB_VERSION = AnsiString('1.3.1');
ZLIB_VERNUM = $1310;
ZLIB_VER_MAJOR = 1;
ZLIB_VER_MINOR = 3;
ZLIB_VER_REVISION = 1;
ZLIB_VER_SUBREVISION = 0;
(*
The 'zlib' compression library provides in-memory compression and
decompression functions, including integrity checks of the uncompressed data.
This version of the library supports only one compression method (deflation)
but other algorithms will be added later and will have the same stream
interface.
Compression can be done in a single step if the buffers are large enough,
or can be done by repeated calls of the compression function. In the latter
case, the application must provide more input and/or consume the output
(providing more output space) before each call.
The compressed data format used by default by the in-memory functions is
the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
around a deflate stream, which is itself documented in RFC 1951.
The library also supports reading and writing files in gzip (.gz) format
with an interface similar to that of stdio using the functions that start
with "gz". The gzip format is different from the zlib format. gzip is a
gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
This library can optionally read and write gzip and raw deflate streams in
memory as well.
The zlib format was designed to be compact and fast for use in memory
and on communications channels. The gzip format was designed for single-
file compression on file systems, has a larger header than zlib to maintain
directory information, and uses a different, slower check method than zlib.
The library does not install any signal handler. The decoder checks
the consistency of the compressed data, so the library should never crash
even in the case of corrupted input.
*)
type
alloc_func = Function(opaque: Pointer; items, size: uInt): Pointer; cdecl;
free_func = procedure(opaque, address: Pointer); cdecl;
internal_state = record end;
z_stream_s = record
next_in: PByte; (* next input byte *)
avail_in: uInt; (* number of bytes available at next_in *)
total_in: uLong; (* total number of input bytes read so far *)
next_out: PByte; (* next output byte will go here *)
avail_out: uInt; (* remaining free space at next_out *)
total_out: uLong; (* total number of bytes output so far *)
msg: PAnsiChar; (* last error message, NULL if no error *)
state: ^internal_state; (* not visible by applications *)
zalloc: alloc_func; (* used to allocate the internal state *)
zfree: free_func; (* used to free the internal state *)
opaque: Pointer; (* private data object passed to zalloc and zfree *)
data_type: int; (* best guess about the data type: binary or text
for deflate, or the decoding state for inflate *)
adler: uLong; (* Adler-32 or CRC-32 value of the uncompressed data *)
reserved: uLong; (* reserved for future use *)
end;
z_stream = z_stream_s;
z_streamp = ^z_stream_s;
(*
gzip header information passed to and from zlib routines. See RFC 1952
for more details on the meanings of these fields.
*)
gz_header_s = record
text: int; (* true if compressed data believed to be text *)
time: uLong; (* modification time *)
xflags: int; (* extra flags (not used when writing a gzip file) *)
os: int; (* operating system *)
extra: PByte; (* pointer to extra field or Z_NULL if none *)
extra_len: uInt; (* extra field length (valid if extra != Z_NULL) *)
extra_max: uInt; (* space at extra (only when reading header) *)
name: PByte; (* pointer to zero-terminated file name or Z_NULL *)
name_max: uInt; (* space at name (only when reading header) *)
comment: PByte; (* pointer to zero-terminated comment or Z_NULL *)
comm_max: uInt; (* space at comment (only when reading header) *)
hcrc: int; (* true if there was or will be a header crc *)
done: int; (* true when done reading gzip header (not used
when writing a gzip file) *)
end;
gz_header = gz_header_s;
gz_headerp = ^gz_header;
(*
The application must update next_in and avail_in when avail_in has dropped
to zero. It must update next_out and avail_out when avail_out has dropped
to zero. The application must initialize zalloc, zfree and opaque before
calling the init function. All other fields are set by the compression
library and must not be updated by the application.
The opaque value provided by the application will be passed as the first
parameter for calls of zalloc and zfree. This can be useful for custom
memory management. The compression library attaches no meaning to the
opaque value.
zalloc must return Z_NULL if there is not enough memory for the object.
If zlib is used in a multi-threaded application, zalloc and zfree must be
thread safe. In that case, zlib is thread-safe. When zalloc and zfree are
Z_NULL on entry to the initialization function, they are set to internal
routines that use the standard library functions malloc() and free().
On 16-bit systems, the functions zalloc and zfree must be able to allocate
exactly 65536 bytes, but will not be required to allocate more than this if
the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers
returned by zalloc for objects of exactly 65536 bytes *must* have their
offset normalized to zero. The default allocation function provided by this
library ensures this (see zutil.c). To reduce memory requirements and avoid
any allocation of 64K objects, at the expense of compression ratio, compile
the library with -DMAX_WBITS=14 (see zconf.h).
The fields total_in and total_out can be used for statistics or progress
reports. After compression, total_in holds the total size of the
uncompressed data and may be saved for use by the decompressor (particularly
if the decompressor wants to decompress everything in a single step).
*)
(* constants *)
const
Z_NO_FLUSH = 0;
Z_PARTIAL_FLUSH = 1;
Z_SYNC_FLUSH = 2;
Z_FULL_FLUSH = 3;
Z_FINISH = 4;
Z_BLOCK = 5;
Z_TREES = 6;
(* Allowed flush values; see deflate() and inflate() below for details *)
Z_OK = 0;
Z_STREAM_END = 1;
Z_NEED_DICT = 2;
Z_ERRNO = -1;
Z_STREAM_ERROR = -2;
Z_DATA_ERROR = -3;
Z_MEM_ERROR = -4;
Z_BUF_ERROR = -5;
Z_VERSION_ERROR = -6;
(* Return codes for the compression/decompression functions. Negative values
* are errors, positive values are used for special but normal events.
*)
Z_NO_COMPRESSION = 0;
Z_BEST_SPEED = 1;
Z_BEST_COMPRESSION = 9;
Z_DEFAULT_COMPRESSION = -1;
(* compression levels *)
Z_FILTERED = 1;
Z_HUFFMAN_ONLY = 2;
Z_RLE = 3;
Z_FIXED = 4;
Z_DEFAULT_STRATEGY = 0;
(* compression strategy; see deflateInit2() below for details *)
Z_BINARY = 0;
Z_TEXT = 1;
Z_ASCII = Z_TEXT; (* for compatibility with 1.2.2 and earlier *)
Z_UNKNOWN = 2;
(* Possible values of the data_type field for deflate() *)
Z_DEFLATED = 8;
(* The deflate compression method (the only one supported in this version) *)
Z_NULL = nil; (* for initializing zalloc, zfree, opaque *)
type
in_func = Function(Ptr: Pointer; Buff: PPByte): unsigned; cdecl;
out_func = Function(Ptr: Pointer; Buff: PByte; Len: unsigned): int; cdecl;
gzFile_s = record
have: unsigned;
next: PByte;
pos: z_off64_t;
end;
gzFile = ^gzFile_s; (* semi-opaque gzip file descriptor *)
{===============================================================================
Auxiliary types and functions
===============================================================================}
type
EZLibException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
procedure CheckCompatibility(Flags: uLong);
implementation
procedure CheckCompatibility(Flags: uLong);
begin
// check sizes of integer types
Assert((Flags and 3) = 1,'uInt is not 32bit in size');
{$IF Defined(Linux) and Defined(x64)}
Assert(((Flags shr 2) and 3) = 2,'uLong is not 64bit in size');
Assert(((Flags shr 6) and 3) = 2,'z_off_t is not 64bit in size');
{$ELSE}
Assert(((Flags shr 2) and 3) = 1,'uLong is not 32bit in size');
Assert(((Flags shr 6) and 3) = 1,'z_off_t is not 32bit in size');
{$IFEND}
{$IFDEF x64}
Assert(((Flags shr 4) and 3) = 2,'voidpf is not 64bit in size');
{$ELSE}
Assert(((Flags shr 4) and 3) = 1,'voidpf is not 32bit in size');
{$ENDIF}
// check whether calling convention is *not* STDCALL (ie. it is CDECL))
Assert(((Flags shr 10) and 1) = 0,'incomatible calling convention');
// check if all funcionality is available
Assert(((Flags shr 16) and 1) = 0,'gz* functions cannot compress');
Assert(((Flags shr 17) and 1) = 0,'unable to write gzip stream');
end;
end.