gzio.pas 31 KB

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