gzio.pas 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. Unit gzIO;
  2. {
  3. Pascal unit based on gzio.c -- IO on .gz files
  4. Copyright (C) 1995-1998 Jean-loup Gailly.
  5. Define NO_DEFLATE to compile this file without the compression code
  6. Pascal tranlastion based on code contributed by Francisco Javier Crespo
  7. Copyright (C) 1998 by Jacques Nomssi Nzali
  8. For conditions of distribution and use, see copyright notice in readme.txt
  9. }
  10. interface
  11. {$I zconf.inc}
  12. uses
  13. {$ifdef MSDOS}
  14. dos, strings,
  15. {$else}
  16. SysUtils,
  17. {$endif}
  18. zutil, zbase, gzcrc, zdeflate, zinflate;
  19. type gzFile = voidp;
  20. type z_off_t = long;
  21. function gzopen (path:string; mode:string) : gzFile;
  22. function gzsetparams (f:gzfile; level:int; strategy:int) : int;
  23. function gzread (f:gzFile; buf:voidp; len:uInt) : int;
  24. function gzgetc (f:gzfile) : int;
  25. function gzgets (f:gzfile; buf:PChar; len:int) : PChar;
  26. {$ifndef NO_DEFLATE}
  27. function gzwrite (f:gzFile; buf:voidp; len:uInt) : int;
  28. function gzputc (f:gzfile; c:char) : int;
  29. function gzputs (f:gzfile; s:PChar) : int;
  30. function gzflush (f:gzFile; flush:int) : int;
  31. {$ifdef GZ_FORMAT_STRING}
  32. function gzprintf (zfile : gzFile;
  33. const format : string;
  34. a : array of int); { doesn't compile }
  35. {$endif}
  36. {$endif}
  37. function gzseek (f:gzfile; offset:z_off_t; whence:int) : z_off_t;
  38. function gzrewind (f:gzFile) : int;
  39. function gztell (f:gzfile) : z_off_t;
  40. function gzeof (f:gzfile) : boolean;
  41. function gzclose (f:gzFile) : int;
  42. function gzerror (f:gzFile; var errnum:Int) : string;
  43. const
  44. SEEK_SET {: z_off_t} = 0; { seek from beginning of file }
  45. SEEK_CUR {: z_off_t} = 1; { seek from current position }
  46. SEEK_END {: z_off_t} = 2;
  47. implementation
  48. const
  49. Z_EOF = -1; { same value as in STDIO.H }
  50. Z_BUFSIZE = 16384;
  51. { Z_PRINTF_BUFSIZE = 4096; }
  52. gz_magic : array[0..1] of byte = ($1F, $8B); { gzip magic header }
  53. { gzip flag byte }
  54. ASCII_FLAG = $01; { bit 0 set: file probably ascii text }
  55. HEAD_CRC = $02; { bit 1 set: header CRC present }
  56. EXTRA_FIELD = $04; { bit 2 set: extra field present }
  57. ORIG_NAME = $08; { bit 3 set: original file name present }
  58. COMMENT = $10; { bit 4 set: file comment present }
  59. RESERVED = $E0; { bits 5..7: reserved }
  60. type gz_stream = record
  61. stream : z_stream;
  62. z_err : int; { error code for last stream operation }
  63. z_eof : boolean; { set if end of input file }
  64. gzfile : file; { .gz file }
  65. inbuf : pBytef; { input buffer }
  66. outbuf : pBytef; { output buffer }
  67. crc : uLong; { crc32 of uncompressed data }
  68. msg, { error message - limit 79 chars }
  69. path : string[79]; { path name for debugging only - limit 79 chars }
  70. transparent : boolean; { true if input file is not a .gz file }
  71. mode : char; { 'w' or 'r' }
  72. startpos : long; { start of compressed data in file (header skipped) }
  73. end;
  74. type gz_streamp = ^gz_stream;
  75. function destroy (var s:gz_streamp) : int; forward;
  76. procedure check_header(s:gz_streamp); forward;
  77. { GZOPEN ====================================================================
  78. Opens a gzip (.gz) file for reading or writing. As Pascal does not use
  79. file descriptors, the code has been changed to accept only path names.
  80. The mode parameter defaults to BINARY read or write operations ('r' or 'w')
  81. but can also include a compression level ('w9') or a strategy: Z_FILTERED
  82. as in 'w6f' or Z_HUFFMAN_ONLY as in 'w1h'. (See the description of
  83. deflateInit2 for more information about the strategy parameter.)
  84. gzopen can be used to open a file which is not in gzip format; in this
  85. case, gzread will directly read from the file without decompression.
  86. gzopen returns NIL if the file could not be opened (non-zero IOResult)
  87. or if there was insufficient memory to allocate the (de)compression state
  88. (zlib error is Z_MEM_ERROR).
  89. ============================================================================}
  90. function gzopen (path:string; mode:string) : gzFile;
  91. var
  92. i : uInt;
  93. err : int;
  94. level : int; { compression level }
  95. strategy : int; { compression strategy }
  96. s : gz_streamp;
  97. {$IFDEF MSDOS}
  98. attr : word; { file attributes }
  99. {$ENDIF}
  100. {$IFNDEF NO_DEFLATE}
  101. gzheader : array [0..9] of byte;
  102. {$ENDIF}
  103. begin
  104. if (path='') or (mode='') then begin
  105. gzopen := Z_NULL;
  106. exit;
  107. end;
  108. GetMem (s,sizeof(gz_stream));
  109. if not Assigned (s) then begin
  110. gzopen := Z_NULL;
  111. exit;
  112. end;
  113. level := Z_DEFAULT_COMPRESSION;
  114. strategy := Z_DEFAULT_STRATEGY;
  115. s^.stream.zalloc := NIL; { (alloc_func)0 }
  116. s^.stream.zfree := NIL; { (free_func)0 }
  117. s^.stream.opaque := NIL; { (voidpf)0 }
  118. s^.stream.next_in := Z_NULL;
  119. s^.stream.next_out := Z_NULL;
  120. s^.stream.avail_in := 0;
  121. s^.stream.avail_out := 0;
  122. s^.z_err := Z_OK;
  123. s^.z_eof := false;
  124. s^.inbuf := Z_NULL;
  125. s^.outbuf := Z_NULL;
  126. s^.crc := crc32(0, Z_NULL, 0);
  127. s^.msg := '';
  128. s^.transparent := false;
  129. s^.path := path; { limit to 255 chars }
  130. s^.mode := chr(0);
  131. for i:=1 to Length(mode) do begin
  132. case mode[i] of
  133. 'r' : s^.mode := 'r';
  134. 'w' : s^.mode := 'w';
  135. '0'..'9' : level := Ord(mode[i])-Ord('0');
  136. 'f' : strategy := Z_FILTERED;
  137. 'h' : strategy := Z_HUFFMAN_ONLY;
  138. end;
  139. end;
  140. if (s^.mode=chr(0)) then begin
  141. destroy(s);
  142. gzopen := gzFile(Z_NULL);
  143. exit;
  144. end;
  145. if (s^.mode='w') then begin
  146. {$IFDEF NO_DEFLATE}
  147. err := Z_STREAM_ERROR;
  148. {$ELSE}
  149. err := deflateInit2 (s^.stream, level, Z_DEFLATED, -MAX_WBITS,
  150. DEF_MEM_LEVEL, strategy);
  151. { windowBits is passed < 0 to suppress zlib header }
  152. GetMem (s^.outbuf, Z_BUFSIZE);
  153. s^.stream.next_out := s^.outbuf;
  154. {$ENDIF}
  155. if (err <> Z_OK) or (s^.outbuf = Z_NULL) then begin
  156. destroy(s);
  157. gzopen := gzFile(Z_NULL);
  158. exit;
  159. end;
  160. end
  161. else begin
  162. GetMem (s^.inbuf, Z_BUFSIZE);
  163. s^.stream.next_in := s^.inbuf;
  164. err := inflateInit2_ (s^.stream, -MAX_WBITS, ZLIB_VERSION, sizeof(z_stream));
  165. { windowBits is passed < 0 to tell that there is no zlib header }
  166. if (err <> Z_OK) or (s^.inbuf = Z_NULL) then begin
  167. destroy(s);
  168. gzopen := gzFile(Z_NULL);
  169. exit;
  170. end;
  171. end;
  172. s^.stream.avail_out := Z_BUFSIZE;
  173. {$IFOPT I+} {$I-} {$define IOcheck} {$ENDIF}
  174. Assign (s^.gzfile, s^.path);
  175. {$ifdef MSDOS}
  176. GetFAttr(s^.gzfile, Attr);
  177. if (DosError <> 0) and (s^.mode='w') then
  178. ReWrite (s^.gzfile,1)
  179. else
  180. Reset (s^.gzfile,1);
  181. {$else}
  182. if (not FileExists(s^.path)) and (s^.mode='w') then
  183. ReWrite (s^.gzfile,1)
  184. else
  185. Reset (s^.gzfile,1);
  186. {$endif}
  187. {$IFDEF IOCheck} {$I+} {$ENDIF}
  188. if (IOResult <> 0) then begin
  189. destroy(s);
  190. gzopen := gzFile(Z_NULL);
  191. exit;
  192. end;
  193. if (s^.mode = 'w') then begin { Write a very simple .gz header }
  194. {$IFNDEF NO_DEFLATE}
  195. gzheader [0] := gz_magic [0];
  196. gzheader [1] := gz_magic [1];
  197. gzheader [2] := Z_DEFLATED; { method }
  198. gzheader [3] := 0; { flags }
  199. gzheader [4] := 0; { time[0] }
  200. gzheader [5] := 0; { time[1] }
  201. gzheader [6] := 0; { time[2] }
  202. gzheader [7] := 0; { time[3] }
  203. gzheader [8] := 0; { xflags }
  204. gzheader [9] := 0; { OS code = MS-DOS }
  205. blockwrite (s^.gzfile, gzheader, 10);
  206. s^.startpos := LONG(10);
  207. {$ENDIF}
  208. end
  209. else begin
  210. check_header(s); { skip the .gz header }
  211. s^.startpos := FilePos(s^.gzfile) - s^.stream.avail_in;
  212. end;
  213. gzopen := gzFile(s);
  214. end;
  215. { GZSETPARAMS ===============================================================
  216. Update the compression level and strategy.
  217. ============================================================================}
  218. function gzsetparams (f:gzfile; level:int; strategy:int) : int;
  219. var
  220. s : gz_streamp;
  221. written: integer;
  222. begin
  223. s := gz_streamp(f);
  224. if (s = NIL) or (s^.mode <> 'w') then begin
  225. gzsetparams := Z_STREAM_ERROR;
  226. exit;
  227. end;
  228. { Make room to allow flushing }
  229. if (s^.stream.avail_out = 0) then begin
  230. s^.stream.next_out := s^.outbuf;
  231. blockwrite(s^.gzfile, s^.outbuf^, Z_BUFSIZE, written);
  232. if (written <> Z_BUFSIZE) then s^.z_err := Z_ERRNO;
  233. s^.stream.avail_out := Z_BUFSIZE;
  234. end;
  235. gzsetparams := deflateParams (s^.stream, level, strategy);
  236. end;
  237. { GET_BYTE ==================================================================
  238. Read a byte from a gz_stream. Updates next_in and avail_in.
  239. Returns EOF for end of file.
  240. IN assertion: the stream s has been sucessfully opened for reading.
  241. ============================================================================}
  242. function get_byte (s:gz_streamp) : int;
  243. begin
  244. if (s^.z_eof = true) then begin
  245. get_byte := Z_EOF;
  246. exit;
  247. end;
  248. if (s^.stream.avail_in = 0) then begin
  249. {$I-}
  250. blockread (s^.gzfile, s^.inbuf^, Z_BUFSIZE, Int(s^.stream.avail_in));
  251. {$I+}
  252. if (s^.stream.avail_in = 0) then begin
  253. s^.z_eof := true;
  254. if (IOResult <> 0) then s^.z_err := Z_ERRNO;
  255. get_byte := Z_EOF;
  256. exit;
  257. end;
  258. s^.stream.next_in := s^.inbuf;
  259. end;
  260. Dec(s^.stream.avail_in);
  261. get_byte := s^.stream.next_in^;
  262. Inc(s^.stream.next_in);
  263. end;
  264. { GETLONG ===================================================================
  265. Reads a Longint in LSB order from the given gz_stream.
  266. ============================================================================}
  267. {
  268. function getLong (s:gz_streamp) : uLong;
  269. var
  270. x : array [0..3] of byte;
  271. i : byte;
  272. c : int;
  273. n1 : longint;
  274. n2 : longint;
  275. begin
  276. for i:=0 to 3 do begin
  277. c := get_byte(s);
  278. if (c = Z_EOF) then s^.z_err := Z_DATA_ERROR;
  279. x[i] := (c and $FF)
  280. end;
  281. n1 := (ush(x[3] shl 8)) or x[2];
  282. n2 := (ush(x[1] shl 8)) or x[0];
  283. getlong := (n1 shl 16) or n2;
  284. end;
  285. }
  286. function getLong(s : gz_streamp) : uLong;
  287. var
  288. x : packed array [0..3] of byte;
  289. c : int;
  290. begin
  291. { x := uLong(get_byte(s)); - you can't do this with TP, no unsigned long }
  292. { the following assumes a little endian machine and TP }
  293. x[0] := Byte(get_byte(s));
  294. x[1] := Byte(get_byte(s));
  295. x[2] := Byte(get_byte(s));
  296. c := get_byte(s);
  297. x[3] := Byte(c);
  298. if (c = Z_EOF) then
  299. s^.z_err := Z_DATA_ERROR;
  300. GetLong := uLong(longint(x));
  301. end;
  302. { CHECK_HEADER ==============================================================
  303. Check the gzip header of a gz_stream opened for reading.
  304. Set the stream mode to transparent if the gzip magic header is not present.
  305. Set s^.err to Z_DATA_ERROR if the magic header is present but the rest of
  306. the header is incorrect.
  307. IN assertion: the stream s has already been created sucessfully;
  308. s^.stream.avail_in is zero for the first time, but may be non-zero
  309. for concatenated .gz files
  310. ============================================================================}
  311. procedure check_header (s:gz_streamp);
  312. var
  313. method : int; { method byte }
  314. flags : int; { flags byte }
  315. len : uInt;
  316. c : int;
  317. begin
  318. { Check the gzip magic header }
  319. for len := 0 to 1 do begin
  320. c := get_byte(s);
  321. if (c <> gz_magic[len]) then begin
  322. if (len <> 0) then begin
  323. Inc(s^.stream.avail_in);
  324. Dec(s^.stream.next_in);
  325. end;
  326. if (c <> Z_EOF) then begin
  327. Inc(s^.stream.avail_in);
  328. Dec(s^.stream.next_in);
  329. s^.transparent := TRUE;
  330. end;
  331. if (s^.stream.avail_in <> 0) then s^.z_err := Z_OK
  332. else s^.z_err := Z_STREAM_END;
  333. exit;
  334. end;
  335. end;
  336. method := get_byte(s);
  337. flags := get_byte(s);
  338. if (method <> Z_DEFLATED) or ((flags and RESERVED) <> 0) then begin
  339. s^.z_err := Z_DATA_ERROR;
  340. exit;
  341. end;
  342. for len := 0 to 5 do get_byte(s); { Discard time, xflags and OS code }
  343. if ((flags and EXTRA_FIELD) <> 0) then begin { skip the extra field }
  344. len := uInt(get_byte(s));
  345. len := len + (uInt(get_byte(s)) shr 8);
  346. { len is garbage if EOF but the loop below will quit anyway }
  347. while (len <> 0) and (get_byte(s) <> Z_EOF) do Dec(len);
  348. end;
  349. if ((flags and ORIG_NAME) <> 0) then begin { skip the original file name }
  350. repeat
  351. c := get_byte(s);
  352. until (c = 0) or (c = Z_EOF);
  353. end;
  354. if ((flags and COMMENT) <> 0) then begin { skip the .gz file comment }
  355. repeat
  356. c := get_byte(s);
  357. until (c = 0) or (c = Z_EOF);
  358. end;
  359. if ((flags and HEAD_CRC) <> 0) then begin { skip the header crc }
  360. get_byte(s);
  361. get_byte(s);
  362. end;
  363. if (s^.z_eof = true) then
  364. s^.z_err := Z_DATA_ERROR
  365. else
  366. s^.z_err := Z_OK;
  367. end;
  368. { DESTROY ===================================================================
  369. Cleanup then free the given gz_stream. Return a zlib error code.
  370. Try freeing in the reverse order of allocations.
  371. ============================================================================}
  372. function destroy (var s:gz_streamp) : int;
  373. begin
  374. destroy := Z_OK;
  375. if not Assigned (s) then begin
  376. destroy := Z_STREAM_ERROR;
  377. exit;
  378. end;
  379. if (s^.stream.state <> NIL) then begin
  380. if (s^.mode = 'w') then begin
  381. {$IFDEF NO_DEFLATE}
  382. destroy := Z_STREAM_ERROR;
  383. {$ELSE}
  384. destroy := deflateEnd(s^.stream);
  385. {$ENDIF}
  386. end
  387. else if (s^.mode = 'r') then begin
  388. destroy := inflateEnd(s^.stream);
  389. end;
  390. end;
  391. if (s^.path <> '') then begin
  392. {$I-}
  393. close(s^.gzfile);
  394. {$I+}
  395. if (IOResult <> 0) then destroy := Z_ERRNO;
  396. end;
  397. if (s^.z_err < 0) then destroy := s^.z_err;
  398. if Assigned (s^.inbuf) then
  399. FreeMem(s^.inbuf, Z_BUFSIZE);
  400. if Assigned (s^.outbuf) then
  401. FreeMem(s^.outbuf, Z_BUFSIZE);
  402. FreeMem(s, sizeof(gz_stream));
  403. end;
  404. { GZREAD ====================================================================
  405. Reads the given number of uncompressed bytes from the compressed file.
  406. If the input file was not in gzip format, gzread copies the given number
  407. of bytes into the buffer.
  408. gzread returns the number of uncompressed bytes actually read
  409. (0 for end of file, -1 for error).
  410. ============================================================================}
  411. function gzread (f:gzFile; buf:voidp; len:uInt) : int;
  412. var
  413. s : gz_streamp;
  414. start : pBytef;
  415. next_out : pBytef;
  416. n : uInt;
  417. crclen : uInt; { Buffer length to update CRC32 }
  418. filecrc : uLong; { CRC32 stored in GZIP'ed file }
  419. filelen : uLong; { Total lenght of uncompressed file }
  420. bytes : integer; { bytes actually read in I/O blockread }
  421. total_in : uLong;
  422. total_out : uLong;
  423. begin
  424. s := gz_streamp(f);
  425. start := pBytef(buf); { starting point for crc computation }
  426. if (s = NIL) or (s^.mode <> 'r') then begin
  427. gzread := Z_STREAM_ERROR;
  428. exit;
  429. end;
  430. if (s^.z_err = Z_DATA_ERROR) or (s^.z_err = Z_ERRNO) then begin
  431. gzread := -1;
  432. exit;
  433. end;
  434. if (s^.z_err = Z_STREAM_END) then begin
  435. gzread := 0; { EOF }
  436. exit;
  437. end;
  438. s^.stream.next_out := pBytef(buf);
  439. s^.stream.avail_out := len;
  440. while (s^.stream.avail_out <> 0) do begin
  441. if (s^.transparent = true) then begin
  442. { Copy first the lookahead bytes: }
  443. n := s^.stream.avail_in;
  444. if (n > s^.stream.avail_out) then n := s^.stream.avail_out;
  445. if (n > 0) then begin
  446. zmemcpy(s^.stream.next_out, s^.stream.next_in, n);
  447. inc (s^.stream.next_out, n);
  448. inc (s^.stream.next_in, n);
  449. dec (s^.stream.avail_out, n);
  450. dec (s^.stream.avail_in, n);
  451. end;
  452. if (s^.stream.avail_out > 0) then begin
  453. blockread (s^.gzfile, s^.stream.next_out^, s^.stream.avail_out, bytes);
  454. dec (s^.stream.avail_out, uInt(bytes));
  455. end;
  456. dec (len, s^.stream.avail_out);
  457. inc (s^.stream.total_in, uLong(len));
  458. inc (s^.stream.total_out, uLong(len));
  459. gzread := int(len);
  460. exit;
  461. end; { IF transparent }
  462. if (s^.stream.avail_in = 0) and (s^.z_eof = false) then begin
  463. {$I-}
  464. blockread (s^.gzfile, s^.inbuf^, Z_BUFSIZE, Int(s^.stream.avail_in));
  465. {$I+}
  466. if (s^.stream.avail_in = 0) then begin
  467. s^.z_eof := true;
  468. if (IOResult <> 0) then begin
  469. s^.z_err := Z_ERRNO;
  470. break;
  471. end;
  472. end;
  473. s^.stream.next_in := s^.inbuf;
  474. end;
  475. s^.z_err := inflate(s^.stream, Z_NO_FLUSH);
  476. if (s^.z_err = Z_STREAM_END) then begin
  477. crclen := 0;
  478. next_out := s^.stream.next_out;
  479. while (next_out <> start ) do begin
  480. dec (next_out);
  481. inc (crclen); { Hack because Pascal cannot substract pointers }
  482. end;
  483. { Check CRC and original size }
  484. s^.crc := crc32(s^.crc, start, crclen);
  485. start := s^.stream.next_out;
  486. filecrc := getLong (s);
  487. filelen := getLong (s);
  488. if (s^.crc <> filecrc) or (s^.stream.total_out <> filelen)
  489. then s^.z_err := Z_DATA_ERROR
  490. else begin
  491. { Check for concatenated .gz files: }
  492. check_header(s);
  493. if (s^.z_err = Z_OK) then begin
  494. total_in := s^.stream.total_in;
  495. total_out := s^.stream.total_out;
  496. inflateReset (s^.stream);
  497. s^.stream.total_in := total_in;
  498. s^.stream.total_out := total_out;
  499. s^.crc := crc32 (0, Z_NULL, 0);
  500. end;
  501. end; {IF-THEN-ELSE}
  502. end;
  503. if (s^.z_err <> Z_OK) or (s^.z_eof = true) then break;
  504. end; {WHILE}
  505. crclen := 0;
  506. next_out := s^.stream.next_out;
  507. while (next_out <> start ) do begin
  508. dec (next_out);
  509. inc (crclen); { Hack because Pascal cannot substract pointers }
  510. end;
  511. s^.crc := crc32 (s^.crc, start, crclen);
  512. gzread := int(len - s^.stream.avail_out);
  513. end;
  514. { GZGETC ====================================================================
  515. Reads one byte from the compressed file.
  516. gzgetc returns this byte or -1 in case of end of file or error.
  517. ============================================================================}
  518. function gzgetc (f:gzfile) : int;
  519. var c:byte;
  520. begin
  521. if (gzread (f,@c,1) = 1) then gzgetc := c else gzgetc := -1;
  522. end;
  523. { GZGETS ====================================================================
  524. Reads bytes from the compressed file until len-1 characters are read,
  525. or a newline character is read and transferred to buf, or an end-of-file
  526. condition is encountered. The string is then Null-terminated.
  527. gzgets returns buf, or Z_NULL in case of error.
  528. The current implementation is not optimized at all.
  529. ============================================================================}
  530. function gzgets (f:gzfile; buf:PChar; len:int) : PChar;
  531. var
  532. b : PChar; { start of buffer }
  533. bytes : Int; { number of bytes read by gzread }
  534. gzchar : char; { char read by gzread }
  535. begin
  536. if (buf = Z_NULL) or (len <= 0) then begin
  537. gzgets := Z_NULL;
  538. exit;
  539. end;
  540. b := buf;
  541. repeat
  542. dec (len);
  543. bytes := gzread (f, buf, 1);
  544. gzchar := buf^;
  545. inc (buf);
  546. until (len = 0) or (bytes <> 1) or (gzchar = Chr(13));
  547. buf^ := Chr(0);
  548. if (b = buf) and (len > 0) then gzgets := Z_NULL else gzgets := b;
  549. end;
  550. {$IFNDEF NO_DEFLATE}
  551. { GZWRITE ===================================================================
  552. Writes the given number of uncompressed bytes into the compressed file.
  553. gzwrite returns the number of uncompressed bytes actually written
  554. (0 in case of error).
  555. ============================================================================}
  556. function gzwrite (f:gzfile; buf:voidp; len:uInt) : int;
  557. var
  558. s : gz_streamp;
  559. written : integer;
  560. begin
  561. s := gz_streamp(f);
  562. if (s = NIL) or (s^.mode <> 'w') then begin
  563. gzwrite := Z_STREAM_ERROR;
  564. exit;
  565. end;
  566. s^.stream.next_in := pBytef(buf);
  567. s^.stream.avail_in := len;
  568. while (s^.stream.avail_in <> 0) do begin
  569. if (s^.stream.avail_out = 0) then begin
  570. s^.stream.next_out := s^.outbuf;
  571. blockwrite (s^.gzfile, s^.outbuf^, Z_BUFSIZE, written);
  572. if (written <> Z_BUFSIZE) then begin
  573. s^.z_err := Z_ERRNO;
  574. break;
  575. end;
  576. s^.stream.avail_out := Z_BUFSIZE;
  577. end;
  578. s^.z_err := deflate(s^.stream, Z_NO_FLUSH);
  579. if (s^.z_err <> Z_OK) then break;
  580. end; {WHILE}
  581. s^.crc := crc32(s^.crc, buf, len);
  582. gzwrite := int(len - s^.stream.avail_in);
  583. end;
  584. { ===========================================================================
  585. Converts, formats, and writes the args to the compressed file under
  586. control of the format string, as in fprintf. gzprintf returns the number of
  587. uncompressed bytes actually written (0 in case of error).
  588. }
  589. {$IFDEF GZ_FORMAT_STRING}
  590. function gzprintf (zfile : gzFile;
  591. const format : string;
  592. a : array of int) : int;
  593. var
  594. buf : array[0..Z_PRINTF_BUFSIZE-1] of char;
  595. len : int;
  596. begin
  597. {$ifdef HAS_snprintf}
  598. snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
  599. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  600. {$else}
  601. sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
  602. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  603. {$endif}
  604. len := strlen(buf); { old sprintf doesn't return the nb of bytes written }
  605. if (len <= 0) return 0;
  606. gzprintf := gzwrite(file, buf, len);
  607. end;
  608. {$ENDIF}
  609. { GZPUTC ====================================================================
  610. Writes c, converted to an unsigned char, into the compressed file.
  611. gzputc returns the value that was written, or -1 in case of error.
  612. ============================================================================}
  613. function gzputc (f:gzfile; c:char) : int;
  614. begin
  615. if (gzwrite (f,@c,1) = 1) then
  616. {$IFDEF FPC}
  617. gzputc := int(ord(c))
  618. {$ELSE}
  619. gzputc := int(c)
  620. {$ENDIF}
  621. else
  622. gzputc := -1;
  623. end;
  624. { GZPUTS ====================================================================
  625. Writes the given null-terminated string to the compressed file, excluding
  626. the terminating null character.
  627. gzputs returns the number of characters written, or -1 in case of error.
  628. ============================================================================}
  629. function gzputs (f:gzfile; s:PChar) : int;
  630. begin
  631. gzputs := gzwrite (f, voidp(s), strlen(s));
  632. end;
  633. { DO_FLUSH ==================================================================
  634. Flushes all pending output into the compressed file.
  635. The parameter flush is as in the zdeflate() function.
  636. ============================================================================}
  637. function do_flush (f:gzfile; flush:int) : int;
  638. var
  639. len : uInt;
  640. done : boolean;
  641. s : gz_streamp;
  642. written : integer;
  643. begin
  644. done := false;
  645. s := gz_streamp(f);
  646. if (s = NIL) or (s^.mode <> 'w') then begin
  647. do_flush := Z_STREAM_ERROR;
  648. exit;
  649. end;
  650. s^.stream.avail_in := 0; { should be zero already anyway }
  651. while true do begin
  652. len := Z_BUFSIZE - s^.stream.avail_out;
  653. if (len <> 0) then begin
  654. {$I-}
  655. blockwrite(s^.gzfile, s^.outbuf^, len, written);
  656. {$I+}
  657. if (written <> len) then begin
  658. s^.z_err := Z_ERRNO;
  659. do_flush := Z_ERRNO;
  660. exit;
  661. end;
  662. s^.stream.next_out := s^.outbuf;
  663. s^.stream.avail_out := Z_BUFSIZE;
  664. end;
  665. if (done = true) then break;
  666. s^.z_err := deflate(s^.stream, flush);
  667. { Ignore the second of two consecutive flushes: }
  668. if (len = 0) and (s^.z_err = Z_BUF_ERROR) then s^.z_err := Z_OK;
  669. { deflate has finished flushing only when it hasn't used up
  670. all the available space in the output buffer: }
  671. done := (s^.stream.avail_out <> 0) or (s^.z_err = Z_STREAM_END);
  672. if (s^.z_err <> Z_OK) and (s^.z_err <> Z_STREAM_END) then break;
  673. end; {WHILE}
  674. if (s^.z_err = Z_STREAM_END) then do_flush:=Z_OK else do_flush:=s^.z_err;
  675. end;
  676. { GZFLUSH ===================================================================
  677. Flushes all pending output into the compressed file.
  678. The parameter flush is as in the zdeflate() function.
  679. The return value is the zlib error number (see function gzerror below).
  680. gzflush returns Z_OK if the flush parameter is Z_FINISH and all output
  681. could be flushed.
  682. gzflush should be called only when strictly necessary because it can
  683. degrade compression.
  684. ============================================================================}
  685. function gzflush (f:gzfile; flush:int) : int;
  686. var
  687. err : int;
  688. s : gz_streamp;
  689. begin
  690. s := gz_streamp(f);
  691. err := do_flush (f, flush);
  692. if (err <> 0) then begin
  693. gzflush := err;
  694. exit;
  695. end;
  696. if (s^.z_err = Z_STREAM_END) then gzflush := Z_OK else gzflush := s^.z_err;
  697. end;
  698. {$ENDIF} (* NO DEFLATE *)
  699. { GZREWIND ==================================================================
  700. Rewinds input file.
  701. ============================================================================}
  702. function gzrewind (f:gzFile) : int;
  703. var
  704. s:gz_streamp;
  705. begin
  706. s := gz_streamp(f);
  707. if (s = NIL) or (s^.mode <> 'r') then begin
  708. gzrewind := -1;
  709. exit;
  710. end;
  711. s^.z_err := Z_OK;
  712. s^.z_eof := false;
  713. s^.stream.avail_in := 0;
  714. s^.stream.next_in := s^.inbuf;
  715. if (s^.startpos = 0) then begin { not a compressed file }
  716. {$I-}
  717. seek (s^.gzfile, 0);
  718. {$I+}
  719. gzrewind := 0;
  720. exit;
  721. end;
  722. inflateReset(s^.stream);
  723. {$I-}
  724. seek (s^.gzfile, s^.startpos);
  725. {$I+}
  726. gzrewind := int(IOResult);
  727. exit;
  728. end;
  729. { GZSEEK ====================================================================
  730. Sets the starting position for the next gzread or gzwrite on the given
  731. compressed file. The offset represents a number of bytes from the beginning
  732. of the uncompressed stream.
  733. gzseek returns the resulting offset, or -1 in case of error.
  734. SEEK_END is not implemented, returns error.
  735. In this version of the library, gzseek can be extremely slow.
  736. ============================================================================}
  737. function gzseek (f:gzfile; offset:z_off_t; whence:int) : z_off_t;
  738. var
  739. s : gz_streamp;
  740. size : uInt;
  741. begin
  742. s := gz_streamp(f);
  743. if (s = NIL) or (whence = SEEK_END) or (s^.z_err = Z_ERRNO)
  744. or (s^.z_err = Z_DATA_ERROR) then begin
  745. gzseek := z_off_t(-1);
  746. exit;
  747. end;
  748. if (s^.mode = 'w') then begin
  749. {$IFDEF NO_DEFLATE}
  750. gzseek := z_off_t(-1);
  751. exit;
  752. {$ELSE}
  753. if (whence = SEEK_SET) then dec(offset, s^.stream.total_out);
  754. if (offset < 0) then begin;
  755. gzseek := z_off_t(-1);
  756. exit;
  757. end;
  758. { At this point, offset is the number of zero bytes to write. }
  759. if (s^.inbuf = Z_NULL) then begin
  760. GetMem (s^.inbuf, Z_BUFSIZE);
  761. zmemzero(s^.inbuf, Z_BUFSIZE);
  762. end;
  763. while (offset > 0) do begin
  764. size := Z_BUFSIZE;
  765. if (offset < Z_BUFSIZE) then size := uInt(offset);
  766. size := gzwrite(f, s^.inbuf, size);
  767. if (size = 0) then begin
  768. gzseek := z_off_t(-1);
  769. exit;
  770. end;
  771. dec (offset,size);
  772. end;
  773. gzseek := z_off_t(s^.stream.total_in);
  774. exit;
  775. {$ENDIF}
  776. end;
  777. { Rest of function is for reading only }
  778. { compute absolute position }
  779. if (whence = SEEK_CUR) then inc (offset, s^.stream.total_out);
  780. if (offset < 0) then begin
  781. gzseek := z_off_t(-1);
  782. exit;
  783. end;
  784. if (s^.transparent = true) then begin
  785. s^.stream.avail_in := 0;
  786. s^.stream.next_in := s^.inbuf;
  787. {$I-}
  788. seek (s^.gzfile, offset);
  789. {$I+}
  790. if (IOResult <> 0) then begin
  791. gzseek := z_off_t(-1);
  792. exit;
  793. end;
  794. s^.stream.total_in := uLong(offset);
  795. s^.stream.total_out := uLong(offset);
  796. gzseek := z_off_t(offset);
  797. exit;
  798. end;
  799. { For a negative seek, rewind and use positive seek }
  800. if (uLong(offset) >= s^.stream.total_out)
  801. then dec (offset, s^.stream.total_out)
  802. else if (gzrewind(f) <> 0) then begin
  803. gzseek := z_off_t(-1);
  804. exit;
  805. end;
  806. { offset is now the number of bytes to skip. }
  807. if (offset <> 0) and (s^.outbuf = Z_NULL)
  808. then GetMem (s^.outbuf, Z_BUFSIZE);
  809. while (offset > 0) do begin
  810. size := Z_BUFSIZE;
  811. if (offset < Z_BUFSIZE) then size := int(offset);
  812. size := gzread (f, s^.outbuf, size);
  813. if (size <= 0) then begin
  814. gzseek := z_off_t(-1);
  815. exit;
  816. end;
  817. dec(offset, size);
  818. end;
  819. gzseek := z_off_t(s^.stream.total_out);
  820. end;
  821. { GZTELL ====================================================================
  822. Returns the starting position for the next gzread or gzwrite on the
  823. given compressed file. This position represents a number of bytes in the
  824. uncompressed data stream.
  825. ============================================================================}
  826. function gztell (f:gzfile) : z_off_t;
  827. begin
  828. gztell := gzseek (f, 0, SEEK_CUR);
  829. end;
  830. { GZEOF =====================================================================
  831. Returns TRUE when EOF has previously been detected reading the given
  832. input stream, otherwise FALSE.
  833. ============================================================================}
  834. function gzeof (f:gzfile) : boolean;
  835. var
  836. s:gz_streamp;
  837. begin
  838. s := gz_streamp(f);
  839. if (s=NIL) or (s^.mode<>'r') then
  840. gzeof := false
  841. else
  842. gzeof := s^.z_eof;
  843. end;
  844. { PUTLONG ===================================================================
  845. Outputs a Longint in LSB order to the given file
  846. ============================================================================}
  847. procedure putLong (var f:file; x:uLong);
  848. var
  849. n : int;
  850. c : byte;
  851. begin
  852. for n:=0 to 3 do begin
  853. c := x and $FF;
  854. blockwrite (f, c, 1);
  855. x := x shr 8;
  856. end;
  857. end;
  858. { GZCLOSE ===================================================================
  859. Flushes all pending output if necessary, closes the compressed file
  860. and deallocates all the (de)compression state.
  861. The return value is the zlib error number (see function gzerror below).
  862. ============================================================================}
  863. function gzclose (f:gzFile) : int;
  864. var
  865. err : int;
  866. s : gz_streamp;
  867. begin
  868. s := gz_streamp(f);
  869. if (s = NIL) then begin
  870. gzclose := Z_STREAM_ERROR;
  871. exit;
  872. end;
  873. if (s^.mode = 'w') then begin
  874. {$IFDEF NO_DEFLATE}
  875. gzclose := Z_STREAM_ERROR;
  876. exit;
  877. {$ELSE}
  878. err := do_flush (f, Z_FINISH);
  879. if (err <> Z_OK) then begin
  880. gzclose := destroy (gz_streamp(f));
  881. exit;
  882. end;
  883. putLong (s^.gzfile, s^.crc);
  884. putLong (s^.gzfile, s^.stream.total_in);
  885. {$ENDIF}
  886. end;
  887. gzclose := destroy (gz_streamp(f));
  888. end;
  889. { GZERROR ===================================================================
  890. Returns the error message for the last error which occured on the
  891. given compressed file. errnum is set to zlib error number. If an
  892. error occured in the file system and not in the compression library,
  893. errnum is set to Z_ERRNO and the application may consult errno
  894. to get the exact error code.
  895. ============================================================================}
  896. function gzerror (f:gzfile; var errnum:int) : string;
  897. var
  898. m : string;
  899. s : gz_streamp;
  900. begin
  901. s := gz_streamp(f);
  902. if (s = NIL) then begin
  903. errnum := Z_STREAM_ERROR;
  904. gzerror := zError(Z_STREAM_ERROR);
  905. end;
  906. errnum := s^.z_err;
  907. if (errnum = Z_OK) then begin
  908. gzerror := zError(Z_OK);
  909. exit;
  910. end;
  911. m := s^.stream.msg;
  912. if (errnum = Z_ERRNO) then m := '';
  913. if (m = '') then m := zError(s^.z_err);
  914. s^.msg := s^.path+': '+m;
  915. gzerror := s^.msg;
  916. end;
  917. end.