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.

951 lines
28 KiB

3 years ago
  1. Unit iminfblock;
  2. { infblock.h and
  3. infblock.c -- interpret and process block types to last block
  4. Copyright (C) 1995-1998 Mark Adler
  5. Pascal tranlastion
  6. Copyright (C) 1998 by Jacques Nomssi Nzali
  7. For conditions of distribution and use, see copyright notice in readme.txt
  8. }
  9. interface
  10. {$I imzconf.inc}
  11. uses
  12. {$IFDEF DEBUG}
  13. SysUtils, strutils,
  14. {$ENDIF}
  15. imzutil, impaszlib;
  16. function inflate_blocks_new(var z : z_stream;
  17. c : check_func; { check function }
  18. w : uInt { window size }
  19. ) : pInflate_blocks_state;
  20. function inflate_blocks (var s : inflate_blocks_state;
  21. var z : z_stream;
  22. r : int { initial return code }
  23. ) : int;
  24. procedure inflate_blocks_reset (var s : inflate_blocks_state;
  25. var z : z_stream;
  26. c : puLong); { check value on output }
  27. function inflate_blocks_free(s : pInflate_blocks_state;
  28. var z : z_stream) : int;
  29. procedure inflate_set_dictionary(var s : inflate_blocks_state;
  30. const d : array of byte; { dictionary }
  31. n : uInt); { dictionary length }
  32. function inflate_blocks_sync_point(var s : inflate_blocks_state) : int;
  33. implementation
  34. uses
  35. iminfcodes, iminftrees, iminfutil;
  36. { Tables for deflate from PKZIP's appnote.txt. }
  37. Const
  38. border : Array [0..18] Of Word { Order of the bit length code lengths }
  39. = (16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15);
  40. { Notes beyond the 1.93a appnote.txt:
  41. 1. Distance pointers never point before the beginning of the output
  42. stream.
  43. 2. Distance pointers can point back across blocks, up to 32k away.
  44. 3. There is an implied maximum of 7 bits for the bit length table and
  45. 15 bits for the actual data.
  46. 4. If only one code exists, then it is encoded using one bit. (Zero
  47. would be more efficient, but perhaps a little confusing.) If two
  48. codes exist, they are coded using one bit each (0 and 1).
  49. 5. There is no way of sending zero distance codes--a dummy must be
  50. sent if there are none. (History: a pre 2.0 version of PKZIP would
  51. store blocks with no distance codes, but this was discovered to be
  52. too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
  53. zero distance codes, which is sent as one code of zero bits in
  54. length.
  55. 6. There are up to 286 literal/length codes. Code 256 represents the
  56. end-of-block. Note however that the static length tree defines
  57. 288 codes just to fill out the Huffman codes. Codes 286 and 287
  58. cannot be used though, since there is no length base or extra bits
  59. defined for them. Similarily, there are up to 30 distance codes.
  60. However, static trees define 32 codes (all 5 bits) to fill out the
  61. Huffman codes, but the last two had better not show up in the data.
  62. 7. Unzip can check dynamic Huffman blocks for complete code sets.
  63. The exception is that a single code would not be complete (see #4).
  64. 8. The five bits following the block type is really the number of
  65. literal codes sent minus 257.
  66. 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  67. (1+6+6). Therefore, to output three times the length, you output
  68. three codes (1+1+1), whereas to output four times the same length,
  69. you only need two codes (1+3). Hmm.
  70. 10. In the tree reconstruction algorithm, Code = Code + Increment
  71. only if BitLength(i) is not zero. (Pretty obvious.)
  72. 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
  73. 12. Note: length code 284 can represent 227-258, but length code 285
  74. really is 258. The last length deserves its own, short code
  75. since it gets used a lot in very redundant files. The length
  76. 258 is special since 258 - 3 (the min match length) is 255.
  77. 13. The literal/length and distance code bit lengths are read as a
  78. single stream of lengths. It is possible (and advantageous) for
  79. a repeat code (16, 17, or 18) to go across the boundary between
  80. the two sets of lengths. }
  81. procedure inflate_blocks_reset (var s : inflate_blocks_state;
  82. var z : z_stream;
  83. c : puLong); { check value on output }
  84. begin
  85. if (c <> Z_NULL) then
  86. c^ := s.check;
  87. if (s.mode = BTREE) or (s.mode = DTREE) then
  88. ZFREE(z, s.sub.trees.blens);
  89. if (s.mode = CODES) then
  90. inflate_codes_free(s.sub.decode.codes, z);
  91. s.mode := ZTYPE;
  92. s.bitk := 0;
  93. s.bitb := 0;
  94. s.write := s.window;
  95. s.read := s.window;
  96. if Assigned(s.checkfn) then
  97. begin
  98. s.check := s.checkfn(uLong(0), pBytef(NIL), 0);
  99. z.adler := s.check;
  100. end;
  101. {$IFDEF DEBUG}
  102. Tracev('inflate: blocks reset');
  103. {$ENDIF}
  104. end;
  105. function inflate_blocks_new(var z : z_stream;
  106. c : check_func; { check function }
  107. w : uInt { window size }
  108. ) : pInflate_blocks_state;
  109. var
  110. s : pInflate_blocks_state;
  111. begin
  112. s := pInflate_blocks_state( ZALLOC(z,1, sizeof(inflate_blocks_state)) );
  113. if (s = Z_NULL) then
  114. begin
  115. inflate_blocks_new := s;
  116. exit;
  117. end;
  118. s^.hufts := huft_ptr( ZALLOC(z, sizeof(inflate_huft), MANY) );
  119. if (s^.hufts = Z_NULL) then
  120. begin
  121. ZFREE(z, s);
  122. inflate_blocks_new := Z_NULL;
  123. exit;
  124. end;
  125. s^.window := pBytef( ZALLOC(z, 1, w) );
  126. if (s^.window = Z_NULL) then
  127. begin
  128. ZFREE(z, s^.hufts);
  129. ZFREE(z, s);
  130. inflate_blocks_new := Z_NULL;
  131. exit;
  132. end;
  133. s^.zend := s^.window;
  134. Inc(s^.zend, w);
  135. s^.checkfn := c;
  136. s^.mode := ZTYPE;
  137. {$IFDEF DEBUG}
  138. Tracev('inflate: blocks allocated');
  139. {$ENDIF}
  140. inflate_blocks_reset(s^, z, Z_NULL);
  141. inflate_blocks_new := s;
  142. end;
  143. function inflate_blocks (var s : inflate_blocks_state;
  144. var z : z_stream;
  145. r : int) : int; { initial return code }
  146. label
  147. start_btree, start_dtree,
  148. start_blkdone, start_dry,
  149. start_codes;
  150. var
  151. t : uInt; { temporary storage }
  152. b : uLong; { bit buffer }
  153. k : uInt; { bits in bit buffer }
  154. p : pBytef; { input data pointer }
  155. n : uInt; { bytes available there }
  156. q : pBytef; { output window write pointer }
  157. m : uInt; { bytes to end of window or read pointer }
  158. { fixed code blocks }
  159. var
  160. bl, bd : uInt;
  161. tl, td : pInflate_huft;
  162. var
  163. h : pInflate_huft;
  164. i, j, c : uInt;
  165. var
  166. cs : pInflate_codes_state;
  167. begin
  168. { copy input/output information to locals }
  169. p := z.next_in;
  170. n := z.avail_in;
  171. b := s.bitb;
  172. k := s.bitk;
  173. q := s.write;
  174. if ptr2int(q) < ptr2int(s.read) then
  175. m := uInt(ptr2int(s.read)-ptr2int(q)-1)
  176. else
  177. m := uInt(ptr2int(s.zend)-ptr2int(q));
  178. { decompress an inflated block }
  179. { process input based on current state }
  180. while True do
  181. Case s.mode of
  182. ZTYPE:
  183. begin
  184. {NEEDBITS(3);}
  185. while (k < 3) do
  186. begin
  187. {NEEDBYTE;}
  188. if (n <> 0) then
  189. r :=Z_OK
  190. else
  191. begin
  192. {UPDATE}
  193. s.bitb := b;
  194. s.bitk := k;
  195. z.avail_in := n;
  196. Inc(z.total_in, ptr2int(p)-ptr2int(z.next_in));
  197. z.next_in := p;
  198. s.write := q;
  199. inflate_blocks := inflate_flush(s,z,r);
  200. exit;
  201. end;
  202. Dec(n);
  203. b := b or (uLong(p^) shl k);
  204. Inc(p);
  205. Inc(k, 8);
  206. end;
  207. t := uInt(b) and 7;
  208. s.last := boolean(t and 1);
  209. case (t shr 1) of
  210. 0: { stored }
  211. begin
  212. {$IFDEF DEBUG}
  213. if s.last then
  214. Tracev('inflate: stored block (last)')
  215. else
  216. Tracev('inflate: stored block');
  217. {$ENDIF}
  218. {DUMPBITS(3);}
  219. b := b shr 3;
  220. Dec(k, 3);
  221. t := k and 7; { go to byte boundary }
  222. {DUMPBITS(t);}
  223. b := b shr t;
  224. Dec(k, t);
  225. s.mode := LENS; { get length of stored block }
  226. end;
  227. 1: { fixed }
  228. begin
  229. begin
  230. {$IFDEF DEBUG}
  231. if s.last then
  232. Tracev('inflate: fixed codes blocks (last)')
  233. else
  234. Tracev('inflate: fixed codes blocks');
  235. {$ENDIF}
  236. inflate_trees_fixed(bl, bd, tl, td, z);
  237. s.sub.decode.codes := inflate_codes_new(bl, bd, tl, td, z);
  238. if (s.sub.decode.codes = Z_NULL) then
  239. begin
  240. r := Z_MEM_ERROR;
  241. { update pointers and return }
  242. s.bitb := b;
  243. s.bitk := k;
  244. z.avail_in := n;
  245. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  246. z.next_in := p;
  247. s.write := q;
  248. inflate_blocks := inflate_flush(s,z,r);
  249. exit;
  250. end;
  251. end;
  252. {DUMPBITS(3);}
  253. b := b shr 3;
  254. Dec(k, 3);
  255. s.mode := CODES;
  256. end;
  257. 2: { dynamic }
  258. begin
  259. {$IFDEF DEBUG}
  260. if s.last then
  261. Tracev('inflate: dynamic codes block (last)')
  262. else
  263. Tracev('inflate: dynamic codes block');
  264. {$ENDIF}
  265. {DUMPBITS(3);}
  266. b := b shr 3;
  267. Dec(k, 3);
  268. s.mode := TABLE;
  269. end;
  270. 3:
  271. begin { illegal }
  272. {DUMPBITS(3);}
  273. b := b shr 3;
  274. Dec(k, 3);
  275. s.mode := BLKBAD;
  276. z.msg := 'invalid block type';
  277. r := Z_DATA_ERROR;
  278. { update pointers and return }
  279. s.bitb := b;
  280. s.bitk := k;
  281. z.avail_in := n;
  282. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  283. z.next_in := p;
  284. s.write := q;
  285. inflate_blocks := inflate_flush(s,z,r);
  286. exit;
  287. end;
  288. end;
  289. end;
  290. LENS:
  291. begin
  292. {NEEDBITS(32);}
  293. while (k < 32) do
  294. begin
  295. {NEEDBYTE;}
  296. if (n <> 0) then
  297. r :=Z_OK
  298. else
  299. begin
  300. {UPDATE}
  301. s.bitb := b;
  302. s.bitk := k;
  303. z.avail_in := n;
  304. Inc(z.total_in, ptr2int(p)-ptr2int(z.next_in));
  305. z.next_in := p;
  306. s.write := q;
  307. inflate_blocks := inflate_flush(s,z,r);
  308. exit;
  309. end;
  310. Dec(n);
  311. b := b or (uLong(p^) shl k);
  312. Inc(p);
  313. Inc(k, 8);
  314. end;
  315. if (((not b) shr 16) and $ffff) <> (b and $ffff) then
  316. begin
  317. s.mode := BLKBAD;
  318. z.msg := 'invalid stored block lengths';
  319. r := Z_DATA_ERROR;
  320. { update pointers and return }
  321. s.bitb := b;
  322. s.bitk := k;
  323. z.avail_in := n;
  324. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  325. z.next_in := p;
  326. s.write := q;
  327. inflate_blocks := inflate_flush(s,z,r);
  328. exit;
  329. end;
  330. s.sub.left := uInt(b) and $ffff;
  331. k := 0;
  332. b := 0; { dump bits }
  333. {$IFDEF DEBUG}
  334. Tracev('inflate: stored length '+IntToStr(s.sub.left));
  335. {$ENDIF}
  336. if s.sub.left <> 0 then
  337. s.mode := STORED
  338. else
  339. if s.last then
  340. s.mode := DRY
  341. else
  342. s.mode := ZTYPE;
  343. end;
  344. STORED:
  345. begin
  346. if (n = 0) then
  347. begin
  348. { update pointers and return }
  349. s.bitb := b;
  350. s.bitk := k;
  351. z.avail_in := n;
  352. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  353. z.next_in := p;
  354. s.write := q;
  355. inflate_blocks := inflate_flush(s,z,r);
  356. exit;
  357. end;
  358. {NEEDOUT}
  359. if (m = 0) then
  360. begin
  361. {WRAP}
  362. if (q = s.zend) and (s.read <> s.window) then
  363. begin
  364. q := s.window;
  365. if ptr2int(q) < ptr2int(s.read) then
  366. m := uInt(ptr2int(s.read)-ptr2int(q)-1)
  367. else
  368. m := uInt(ptr2int(s.zend)-ptr2int(q));
  369. end;
  370. if (m = 0) then
  371. begin
  372. {FLUSH}
  373. s.write := q;
  374. r := inflate_flush(s,z,r);
  375. q := s.write;
  376. if ptr2int(q) < ptr2int(s.read) then
  377. m := uInt(ptr2int(s.read)-ptr2int(q)-1)
  378. else
  379. m := uInt(ptr2int(s.zend)-ptr2int(q));
  380. {WRAP}
  381. if (q = s.zend) and (s.read <> s.window) then
  382. begin
  383. q := s.window;
  384. if ptr2int(q) < ptr2int(s.read) then
  385. m := uInt(ptr2int(s.read)-ptr2int(q)-1)
  386. else
  387. m := uInt(ptr2int(s.zend)-ptr2int(q));
  388. end;
  389. if (m = 0) then
  390. begin
  391. {UPDATE}
  392. s.bitb := b;
  393. s.bitk := k;
  394. z.avail_in := n;
  395. Inc(z.total_in, ptr2int(p)-ptr2int(z.next_in));
  396. z.next_in := p;
  397. s.write := q;
  398. inflate_blocks := inflate_flush(s,z,r);
  399. exit;
  400. end;
  401. end;
  402. end;
  403. r := Z_OK;
  404. t := s.sub.left;
  405. if (t > n) then
  406. t := n;
  407. if (t > m) then
  408. t := m;
  409. zmemcpy(q, p, t);
  410. Inc(p, t); Dec(n, t);
  411. Inc(q, t); Dec(m, t);
  412. Dec(s.sub.left, t);
  413. if (s.sub.left = 0) then
  414. begin
  415. {$IFDEF DEBUG}
  416. if (ptr2int(q) >= ptr2int(s.read)) then
  417. Tracev('inflate: stored end '+
  418. IntToStr(z.total_out + ptr2int(q) - ptr2int(s.read)) + ' total out')
  419. else
  420. Tracev('inflate: stored end '+
  421. IntToStr(z.total_out + ptr2int(s.zend) - ptr2int(s.read) +
  422. ptr2int(q) - ptr2int(s.window)) + ' total out');
  423. {$ENDIF}
  424. if s.last then
  425. s.mode := DRY
  426. else
  427. s.mode := ZTYPE;
  428. end;
  429. end;
  430. TABLE:
  431. begin
  432. {NEEDBITS(14);}
  433. while (k < 14) do
  434. begin
  435. {NEEDBYTE;}
  436. if (n <> 0) then
  437. r :=Z_OK
  438. else
  439. begin
  440. {UPDATE}
  441. s.bitb := b;
  442. s.bitk := k;
  443. z.avail_in := n;
  444. Inc(z.total_in, ptr2int(p)-ptr2int(z.next_in));
  445. z.next_in := p;
  446. s.write := q;
  447. inflate_blocks := inflate_flush(s,z,r);
  448. exit;
  449. end;
  450. Dec(n);
  451. b := b or (uLong(p^) shl k);
  452. Inc(p);
  453. Inc(k, 8);
  454. end;
  455. t := uInt(b) and $3fff;
  456. s.sub.trees.table := t;
  457. {$ifndef PKZIP_BUG_WORKAROUND}
  458. if ((t and $1f) > 29) or (((t shr 5) and $1f) > 29) then
  459. begin
  460. s.mode := BLKBAD;
  461. z.msg := 'too many length or distance symbols';
  462. r := Z_DATA_ERROR;
  463. { update pointers and return }
  464. s.bitb := b;
  465. s.bitk := k;
  466. z.avail_in := n;
  467. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  468. z.next_in := p;
  469. s.write := q;
  470. inflate_blocks := inflate_flush(s,z,r);
  471. exit;
  472. end;
  473. {$endif}
  474. t := 258 + (t and $1f) + ((t shr 5) and $1f);
  475. s.sub.trees.blens := puIntArray( ZALLOC(z, t, sizeof(uInt)) );
  476. if (s.sub.trees.blens = Z_NULL) then
  477. begin
  478. r := Z_MEM_ERROR;
  479. { update pointers and return }
  480. s.bitb := b;
  481. s.bitk := k;
  482. z.avail_in := n;
  483. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  484. z.next_in := p;
  485. s.write := q;
  486. inflate_blocks := inflate_flush(s,z,r);
  487. exit;
  488. end;
  489. {DUMPBITS(14);}
  490. b := b shr 14;
  491. Dec(k, 14);
  492. s.sub.trees.index := 0;
  493. {$IFDEF DEBUG}
  494. Tracev('inflate: table sizes ok');
  495. {$ENDIF}
  496. s.mode := BTREE;
  497. { fall trough case is handled by the while }
  498. { try GOTO for speed - Nomssi }
  499. goto start_btree;
  500. end;
  501. BTREE:
  502. begin
  503. start_btree:
  504. while (s.sub.trees.index < 4 + (s.sub.trees.table shr 10)) do
  505. begin
  506. {NEEDBITS(3);}
  507. while (k < 3) do
  508. begin
  509. {NEEDBYTE;}
  510. if (n <> 0) then
  511. r :=Z_OK
  512. else
  513. begin
  514. {UPDATE}
  515. s.bitb := b;
  516. s.bitk := k;
  517. z.avail_in := n;
  518. Inc(z.total_in, ptr2int(p)-ptr2int(z.next_in));
  519. z.next_in := p;
  520. s.write := q;
  521. inflate_blocks := inflate_flush(s,z,r);
  522. exit;
  523. end;
  524. Dec(n);
  525. b := b or (uLong(p^) shl k);
  526. Inc(p);
  527. Inc(k, 8);
  528. end;
  529. s.sub.trees.blens^[border[s.sub.trees.index]] := uInt(b) and 7;
  530. Inc(s.sub.trees.index);
  531. {DUMPBITS(3);}
  532. b := b shr 3;
  533. Dec(k, 3);
  534. end;
  535. while (s.sub.trees.index < 19) do
  536. begin
  537. s.sub.trees.blens^[border[s.sub.trees.index]] := 0;
  538. Inc(s.sub.trees.index);
  539. end;
  540. s.sub.trees.bb := 7;
  541. t := inflate_trees_bits(s.sub.trees.blens^, s.sub.trees.bb,
  542. s.sub.trees.tb, s.hufts^, z);
  543. if (t <> Z_OK) then
  544. begin
  545. ZFREE(z, s.sub.trees.blens);
  546. r := t;
  547. if (r = Z_DATA_ERROR) then
  548. s.mode := BLKBAD;
  549. { update pointers and return }
  550. s.bitb := b;
  551. s.bitk := k;
  552. z.avail_in := n;
  553. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  554. z.next_in := p;
  555. s.write := q;
  556. inflate_blocks := inflate_flush(s,z,r);
  557. exit;
  558. end;
  559. s.sub.trees.index := 0;
  560. {$IFDEF DEBUG}
  561. Tracev('inflate: bits tree ok');
  562. {$ENDIF}
  563. s.mode := DTREE;
  564. { fall through again }
  565. goto start_dtree;
  566. end;
  567. DTREE:
  568. begin
  569. start_dtree:
  570. while TRUE do
  571. begin
  572. t := s.sub.trees.table;
  573. if not (s.sub.trees.index < 258 +
  574. (t and $1f) + ((t shr 5) and $1f)) then
  575. break;
  576. t := s.sub.trees.bb;
  577. {NEEDBITS(t);}
  578. while (k < t) do
  579. begin
  580. {NEEDBYTE;}
  581. if (n <> 0) then
  582. r :=Z_OK
  583. else
  584. begin
  585. {UPDATE}
  586. s.bitb := b;
  587. s.bitk := k;
  588. z.avail_in := n;
  589. Inc(z.total_in, ptr2int(p)-ptr2int(z.next_in));
  590. z.next_in := p;
  591. s.write := q;
  592. inflate_blocks := inflate_flush(s,z,r);
  593. exit;
  594. end;
  595. Dec(n);
  596. b := b or (uLong(p^) shl k);
  597. Inc(p);
  598. Inc(k, 8);
  599. end;
  600. h := s.sub.trees.tb;
  601. Inc(h, uInt(b) and inflate_mask[t]);
  602. t := h^.Bits;
  603. c := h^.Base;
  604. if (c < 16) then
  605. begin
  606. {DUMPBITS(t);}
  607. b := b shr t;
  608. Dec(k, t);
  609. s.sub.trees.blens^[s.sub.trees.index] := c;
  610. Inc(s.sub.trees.index);
  611. end
  612. else { c = 16..18 }
  613. begin
  614. if c = 18 then
  615. begin
  616. i := 7;
  617. j := 11;
  618. end
  619. else
  620. begin
  621. i := c - 14;
  622. j := 3;
  623. end;
  624. {NEEDBITS(t + i);}
  625. while (k < t + i) do
  626. begin
  627. {NEEDBYTE;}
  628. if (n <> 0) then
  629. r :=Z_OK
  630. else
  631. begin
  632. {UPDATE}
  633. s.bitb := b;
  634. s.bitk := k;
  635. z.avail_in := n;
  636. Inc(z.total_in, ptr2int(p)-ptr2int(z.next_in));
  637. z.next_in := p;
  638. s.write := q;
  639. inflate_blocks := inflate_flush(s,z,r);
  640. exit;
  641. end;
  642. Dec(n);
  643. b := b or (uLong(p^) shl k);
  644. Inc(p);
  645. Inc(k, 8);
  646. end;
  647. {DUMPBITS(t);}
  648. b := b shr t;
  649. Dec(k, t);
  650. Inc(j, uInt(b) and inflate_mask[i]);
  651. {DUMPBITS(i);}
  652. b := b shr i;
  653. Dec(k, i);
  654. i := s.sub.trees.index;
  655. t := s.sub.trees.table;
  656. if (i + j > 258 + (t and $1f) + ((t shr 5) and $1f)) or
  657. ((c = 16) and (i < 1)) then
  658. begin
  659. ZFREE(z, s.sub.trees.blens);
  660. s.mode := BLKBAD;
  661. z.msg := 'invalid bit length repeat';
  662. r := Z_DATA_ERROR;
  663. { update pointers and return }
  664. s.bitb := b;
  665. s.bitk := k;
  666. z.avail_in := n;
  667. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  668. z.next_in := p;
  669. s.write := q;
  670. inflate_blocks := inflate_flush(s,z,r);
  671. exit;
  672. end;
  673. if c = 16 then
  674. c := s.sub.trees.blens^[i - 1]
  675. else
  676. c := 0;
  677. repeat
  678. s.sub.trees.blens^[i] := c;
  679. Inc(i);
  680. Dec(j);
  681. until (j=0);
  682. s.sub.trees.index := i;
  683. end;
  684. end; { while }
  685. s.sub.trees.tb := Z_NULL;
  686. begin
  687. bl := 9; { must be <= 9 for lookahead assumptions }
  688. bd := 6; { must be <= 9 for lookahead assumptions }
  689. t := s.sub.trees.table;
  690. t := inflate_trees_dynamic(257 + (t and $1f),
  691. 1 + ((t shr 5) and $1f),
  692. s.sub.trees.blens^, bl, bd, tl, td, s.hufts^, z);
  693. ZFREE(z, s.sub.trees.blens);
  694. if (t <> Z_OK) then
  695. begin
  696. if (t = uInt(Z_DATA_ERROR)) then
  697. s.mode := BLKBAD;
  698. r := t;
  699. { update pointers and return }
  700. s.bitb := b;
  701. s.bitk := k;
  702. z.avail_in := n;
  703. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  704. z.next_in := p;
  705. s.write := q;
  706. inflate_blocks := inflate_flush(s,z,r);
  707. exit;
  708. end;
  709. {$IFDEF DEBUG}
  710. Tracev('inflate: trees ok');
  711. {$ENDIF}
  712. { c renamed to cs }
  713. cs := inflate_codes_new(bl, bd, tl, td, z);
  714. if (cs = Z_NULL) then
  715. begin
  716. r := Z_MEM_ERROR;
  717. { update pointers and return }
  718. s.bitb := b;
  719. s.bitk := k;
  720. z.avail_in := n;
  721. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  722. z.next_in := p;
  723. s.write := q;
  724. inflate_blocks := inflate_flush(s,z,r);
  725. exit;
  726. end;
  727. s.sub.decode.codes := cs;
  728. end;
  729. s.mode := CODES;
  730. { yet another falltrough }
  731. goto start_codes;
  732. end;
  733. CODES:
  734. begin
  735. start_codes:
  736. { update pointers }
  737. s.bitb := b;
  738. s.bitk := k;
  739. z.avail_in := n;
  740. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  741. z.next_in := p;
  742. s.write := q;
  743. r := inflate_codes(s, z, r);
  744. if (r <> Z_STREAM_END) then
  745. begin
  746. inflate_blocks := inflate_flush(s, z, r);
  747. exit;
  748. end;
  749. r := Z_OK;
  750. inflate_codes_free(s.sub.decode.codes, z);
  751. { load local pointers }
  752. p := z.next_in;
  753. n := z.avail_in;
  754. b := s.bitb;
  755. k := s.bitk;
  756. q := s.write;
  757. if ptr2int(q) < ptr2int(s.read) then
  758. m := uInt(ptr2int(s.read)-ptr2int(q)-1)
  759. else
  760. m := uInt(ptr2int(s.zend)-ptr2int(q));
  761. {$IFDEF DEBUG}
  762. if (ptr2int(q) >= ptr2int(s.read)) then
  763. Tracev('inflate: codes end '+
  764. IntToStr(z.total_out + ptr2int(q) - ptr2int(s.read)) + ' total out')
  765. else
  766. Tracev('inflate: codes end '+
  767. IntToStr(z.total_out + ptr2int(s.zend) - ptr2int(s.read) +
  768. ptr2int(q) - ptr2int(s.window)) + ' total out');
  769. {$ENDIF}
  770. if (not s.last) then
  771. begin
  772. s.mode := ZTYPE;
  773. continue; { break for switch statement in C-code }
  774. end;
  775. {$ifndef patch112}
  776. if (k > 7) then { return unused byte, if any }
  777. begin
  778. {$IFDEF DEBUG}
  779. Assert(k < 16, 'inflate_codes grabbed too many bytes');
  780. {$ENDIF}
  781. Dec(k, 8);
  782. Inc(n);
  783. Dec(p); { can always return one }
  784. end;
  785. {$endif}
  786. s.mode := DRY;
  787. { another falltrough }
  788. goto start_dry;
  789. end;
  790. DRY:
  791. begin
  792. start_dry:
  793. {FLUSH}
  794. s.write := q;
  795. r := inflate_flush(s,z,r);
  796. q := s.write;
  797. { not needed anymore, we are done:
  798. if ptr2int(q) < ptr2int(s.read) then
  799. m := uInt(ptr2int(s.read)-ptr2int(q)-1)
  800. else
  801. m := uInt(ptr2int(s.zend)-ptr2int(q));
  802. }
  803. if (s.read <> s.write) then
  804. begin
  805. { update pointers and return }
  806. s.bitb := b;
  807. s.bitk := k;
  808. z.avail_in := n;
  809. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  810. z.next_in := p;
  811. s.write := q;
  812. inflate_blocks := inflate_flush(s,z,r);
  813. exit;
  814. end;
  815. s.mode := BLKDONE;
  816. goto start_blkdone;
  817. end;
  818. BLKDONE:
  819. begin
  820. start_blkdone:
  821. r := Z_STREAM_END;
  822. { update pointers and return }
  823. s.bitb := b;
  824. s.bitk := k;
  825. z.avail_in := n;
  826. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  827. z.next_in := p;
  828. s.write := q;
  829. inflate_blocks := inflate_flush(s,z,r);
  830. exit;
  831. end;
  832. BLKBAD:
  833. begin
  834. r := Z_DATA_ERROR;
  835. { update pointers and return }
  836. s.bitb := b;
  837. s.bitk := k;
  838. z.avail_in := n;
  839. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  840. z.next_in := p;
  841. s.write := q;
  842. inflate_blocks := inflate_flush(s,z,r);
  843. exit;
  844. end;
  845. else
  846. begin
  847. r := Z_STREAM_ERROR;
  848. { update pointers and return }
  849. s.bitb := b;
  850. s.bitk := k;
  851. z.avail_in := n;
  852. Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
  853. z.next_in := p;
  854. s.write := q;
  855. inflate_blocks := inflate_flush(s,z,r);
  856. exit;
  857. end;
  858. end; { Case s.mode of }
  859. end;
  860. function inflate_blocks_free(s : pInflate_blocks_state;
  861. var z : z_stream) : int;
  862. begin
  863. inflate_blocks_reset(s^, z, Z_NULL);
  864. ZFREE(z, s^.window);
  865. ZFREE(z, s^.hufts);
  866. ZFREE(z, s);
  867. {$IFDEF DEBUG}
  868. Trace('inflate: blocks freed');
  869. {$ENDIF}
  870. inflate_blocks_free := Z_OK;
  871. end;
  872. procedure inflate_set_dictionary(var s : inflate_blocks_state;
  873. const d : array of byte; { dictionary }
  874. n : uInt); { dictionary length }
  875. begin
  876. zmemcpy(s.window, pBytef(@d), n);
  877. s.write := s.window;
  878. Inc(s.write, n);
  879. s.read := s.write;
  880. end;
  881. { Returns true if inflate is currently at the end of a block generated
  882. by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  883. IN assertion: s <> Z_NULL }
  884. function inflate_blocks_sync_point(var s : inflate_blocks_state) : int;
  885. begin
  886. inflate_blocks_sync_point := int(s.mode = LENS);
  887. end;
  888. end.