infblock.pas 28 KB

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