Debian Codegraph @

src/base/castleinternalzlib.pas

raw
1 { Zlib bindings, for Castle Game Engine.
2
3 Based on FPC zlib.pp unit.
4 Adjusted to:
5
6 @unorderedList(
7 @item(link to zlib1.dll under Windows (from [http://gnuwin32.sourceforge.net/]),
8 this uses cdecl calling convention)
9 @item(name changed from zlib to CastleInternalZLib to not collide with FPC and Kylix
10 zlib units)
11 @item(changed to link using CGE TDynLib instead of linking with
12 "external libz ...")
13 @item(just like CastleInternalPng: zlib library does not need to be
14 actually present on the user system.
15 If zlib is not be installed, CastleZLibInitialized will be simply set to @false.)
16 @item(honor ALLOW_DLOPEN_FROM_UNIT_INITIALIZATION)
17 @item(For both FPC and Delphi (uses PAnsiChar for new unicode Delphis).)
18
19 )
20
21 @exclude (This is only a C header translation --- no nice PasDoc docs.)
22 }
23
24 unit CastleInternalZLib;
25
26 {$I castleconf.inc}
27
28 {$ifdef CASTLE_ZLIB_USING_PASZLIB}
29 interface
30 { CastleInternalZLib unit doesn't expose anything when CASTLE_ZLIB_USING_PASZLIB
31 is defined. Instead, use PasZLib, which implements Zlib API in pure Pascal. }
32 implementation
33 end.
34 {$else}
35
36 interface
37
38 {$ifdef FPC}
39 { for linux for linking with libc }
40 {$ifdef unix}
41 {$linklib c}
42 {$endif}
43 {$PACKRECORDS 4}
44 {$else}
45 {$ALIGN 4}
46 {$endif}
47
48 uses CTypes;
49
50 const
51 ZLIB_VERSION = '1.1.3';
52
53 type
54 { Compatible with paszlib }
55 { }
56 Uint = CInt32;
57 Ulong = CInt32;
58 Ulongf = CInt32;
59 Pulongf = ^Ulongf;
60 z_off_t = CInt32;
61
62 TAllocfunc = function (opaque:pointer; items:uInt; size:uInt):pointer;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
63 TFreeFunc = procedure (opaque:pointer; address:pointer);{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
64
65 TInternalState = record
66 end;
67 PInternalState = ^TInternalstate;
68
69 TZStream = record
70 next_in : PByte;
71 avail_in : uInt;
72 total_in : uLong;
73 next_out : PByte;
74 avail_out : uInt;
75 total_out : uLong;
76 msg : PByte;
77 state : PInternalState;
78 zalloc : TAllocFunc;
79 zfree : TFreeFunc;
80 opaque : pointer;
81 data_type : CInt32;
82 adler : uLong;
83 reserved : uLong;
84 end;
85 PZstream = ^TZStream;
86 gzFile = pointer;
87
88 const
89 Z_NO_FLUSH = 0;
90
91 Z_PARTIAL_FLUSH = 1;
92 Z_SYNC_FLUSH = 2;
93 Z_FULL_FLUSH = 3;
94 Z_FINISH = 4;
95
96 Z_OK = 0;
97 Z_STREAM_END = 1;
98 Z_NEED_DICT = 2;
99 Z_ERRNO = -(1);
100 Z_STREAM_ERROR = -(2);
101 Z_DATA_ERROR = -(3);
102 Z_MEM_ERROR = -(4);
103 Z_BUF_ERROR = -(5);
104 Z_VERSION_ERROR = -(6);
105
106 Z_NO_COMPRESSION = 0;
107 Z_BEST_SPEED = 1;
108 Z_BEST_COMPRESSION = 9;
109 Z_DEFAULT_COMPRESSION = -(1);
110
111 Z_FILTERED = 1;
112 Z_HUFFMAN_ONLY = 2;
113 Z_DEFAULT_STRATEGY = 0;
114
115 Z_BINARY = 0;
116 Z_ASCII = 1;
117 Z_UNKNOWN = 2;
118
119 Z_DEFLATED = 8;
120
121 Z_NULL = 0;
122
123 var
124 zlibVersionpchar: function:PAnsiChar;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
125 deflate: function(var strm:TZStream; flush:CInt32):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
126 deflateEnd: function(var strm:TZStream):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
127 inflate: function(var strm:TZStream; flush:CInt32):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
128 inflateEnd: function(var strm:TZStream):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
129 deflateSetDictionary: function(var strm:TZStream;dictionary : PByte; dictLength:uInt):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
130 deflateCopy: function(var dest,source:TZstream):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
131 deflateReset: function(var strm:TZStream):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
132 deflateParams: function(var strm:TZStream; level:CInt32; strategy:CInt32):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
133 inflateSetDictionary: function(var strm:TZStream;dictionary : PByte; dictLength:uInt):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
134 inflateSync: function(var strm:TZStream):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
135 inflateReset: function(var strm:TZStream):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
136 compress: function(dest:PByte;destLen:uLongf; source : PByte; sourceLen:uLong):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
137 compress2: function(dest:PByte;destLen:uLongf; source : PByte; sourceLen:uLong; level:CInt32):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
138 uncompress: function(dest:PByte;destLen:uLongf; source : PByte; sourceLen:uLong):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
139 gzopen: function(path:PAnsiChar; mode:PAnsiChar):gzFile;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
140 gzdopen: function(fd:CInt32; mode:PAnsiChar):gzFile;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
141 gzsetparams: function(thefile:gzFile; level:CInt32; strategy:CInt32):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
142 gzread: function(thefile:gzFile; buf:pointer; len:cardinal):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
143 gzwrite: function(thefile:gzFile; buf:pointer; len:cardinal):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
144 (* gzprintf commented out,
145 FPC 2.2.0 warning "Warning: cdecl'ared functions have no high parameter"
146 and on mailing list Jonas suggests that C varargs will never be reliably
147 handled anyway.
148 http://www.mail-archive.com/[email protected]/msg09040.html
149 (although the point of thread is about something else completely,
150 and even the question was about "array of PChar" which is not varargs...) *)
151 //gzprintf: function(thefile:gzFile; format:PByte; args:array of const):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
152 gzputs: function(thefile:gzFile; s:PByte):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
153 gzgets: function(thefile:gzFile; buf:PByte; len:CInt32):PByte;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
154 gzputc: function(thefile:gzFile; c:char):char;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
155 gzgetc: function(thefile:gzFile):char;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
156 gzflush: function(thefile:gzFile; flush:CInt32):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
157 gzseek: function(thefile:gzFile; offset:z_off_t; whence:CInt32):z_off_t;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
158 gzrewind: function(thefile:gzFile):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
159 gztell: function(thefile:gzFile):z_off_t;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
160 gzeof: function(thefile:gzFile):longbool;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
161 gzclose: function(thefile:gzFile):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
162 gzerror: function(thefile:gzFile; var errnum:CInt32):PByte;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
163 adler32: function(adler:uLong;buf : PByte; len:uInt):uLong;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
164 crc32: function(crc:uLong;buf : PByte; len:uInt):uLong;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
165 deflateInit_: function(var strm:TZStream; level:CInt32; version:PAnsiChar; stream_size:CInt32):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
166 inflateInit_: function(var strm:TZStream; version:PAnsiChar; stream_size:CInt32):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
167 deflateInit2_: function(var strm:TZStream; level:CInt32; method:CInt32; windowBits:CInt32; memLevel:CInt32;strategy:CInt32; version:PAnsiChar; stream_size:CInt32):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
168 inflateInit2_: function(var strm:TZStream; windowBits:CInt32; version:PAnsiChar; stream_size:CInt32):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
169 zErrorpchar: function(err:CInt32):PAnsiChar;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
170 inflateSyncPoint: function(z:PZstream):CInt32;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
171 get_crc_table: function:pointer;{$ifdef ZLIB_STDCALL} stdcall {$else} cdecl {$endif};
172
173 { These are simple comfortable Pascal wrappers above appropriate functions
174 exported from zlib. }
175 { }
176 function zlibVersion:string;
177 function deflateInit(var strm:TZStream;level : CInt32) : CInt32;
178 function inflateInit(var strm:TZStream) : CInt32;
179 function deflateInit2(var strm:TZStream;level,method,windowBits,memLevel,strategy : CInt32) : CInt32;
180 function inflateInit2(var strm:TZStream;windowBits : CInt32) : CInt32;
181 function zError(err:CInt32):string;
182
183 { Was zlib library found. If true that all function pointers in this unit
184 are initialized and you can simply use zlib. If false then zlib was not
185 installed, all function pointers in this unit are nil
186 and you can't use anything fro zlib. }
187 function CastleZLibInitialized: boolean;
188
189 procedure ZLibInitialization;
190
191 implementation
192
193 uses SysUtils, CastleDynLib, CastleUtils, CastleLog;
194
195 function zlibversion : string;
196 begin
197 zlibversion:=strpas(zlibversionpchar());
198 end;
199
200 function deflateInit(var strm:TZStream;level : CInt32) : CInt32;
201 begin
202 deflateInit:=deflateInit_(strm,level,ZLIB_VERSION,sizeof(TZStream));
203 end;
204
205 function inflateInit(var strm:TZStream) : CInt32;
206 begin
207 inflateInit:=inflateInit_(strm,ZLIB_VERSION,sizeof(TZStream));
208 end;
209
210 function deflateInit2(var strm:TZStream;level,method,windowBits,memLevel,strategy : CInt32) : CInt32;
211 begin
212 deflateInit2:=deflateInit2_(strm,level,method,windowBits,memLevel,strategy,ZLIB_VERSION,sizeof(TZStream));
213 end;
214
215 function inflateInit2(var strm:TZStream;windowBits : CInt32) : CInt32;
216 begin
217 inflateInit2:=inflateInit2_(strm,windowBits,ZLIB_VERSION,sizeof(TZStream));
218 end;
219
220 function zError(err:CInt32):string;
221 begin
222 zerror:=Strpas(zErrorpchar(err));
223 end;
224
225 var
226 ZLibrary: TDynLib;
227
228 function CastleZLibInitialized: boolean;
229 begin
230 Result := ZLibrary <> nil;
231 end;
232
233 procedure ZLibInitialization;
234 const
235 ZLibraryName =
236 {$ifdef UNIX}
237 {$ifdef DARWIN} 'libz.dylib'
238 {$else} 'libz.so.1'
239 {$endif}
240 {$endif}
241 {$ifdef MSWINDOWS} 'zlib1.dll' {$endif};
242 begin
243 ZLibrary := TDynLib.Load(ZLibraryName, false);
244
245 {$ifdef FREEBSD}
246 if ZLibrary = nil then
247 ZLibrary := TDynLib.Load('libz.so.6', false);
248 {$endif}
249
250 if ZLibrary <> nil then
251 begin
252 Pointer({$ifndef FPC}@{$endif} zlibVersionpchar) := ZLibrary.Symbol('zlibVersion');
253 Pointer({$ifndef FPC}@{$endif} deflate) := ZLibrary.Symbol('deflate');
254 Pointer({$ifndef FPC}@{$endif} deflateEnd) := ZLibrary.Symbol('deflateEnd');
255 Pointer({$ifndef FPC}@{$endif} inflate) := ZLibrary.Symbol('inflate');
256 Pointer({$ifndef FPC}@{$endif} inflateEnd) := ZLibrary.Symbol('inflateEnd');
257 Pointer({$ifndef FPC}@{$endif} deflateSetDictionary) := ZLibrary.Symbol('deflateSetDictionary');
258 Pointer({$ifndef FPC}@{$endif} deflateCopy) := ZLibrary.Symbol('deflateCopy');
259 Pointer({$ifndef FPC}@{$endif} deflateReset) := ZLibrary.Symbol('deflateReset');
260 Pointer({$ifndef FPC}@{$endif} deflateParams) := ZLibrary.Symbol('deflateParams');
261 Pointer({$ifndef FPC}@{$endif} inflateSetDictionary) := ZLibrary.Symbol('inflateSetDictionary');
262 Pointer({$ifndef FPC}@{$endif} inflateSync) := ZLibrary.Symbol('inflateSync');
263 Pointer({$ifndef FPC}@{$endif} inflateReset) := ZLibrary.Symbol('inflateReset');
264 Pointer({$ifndef FPC}@{$endif} compress) := ZLibrary.Symbol('compress');
265 Pointer({$ifndef FPC}@{$endif} compress2) := ZLibrary.Symbol('compress2');
266 Pointer({$ifndef FPC}@{$endif} uncompress) := ZLibrary.Symbol('uncompress');
267 Pointer({$ifndef FPC}@{$endif} gzopen) := ZLibrary.Symbol('gzopen');
268 Pointer({$ifndef FPC}@{$endif} gzdopen) := ZLibrary.Symbol('gzdopen');
269 Pointer({$ifndef FPC}@{$endif} gzsetparams) := ZLibrary.Symbol('gzsetparams');
270 Pointer({$ifndef FPC}@{$endif} gzread) := ZLibrary.Symbol('gzread');
271 Pointer({$ifndef FPC}@{$endif} gzwrite) := ZLibrary.Symbol('gzwrite');
272 // Pointer({$ifndef FPC}@{$endif} gzprintf) := ZLibrary.Symbol('gzprintf');
273 Pointer({$ifndef FPC}@{$endif} gzputs) := ZLibrary.Symbol('gzputs');
274 Pointer({$ifndef FPC}@{$endif} gzgets) := ZLibrary.Symbol('gzgets');
275 Pointer({$ifndef FPC}@{$endif} gzputc) := ZLibrary.Symbol('gzputc');
276 Pointer({$ifndef FPC}@{$endif} gzgetc) := ZLibrary.Symbol('gzgetc');
277 Pointer({$ifndef FPC}@{$endif} gzflush) := ZLibrary.Symbol('gzflush');
278 Pointer({$ifndef FPC}@{$endif} gzseek) := ZLibrary.Symbol('gzseek');
279 Pointer({$ifndef FPC}@{$endif} gzrewind) := ZLibrary.Symbol('gzrewind');
280 Pointer({$ifndef FPC}@{$endif} gztell) := ZLibrary.Symbol('gztell');
281 Pointer({$ifndef FPC}@{$endif} gzeof) := ZLibrary.Symbol('gzeof');
282 Pointer({$ifndef FPC}@{$endif} gzclose) := ZLibrary.Symbol('gzclose');
283 Pointer({$ifndef FPC}@{$endif} gzerror) := ZLibrary.Symbol('gzerror');
284 Pointer({$ifndef FPC}@{$endif} adler32) := ZLibrary.Symbol('adler32');
285 Pointer({$ifndef FPC}@{$endif} crc32) := ZLibrary.Symbol('crc32');
286 Pointer({$ifndef FPC}@{$endif} deflateInit_) := ZLibrary.Symbol('deflateInit_');
287 Pointer({$ifndef FPC}@{$endif} inflateInit_) := ZLibrary.Symbol('inflateInit_');
288 Pointer({$ifndef FPC}@{$endif} deflateInit2_) := ZLibrary.Symbol('deflateInit2_');
289 Pointer({$ifndef FPC}@{$endif} inflateInit2_) := ZLibrary.Symbol('inflateInit2_');
290 Pointer({$ifndef FPC}@{$endif} zErrorpchar) := ZLibrary.Symbol('zError');
291 Pointer({$ifndef FPC}@{$endif} inflateSyncPoint) := ZLibrary.Symbol('inflateSyncPoint');
292 Pointer({$ifndef FPC}@{$endif} get_crc_table) := ZLibrary.Symbol('get_crc_table');
293
294 WritelnLog('ZLib detected (version %s).', [zlibVersionpchar()]);
295 end;
296 end;
297
298 initialization
299 {$ifdef ALLOW_DLOPEN_FROM_UNIT_INITIALIZATION}
300 ZLibInitialization;
301 {$endif}
302 finalization
303 FreeAndNil(ZLibrary);
304 end.
305
306 {$endif CASTLE_ZLIB_USING_PASZLIB}