gzio.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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:integer) : 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 := chr(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=chr(0)) then begin
  140. destroy(s);
  141. gzopen := gzFile(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, s^.path);
  174. {$ifdef unix}
  175. if (fpstat(s^.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 = true) 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. { the following assumes a little endian machine and TP }
  292. x[0] := Byte(get_byte(s));
  293. x[1] := Byte(get_byte(s));
  294. x[2] := Byte(get_byte(s));
  295. c := get_byte(s);
  296. x[3] := Byte(c);
  297. if (c = Z_EOF) then
  298. s^.z_err := Z_DATA_ERROR;
  299. GetLong := cardinal(longint(x));
  300. end;
  301. { CHECK_HEADER ==============================================================
  302. Check the gzip header of a gz_stream opened for reading.
  303. Set the stream mode to transparent if the gzip magic header is not present.
  304. Set s^.err to Z_DATA_ERROR if the magic header is present but the rest of
  305. the header is incorrect.
  306. IN assertion: the stream s has already been created sucessfully;
  307. s^.stream.avail_in is zero for the first time, but may be non-zero
  308. for concatenated .gz files
  309. ============================================================================}
  310. procedure check_header (s:gz_streamp);
  311. var
  312. method : integer; { method byte }
  313. flags : integer; { flags byte }
  314. len : cardinal;
  315. c : integer;
  316. begin
  317. { Check the gzip magic header }
  318. for len := 0 to 1 do begin
  319. c := get_byte(s);
  320. if (c <> gz_magic[len]) then begin
  321. if (len <> 0) then begin
  322. Inc(s^.stream.avail_in);
  323. Dec(s^.stream.next_in);
  324. end;
  325. if (c <> Z_EOF) then begin
  326. Inc(s^.stream.avail_in);
  327. Dec(s^.stream.next_in);
  328. s^.transparent := TRUE;
  329. end;
  330. if (s^.stream.avail_in <> 0) then s^.z_err := Z_OK
  331. else s^.z_err := Z_STREAM_END;
  332. exit;
  333. end;
  334. end;
  335. method := get_byte(s);
  336. flags := get_byte(s);
  337. if (method <> Z_DEFLATED) or ((flags and RESERVED) <> 0) then begin
  338. s^.z_err := Z_DATA_ERROR;
  339. exit;
  340. end;
  341. for len := 0 to 5 do get_byte(s); { Discard time, xflags and OS code }
  342. if ((flags and EXTRA_FIELD) <> 0) then begin { skip the extra field }
  343. len := cardinal(get_byte(s));
  344. len := len + (cardinal(get_byte(s)) shr 8);
  345. { len is garbage if EOF but the loop below will quit anyway }
  346. while (len <> 0) and (get_byte(s) <> Z_EOF) do Dec(len);
  347. end;
  348. if ((flags and ORIG_NAME) <> 0) then begin { skip the original file name }
  349. repeat
  350. c := get_byte(s);
  351. until (c = 0) or (c = Z_EOF);
  352. end;
  353. if ((flags and COMMENT) <> 0) then begin { skip the .gz file comment }
  354. repeat
  355. c := get_byte(s);
  356. until (c = 0) or (c = Z_EOF);
  357. end;
  358. if ((flags and HEAD_CRC) <> 0) then begin { skip the header crc }
  359. get_byte(s);
  360. get_byte(s);
  361. end;
  362. if (s^.z_eof = true) then
  363. s^.z_err := Z_DATA_ERROR
  364. else
  365. s^.z_err := Z_OK;
  366. end;
  367. { DESTROY ===================================================================
  368. Cleanup then free the given gz_stream. Return a zlib error code.
  369. Try freeing in the reverse order of allocations.
  370. ============================================================================}
  371. function destroy (var s:gz_streamp) : integer;
  372. begin
  373. destroy := Z_OK;
  374. if not Assigned (s) then begin
  375. destroy := Z_STREAM_ERROR;
  376. exit;
  377. end;
  378. if (s^.stream.state <> nil) then begin
  379. if (s^.mode = 'w') then begin
  380. {$IFDEF NO_DEFLATE}
  381. destroy := Z_STREAM_ERROR;
  382. {$ELSE}
  383. destroy := deflateEnd(s^.stream);
  384. {$ENDIF}
  385. end
  386. else if (s^.mode = 'r') then begin
  387. destroy := inflateEnd(s^.stream);
  388. end;
  389. end;
  390. if (s^.path <> '') then begin
  391. {$I-}
  392. close(s^.gzfile);
  393. {$I+}
  394. if (IOResult <> 0) then destroy := Z_ERRNO;
  395. end;
  396. if (s^.z_err < 0) then destroy := s^.z_err;
  397. if Assigned (s^.inbuf) then
  398. FreeMem(s^.inbuf, Z_BUFSIZE);
  399. if Assigned (s^.outbuf) then
  400. FreeMem(s^.outbuf, Z_BUFSIZE);
  401. FreeMem(s, sizeof(gz_stream));
  402. end;
  403. { GZREAD ====================================================================
  404. Reads the given number of uncompressed bytes from the compressed file.
  405. If the input file was not in gzip format, gzread copies the given number
  406. of bytes into the buffer.
  407. gzread returns the number of uncompressed bytes actually read
  408. (0 for end of file, -1 for error).
  409. ============================================================================}
  410. function gzread (f:gzFile; buf:pointer; len:cardinal) : integer;
  411. var
  412. s : gz_streamp;
  413. start : Pbyte;
  414. next_out : Pbyte;
  415. n : cardinal;
  416. crclen : cardinal; { Buffer length to update CRC32 }
  417. filecrc : cardinal; { CRC32 stored in GZIP'ed file }
  418. filelen : cardinal; { Total lenght of uncompressed file }
  419. bytes : integer; { bytes actually read in I/O blockread }
  420. total_in : cardinal;
  421. total_out : cardinal;
  422. begin
  423. s := gz_streamp(f);
  424. start := Pbyte(buf); { starting point for crc computation }
  425. if (s = nil) or (s^.mode <> 'r') then begin
  426. gzread := Z_STREAM_ERROR;
  427. exit;
  428. end;
  429. if (s^.z_err = Z_DATA_ERROR) or (s^.z_err = Z_ERRNO) then begin
  430. gzread := -1;
  431. exit;
  432. end;
  433. if (s^.z_err = Z_STREAM_END) then begin
  434. gzread := 0; { EOF }
  435. exit;
  436. end;
  437. s^.stream.next_out := Pbyte(buf);
  438. s^.stream.avail_out := len;
  439. while (s^.stream.avail_out <> 0) do begin
  440. if (s^.transparent = true) then begin
  441. { Copy first the lookahead bytes: }
  442. n := s^.stream.avail_in;
  443. if (n > s^.stream.avail_out) then n := s^.stream.avail_out;
  444. if (n > 0) then begin
  445. move(s^.stream.next_in^,s^.stream.next_out^,n);
  446. inc (s^.stream.next_out, n);
  447. inc (s^.stream.next_in, n);
  448. dec (s^.stream.avail_out, n);
  449. dec (s^.stream.avail_in, n);
  450. end;
  451. if (s^.stream.avail_out > 0) then begin
  452. blockread (s^.gzfile, s^.stream.next_out^, s^.stream.avail_out, bytes);
  453. dec (s^.stream.avail_out, cardinal(bytes));
  454. end;
  455. dec (len, s^.stream.avail_out);
  456. inc (s^.stream.total_in, cardinal(len));
  457. inc (s^.stream.total_out, cardinal(len));
  458. gzread := integer(len);
  459. exit;
  460. end; { IF transparent }
  461. if (s^.stream.avail_in = 0) and (s^.z_eof = false) then begin
  462. {$I-}
  463. blockread (s^.gzfile, s^.inbuf^, Z_BUFSIZE, s^.stream.avail_in);
  464. {$I+}
  465. if (s^.stream.avail_in = 0) then begin
  466. s^.z_eof := true;
  467. if (IOResult <> 0) then begin
  468. s^.z_err := Z_ERRNO;
  469. break;
  470. end;
  471. end;
  472. s^.stream.next_in := s^.inbuf;
  473. end;
  474. s^.z_err := inflate(s^.stream, Z_NO_FLUSH);
  475. if (s^.z_err = Z_STREAM_END) then begin
  476. crclen := 0;
  477. next_out := s^.stream.next_out;
  478. while (next_out <> start ) do begin
  479. dec (next_out);
  480. inc (crclen); { Hack because Pascal cannot substract pointers }
  481. end;
  482. { Check CRC and original size }
  483. s^.crc := crc32(s^.crc, start, crclen);
  484. start := s^.stream.next_out;
  485. filecrc := getLong (s);
  486. filelen := getLong (s);
  487. if (s^.crc <> filecrc) or (s^.stream.total_out <> filelen)
  488. then s^.z_err := Z_DATA_ERROR
  489. else begin
  490. { Check for concatenated .gz files: }
  491. check_header(s);
  492. if (s^.z_err = Z_OK) then begin
  493. total_in := s^.stream.total_in;
  494. total_out := s^.stream.total_out;
  495. inflateReset (s^.stream);
  496. s^.stream.total_in := total_in;
  497. s^.stream.total_out := total_out;
  498. s^.crc := crc32 (0, nil, 0);
  499. end;
  500. end; {IF-THEN-ELSE}
  501. end;
  502. if (s^.z_err <> Z_OK) or (s^.z_eof = true) then break;
  503. end; {WHILE}
  504. crclen := 0;
  505. next_out := s^.stream.next_out;
  506. while (next_out <> start ) do begin
  507. dec (next_out);
  508. inc (crclen); { Hack because Pascal cannot substract pointers }
  509. end;
  510. s^.crc := crc32 (s^.crc, start, crclen);
  511. gzread := integer(len - s^.stream.avail_out);
  512. end;
  513. { GZGETC ====================================================================
  514. Reads one byte from the compressed file.
  515. gzgetc returns this byte or -1 in case of end of file or error.
  516. ============================================================================}
  517. function gzgetc (f:gzfile) : integer;
  518. var c:byte;
  519. begin
  520. if (gzread (f,@c,1) = 1) then gzgetc := c else gzgetc := -1;
  521. end;
  522. { GZGETS ====================================================================
  523. Reads bytes from the compressed file until len-1 characters are read,
  524. or a newline character is read and transferred to buf, or an end-of-file
  525. condition is encountered. The string is then Null-terminated.
  526. gzgets returns buf, or nil in case of error.
  527. The current implementation is not optimized at all.
  528. ============================================================================}
  529. function gzgets (f:gzfile; buf:Pchar; len:integer) : Pchar;
  530. var
  531. b : Pchar; { start of buffer }
  532. bytes : integer; { number of bytes read by gzread }
  533. gzchar : char; { char read by gzread }
  534. begin
  535. if (buf = nil) or (len <= 0) then begin
  536. gzgets := nil;
  537. exit;
  538. end;
  539. b := buf;
  540. repeat
  541. dec (len);
  542. bytes := gzread (f, buf, 1);
  543. gzchar := buf^;
  544. inc (buf);
  545. until (len = 0) or (bytes <> 1) or (gzchar = Chr(13));
  546. buf^ := Chr(0);
  547. if (b = buf) and (len > 0) then gzgets := nil else gzgets := b;
  548. end;
  549. {$IFNDEF NO_DEFLATE}
  550. { GZWRITE ===================================================================
  551. Writes the given number of uncompressed bytes into the compressed file.
  552. gzwrite returns the number of uncompressed bytes actually written
  553. (0 in case of error).
  554. ============================================================================}
  555. function gzwrite (f:gzfile; buf:pointer; len:cardinal) : integer;
  556. var
  557. s : gz_streamp;
  558. written : integer;
  559. begin
  560. s := gz_streamp(f);
  561. if (s = nil) or (s^.mode <> 'w') then begin
  562. gzwrite := Z_STREAM_ERROR;
  563. exit;
  564. end;
  565. s^.stream.next_in := Pbyte(buf);
  566. s^.stream.avail_in := len;
  567. while (s^.stream.avail_in <> 0) do begin
  568. if (s^.stream.avail_out = 0) then begin
  569. s^.stream.next_out := s^.outbuf;
  570. blockwrite (s^.gzfile, s^.outbuf^, Z_BUFSIZE, written);
  571. if (written <> Z_BUFSIZE) then begin
  572. s^.z_err := Z_ERRNO;
  573. break;
  574. end;
  575. s^.stream.avail_out := Z_BUFSIZE;
  576. end;
  577. s^.z_err := deflate(s^.stream, Z_NO_FLUSH);
  578. if (s^.z_err <> Z_OK) then break;
  579. end; {WHILE}
  580. s^.crc := crc32(s^.crc, buf, len);
  581. gzwrite := integer(len - s^.stream.avail_in);
  582. end;
  583. { ===========================================================================
  584. Converts, formats, and writes the args to the compressed file under
  585. control of the format string, as in fprintf. gzprintf returns the number of
  586. uncompressed bytes actually written (0 in case of error).
  587. }
  588. {$IFDEF GZ_FORMAT_STRING}
  589. function gzprintf (zfile : gzFile;
  590. const format : string;
  591. a : array of integer) : integer;
  592. var
  593. buf : array[0..Z_PRINTF_BUFSIZE-1] of char;
  594. len : integer;
  595. begin
  596. {$ifdef HAS_snprintf}
  597. snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
  598. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  599. {$else}
  600. sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
  601. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  602. {$endif}
  603. len := strlen(buf); { old sprintf doesn't return the nb of bytes written }
  604. if (len <= 0) return 0;
  605. gzprintf := gzwrite(file, buf, len);
  606. end;
  607. {$ENDIF}
  608. { GZPUTC ====================================================================
  609. Writes c, converted to an unsigned char, into the compressed file.
  610. gzputc returns the value that was written, or -1 in case of error.
  611. ============================================================================}
  612. function gzputc (f:gzfile; c:char) : integer;
  613. begin
  614. if (gzwrite (f,@c,1) = 1) then
  615. {$IFDEF FPC}
  616. gzputc := integer(ord(c))
  617. {$ELSE}
  618. gzputc := integer(c)
  619. {$ENDIF}
  620. else
  621. gzputc := -1;
  622. end;
  623. { GZPUTS ====================================================================
  624. Writes the given null-terminated string to the compressed file, excluding
  625. the terminating null character.
  626. gzputs returns the number of characters written, or -1 in case of error.
  627. ============================================================================}
  628. function gzputs (f:gzfile; s:Pchar) : integer;
  629. begin
  630. gzputs := gzwrite (f, pointer(s), strlen(s));
  631. end;
  632. { DO_FLUSH ==================================================================
  633. Flushes all pending output into the compressed file.
  634. The parameter flush is as in the zdeflate() function.
  635. ============================================================================}
  636. function do_flush (f:gzfile; flush:integer) : integer;
  637. var
  638. len : cardinal;
  639. done : boolean;
  640. s : gz_streamp;
  641. written : integer;
  642. begin
  643. done := false;
  644. s := gz_streamp(f);
  645. if (s = nil) or (s^.mode <> 'w') then begin
  646. do_flush := Z_STREAM_ERROR;
  647. exit;
  648. end;
  649. s^.stream.avail_in := 0; { should be zero already anyway }
  650. while true do begin
  651. len := Z_BUFSIZE - s^.stream.avail_out;
  652. if (len <> 0) then begin
  653. {$I-}
  654. blockwrite(s^.gzfile, s^.outbuf^, len, written);
  655. {$I+}
  656. if (written <> len) then begin
  657. s^.z_err := Z_ERRNO;
  658. do_flush := Z_ERRNO;
  659. exit;
  660. end;
  661. s^.stream.next_out := s^.outbuf;
  662. s^.stream.avail_out := Z_BUFSIZE;
  663. end;
  664. if (done = true) then break;
  665. s^.z_err := deflate(s^.stream, flush);
  666. { Ignore the second of two consecutive flushes: }
  667. if (len = 0) and (s^.z_err = Z_BUF_ERROR) then s^.z_err := Z_OK;
  668. { deflate has finished flushing only when it hasn't used up
  669. all the available space in the output buffer: }
  670. done := (s^.stream.avail_out <> 0) or (s^.z_err = Z_STREAM_END);
  671. if (s^.z_err <> Z_OK) and (s^.z_err <> Z_STREAM_END) then break;
  672. end; {WHILE}
  673. if (s^.z_err = Z_STREAM_END) then do_flush:=Z_OK else do_flush:=s^.z_err;
  674. end;
  675. { GZFLUSH ===================================================================
  676. Flushes all pending output into the compressed file.
  677. The parameter flush is as in the zdeflate() function.
  678. The return value is the zlib error number (see function gzerror below).
  679. gzflush returns Z_OK if the flush parameter is Z_FINISH and all output
  680. could be flushed.
  681. gzflush should be called only when strictly necessary because it can
  682. degrade compression.
  683. ============================================================================}
  684. function gzflush (f:gzfile; flush:integer) : integer;
  685. var
  686. err : integer;
  687. s : gz_streamp;
  688. begin
  689. s := gz_streamp(f);
  690. err := do_flush (f, flush);
  691. if (err <> 0) then begin
  692. gzflush := err;
  693. exit;
  694. end;
  695. if (s^.z_err = Z_STREAM_END) then gzflush := Z_OK else gzflush := s^.z_err;
  696. end;
  697. {$ENDIF} (* NO DEFLATE *)
  698. { GZREWIND ==================================================================
  699. Rewinds input file.
  700. ============================================================================}
  701. function gzrewind (f:gzFile) : integer;
  702. var
  703. s:gz_streamp;
  704. begin
  705. s := gz_streamp(f);
  706. if (s = nil) or (s^.mode <> 'r') then begin
  707. gzrewind := -1;
  708. exit;
  709. end;
  710. s^.z_err := Z_OK;
  711. s^.z_eof := false;
  712. s^.stream.avail_in := 0;
  713. s^.stream.next_in := s^.inbuf;
  714. if (s^.startpos = 0) then begin { not a compressed file }
  715. {$I-}
  716. seek (s^.gzfile, 0);
  717. {$I+}
  718. gzrewind := 0;
  719. exit;
  720. end;
  721. inflateReset(s^.stream);
  722. {$I-}
  723. seek (s^.gzfile, s^.startpos);
  724. {$I+}
  725. gzrewind := integer(IOResult);
  726. exit;
  727. end;
  728. { GZSEEK ====================================================================
  729. Sets the starting position for the next gzread or gzwrite on the given
  730. compressed file. The offset represents a number of bytes from the beginning
  731. of the uncompressed stream.
  732. gzseek returns the resulting offset, or -1 in case of error.
  733. SEEK_END is not implemented, returns error.
  734. In this version of the library, gzseek can be extremely slow.
  735. ============================================================================}
  736. function gzseek (f:gzfile; offset:z_off_t; whence:integer) : z_off_t;
  737. var
  738. s : gz_streamp;
  739. size : cardinal;
  740. begin
  741. s := gz_streamp(f);
  742. if (s = nil) or (whence = SEEK_END) or (s^.z_err = Z_ERRNO)
  743. or (s^.z_err = Z_DATA_ERROR) then begin
  744. gzseek := z_off_t(-1);
  745. exit;
  746. end;
  747. if (s^.mode = 'w') then begin
  748. {$IFDEF NO_DEFLATE}
  749. gzseek := z_off_t(-1);
  750. exit;
  751. {$ELSE}
  752. if (whence = SEEK_SET) then dec(offset, s^.stream.total_out);
  753. if (offset < 0) then begin;
  754. gzseek := z_off_t(-1);
  755. exit;
  756. end;
  757. { At this point, offset is the number of zero bytes to write. }
  758. if s^.inbuf=nil then begin
  759. getmem(s^.inbuf,Z_BUFSIZE);
  760. fillchar(s^.inbuf^,Z_BUFSIZE,0);
  761. end;
  762. while (offset > 0) do begin
  763. size := Z_BUFSIZE;
  764. if (offset < Z_BUFSIZE) then size := cardinal(offset);
  765. size := gzwrite(f, s^.inbuf, size);
  766. if (size = 0) then begin
  767. gzseek := z_off_t(-1);
  768. exit;
  769. end;
  770. dec (offset,size);
  771. end;
  772. gzseek := z_off_t(s^.stream.total_in);
  773. exit;
  774. {$ENDIF}
  775. end;
  776. { Rest of function is for reading only }
  777. { compute absolute position }
  778. if (whence = SEEK_CUR) then inc (offset, s^.stream.total_out);
  779. if (offset < 0) then begin
  780. gzseek := z_off_t(-1);
  781. exit;
  782. end;
  783. if (s^.transparent = true) then begin
  784. s^.stream.avail_in := 0;
  785. s^.stream.next_in := s^.inbuf;
  786. {$I-}
  787. seek (s^.gzfile, offset);
  788. {$I+}
  789. if (IOResult <> 0) then begin
  790. gzseek := z_off_t(-1);
  791. exit;
  792. end;
  793. s^.stream.total_in := cardinal(offset);
  794. s^.stream.total_out := cardinal(offset);
  795. gzseek := z_off_t(offset);
  796. exit;
  797. end;
  798. { For a negative seek, rewind and use positive seek }
  799. if (cardinal(offset) >= s^.stream.total_out)
  800. then dec (offset, s^.stream.total_out)
  801. else if (gzrewind(f) <> 0) then begin
  802. gzseek := z_off_t(-1);
  803. exit;
  804. end;
  805. { offset is now the number of bytes to skip. }
  806. if (offset <> 0) and (s^.outbuf = nil)
  807. then GetMem (s^.outbuf, Z_BUFSIZE);
  808. while (offset > 0) do begin
  809. size := Z_BUFSIZE;
  810. if (offset < Z_BUFSIZE) then size := integer(offset);
  811. size := gzread (f, s^.outbuf, size);
  812. if (size <= 0) then begin
  813. gzseek := z_off_t(-1);
  814. exit;
  815. end;
  816. dec(offset, size);
  817. end;
  818. gzseek := z_off_t(s^.stream.total_out);
  819. end;
  820. { GZTELL ====================================================================
  821. Returns the starting position for the next gzread or gzwrite on the
  822. given compressed file. This position represents a number of bytes in the
  823. uncompressed data stream.
  824. ============================================================================}
  825. function gztell (f:gzfile) : z_off_t;
  826. begin
  827. gztell := gzseek (f, 0, SEEK_CUR);
  828. end;
  829. { GZEOF =====================================================================
  830. Returns TRUE when EOF has previously been detected reading the given
  831. input stream, otherwise FALSE.
  832. ============================================================================}
  833. function gzeof (f:gzfile) : boolean;
  834. var
  835. s:gz_streamp;
  836. begin
  837. s := gz_streamp(f);
  838. if (s=nil) or (s^.mode<>'r') then
  839. gzeof := false
  840. else
  841. gzeof := s^.z_eof;
  842. end;
  843. { PUTLONG ===================================================================
  844. Outputs a Longint in LSB order to the given file
  845. ============================================================================}
  846. procedure putLong (var f:file; x:cardinal);
  847. var
  848. n : integer;
  849. c : byte;
  850. begin
  851. for n:=0 to 3 do begin
  852. c := x and $FF;
  853. blockwrite (f, c, 1);
  854. x := x shr 8;
  855. end;
  856. end;
  857. { GZCLOSE ===================================================================
  858. Flushes all pending output if necessary, closes the compressed file
  859. and deallocates all the (de)compression state.
  860. The return value is the zlib error number (see function gzerror below).
  861. ============================================================================}
  862. function gzclose (f:gzFile) : integer;
  863. var
  864. err : integer;
  865. s : gz_streamp;
  866. begin
  867. s := gz_streamp(f);
  868. if (s = nil) then begin
  869. gzclose := Z_STREAM_ERROR;
  870. exit;
  871. end;
  872. if (s^.mode = 'w') then begin
  873. {$IFDEF NO_DEFLATE}
  874. gzclose := Z_STREAM_ERROR;
  875. exit;
  876. {$ELSE}
  877. err := do_flush (f, Z_FINISH);
  878. if (err <> Z_OK) then begin
  879. gzclose := destroy (gz_streamp(f));
  880. exit;
  881. end;
  882. putLong (s^.gzfile, s^.crc);
  883. putLong (s^.gzfile, s^.stream.total_in);
  884. {$ENDIF}
  885. end;
  886. gzclose := destroy (gz_streamp(f));
  887. end;
  888. { GZERROR ===================================================================
  889. Returns the error message for the last error which occured on the
  890. given compressed file. errnum is set to zlib error number. If an
  891. error occured in the file system and not in the compression library,
  892. errnum is set to Z_ERRNO and the application may consult errno
  893. to get the exact error code.
  894. ============================================================================}
  895. function gzerror (f:gzfile; var errnum:integer) : string;
  896. var
  897. m : string;
  898. s : gz_streamp;
  899. begin
  900. s := gz_streamp(f);
  901. if (s = nil) then begin
  902. errnum := Z_STREAM_ERROR;
  903. gzerror := zError(Z_STREAM_ERROR);
  904. end;
  905. errnum := s^.z_err;
  906. if (errnum = Z_OK) then begin
  907. gzerror := zError(Z_OK);
  908. exit;
  909. end;
  910. m := s^.stream.msg;
  911. if (errnum = Z_ERRNO) then m := '';
  912. if (m = '') then m := zError(s^.z_err);
  913. s^.msg := s^.path+': '+m;
  914. gzerror := s^.msg;
  915. end;
  916. end.