infcodes.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. //if this is the last block, there are no bytes left in stream and the block end code follows, finish processing this block
  136. if s.last then
  137. begin
  138. t := c^.sub.code.tree;
  139. if t^.exop and 32 <> 0 then
  140. break;
  141. end;
  142. exit;
  143. end;
  144. dec(n);
  145. b := b or (cardinal(p^) shl k);
  146. Inc(p);
  147. Inc(k, 8);
  148. end;
  149. t := c^.sub.code.tree;
  150. Inc(t, cardinal(b) and inflate_mask[j]);
  151. {DUMPBITS(t^.bits);}
  152. b := b shr t^.bits;
  153. dec(k, t^.bits);
  154. e := cardinal(t^.exop);
  155. if (e = 0) then { literal }
  156. begin
  157. c^.sub.lit := t^.base;
  158. {$IFDEF ZLIB_DEBUG}
  159. if (t^.base >= $20) and (t^.base < $7f) then
  160. Tracevv('inflate: literal '+char(t^.base))
  161. else
  162. Tracevv('inflate: literal '+IntToStr(t^.base));
  163. {$ENDIF}
  164. c^.mode := LIT;
  165. continue; { break switch statement }
  166. end;
  167. if (e and 16 <> 0) then { length }
  168. begin
  169. c^.sub.copy.get := e and 15;
  170. c^.len := t^.base;
  171. c^.mode := LENEXT;
  172. continue; { break C-switch statement }
  173. end;
  174. if (e and 64 = 0) then { next table }
  175. begin
  176. c^.sub.code.need := e;
  177. c^.sub.code.tree := @huft_ptr(t)^[t^.base];
  178. continue; { break C-switch statement }
  179. end;
  180. if (e and 32 <> 0) then { end of block }
  181. begin
  182. {$IFDEF ZLIB_DEBUG}
  183. Tracevv('inflate: end of block');
  184. {$ENDIF}
  185. c^.mode := WASH;
  186. continue; { break C-switch statement }
  187. end;
  188. c^.mode := BADCODE; { invalid code }
  189. z.msg := 'invalid literal/length code';
  190. r := Z_DATA_ERROR;
  191. {UPDATE}
  192. s.bitb := b;
  193. s.bitk := k;
  194. z.avail_in := n;
  195. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  196. z.next_in := p;
  197. s.write := q;
  198. inflate_codes := inflate_flush(s,z,r);
  199. exit;
  200. end;
  201. LENEXT: { i: getting length extra (have base) }
  202. begin
  203. j := c^.sub.copy.get;
  204. {NEEDBITS(j);}
  205. while (k < j) do
  206. begin
  207. {NEEDBYTE;}
  208. if (n <> 0) then
  209. r :=Z_OK
  210. else
  211. begin
  212. {UPDATE}
  213. s.bitb := b;
  214. s.bitk := k;
  215. z.avail_in := n;
  216. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  217. z.next_in := p;
  218. s.write := q;
  219. inflate_codes := inflate_flush(s,z,r);
  220. exit;
  221. end;
  222. dec(n);
  223. b := b or (cardinal(p^) shl k);
  224. Inc(p);
  225. Inc(k, 8);
  226. end;
  227. Inc(c^.len, cardinal(b and inflate_mask[j]));
  228. {DUMPBITS(j);}
  229. b := b shr j;
  230. dec(k, j);
  231. c^.sub.code.need := c^.dbits;
  232. c^.sub.code.tree := c^.dtree;
  233. {$IFDEF ZLIB_DEBUG}
  234. Tracevv('inflate: length '+IntToStr(c^.len));
  235. {$ENDIF}
  236. c^.mode := DIST;
  237. { falltrough }
  238. end;
  239. DIST: { i: get distance next }
  240. begin
  241. j := c^.sub.code.need;
  242. {NEEDBITS(j);}
  243. while (k < j) do
  244. begin
  245. {NEEDBYTE;}
  246. if (n <> 0) then
  247. r :=Z_OK
  248. else
  249. begin
  250. {UPDATE}
  251. s.bitb := b;
  252. s.bitk := k;
  253. z.avail_in := n;
  254. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  255. z.next_in := p;
  256. s.write := q;
  257. inflate_codes := inflate_flush(s,z,r);
  258. exit;
  259. end;
  260. dec(n);
  261. b := b or (cardinal(p^) shl k);
  262. Inc(p);
  263. Inc(k, 8);
  264. end;
  265. t := @huft_ptr(c^.sub.code.tree)^[cardinal(b) and inflate_mask[j]];
  266. {DUMPBITS(t^.bits);}
  267. b := b shr t^.bits;
  268. dec(k, t^.bits);
  269. e := cardinal(t^.exop);
  270. if (e and 16 <> 0) then { distance }
  271. begin
  272. c^.sub.copy.get := e and 15;
  273. c^.sub.copy.dist := t^.base;
  274. c^.mode := DISTEXT;
  275. continue; { break C-switch statement }
  276. end;
  277. if (e and 64 = 0) then { next table }
  278. begin
  279. c^.sub.code.need := e;
  280. c^.sub.code.tree := @huft_ptr(t)^[t^.base];
  281. continue; { break C-switch statement }
  282. end;
  283. c^.mode := BADCODE; { invalid code }
  284. z.msg := 'invalid distance code';
  285. r := Z_DATA_ERROR;
  286. {UPDATE}
  287. s.bitb := b;
  288. s.bitk := k;
  289. z.avail_in := n;
  290. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  291. z.next_in := p;
  292. s.write := q;
  293. inflate_codes := inflate_flush(s,z,r);
  294. exit;
  295. end;
  296. DISTEXT: { i: getting distance extra }
  297. begin
  298. j := c^.sub.copy.get;
  299. {NEEDBITS(j);}
  300. while (k < j) do
  301. begin
  302. {NEEDBYTE;}
  303. if (n <> 0) then
  304. r :=Z_OK
  305. else
  306. begin
  307. {UPDATE}
  308. s.bitb := b;
  309. s.bitk := k;
  310. z.avail_in := n;
  311. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  312. z.next_in := p;
  313. s.write := q;
  314. inflate_codes := inflate_flush(s,z,r);
  315. exit;
  316. end;
  317. dec(n);
  318. b := b or (cardinal(p^) shl k);
  319. Inc(p);
  320. Inc(k, 8);
  321. end;
  322. Inc(c^.sub.copy.dist, cardinal(b) and inflate_mask[j]);
  323. {DUMPBITS(j);}
  324. b := b shr j;
  325. dec(k, j);
  326. {$IFDEF ZLIB_DEBUG}
  327. Tracevv('inflate: distance '+ IntToStr(c^.sub.copy.dist));
  328. {$ENDIF}
  329. c^.mode := COPY;
  330. { falltrough }
  331. end;
  332. COPY: { o: copying bytes in window, waiting for space }
  333. begin
  334. f := q;
  335. dec(f, c^.sub.copy.dist);
  336. if (cardinal(ptruint(q) - ptruint(s.window)) < c^.sub.copy.dist) then
  337. begin
  338. f := s.zend;
  339. dec(f, c^.sub.copy.dist - cardinal(ptruint(q) - ptruint(s.window)));
  340. end;
  341. while (c^.len <> 0) do
  342. begin
  343. {NEEDOUT}
  344. if (m = 0) then
  345. begin
  346. {WRAP}
  347. if (q = s.zend) and (s.read <> s.window) then
  348. begin
  349. q := s.window;
  350. if ptruint(q) < ptruint(s.read) then
  351. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  352. else
  353. m := cardinal(ptruint(s.zend)-ptruint(q));
  354. end;
  355. if (m = 0) then
  356. begin
  357. {FLUSH}
  358. s.write := q;
  359. r := inflate_flush(s,z,r);
  360. q := s.write;
  361. if ptruint(q) < ptruint(s.read) then
  362. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  363. else
  364. m := cardinal(ptruint(s.zend)-ptruint(q));
  365. {WRAP}
  366. if (q = s.zend) and (s.read <> s.window) then
  367. begin
  368. q := s.window;
  369. if ptruint(q) < ptruint(s.read) then
  370. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  371. else
  372. m := cardinal(ptruint(s.zend)-ptruint(q));
  373. end;
  374. if (m = 0) then
  375. begin
  376. {UPDATE}
  377. s.bitb := b;
  378. s.bitk := k;
  379. z.avail_in := n;
  380. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  381. z.next_in := p;
  382. s.write := q;
  383. inflate_codes := inflate_flush(s,z,r);
  384. exit;
  385. end;
  386. end;
  387. end;
  388. r := Z_OK;
  389. {OUTBYTE( *f++)}
  390. q^ := f^;
  391. Inc(q);
  392. Inc(f);
  393. dec(m);
  394. if (f = s.zend) then
  395. f := s.window;
  396. dec(c^.len);
  397. end;
  398. c^.mode := START;
  399. { C-switch break; not needed }
  400. end;
  401. LIT: { o: got literal, waiting for output space }
  402. begin
  403. {NEEDOUT}
  404. if (m = 0) then
  405. begin
  406. {WRAP}
  407. if (q = s.zend) and (s.read <> s.window) then
  408. begin
  409. q := s.window;
  410. if ptruint(q) < ptruint(s.read) then
  411. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  412. else
  413. m := cardinal(ptruint(s.zend)-ptruint(q));
  414. end;
  415. if (m = 0) then
  416. begin
  417. {FLUSH}
  418. s.write := q;
  419. r := inflate_flush(s,z,r);
  420. q := s.write;
  421. if ptruint(q) < ptruint(s.read) then
  422. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  423. else
  424. m := cardinal(ptruint(s.zend)-ptruint(q));
  425. {WRAP}
  426. if (q = s.zend) and (s.read <> s.window) then
  427. begin
  428. q := s.window;
  429. if ptruint(q) < ptruint(s.read) then
  430. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  431. else
  432. m := cardinal(ptruint(s.zend)-ptruint(q));
  433. end;
  434. if (m = 0) then
  435. begin
  436. {UPDATE}
  437. s.bitb := b;
  438. s.bitk := k;
  439. z.avail_in := n;
  440. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  441. z.next_in := p;
  442. s.write := q;
  443. inflate_codes := inflate_flush(s,z,r);
  444. exit;
  445. end;
  446. end;
  447. end;
  448. r := Z_OK;
  449. {OUTBYTE(c^.sub.lit);}
  450. q^ := c^.sub.lit;
  451. Inc(q);
  452. dec(m);
  453. c^.mode := START;
  454. {break;}
  455. end;
  456. WASH: { o: got eob, possibly more output }
  457. begin
  458. {$ifdef patch112}
  459. if (k > 7) then { return unused byte, if any }
  460. begin
  461. {$IFDEF ZLIB_DEBUG}
  462. Assert(k < 16, 'inflate_codes grabbed too many bytes');
  463. {$ENDIF}
  464. dec(k, 8);
  465. Inc(n);
  466. dec(p); { can always return one }
  467. end;
  468. {$endif}
  469. {FLUSH}
  470. s.write := q;
  471. r := inflate_flush(s,z,r);
  472. q := s.write;
  473. if ptruint(q) < ptruint(s.read) then
  474. m := cardinal(ptruint(s.read)-ptruint(q)-1)
  475. else
  476. m := cardinal(ptruint(s.zend)-ptruint(q));
  477. if (s.read <> s.write) then
  478. begin
  479. {UPDATE}
  480. s.bitb := b;
  481. s.bitk := k;
  482. z.avail_in := n;
  483. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  484. z.next_in := p;
  485. s.write := q;
  486. inflate_codes := inflate_flush(s,z,r);
  487. exit;
  488. end;
  489. c^.mode := ZEND;
  490. { falltrough }
  491. end;
  492. ZEND:
  493. begin
  494. r := Z_STREAM_END;
  495. {UPDATE}
  496. s.bitb := b;
  497. s.bitk := k;
  498. z.avail_in := n;
  499. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  500. z.next_in := p;
  501. s.write := q;
  502. inflate_codes := inflate_flush(s,z,r);
  503. exit;
  504. end;
  505. BADCODE: { x: got error }
  506. begin
  507. r := Z_DATA_ERROR;
  508. {UPDATE}
  509. s.bitb := b;
  510. s.bitk := k;
  511. z.avail_in := n;
  512. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  513. z.next_in := p;
  514. s.write := q;
  515. inflate_codes := inflate_flush(s,z,r);
  516. exit;
  517. end;
  518. else
  519. begin
  520. r := Z_STREAM_ERROR;
  521. {UPDATE}
  522. s.bitb := b;
  523. s.bitk := k;
  524. z.avail_in := n;
  525. Inc(z.total_in, ptruint(p)-ptruint(z.next_in));
  526. z.next_in := p;
  527. s.write := q;
  528. inflate_codes := inflate_flush(s,z,r);
  529. exit;
  530. end;
  531. end;
  532. {NEED_DUMMY_RETURN - Delphi2+ dumb compilers complain without this }
  533. inflate_codes := Z_STREAM_ERROR;
  534. end;
  535. procedure inflate_codes_free(c : pInflate_codes_state;
  536. var z : z_stream);
  537. begin
  538. dispose(c);
  539. {$IFDEF ZLIB_DEBUG}
  540. Tracev('inflate: codes free');
  541. {$ENDIF}
  542. end;
  543. end.