infblock.pas 27 KB

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