/[pngwrite]/trunk/zlib.pas
ViewVC logotype

Contents of /trunk/zlib.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Sun Apr 13 19:38:04 2008 UTC (9 years, 8 months ago) by plugwash
File size: 18002 byte(s)
initial import
1 Unit Zlib;
2
3
4 { Original:
5 zlib.h -- interface of the 'zlib' general purpose compression library
6 version 1.1.0, Feb 24th, 1998
7
8 Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
9
10 This software is provided 'as-is', without any express or implied
11 warranty. In no event will the authors be held liable for any damages
12 arising from the use of this software.
13
14 Permission is granted to anyone to use this software for any purpose,
15 including commercial applications, and to alter it and redistribute it
16 freely, subject to the following restrictions:
17
18 1. The origin of this software must not be misrepresented; you must not
19 claim that you wrote the original software. If you use this software
20 in a product, an acknowledgment in the product documentation would be
21 appreciated but is not required.
22 2. Altered source versions must be plainly marked as such, and must not be
23 misrepresented as being the original software.
24 3. This notice may not be removed or altered from any source distribution.
25
26 Jean-loup Gailly Mark Adler
27 jloup@gzip.org madler@alumni.caltech.edu
28
29
30 The data format used by the zlib library is described by RFCs (Request for
31 Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
32 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
33
34
35 Pascal tranlastion
36 Copyright (C) 1998 by Jacques Nomssi Nzali
37 For conditions of distribution and use, see copyright notice in readme.txt
38 }
39
40 interface
41
42 {$I zconf.inc}
43
44 uses
45 zutil;
46
47 { zconf.h -- configuration of the zlib compression library }
48 { zutil.c -- target dependent utility functions for the compression library }
49
50 { The 'zlib' compression library provides in-memory compression and
51 decompression functions, including integrity checks of the uncompressed
52 data. This version of the library supports only one compression method
53 (deflation) but other algorithms will be added later and will have the same
54 stream interface.
55
56 Compression can be done in a single step if the buffers are large
57 enough (for example if an input file is mmap'ed), or can be done by
58 repeated calls of the compression function. In the latter case, the
59 application must provide more input and/or consume the output
60 (providing more output space) before each call.
61
62 The library also supports reading and writing files in gzip (.gz) format
63 with an interface similar to that of stdio.
64
65 The library does not install any signal handler. The decoder checks
66 the consistency of the compressed data, so the library should never
67 crash even in case of corrupted input. }
68
69
70
71 { Compile with -DMAXSEG_64K if the alloc function cannot allocate more
72 than 64k bytes at a time (needed on systems with 16-bit int). }
73
74 { Maximum value for memLevel in deflateInit2 }
75 {$ifdef MAXSEG_64K}
76 {$IFDEF VER70}
77 const
78 MAX_MEM_LEVEL = 7;
79 DEF_MEM_LEVEL = MAX_MEM_LEVEL; { default memLevel }
80 {$ELSE}
81 const
82 MAX_MEM_LEVEL = 8;
83 DEF_MEM_LEVEL = MAX_MEM_LEVEL; { default memLevel }
84 {$ENDIF}
85 {$else}
86 const
87 MAX_MEM_LEVEL = 9;
88 DEF_MEM_LEVEL = 8; { if MAX_MEM_LEVEL > 8 }
89 {$endif}
90
91 { Maximum value for windowBits in deflateInit2 and inflateInit2 }
92 const
93 {$IFDEF VER70}
94 MAX_WBITS = 14; { 32K LZ77 window }
95 {$ELSE}
96 MAX_WBITS = 15; { 32K LZ77 window }
97 {$ENDIF}
98
99 { default windowBits for decompression. MAX_WBITS is for compression only }
100 const
101 DEF_WBITS = MAX_WBITS;
102
103 { The memory requirements for deflate are (in bytes):
104 1 shl (windowBits+2) + 1 shl (memLevel+9)
105 that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
106 plus a few kilobytes for small objects. For example, if you want to reduce
107 the default memory requirements from 256K to 128K, compile with
108 DMAX_WBITS=14 DMAX_MEM_LEVEL=7
109 Of course this will generally degrade compression (there's no free lunch).
110
111 The memory requirements for inflate are (in bytes) 1 shl windowBits
112 that is, 32K for windowBits=15 (default value) plus a few kilobytes
113 for small objects. }
114
115
116 { Huffman code lookup table entry--this entry is four bytes for machines
117 that have 16-bit pointers (e.g. PC's in the small or medium model). }
118
119 type
120 pInflate_huft = ^inflate_huft;
121 inflate_huft = Record
122 Exop, { number of extra bits or operation }
123 bits : Byte; { number of bits in this code or subcode }
124 {pad : uInt;} { pad structure to a power of 2 (4 bytes for }
125 { 16-bit, 8 bytes for 32-bit int's) }
126 base : uInt; { literal, length base, or distance base }
127 { or table offset }
128 End;
129
130 type
131 huft_field = Array[0..(MaxMemBlock div SizeOf(inflate_huft))-1] of inflate_huft;
132 huft_ptr = ^huft_field;
133 type
134 ppInflate_huft = ^pInflate_huft;
135
136 type
137 inflate_codes_mode = ( { waiting for "i:"=input, "o:"=output, "x:"=nothing }
138 START, { x: set up for LEN }
139 LEN, { i: get length/literal/eob next }
140 LENEXT, { i: getting length extra (have base) }
141 DIST, { i: get distance next }
142 DISTEXT, { i: getting distance extra }
143 COPY, { o: copying bytes in window, waiting for space }
144 LIT, { o: got literal, waiting for output space }
145 WASH, { o: got eob, possibly still output waiting }
146 ZEND, { x: got eob and all data flushed }
147 BADCODE); { x: got error }
148
149 { inflate codes private state }
150 type
151 pInflate_codes_state = ^inflate_codes_state;
152 inflate_codes_state = record
153
154 mode : inflate_codes_mode; { current inflate_codes mode }
155
156 { mode dependent information }
157 len : uInt;
158 sub : record { submode }
159 Case Byte of
160 0:(code : record { if LEN or DIST, where in tree }
161 tree : pInflate_huft; { pointer into tree }
162 need : uInt; { bits needed }
163 end);
164 1:(lit : uInt); { if LIT, literal }
165 2:(copy: record { if EXT or COPY, where and how much }
166 get : uInt; { bits to get for extra }
167 dist : uInt; { distance back to copy from }
168 end);
169 end;
170
171 { mode independent information }
172 lbits : Byte; { ltree bits decoded per branch }
173 dbits : Byte; { dtree bits decoder per branch }
174 ltree : pInflate_huft; { literal/length/eob tree }
175 dtree : pInflate_huft; { distance tree }
176 end;
177
178 type
179 check_func = function(check : uLong;
180 buf : pBytef;
181 {const buf : array of byte;}
182 len : uInt) : uLong;
183 type
184 inflate_block_mode =
185 (ZTYPE, { get type bits (3, including end bit) }
186 LENS, { get lengths for stored }
187 STORED, { processing stored block }
188 TABLE, { get table lengths }
189 BTREE, { get bit lengths tree for a dynamic block }
190 DTREE, { get length, distance trees for a dynamic block }
191 CODES, { processing fixed or dynamic block }
192 DRY, { output remaining window bytes }
193 BLKDONE, { finished last block, done }
194 BLKBAD); { got a data error--stuck here }
195
196 type
197 pInflate_blocks_state = ^inflate_blocks_state;
198
199 { inflate blocks semi-private state }
200 inflate_blocks_state = record
201
202 mode : inflate_block_mode; { current inflate_block mode }
203
204 { mode dependent information }
205 sub : record { submode }
206 case Byte of
207 0:(left : uInt); { if STORED, bytes left to copy }
208 1:(trees : record { if DTREE, decoding info for trees }
209 table : uInt; { table lengths (14 bits) }
210 index : uInt; { index into blens (or border) }
211 blens : PuIntArray; { bit lengths of codes }
212 bb : uInt; { bit length tree depth }
213 tb : pInflate_huft; { bit length decoding tree }
214 end);
215 2:(decode : record { if CODES, current state }
216 tl : pInflate_huft;
217 td : pInflate_huft; { trees to free }
218 codes : pInflate_codes_state;
219 end);
220 end;
221 last : boolean; { true if this block is the last block }
222
223 { mode independent information }
224 bitk : uInt; { bits in bit buffer }
225 bitb : uLong; { bit buffer }
226 hufts : huft_ptr; {pInflate_huft;} { single malloc for tree space }
227 window : pBytef; { sliding window }
228 zend : pBytef; { one byte after sliding window }
229 read : pBytef; { window read pointer }
230 write : pBytef; { window write pointer }
231 checkfn : check_func; { check function }
232 check : uLong; { check on output }
233 end;
234
235 type
236 inflate_mode = (
237 METHOD, { waiting for method byte }
238 FLAG, { waiting for flag byte }
239 DICT4, { four dictionary check bytes to go }
240 DICT3, { three dictionary check bytes to go }
241 DICT2, { two dictionary check bytes to go }
242 DICT1, { one dictionary check byte to go }
243 DICT0, { waiting for inflateSetDictionary }
244 BLOCKS, { decompressing blocks }
245 CHECK4, { four check bytes to go }
246 CHECK3, { three check bytes to go }
247 CHECK2, { two check bytes to go }
248 CHECK1, { one check byte to go }
249 DONE, { finished check, done }
250 BAD); { got an error--stay here }
251
252 { inflate private state }
253 type
254 pInternal_state = ^internal_state; { or point to a deflate_state record }
255 internal_state = record
256
257 mode : inflate_mode; { current inflate mode }
258
259 { mode dependent information }
260 sub : record { submode }
261 case byte of
262 0:(method : uInt); { if FLAGS, method byte }
263 1:(check : record { if CHECK, check values to compare }
264 was : uLong; { computed check value }
265 need : uLong; { stream check value }
266 end);
267 2:(marker : uInt); { if BAD, inflateSync's marker bytes count }
268 end;
269
270 { mode independent information }
271 nowrap : boolean; { flag for no wrapper }
272 wbits : uInt; { log2(window size) (8..15, defaults to 15) }
273 blocks : pInflate_blocks_state; { current inflate_blocks state }
274 end;
275
276 type
277 alloc_func = function(opaque : voidpf; items : uInt; size : uInt) : voidpf;
278 free_func = procedure(opaque : voidpf; address : voidpf);
279
280 type
281 z_streamp = ^z_stream;
282 z_stream = record
283 next_in : pBytef; { next input byte }
284 avail_in : uInt; { number of bytes available at next_in }
285 total_in : uLong; { total nb of input bytes read so far }
286
287 next_out : pBytef; { next output byte should be put there }
288 avail_out : uInt; { remaining free space at next_out }
289 total_out : uLong; { total nb of bytes output so far }
290
291 msg : string[255]; { last error message, '' if no error }
292 state : pInternal_state; { not visible by applications }
293
294 zalloc : alloc_func; { used to allocate the internal state }
295 zfree : free_func; { used to free the internal state }
296 opaque : voidpf; { private data object passed to zalloc and zfree }
297
298 data_type : int; { best guess about the data type: ascii or binary }
299 adler : uLong; { adler32 value of the uncompressed data }
300 reserved : uLong; { reserved for future use }
301 end;
302
303
304 { The application must update next_in and avail_in when avail_in has
305 dropped to zero. It must update next_out and avail_out when avail_out
306 has dropped to zero. The application must initialize zalloc, zfree and
307 opaque before calling the init function. All other fields are set by the
308 compression library and must not be updated by the application.
309
310 The opaque value provided by the application will be passed as the first
311 parameter for calls of zalloc and zfree. This can be useful for custom
312 memory management. The compression library attaches no meaning to the
313 opaque value.
314
315 zalloc must return Z_NULL if there is not enough memory for the object.
316 On 16-bit systems, the functions zalloc and zfree must be able to allocate
317 exactly 65536 bytes, but will not be required to allocate more than this
318 if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
319 pointers returned by zalloc for objects of exactly 65536 bytes *must*
320 have their offset normalized to zero. The default allocation function
321 provided by this library ensures this (see zutil.c). To reduce memory
322 requirements and avoid any allocation of 64K objects, at the expense of
323 compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
324
325 The fields total_in and total_out can be used for statistics or
326 progress reports. After compression, total_in holds the total size of
327 the uncompressed data and may be saved for use in the decompressor
328 (particularly if the decompressor wants to decompress everything in
329 a single step). }
330
331 const { constants }
332 Z_NO_FLUSH = 0;
333 Z_PARTIAL_FLUSH = 1;
334 Z_SYNC_FLUSH = 2;
335 Z_FULL_FLUSH = 3;
336 Z_FINISH = 4;
337 { Allowed flush values; see deflate() below for details }
338
339 Z_OK = 0;
340 Z_STREAM_END = 1;
341 Z_NEED_DICT = 2;
342 Z_ERRNO = (-1);
343 Z_STREAM_ERROR = (-2);
344 Z_DATA_ERROR = (-3);
345 Z_MEM_ERROR = (-4);
346 Z_BUF_ERROR = (-5);
347 Z_VERSION_ERROR = (-6);
348 { Return codes for the compression/decompression functions. Negative
349 values are errors, positive values are used for special but normal events.}
350
351 Z_NO_COMPRESSION = 0;
352 Z_BEST_SPEED = 1;
353 Z_BEST_COMPRESSION = 9;
354 Z_DEFAULT_COMPRESSION = (-1);
355 { compression levels }
356
357 Z_FILTERED = 1;
358 Z_HUFFMAN_ONLY = 2;
359 Z_DEFAULT_STRATEGY = 0;
360 { compression strategy; see deflateInit2() below for details }
361
362 Z_BINARY = 0;
363 Z_ASCII = 1;
364 Z_UNKNOWN = 2;
365 { Possible values of the data_type field }
366
367 Z_DEFLATED = 8;
368 { The deflate compression method (the only one supported in this version) }
369
370 Z_NULL = NIL; { for initializing zalloc, zfree, opaque }
371
372 {$IFDEF GZIO}
373 var
374 errno : int;
375 {$ENDIF}
376
377 { common constants }
378
379
380 { The three kinds of block type }
381 const
382 STORED_BLOCK = 0;
383 STATIC_TREES = 1;
384 DYN_TREES = 2;
385 { The minimum and maximum match lengths }
386 const
387 MIN_MATCH = 3;
388 {$ifdef MAX_MATCH_IS_258}
389 MAX_MATCH = 258;
390 {$else}
391 MAX_MATCH = ??; { deliberate syntax error }
392 {$endif}
393
394 const
395 PRESET_DICT = $20; { preset dictionary flag in zlib header }
396
397
398 {$IFDEF DEBUG}
399 procedure Assert(cond : boolean; msg : string);
400 {$ENDIF}
401
402 procedure Trace(x : string);
403 procedure Tracev(x : string);
404 procedure Tracevv(x : string);
405 procedure Tracevvv(x : string);
406 procedure Tracec(c : boolean; x : string);
407 procedure Tracecv(c : boolean; x : string);
408
409 function zlibVersion : string;
410 { The application can compare zlibVersion and ZLIB_VERSION for consistency.
411 If the first character differs, the library code actually used is
412 not compatible with the zlib.h header file used by the application.
413 This check is automatically made by deflateInit and inflateInit. }
414
415 function zError(err : int) : string;
416
417 function ZALLOC (var strm : z_stream; items : uInt; size : uInt) : voidpf;
418
419 procedure ZFREE (var strm : z_stream; ptr : voidpf);
420
421 procedure TRY_FREE (var strm : z_stream; ptr : voidpf);
422
423 const
424 ZLIB_VERSION : string[10] = '1.1.2';
425
426 const
427 z_errbase = Z_NEED_DICT;
428 z_errmsg : Array[0..9] of string[21] = { indexed by 2-zlib_error }
429 ('need dictionary', { Z_NEED_DICT 2 }
430 'stream end', { Z_STREAM_END 1 }
431 '', { Z_OK 0 }
432 'file error', { Z_ERRNO (-1) }
433 'stream error', { Z_STREAM_ERROR (-2) }
434 'data error', { Z_DATA_ERROR (-3) }
435 'insufficient memory', { Z_MEM_ERROR (-4) }
436 'buffer error', { Z_BUF_ERROR (-5) }
437 'incompatible version',{ Z_VERSION_ERROR (-6) }
438 '');
439 const
440 z_verbose : int = 1;
441
442 {$IFDEF DEBUG}
443 procedure z_error (m : string);
444 {$ENDIF}
445
446 implementation
447
448 function zError(err : int) : string;
449 begin
450 zError := z_errmsg[Z_NEED_DICT-err];
451 end;
452
453 function zlibVersion : string;
454 begin
455 zlibVersion := ZLIB_VERSION;
456 end;
457
458 procedure z_error (m : string);
459 begin
460 WriteLn(output, m);
461 Write('Zlib - Halt...');
462 ReadLn;
463 Halt(1);
464 end;
465
466 procedure Assert(cond : boolean; msg : string);
467 begin
468 if not cond then
469 z_error(msg);
470 end;
471
472 procedure Trace(x : string);
473 begin
474 WriteLn(x);
475 end;
476
477 procedure Tracev(x : string);
478 begin
479 if (z_verbose>0) then
480 WriteLn(x);
481 end;
482
483 procedure Tracevv(x : string);
484 begin
485 if (z_verbose>1) then
486 WriteLn(x);
487 end;
488
489 procedure Tracevvv(x : string);
490 begin
491 if (z_verbose>2) then
492 WriteLn(x);
493 end;
494
495 procedure Tracec(c : boolean; x : string);
496 begin
497 if (z_verbose>0) and (c) then
498 WriteLn(x);
499 end;
500
501 procedure Tracecv(c : boolean; x : string);
502 begin
503 if (z_verbose>1) and c then
504 WriteLn(x);
505 end;
506
507 function ZALLOC (var strm : z_stream; items : uInt; size : uInt) : voidpf;
508 begin
509 ZALLOC := strm.zalloc(strm.opaque, items, size);
510 end;
511
512 procedure ZFREE (var strm : z_stream; ptr : voidpf);
513 begin
514 strm.zfree(strm.opaque, ptr);
515 end;
516
517 procedure TRY_FREE (var strm : z_stream; ptr : voidpf);
518 begin
519 {if @strm <> Z_NULL then}
520 strm.zfree(strm.opaque, ptr);
521 end;
522
523 end.

Properties

Name Value
svn:executable

No admin address has been configured">No admin address has been configured
ViewVC Help
Powered by ViewVC 1.1.5