gzio.pas 31 KB

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