zinflate.pas 21 KB

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