infblock.pas 28 KB

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