infcodes.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. unit infcodes;
  2. { infcodes.c -- process literals and length/distance pairs
  3. Copyright (C) 1995-1998 Mark Adler
  4. Pascal tranlastion
  5. Copyright (C) 1998 by Jacques Nomssi Nzali
  6. For conditions of distribution and use, see copyright notice in readme.txt
  7. }
  8. interface
  9. {$I zconf.inc}
  10. uses
  11. zbase;
  12. function inflate_codes_new (bl : cardinal;
  13. bd : cardinal;
  14. tl : pInflate_huft;
  15. td : pInflate_huft;
  16. var z : z_stream): pInflate_codes_state;
  17. function inflate_codes(var s : inflate_blocks_state;
  18. var z : z_stream;
  19. r : integer) : integer;
  20. procedure inflate_codes_free(c : pInflate_codes_state;
  21. var z : z_stream);
  22. implementation
  23. uses
  24. infutil, inffast;
  25. function inflate_codes_new (bl : cardinal;
  26. bd : cardinal;
  27. tl : pInflate_huft;
  28. td : pInflate_huft;
  29. var z : z_stream): pInflate_codes_state;
  30. var
  31. c : pInflate_codes_state;
  32. begin
  33. new(c);
  34. if c<>nil then
  35. begin
  36. c^.mode := START;
  37. c^.lbits := Byte(bl);
  38. c^.dbits := Byte(bd);
  39. c^.ltree := tl;
  40. c^.dtree := td;
  41. {$IFDEF ZLIB_DEBUG}
  42. Tracev('inflate: codes new');
  43. {$ENDIF}
  44. end;
  45. inflate_codes_new := c;
  46. end;
  47. function inflate_codes(var s : inflate_blocks_state;
  48. var z : z_stream;
  49. r : integer) : integer;
  50. var
  51. j : cardinal; { temporary storage }
  52. t : pInflate_huft; { temporary pointer }
  53. e : cardinal; { extra bits or operation }
  54. b : cardinal; { bit buffer }
  55. k : cardinal; { bits in bit buffer }
  56. p : Pbyte; { input data pointer }
  57. n : cardinal; { bytes available there }
  58. q : Pbyte; { output window write pointer }
  59. m : cardinal; { bytes to end of window or read pointer }
  60. f : Pbyte; { pointer to copy strings from }
  61. var
  62. c : pInflate_codes_state;
  63. begin
  64. c := s.sub.decode.codes; { codes state }
  65. { copy input/output information to locals }
  66. p := z.next_in;
  67. n := z.avail_in;
  68. b := s.bitb;
  69. k := s.bitk;
  70. q := s.write;
  71. if ptruint(q) < ptruint(s.read) then
  72. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  73. else
  74. m := cardinal(ptruint(s.zend)-ptruint(q));
  75. { process input and output based on current state }
  76. while True do
  77. case (c^.mode) of
  78. { waiting for "i:"=input, "o:"=output, "x:"=nothing }
  79. START: { x: set up for LEN }
  80. begin
  81. {$ifndef SLOW}
  82. if (m >= 258) and (n >= 10) then
  83. begin
  84. {UPDATE}
  85. s.bitb := b;
  86. s.bitk := k;
  87. z.avail_in := n;
  88. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  89. z.next_in := p;
  90. s.write := q;
  91. r := inflate_fast(c^.lbits, c^.dbits, c^.ltree, c^.dtree, s, z);
  92. {LOAD}
  93. p := z.next_in;
  94. n := z.avail_in;
  95. b := s.bitb;
  96. k := s.bitk;
  97. q := s.write;
  98. if ptruint(q) < ptruint(s.read) then
  99. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  100. else
  101. m := cardinal(ptruint(s.zend)-ptruint(q));
  102. if (r <> Z_OK) then
  103. begin
  104. if (r = Z_STREAM_END) then
  105. c^.mode := WASH
  106. else
  107. c^.mode := BADCODE;
  108. continue; { break for switch-statement in C }
  109. end;
  110. end;
  111. {$endif} { not SLOW }
  112. c^.sub.code.need := c^.lbits;
  113. c^.sub.code.tree := c^.ltree;
  114. c^.mode := LEN; { falltrough }
  115. end;
  116. LEN: { i: get length/literal/eob next }
  117. begin
  118. j := c^.sub.code.need;
  119. {NEEDBITS(j);}
  120. while (k < j) do
  121. begin
  122. {NEEDBYTE;}
  123. if (n <> 0) then
  124. r :=Z_OK
  125. else
  126. begin
  127. {UPDATE}
  128. s.bitb := b;
  129. s.bitk := k;
  130. z.avail_in := n;
  131. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  132. z.next_in := p;
  133. s.write := q;
  134. inflate_codes := inflate_flush(s,z,r);
  135. exit;
  136. end;
  137. dec(n);
  138. b := b or (cardinal(p^) shl k);
  139. Inc(p);
  140. Inc(k, 8);
  141. end;
  142. t := c^.sub.code.tree;
  143. Inc(t, cardinal(b) and inflate_mask[j]);
  144. {DUMPBITS(t^.bits);}
  145. b := b shr t^.bits;
  146. dec(k, t^.bits);
  147. e := cardinal(t^.exop);
  148. if (e = 0) then { literal }
  149. begin
  150. c^.sub.lit := t^.base;
  151. {$IFDEF ZLIB_DEBUG}
  152. if (t^.base >= $20) and (t^.base < $7f) then
  153. Tracevv('inflate: literal '+char(t^.base))
  154. else
  155. Tracevv('inflate: literal '+IntToStr(t^.base));
  156. {$ENDIF}
  157. c^.mode := LIT;
  158. continue; { break switch statement }
  159. end;
  160. if (e and 16 <> 0) then { length }
  161. begin
  162. c^.sub.copy.get := e and 15;
  163. c^.len := t^.base;
  164. c^.mode := LENEXT;
  165. continue; { break C-switch statement }
  166. end;
  167. if (e and 64 = 0) then { next table }
  168. begin
  169. c^.sub.code.need := e;
  170. c^.sub.code.tree := @huft_ptr(t)^[t^.base];
  171. continue; { break C-switch statement }
  172. end;
  173. if (e and 32 <> 0) then { end of block }
  174. begin
  175. {$IFDEF ZLIB_DEBUG}
  176. Tracevv('inflate: end of block');
  177. {$ENDIF}
  178. c^.mode := WASH;
  179. continue; { break C-switch statement }
  180. end;
  181. c^.mode := BADCODE; { invalid code }
  182. z.msg := 'invalid literal/length code';
  183. r := Z_DATA_ERROR;
  184. {UPDATE}
  185. s.bitb := b;
  186. s.bitk := k;
  187. z.avail_in := n;
  188. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  189. z.next_in := p;
  190. s.write := q;
  191. inflate_codes := inflate_flush(s,z,r);
  192. exit;
  193. end;
  194. LENEXT: { i: getting length extra (have base) }
  195. begin
  196. j := c^.sub.copy.get;
  197. {NEEDBITS(j);}
  198. while (k < j) do
  199. begin
  200. {NEEDBYTE;}
  201. if (n <> 0) then
  202. r :=Z_OK
  203. else
  204. begin
  205. {UPDATE}
  206. s.bitb := b;
  207. s.bitk := k;
  208. z.avail_in := n;
  209. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  210. z.next_in := p;
  211. s.write := q;
  212. inflate_codes := inflate_flush(s,z,r);
  213. exit;
  214. end;
  215. dec(n);
  216. b := b or (cardinal(p^) shl k);
  217. Inc(p);
  218. Inc(k, 8);
  219. end;
  220. Inc(c^.len, cardinal(b and inflate_mask[j]));
  221. {DUMPBITS(j);}
  222. b := b shr j;
  223. dec(k, j);
  224. c^.sub.code.need := c^.dbits;
  225. c^.sub.code.tree := c^.dtree;
  226. {$IFDEF ZLIB_DEBUG}
  227. Tracevv('inflate: length '+IntToStr(c^.len));
  228. {$ENDIF}
  229. c^.mode := DIST;
  230. { falltrough }
  231. end;
  232. DIST: { i: get distance next }
  233. begin
  234. j := c^.sub.code.need;
  235. {NEEDBITS(j);}
  236. while (k < j) do
  237. begin
  238. {NEEDBYTE;}
  239. if (n <> 0) then
  240. r :=Z_OK
  241. else
  242. begin
  243. {UPDATE}
  244. s.bitb := b;
  245. s.bitk := k;
  246. z.avail_in := n;
  247. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  248. z.next_in := p;
  249. s.write := q;
  250. inflate_codes := inflate_flush(s,z,r);
  251. exit;
  252. end;
  253. dec(n);
  254. b := b or (cardinal(p^) shl k);
  255. Inc(p);
  256. Inc(k, 8);
  257. end;
  258. t := @huft_ptr(c^.sub.code.tree)^[cardinal(b) and inflate_mask[j]];
  259. {DUMPBITS(t^.bits);}
  260. b := b shr t^.bits;
  261. dec(k, t^.bits);
  262. e := cardinal(t^.exop);
  263. if (e and 16 <> 0) then { distance }
  264. begin
  265. c^.sub.copy.get := e and 15;
  266. c^.sub.copy.dist := t^.base;
  267. c^.mode := DISTEXT;
  268. continue; { break C-switch statement }
  269. end;
  270. if (e and 64 = 0) then { next table }
  271. begin
  272. c^.sub.code.need := e;
  273. c^.sub.code.tree := @huft_ptr(t)^[t^.base];
  274. continue; { break C-switch statement }
  275. end;
  276. c^.mode := BADCODE; { invalid code }
  277. z.msg := 'invalid distance code';
  278. r := Z_DATA_ERROR;
  279. {UPDATE}
  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_codes := inflate_flush(s,z,r);
  287. exit;
  288. end;
  289. DISTEXT: { i: getting distance extra }
  290. begin
  291. j := c^.sub.copy.get;
  292. {NEEDBITS(j);}
  293. while (k < j) 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, ptruint(p)-ptruint(z.next_in));
  305. z.next_in := p;
  306. s.write := q;
  307. inflate_codes := inflate_flush(s,z,r);
  308. exit;
  309. end;
  310. dec(n);
  311. b := b or (cardinal(p^) shl k);
  312. Inc(p);
  313. Inc(k, 8);
  314. end;
  315. Inc(c^.sub.copy.dist, cardinal(b) and inflate_mask[j]);
  316. {DUMPBITS(j);}
  317. b := b shr j;
  318. dec(k, j);
  319. {$IFDEF ZLIB_DEBUG}
  320. Tracevv('inflate: distance '+ IntToStr(c^.sub.copy.dist));
  321. {$ENDIF}
  322. c^.mode := COPY;
  323. { falltrough }
  324. end;
  325. COPY: { o: copying bytes in window, waiting for space }
  326. begin
  327. f := q;
  328. dec(f, c^.sub.copy.dist);
  329. if (cardinal(ptruint(q) - ptruint(s.window)) < c^.sub.copy.dist) then
  330. begin
  331. f := s.zend;
  332. dec(f, c^.sub.copy.dist - cardinal(ptruint(q) - ptruint(s.window)));
  333. end;
  334. while (c^.len <> 0) do
  335. begin
  336. {NEEDOUT}
  337. if (m = 0) then
  338. begin
  339. {WRAP}
  340. if (q = s.zend) and (s.read <> s.window) then
  341. begin
  342. q := s.window;
  343. if ptruint(q) < ptruint(s.read) then
  344. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  345. else
  346. m := cardinal(ptruint(s.zend)-ptruint(q));
  347. end;
  348. if (m = 0) then
  349. begin
  350. {FLUSH}
  351. s.write := q;
  352. r := inflate_flush(s,z,r);
  353. q := s.write;
  354. if ptruint(q) < ptruint(s.read) then
  355. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  356. else
  357. m := cardinal(ptruint(s.zend)-ptruint(q));
  358. {WRAP}
  359. if (q = s.zend) and (s.read <> s.window) then
  360. begin
  361. q := s.window;
  362. if ptruint(q) < ptruint(s.read) then
  363. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  364. else
  365. m := cardinal(ptruint(s.zend)-ptruint(q));
  366. end;
  367. if (m = 0) then
  368. begin
  369. {UPDATE}
  370. s.bitb := b;
  371. s.bitk := k;
  372. z.avail_in := n;
  373. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  374. z.next_in := p;
  375. s.write := q;
  376. inflate_codes := inflate_flush(s,z,r);
  377. exit;
  378. end;
  379. end;
  380. end;
  381. r := Z_OK;
  382. {OUTBYTE( *f++)}
  383. q^ := f^;
  384. Inc(q);
  385. Inc(f);
  386. dec(m);
  387. if (f = s.zend) then
  388. f := s.window;
  389. dec(c^.len);
  390. end;
  391. c^.mode := START;
  392. { C-switch break; not needed }
  393. end;
  394. LIT: { o: got literal, waiting for output space }
  395. begin
  396. {NEEDOUT}
  397. if (m = 0) then
  398. begin
  399. {WRAP}
  400. if (q = s.zend) and (s.read <> s.window) then
  401. begin
  402. q := s.window;
  403. if ptruint(q) < ptruint(s.read) then
  404. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  405. else
  406. m := cardinal(ptruint(s.zend)-ptruint(q));
  407. end;
  408. if (m = 0) then
  409. begin
  410. {FLUSH}
  411. s.write := q;
  412. r := inflate_flush(s,z,r);
  413. q := s.write;
  414. if ptruint(q) < ptruint(s.read) then
  415. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  416. else
  417. m := cardinal(ptruint(s.zend)-ptruint(q));
  418. {WRAP}
  419. if (q = s.zend) and (s.read <> s.window) then
  420. begin
  421. q := s.window;
  422. if ptruint(q) < ptruint(s.read) then
  423. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  424. else
  425. m := cardinal(ptruint(s.zend)-ptruint(q));
  426. end;
  427. if (m = 0) then
  428. begin
  429. {UPDATE}
  430. s.bitb := b;
  431. s.bitk := k;
  432. z.avail_in := n;
  433. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  434. z.next_in := p;
  435. s.write := q;
  436. inflate_codes := inflate_flush(s,z,r);
  437. exit;
  438. end;
  439. end;
  440. end;
  441. r := Z_OK;
  442. {OUTBYTE(c^.sub.lit);}
  443. q^ := c^.sub.lit;
  444. Inc(q);
  445. dec(m);
  446. c^.mode := START;
  447. {break;}
  448. end;
  449. WASH: { o: got eob, possibly more output }
  450. begin
  451. {$ifdef patch112}
  452. if (k > 7) then { return unused byte, if any }
  453. begin
  454. {$IFDEF ZLIB_DEBUG}
  455. Assert(k < 16, 'inflate_codes grabbed too many bytes');
  456. {$ENDIF}
  457. dec(k, 8);
  458. Inc(n);
  459. dec(p); { can always return one }
  460. end;
  461. {$endif}
  462. {FLUSH}
  463. s.write := q;
  464. r := inflate_flush(s,z,r);
  465. q := s.write;
  466. if ptruint(q) < ptruint(s.read) then
  467. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  468. else
  469. m := cardinal(ptruint(s.zend)-ptruint(q));
  470. if (s.read <> s.write) then
  471. begin
  472. {UPDATE}
  473. s.bitb := b;
  474. s.bitk := k;
  475. z.avail_in := n;
  476. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  477. z.next_in := p;
  478. s.write := q;
  479. inflate_codes := inflate_flush(s,z,r);
  480. exit;
  481. end;
  482. c^.mode := ZEND;
  483. { falltrough }
  484. end;
  485. ZEND:
  486. begin
  487. r := Z_STREAM_END;
  488. {UPDATE}
  489. s.bitb := b;
  490. s.bitk := k;
  491. z.avail_in := n;
  492. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  493. z.next_in := p;
  494. s.write := q;
  495. inflate_codes := inflate_flush(s,z,r);
  496. exit;
  497. end;
  498. BADCODE: { x: got error }
  499. begin
  500. r := Z_DATA_ERROR;
  501. {UPDATE}
  502. s.bitb := b;
  503. s.bitk := k;
  504. z.avail_in := n;
  505. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  506. z.next_in := p;
  507. s.write := q;
  508. inflate_codes := inflate_flush(s,z,r);
  509. exit;
  510. end;
  511. else
  512. begin
  513. r := Z_STREAM_ERROR;
  514. {UPDATE}
  515. s.bitb := b;
  516. s.bitk := k;
  517. z.avail_in := n;
  518. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  519. z.next_in := p;
  520. s.write := q;
  521. inflate_codes := inflate_flush(s,z,r);
  522. exit;
  523. end;
  524. end;
  525. {NEED_DUMMY_RETURN - Delphi2+ dumb compilers complain without this }
  526. inflate_codes := Z_STREAM_ERROR;
  527. end;
  528. procedure inflate_codes_free(c : pInflate_codes_state;
  529. var z : z_stream);
  530. begin
  531. dispose(c);
  532. {$IFDEF ZLIB_DEBUG}
  533. Tracev('inflate: codes free');
  534. {$ENDIF}
  535. end;
  536. end.