zinflate.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. Unit zInflate;
  2. { inflate.c -- zlib interface to inflate modules
  3. Copyright (C) 1995-1998 Mark Adler
  4. Pascal tranlastion
  5. Copyright (C) 1998 by Jacques Nomssi Nzali
  6. For conditions of distribution and use, see copyright notice in readme.txt
  7. }
  8. interface
  9. {$I zconf.inc}
  10. uses
  11. zutil, zbase, infblock, infutil;
  12. function inflateInit(var z : z_stream) : int;
  13. { Initializes the internal stream state for decompression. The fields
  14. zalloc, zfree and opaque must be initialized before by the caller. If
  15. zalloc and zfree are set to Z_NULL, inflateInit updates them to use default
  16. allocation functions.
  17. inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
  18. enough memory, Z_VERSION_ERROR if the zlib library version is incompatible
  19. with the version assumed by the caller. msg is set to null if there is no
  20. error message. inflateInit does not perform any decompression: this will be
  21. done by inflate(). }
  22. function inflateInit_(z : z_streamp;
  23. const version : string;
  24. stream_size : int) : int;
  25. function inflateInit2_(var z: z_stream;
  26. w : int;
  27. const version : string;
  28. stream_size : int) : int;
  29. {
  30. This is another version of inflateInit with an extra parameter. The
  31. fields next_in, avail_in, zalloc, zfree and opaque must be initialized
  32. before by the caller.
  33. The windowBits parameter is the base two logarithm of the maximum window
  34. size (the size of the history buffer). It should be in the range 8..15 for
  35. this version of the library. The default value is 15 if inflateInit is used
  36. instead. If a compressed stream with a larger window size is given as
  37. input, inflate() will return with the error code Z_DATA_ERROR instead of
  38. trying to allocate a larger window.
  39. inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  40. memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
  41. memLevel). msg is set to null if there is no error message. inflateInit2
  42. does not perform any decompression apart from reading the zlib header if
  43. present: this will be done by inflate(). (So next_in and avail_in may be
  44. modified, but next_out and avail_out are unchanged.)
  45. }
  46. function inflateEnd(var z : z_stream) : int;
  47. {
  48. All dynamically allocated data structures for this stream are freed.
  49. This function discards any unprocessed input and does not flush any
  50. pending output.
  51. inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
  52. was inconsistent. In the error case, msg may be set but then points to a
  53. static string (which must not be deallocated).
  54. }
  55. function inflateReset(var z : z_stream) : int;
  56. {
  57. This function is equivalent to inflateEnd followed by inflateInit,
  58. but does not free and reallocate all the internal decompression state.
  59. The stream will keep attributes that may have been set by inflateInit2.
  60. inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  61. stream state was inconsistent (such as zalloc or state being NULL).
  62. }
  63. function inflate(var z : z_stream;
  64. f : int) : int;
  65. {
  66. inflate decompresses as much data as possible, and stops when the input
  67. buffer becomes empty or the output buffer becomes full. It may introduce
  68. some output latency (reading input without producing any output)
  69. except when forced to flush.
  70. The detailed semantics are as follows. inflate performs one or both of the
  71. following actions:
  72. - Decompress more input starting at next_in and update next_in and avail_in
  73. accordingly. If not all input can be processed (because there is not
  74. enough room in the output buffer), next_in is updated and processing
  75. will resume at this point for the next call of inflate().
  76. - Provide more output starting at next_out and update next_out and avail_out
  77. accordingly. inflate() provides as much output as possible, until there
  78. is no more input data or no more space in the output buffer (see below
  79. about the flush parameter).
  80. Before the call of inflate(), the application should ensure that at least
  81. one of the actions is possible, by providing more input and/or consuming
  82. more output, and updating the next_* and avail_* values accordingly.
  83. The application can consume the uncompressed output when it wants, for
  84. example when the output buffer is full (avail_out == 0), or after each
  85. call of inflate(). If inflate returns Z_OK and with zero avail_out, it
  86. must be called again after making room in the output buffer because there
  87. might be more output pending.
  88. If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
  89. output as possible to the output buffer. The flushing behavior of inflate is
  90. not specified for values of the flush parameter other than Z_SYNC_FLUSH
  91. and Z_FINISH, but the current implementation actually flushes as much output
  92. as possible anyway.
  93. inflate() should normally be called until it returns Z_STREAM_END or an
  94. error. However if all decompression is to be performed in a single step
  95. (a single call of inflate), the parameter flush should be set to
  96. Z_FINISH. In this case all pending input is processed and all pending
  97. output is flushed; avail_out must be large enough to hold all the
  98. uncompressed data. (The size of the uncompressed data may have been saved
  99. by the compressor for this purpose.) The next operation on this stream must
  100. be inflateEnd to deallocate the decompression state. The use of Z_FINISH
  101. is never required, but can be used to inform inflate that a faster routine
  102. may be used for the single inflate() call.
  103. If a preset dictionary is needed at this point (see inflateSetDictionary
  104. below), inflate sets strm-adler to the adler32 checksum of the
  105. dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
  106. it sets strm->adler to the adler32 checksum of all output produced
  107. so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
  108. an error code as described below. At the end of the stream, inflate()
  109. checks that its computed adler32 checksum is equal to that saved by the
  110. compressor and returns Z_STREAM_END only if the checksum is correct.
  111. inflate() returns Z_OK if some progress has been made (more input processed
  112. or more output produced), Z_STREAM_END if the end of the compressed data has
  113. been reached and all uncompressed output has been produced, Z_NEED_DICT if a
  114. preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
  115. corrupted (input stream not conforming to the zlib format or incorrect
  116. adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
  117. (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
  118. enough memory, Z_BUF_ERROR if no progress is possible or if there was not
  119. enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
  120. case, the application may then call inflateSync to look for a good
  121. compression block.
  122. }
  123. function inflateSetDictionary(var z : z_stream;
  124. dictionary : pBytef; {const array of byte}
  125. dictLength : uInt) : int;
  126. {
  127. Initializes the decompression dictionary from the given uncompressed byte
  128. sequence. This function must be called immediately after a call of inflate
  129. if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
  130. can be determined from the Adler32 value returned by this call of
  131. inflate. The compressor and decompressor must use exactly the same
  132. dictionary (see deflateSetDictionary).
  133. inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
  134. parameter is invalid (such as NULL dictionary) or the stream state is
  135. inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
  136. expected one (incorrect Adler32 value). inflateSetDictionary does not
  137. perform any decompression: this will be done by subsequent calls of
  138. inflate().
  139. }
  140. function inflateSync(var z : z_stream) : int;
  141. {
  142. Skips invalid compressed data until a full flush point (see above the
  143. description of deflate with Z_FULL_FLUSH) can be found, or until all
  144. available input is skipped. No output is provided.
  145. inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
  146. if no more input was provided, Z_DATA_ERROR if no flush point has been found,
  147. or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
  148. case, the application may save the current current value of total_in which
  149. indicates where valid compressed data was found. In the error case, the
  150. application may repeatedly call inflateSync, providing more input each time,
  151. until success or end of the input data.
  152. }
  153. function inflateSyncPoint(var z : z_stream) : int;
  154. implementation
  155. uses
  156. adler;
  157. function inflateReset(var z : z_stream) : int;
  158. begin
  159. if (z.state = Z_NULL) then
  160. begin
  161. inflateReset := Z_STREAM_ERROR;
  162. exit;
  163. end;
  164. z.total_out := 0;
  165. z.total_in := 0;
  166. z.msg := '';
  167. if z.state^.nowrap then
  168. z.state^.mode := BLOCKS
  169. else
  170. z.state^.mode := METHOD;
  171. inflate_blocks_reset(z.state^.blocks^, z, Z_NULL);
  172. {$IFDEF DEBUG}
  173. Tracev('inflate: reset');
  174. {$ENDIF}
  175. inflateReset := Z_OK;
  176. end;
  177. function inflateEnd(var z : z_stream) : int;
  178. begin
  179. if (z.state = Z_NULL) or not Assigned(z.zfree) then
  180. begin
  181. inflateEnd := Z_STREAM_ERROR;
  182. exit;
  183. end;
  184. if (z.state^.blocks <> Z_NULL) then
  185. inflate_blocks_free(z.state^.blocks, z);
  186. ZFREE(z, z.state);
  187. z.state := Z_NULL;
  188. {$IFDEF DEBUG}
  189. Tracev('inflate: end');
  190. {$ENDIF}
  191. inflateEnd := Z_OK;
  192. end;
  193. function inflateInit2_(var z: z_stream;
  194. w : int;
  195. const version : string;
  196. stream_size : int) : int;
  197. begin
  198. if (version = '') or (version[1] <> ZLIB_VERSION[1]) or
  199. (stream_size <> sizeof(z_stream)) then
  200. begin
  201. inflateInit2_ := Z_VERSION_ERROR;
  202. exit;
  203. end;
  204. { initialize state }
  205. { SetLength(strm.msg, 255); }
  206. z.msg := '';
  207. {$ifdef fpc}
  208. if not Assigned(z.zalloc) then
  209. begin
  210. z.zalloc := @zcalloc;
  211. z.opaque := voidpf(0);
  212. end;
  213. if not Assigned(z.zfree) then
  214. z.zfree := @zcfree;
  215. {$else}
  216. if not Assigned(z.zalloc) then
  217. begin
  218. z.zalloc := zcalloc;
  219. z.opaque := voidpf(0);
  220. end;
  221. if not Assigned(z.zfree) then
  222. z.zfree := zcfree;
  223. {$endif}
  224. z.state := pInternal_state( ZALLOC(z,1,sizeof(internal_state)) );
  225. if (z.state = Z_NULL) then
  226. begin
  227. inflateInit2_ := Z_MEM_ERROR;
  228. exit;
  229. end;
  230. z.state^.blocks := Z_NULL;
  231. { handle undocumented nowrap option (no zlib header or check) }
  232. z.state^.nowrap := FALSE;
  233. if (w < 0) then
  234. begin
  235. w := - w;
  236. z.state^.nowrap := TRUE;
  237. end;
  238. { set window size }
  239. if (w < 8) or (w > 15) then
  240. begin
  241. inflateEnd(z);
  242. inflateInit2_ := Z_STREAM_ERROR;
  243. exit;
  244. end;
  245. z.state^.wbits := uInt(w);
  246. { create inflate_blocks state }
  247. if z.state^.nowrap then
  248. z.state^.blocks := inflate_blocks_new(z, NIL, uInt(1) shl w)
  249. else
  250. {$ifdef fpc}
  251. z.state^.blocks := inflate_blocks_new(z, @adler32, uInt(1) shl w);
  252. {$else}
  253. z.state^.blocks := inflate_blocks_new(z, adler32, uInt(1) shl w);
  254. {$endif}
  255. if (z.state^.blocks = Z_NULL) then
  256. begin
  257. inflateEnd(z);
  258. inflateInit2_ := Z_MEM_ERROR;
  259. exit;
  260. end;
  261. {$IFDEF DEBUG}
  262. Tracev('inflate: allocated');
  263. {$ENDIF}
  264. { reset state }
  265. inflateReset(z);
  266. inflateInit2_ := Z_OK;
  267. end;
  268. function inflateInit(var z : z_stream) : int;
  269. { inflateInit is a macro to allow checking the zlib version
  270. and the compiler's view of z_stream:
  271. }
  272. begin
  273. inflateInit := inflateInit2_(z, DEF_WBITS, ZLIB_VERSION, sizeof(z_stream));
  274. end;
  275. function inflateInit_(z : z_streamp;
  276. const version : string;
  277. stream_size : int) : int;
  278. begin
  279. { initialize state }
  280. if (z = Z_NULL) then
  281. inflateInit_ := Z_STREAM_ERROR
  282. else
  283. inflateInit_ := inflateInit2_(z^, DEF_WBITS, version, stream_size);
  284. end;
  285. function inflate(var z : z_stream;
  286. f : int) : int;
  287. var
  288. r : int;
  289. b : uInt;
  290. begin
  291. if (z.state = Z_NULL) or (z.next_in = Z_NULL) then
  292. begin
  293. inflate := Z_STREAM_ERROR;
  294. exit;
  295. end;
  296. if f = Z_FINISH then
  297. f := Z_BUF_ERROR
  298. else
  299. f := Z_OK;
  300. r := Z_BUF_ERROR;
  301. while True do
  302. case (z.state^.mode) of
  303. BLOCKS:
  304. begin
  305. r := inflate_blocks(z.state^.blocks^, z, r);
  306. if (r = Z_DATA_ERROR) then
  307. begin
  308. z.state^.mode := BAD;
  309. z.state^.sub.marker := 0; { can try inflateSync }
  310. continue; { break C-switch }
  311. end;
  312. if (r = Z_OK) then
  313. r := f;
  314. if (r <> Z_STREAM_END) then
  315. begin
  316. inflate := r;
  317. exit;
  318. end;
  319. r := f;
  320. inflate_blocks_reset(z.state^.blocks^, z, @z.state^.sub.check.was);
  321. if (z.state^.nowrap) then
  322. begin
  323. z.state^.mode := DONE;
  324. continue; { break C-switch }
  325. end;
  326. z.state^.mode := CHECK4; { falltrough }
  327. end;
  328. CHECK4:
  329. begin
  330. {NEEDBYTE}
  331. if (z.avail_in = 0) then
  332. begin
  333. inflate := r;
  334. exit;
  335. end;
  336. r := f;
  337. {z.state^.sub.check.need := uLong(NEXTBYTE(z)) shl 24;}
  338. Dec(z.avail_in);
  339. Inc(z.total_in);
  340. z.state^.sub.check.need := uLong(z.next_in^) shl 24;
  341. Inc(z.next_in);
  342. z.state^.mode := CHECK3; { falltrough }
  343. end;
  344. CHECK3:
  345. begin
  346. {NEEDBYTE}
  347. if (z.avail_in = 0) then
  348. begin
  349. inflate := r;
  350. exit;
  351. end;
  352. r := f;
  353. {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 16);}
  354. Dec(z.avail_in);
  355. Inc(z.total_in);
  356. Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 16);
  357. Inc(z.next_in);
  358. z.state^.mode := CHECK2; { falltrough }
  359. end;
  360. CHECK2:
  361. begin
  362. {NEEDBYTE}
  363. if (z.avail_in = 0) then
  364. begin
  365. inflate := r;
  366. exit;
  367. end;
  368. r := f;
  369. {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 8);}
  370. Dec(z.avail_in);
  371. Inc(z.total_in);
  372. Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 8);
  373. Inc(z.next_in);
  374. z.state^.mode := CHECK1; { falltrough }
  375. end;
  376. CHECK1:
  377. begin
  378. {NEEDBYTE}
  379. if (z.avail_in = 0) then
  380. begin
  381. inflate := r;
  382. exit;
  383. end;
  384. r := f;
  385. {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) );}
  386. Dec(z.avail_in);
  387. Inc(z.total_in);
  388. Inc(z.state^.sub.check.need, uLong(z.next_in^) );
  389. Inc(z.next_in);
  390. if (z.state^.sub.check.was <> z.state^.sub.check.need) then
  391. begin
  392. z.state^.mode := BAD;
  393. z.msg := 'incorrect data check';
  394. z.state^.sub.marker := 5; { can't try inflateSync }
  395. continue; { break C-switch }
  396. end;
  397. {$IFDEF DEBUG}
  398. Tracev('inflate: zlib check ok');
  399. {$ENDIF}
  400. z.state^.mode := DONE; { falltrough }
  401. end;
  402. DONE:
  403. begin
  404. inflate := Z_STREAM_END;
  405. exit;
  406. end;
  407. METHOD:
  408. begin
  409. {NEEDBYTE}
  410. if (z.avail_in = 0) then
  411. begin
  412. inflate := r;
  413. exit;
  414. end;
  415. r := f; {}
  416. {z.state^.sub.method := NEXTBYTE(z);}
  417. Dec(z.avail_in);
  418. Inc(z.total_in);
  419. z.state^.sub.method := z.next_in^;
  420. Inc(z.next_in);
  421. if ((z.state^.sub.method and $0f) <> Z_DEFLATED) then
  422. begin
  423. z.state^.mode := BAD;
  424. z.msg := 'unknown compression method';
  425. z.state^.sub.marker := 5; { can't try inflateSync }
  426. continue; { break C-switch }
  427. end;
  428. if ((z.state^.sub.method shr 4) + 8 > z.state^.wbits) then
  429. begin
  430. z.state^.mode := BAD;
  431. z.msg := 'invalid window size';
  432. z.state^.sub.marker := 5; { can't try inflateSync }
  433. continue; { break C-switch }
  434. end;
  435. z.state^.mode := FLAG;
  436. { fall trough }
  437. end;
  438. FLAG:
  439. begin
  440. {NEEDBYTE}
  441. if (z.avail_in = 0) then
  442. begin
  443. inflate := r;
  444. exit;
  445. end;
  446. r := f; {}
  447. {b := NEXTBYTE(z);}
  448. Dec(z.avail_in);
  449. Inc(z.total_in);
  450. b := z.next_in^;
  451. Inc(z.next_in);
  452. if (((z.state^.sub.method shl 8) + b) mod 31) <> 0 then {% mod ?}
  453. begin
  454. z.state^.mode := BAD;
  455. z.msg := 'incorrect header check';
  456. z.state^.sub.marker := 5; { can't try inflateSync }
  457. continue; { break C-switch }
  458. end;
  459. {$IFDEF DEBUG}
  460. Tracev('inflate: zlib header ok');
  461. {$ENDIF}
  462. if ((b and PRESET_DICT) = 0) then
  463. begin
  464. z.state^.mode := BLOCKS;
  465. continue; { break C-switch }
  466. end;
  467. z.state^.mode := DICT4;
  468. { falltrough }
  469. end;
  470. DICT4:
  471. begin
  472. if (z.avail_in = 0) then
  473. begin
  474. inflate := r;
  475. exit;
  476. end;
  477. r := f;
  478. {z.state^.sub.check.need := uLong(NEXTBYTE(z)) shl 24;}
  479. Dec(z.avail_in);
  480. Inc(z.total_in);
  481. z.state^.sub.check.need := uLong(z.next_in^) shl 24;
  482. Inc(z.next_in);
  483. z.state^.mode := DICT3; { falltrough }
  484. end;
  485. DICT3:
  486. begin
  487. if (z.avail_in = 0) then
  488. begin
  489. inflate := r;
  490. exit;
  491. end;
  492. r := f;
  493. {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 16);}
  494. Dec(z.avail_in);
  495. Inc(z.total_in);
  496. Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 16);
  497. Inc(z.next_in);
  498. z.state^.mode := DICT2; { falltrough }
  499. end;
  500. DICT2:
  501. begin
  502. if (z.avail_in = 0) then
  503. begin
  504. inflate := r;
  505. exit;
  506. end;
  507. r := f;
  508. {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 8);}
  509. Dec(z.avail_in);
  510. Inc(z.total_in);
  511. Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 8);
  512. Inc(z.next_in);
  513. z.state^.mode := DICT1; { falltrough }
  514. end;
  515. DICT1:
  516. begin
  517. if (z.avail_in = 0) then
  518. begin
  519. inflate := r;
  520. exit;
  521. end;
  522. { r := f; --- wird niemals benutzt }
  523. {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) );}
  524. Dec(z.avail_in);
  525. Inc(z.total_in);
  526. Inc(z.state^.sub.check.need, uLong(z.next_in^) );
  527. Inc(z.next_in);
  528. z.adler := z.state^.sub.check.need;
  529. z.state^.mode := DICT0;
  530. inflate := Z_NEED_DICT;
  531. exit;
  532. end;
  533. DICT0:
  534. begin
  535. z.state^.mode := BAD;
  536. z.msg := 'need dictionary';
  537. z.state^.sub.marker := 0; { can try inflateSync }
  538. inflate := Z_STREAM_ERROR;
  539. exit;
  540. end;
  541. BAD:
  542. begin
  543. inflate := Z_DATA_ERROR;
  544. exit;
  545. end;
  546. else
  547. begin
  548. inflate := Z_STREAM_ERROR;
  549. exit;
  550. end;
  551. end;
  552. {$ifdef NEED_DUMMY_result}
  553. result := Z_STREAM_ERROR; { Some dumb compilers complain without this }
  554. {$endif}
  555. end;
  556. function inflateSetDictionary(var z : z_stream;
  557. dictionary : pBytef; {const array of byte}
  558. dictLength : uInt) : int;
  559. var
  560. length : uInt;
  561. begin
  562. length := dictLength;
  563. if (z.state = Z_NULL) or (z.state^.mode <> DICT0) then
  564. begin
  565. inflateSetDictionary := Z_STREAM_ERROR;
  566. exit;
  567. end;
  568. if (adler32(Long(1), dictionary, dictLength) <> z.adler) then
  569. begin
  570. inflateSetDictionary := Z_DATA_ERROR;
  571. exit;
  572. end;
  573. z.adler := Long(1);
  574. if (length >= (uInt(1) shl z.state^.wbits)) then
  575. begin
  576. length := (1 shl z.state^.wbits)-1;
  577. Inc( dictionary, dictLength - length);
  578. end;
  579. inflate_set_dictionary(z.state^.blocks^, dictionary^, length);
  580. z.state^.mode := BLOCKS;
  581. inflateSetDictionary := Z_OK;
  582. end;
  583. function inflateSync(var z : z_stream) : int;
  584. const
  585. mark : packed array[0..3] of byte = (0, 0, $ff, $ff);
  586. var
  587. n : uInt; { number of bytes to look at }
  588. p : pBytef; { pointer to bytes }
  589. m : uInt; { number of marker bytes found in a row }
  590. r, w : uLong; { temporaries to save total_in and total_out }
  591. begin
  592. { set up }
  593. if (z.state = Z_NULL) then
  594. begin
  595. inflateSync := Z_STREAM_ERROR;
  596. exit;
  597. end;
  598. if (z.state^.mode <> BAD) then
  599. begin
  600. z.state^.mode := BAD;
  601. z.state^.sub.marker := 0;
  602. end;
  603. n := z.avail_in;
  604. if (n = 0) then
  605. begin
  606. inflateSync := Z_BUF_ERROR;
  607. exit;
  608. end;
  609. p := z.next_in;
  610. m := z.state^.sub.marker;
  611. { search }
  612. while (n <> 0) and (m < 4) do
  613. begin
  614. if (p^ = mark[m]) then
  615. Inc(m)
  616. else
  617. if (p^ <> 0) then
  618. m := 0
  619. else
  620. m := 4 - m;
  621. Inc(p);
  622. Dec(n);
  623. end;
  624. { restore }
  625. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  626. z.next_in := p;
  627. z.avail_in := n;
  628. z.state^.sub.marker := m;
  629. { return no joy or set up to restart on a new block }
  630. if (m <> 4) then
  631. begin
  632. inflateSync := Z_DATA_ERROR;
  633. exit;
  634. end;
  635. r := z.total_in;
  636. w := z.total_out;
  637. inflateReset(z);
  638. z.total_in := r;
  639. z.total_out := w;
  640. z.state^.mode := BLOCKS;
  641. inflateSync := Z_OK;
  642. end;
  643. {
  644. returns true if inflate is currently at the end of a block generated
  645. by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  646. implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  647. but removes the length bytes of the resulting empty stored block. When
  648. decompressing, PPP checks that at the end of input packet, inflate is
  649. waiting for these length bytes.
  650. }
  651. function inflateSyncPoint(var z : z_stream) : int;
  652. begin
  653. if (z.state = Z_NULL) or (z.state^.blocks = Z_NULL) then
  654. begin
  655. inflateSyncPoint := Z_STREAM_ERROR;
  656. exit;
  657. end;
  658. inflateSyncPoint := inflate_blocks_sync_point(z.state^.blocks^);
  659. end;
  660. end.