Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

750 lines
22 KiB

3 years ago
  1. Unit imzinflate;
  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 imzconf.inc}
  10. uses
  11. imzutil, impaszlib, iminfblock, iminfutil;
  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 : AnsiString;
  24. stream_size : int) : int;
  25. function inflateInit2_(var z: z_stream;
  26. w : int;
  27. const version : AnsiString;
  28. stream_size : int) : int;
  29. function inflateInit2(var z: z_stream;
  30. windowBits : int) : int;
  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) : int;
  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) : int;
  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 : int) : int;
  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 : pBytef; {const array of byte}
  127. dictLength : uInt) : int;
  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) : int;
  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) : int;
  156. implementation
  157. uses
  158. imadler;
  159. function inflateReset(var z : z_stream) : int;
  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) : int;
  180. begin
  181. if (z.state = Z_NULL) or not Assigned(z.zfree) 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 : int;
  197. const version : AnsiString;
  198. stream_size : int) : int;
  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. if not Assigned(z.zalloc) then
  210. begin
  211. {$IFDEF FPC} z.zalloc := @zcalloc; {$ELSE}
  212. z.zalloc := zcalloc;
  213. {$endif}
  214. z.opaque := voidpf(0);
  215. end;
  216. if not Assigned(z.zfree) then
  217. {$IFDEF FPC} z.zfree := @zcfree; {$ELSE}
  218. z.zfree := zcfree;
  219. {$ENDIF}
  220. z.state := pInternal_state( ZALLOC(z,1,sizeof(internal_state)) );
  221. if (z.state = Z_NULL) then
  222. begin
  223. inflateInit2_ := Z_MEM_ERROR;
  224. exit;
  225. end;
  226. z.state^.blocks := Z_NULL;
  227. { handle undocumented nowrap option (no zlib header or check) }
  228. z.state^.nowrap := FALSE;
  229. if (w < 0) then
  230. begin
  231. w := - w;
  232. z.state^.nowrap := TRUE;
  233. end;
  234. { set window size }
  235. if (w < 8) or (w > 15) then
  236. begin
  237. inflateEnd(z);
  238. inflateInit2_ := Z_STREAM_ERROR;
  239. exit;
  240. end;
  241. z.state^.wbits := uInt(w);
  242. { create inflate_blocks state }
  243. if z.state^.nowrap then
  244. z.state^.blocks := inflate_blocks_new(z, NIL, uInt(1) shl w)
  245. else
  246. {$IFDEF FPC}
  247. z.state^.blocks := inflate_blocks_new(z, @adler32, uInt(1) shl w);
  248. {$ELSE}
  249. z.state^.blocks := inflate_blocks_new(z, adler32, uInt(1) shl w);
  250. {$ENDIF}
  251. if (z.state^.blocks = Z_NULL) then
  252. begin
  253. inflateEnd(z);
  254. inflateInit2_ := Z_MEM_ERROR;
  255. exit;
  256. end;
  257. {$IFDEF DEBUG}
  258. Tracev('inflate: allocated');
  259. {$ENDIF}
  260. { reset state }
  261. inflateReset(z);
  262. inflateInit2_ := Z_OK;
  263. end;
  264. function inflateInit2(var z: z_stream; windowBits : int) : int;
  265. begin
  266. inflateInit2 := inflateInit2_(z, windowBits, ZLIB_VERSION, sizeof(z_stream));
  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. begin
  272. inflateInit := inflateInit2_(z, DEF_WBITS, ZLIB_VERSION, sizeof(z_stream));
  273. end;
  274. function inflateInit_(z : z_streamp;
  275. const version : AnsiString;
  276. stream_size : int) : int;
  277. begin
  278. { initialize state }
  279. if (z = Z_NULL) then
  280. inflateInit_ := Z_STREAM_ERROR
  281. else
  282. inflateInit_ := inflateInit2_(z^, DEF_WBITS, version, stream_size);
  283. end;
  284. function inflate(var z : z_stream;
  285. f : int) : int;
  286. var
  287. r : int;
  288. b : uInt;
  289. begin
  290. if (z.state = Z_NULL) or (z.next_in = Z_NULL) then
  291. begin
  292. inflate := Z_STREAM_ERROR;
  293. exit;
  294. end;
  295. if f = Z_FINISH then
  296. f := Z_BUF_ERROR
  297. else
  298. f := Z_OK;
  299. r := Z_BUF_ERROR;
  300. while True do
  301. case (z.state^.mode) of
  302. BLOCKS:
  303. begin
  304. r := inflate_blocks(z.state^.blocks^, z, r);
  305. if (r = Z_DATA_ERROR) then
  306. begin
  307. z.state^.mode := BAD;
  308. z.state^.sub.marker := 0; { can try inflateSync }
  309. continue; { break C-switch }
  310. end;
  311. if (r = Z_OK) then
  312. r := f;
  313. if (r <> Z_STREAM_END) then
  314. begin
  315. inflate := r;
  316. exit;
  317. end;
  318. r := f;
  319. inflate_blocks_reset(z.state^.blocks^, z, @z.state^.sub.check.was);
  320. if (z.state^.nowrap) then
  321. begin
  322. z.state^.mode := DONE;
  323. continue; { break C-switch }
  324. end;
  325. z.state^.mode := CHECK4; { falltrough }
  326. end;
  327. CHECK4:
  328. begin
  329. {NEEDBYTE}
  330. if (z.avail_in = 0) then
  331. begin
  332. inflate := r;
  333. exit;
  334. end;
  335. r := f;
  336. {z.state^.sub.check.need := uLong(NEXTBYTE(z)) shl 24;}
  337. Dec(z.avail_in);
  338. Inc(z.total_in);
  339. z.state^.sub.check.need := uLong(z.next_in^) shl 24;
  340. Inc(z.next_in);
  341. z.state^.mode := CHECK3; { falltrough }
  342. end;
  343. CHECK3:
  344. begin
  345. {NEEDBYTE}
  346. if (z.avail_in = 0) then
  347. begin
  348. inflate := r;
  349. exit;
  350. end;
  351. r := f;
  352. {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 16);}
  353. Dec(z.avail_in);
  354. Inc(z.total_in);
  355. Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 16);
  356. Inc(z.next_in);
  357. z.state^.mode := CHECK2; { falltrough }
  358. end;
  359. CHECK2:
  360. begin
  361. {NEEDBYTE}
  362. if (z.avail_in = 0) then
  363. begin
  364. inflate := r;
  365. exit;
  366. end;
  367. r := f;
  368. {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 8);}
  369. Dec(z.avail_in);
  370. Inc(z.total_in);
  371. Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 8);
  372. Inc(z.next_in);
  373. z.state^.mode := CHECK1; { falltrough }
  374. end;
  375. CHECK1:
  376. begin
  377. {NEEDBYTE}
  378. if (z.avail_in = 0) then
  379. begin
  380. inflate := r;
  381. exit;
  382. end;
  383. r := f;
  384. {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) );}
  385. Dec(z.avail_in);
  386. Inc(z.total_in);
  387. Inc(z.state^.sub.check.need, uLong(z.next_in^) );
  388. Inc(z.next_in);
  389. if (z.state^.sub.check.was <> z.state^.sub.check.need) then
  390. begin
  391. z.state^.mode := BAD;
  392. z.msg := 'incorrect data check';
  393. z.state^.sub.marker := 5; { can't try inflateSync }
  394. continue; { break C-switch }
  395. end;
  396. {$IFDEF DEBUG}
  397. Tracev('inflate: zlib check ok');
  398. {$ENDIF}
  399. z.state^.mode := DONE; { falltrough }
  400. end;
  401. DONE:
  402. begin
  403. inflate := Z_STREAM_END;
  404. exit;
  405. end;
  406. METHOD:
  407. begin
  408. {NEEDBYTE}
  409. if (z.avail_in = 0) then
  410. begin
  411. inflate := r;
  412. exit;
  413. end;
  414. r := f; {}
  415. {z.state^.sub.method := NEXTBYTE(z);}
  416. Dec(z.avail_in);
  417. Inc(z.total_in);
  418. z.state^.sub.method := z.next_in^;
  419. Inc(z.next_in);
  420. if ((z.state^.sub.method and $0f) <> Z_DEFLATED) then
  421. begin
  422. z.state^.mode := BAD;
  423. z.msg := 'unknown compression method';
  424. z.state^.sub.marker := 5; { can't try inflateSync }
  425. continue; { break C-switch }
  426. end;
  427. if ((z.state^.sub.method shr 4) + 8 > z.state^.wbits) then
  428. begin
  429. z.state^.mode := BAD;
  430. z.msg := 'invalid window size';
  431. z.state^.sub.marker := 5; { can't try inflateSync }
  432. continue; { break C-switch }
  433. end;
  434. z.state^.mode := FLAG;
  435. { fall trough }
  436. end;
  437. FLAG:
  438. begin
  439. {NEEDBYTE}
  440. if (z.avail_in = 0) then
  441. begin
  442. inflate := r;
  443. exit;
  444. end;
  445. r := f; {}
  446. {b := NEXTBYTE(z);}
  447. Dec(z.avail_in);
  448. Inc(z.total_in);
  449. b := z.next_in^;
  450. Inc(z.next_in);
  451. if (((z.state^.sub.method shl 8) + b) mod 31) <> 0 then {% mod ?}
  452. begin
  453. z.state^.mode := BAD;
  454. z.msg := 'incorrect header check';
  455. z.state^.sub.marker := 5; { can't try inflateSync }
  456. continue; { break C-switch }
  457. end;
  458. {$IFDEF DEBUG}
  459. Tracev('inflate: zlib header ok');
  460. {$ENDIF}
  461. if ((b and PRESET_DICT) = 0) then
  462. begin
  463. z.state^.mode := BLOCKS;
  464. continue; { break C-switch }
  465. end;
  466. z.state^.mode := DICT4;
  467. { falltrough }
  468. end;
  469. DICT4:
  470. begin
  471. if (z.avail_in = 0) then
  472. begin
  473. inflate := r;
  474. exit;
  475. end;
  476. r := f;
  477. {z.state^.sub.check.need := uLong(NEXTBYTE(z)) shl 24;}
  478. Dec(z.avail_in);
  479. Inc(z.total_in);
  480. z.state^.sub.check.need := uLong(z.next_in^) shl 24;
  481. Inc(z.next_in);
  482. z.state^.mode := DICT3; { falltrough }
  483. end;
  484. DICT3:
  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, uLong(NEXTBYTE(z)) shl 16);}
  493. Dec(z.avail_in);
  494. Inc(z.total_in);
  495. Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 16);
  496. Inc(z.next_in);
  497. z.state^.mode := DICT2; { falltrough }
  498. end;
  499. DICT2:
  500. begin
  501. if (z.avail_in = 0) then
  502. begin
  503. inflate := r;
  504. exit;
  505. end;
  506. r := f;
  507. {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 8);}
  508. Dec(z.avail_in);
  509. Inc(z.total_in);
  510. Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 8);
  511. Inc(z.next_in);
  512. z.state^.mode := DICT1; { falltrough }
  513. end;
  514. DICT1:
  515. begin
  516. if (z.avail_in = 0) then
  517. begin
  518. inflate := r;
  519. exit;
  520. end;
  521. { r := f; --- wird niemals benutzt }
  522. {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) );}
  523. Dec(z.avail_in);
  524. Inc(z.total_in);
  525. Inc(z.state^.sub.check.need, uLong(z.next_in^) );
  526. Inc(z.next_in);
  527. z.adler := z.state^.sub.check.need;
  528. z.state^.mode := DICT0;
  529. inflate := Z_NEED_DICT;
  530. exit;
  531. end;
  532. DICT0:
  533. begin
  534. z.state^.mode := BAD;
  535. z.msg := 'need dictionary';
  536. z.state^.sub.marker := 0; { can try inflateSync }
  537. inflate := Z_STREAM_ERROR;
  538. exit;
  539. end;
  540. BAD:
  541. begin
  542. inflate := Z_DATA_ERROR;
  543. exit;
  544. end;
  545. else
  546. begin
  547. inflate := Z_STREAM_ERROR;
  548. exit;
  549. end;
  550. end;
  551. {$ifdef NEED_DUMMY_result}
  552. result := Z_STREAM_ERROR; { Some dumb compilers complain without this }
  553. {$endif}
  554. end;
  555. function inflateSetDictionary(var z : z_stream;
  556. dictionary : pBytef; {const array of byte}
  557. dictLength : uInt) : int;
  558. var
  559. length : uInt;
  560. begin
  561. length := dictLength;
  562. if (z.state = Z_NULL) or (z.state^.mode <> DICT0) then
  563. begin
  564. inflateSetDictionary := Z_STREAM_ERROR;
  565. exit;
  566. end;
  567. if (adler32(Long(1), dictionary, dictLength) <> z.adler) then
  568. begin
  569. inflateSetDictionary := Z_DATA_ERROR;
  570. exit;
  571. end;
  572. z.adler := Long(1);
  573. if (length >= (uInt(1) shl z.state^.wbits)) then
  574. begin
  575. length := (1 shl z.state^.wbits)-1;
  576. Inc( dictionary, dictLength - length);
  577. end;
  578. inflate_set_dictionary(z.state^.blocks^, dictionary^, length);
  579. z.state^.mode := BLOCKS;
  580. inflateSetDictionary := Z_OK;
  581. end;
  582. function inflateSync(var z : z_stream) : int;
  583. const
  584. mark : packed array[0..3] of byte = (0, 0, $ff, $ff);
  585. var
  586. n : uInt; { number of bytes to look at }
  587. p : pBytef; { pointer to bytes }
  588. m : uInt; { number of marker bytes found in a row }
  589. r, w : uLong; { temporaries to save total_in and total_out }
  590. begin
  591. { set up }
  592. if (z.state = Z_NULL) then
  593. begin
  594. inflateSync := Z_STREAM_ERROR;
  595. exit;
  596. end;
  597. if (z.state^.mode <> BAD) then
  598. begin
  599. z.state^.mode := BAD;
  600. z.state^.sub.marker := 0;
  601. end;
  602. n := z.avail_in;
  603. if (n = 0) then
  604. begin
  605. inflateSync := Z_BUF_ERROR;
  606. exit;
  607. end;
  608. p := z.next_in;
  609. m := z.state^.sub.marker;
  610. { search }
  611. while (n <> 0) and (m < 4) do
  612. begin
  613. if (p^ = mark[m]) then
  614. Inc(m)
  615. else
  616. if (p^ <> 0) then
  617. m := 0
  618. else
  619. m := 4 - m;
  620. Inc(p);
  621. Dec(n);
  622. end;
  623. { restore }
  624. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  625. z.next_in := p;
  626. z.avail_in := n;
  627. z.state^.sub.marker := m;
  628. { return no joy or set up to restart on a new block }
  629. if (m <> 4) then
  630. begin
  631. inflateSync := Z_DATA_ERROR;
  632. exit;
  633. end;
  634. r := z.total_in;
  635. w := z.total_out;
  636. inflateReset(z);
  637. z.total_in := r;
  638. z.total_out := w;
  639. z.state^.mode := BLOCKS;
  640. inflateSync := Z_OK;
  641. end;
  642. {
  643. returns true if inflate is currently at the end of a block generated
  644. by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  645. implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  646. but removes the length bytes of the resulting empty stored block. When
  647. decompressing, PPP checks that at the end of input packet, inflate is
  648. waiting for these length bytes.
  649. }
  650. function inflateSyncPoint(var z : z_stream) : int;
  651. begin
  652. if (z.state = Z_NULL) or (z.state^.blocks = Z_NULL) then
  653. begin
  654. inflateSyncPoint := Z_STREAM_ERROR;
  655. exit;
  656. end;
  657. inflateSyncPoint := inflate_blocks_sync_point(z.state^.blocks^);
  658. end;
  659. end.