PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/archivers/lha/huf.cpp

https://github.com/tonioni/WinUAE
C++ | 485 lines | 422 code | 30 blank | 33 comment | 126 complexity | 9a6c05ed17ee08648cecf7e11039e974 MD5 | raw file
  1. /* ------------------------------------------------------------------------ */
  2. /* LHa for UNIX */
  3. /* huf.c -- new static Huffman */
  4. /* */
  5. /* Modified Nobutaka Watazaki */
  6. /* */
  7. /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
  8. /* Ver. 1.14i Support LH7 & Bug Fixed 2000.10. 6 t.okamoto */
  9. /* ------------------------------------------------------------------------ */
  10. #include "lha.h"
  11. #ifdef sony_news
  12. #include <sys/param.h>
  13. #endif
  14. #if defined(__STDC__) || defined(NEWSOS)
  15. #include <stdlib.h>
  16. #endif
  17. /* ------------------------------------------------------------------------ */
  18. unsigned short h_left[2 * NC - 1], h_right[2 * NC - 1];
  19. unsigned char c_len[NC], pt_len[NPT];
  20. unsigned short c_freq[2 * NC - 1], c_table[4096], c_code[NC], p_freq[2 * NP - 1],
  21. pt_table[256], pt_code[NPT], t_freq[2 * NT - 1];
  22. static unsigned char *buf;
  23. static unsigned int bufsiz;
  24. static unsigned short blocksize;
  25. static unsigned short output_pos, output_mask;
  26. static int pbit;
  27. static int np;
  28. /* ------------------------------------------------------------------------ */
  29. /* Encording */
  30. /* ------------------------------------------------------------------------ */
  31. static void count_t_freq(void)
  32. {
  33. short i, k, n, count;
  34. for (i = 0; i < NT; i++)
  35. t_freq[i] = 0;
  36. n = NC;
  37. while (n > 0 && c_len[n - 1] == 0)
  38. n--;
  39. i = 0;
  40. while (i < n) {
  41. k = c_len[i++];
  42. if (k == 0) {
  43. count = 1;
  44. while (i < n && c_len[i] == 0) {
  45. i++;
  46. count++;
  47. }
  48. if (count <= 2)
  49. t_freq[0] += count;
  50. else if (count <= 18)
  51. t_freq[1]++;
  52. else if (count == 19) {
  53. t_freq[0]++;
  54. t_freq[1]++;
  55. }
  56. else
  57. t_freq[2]++;
  58. } else
  59. t_freq[k + 2]++;
  60. }
  61. }
  62. /* ------------------------------------------------------------------------ */
  63. #if 0
  64. static void
  65. write_pt_len(n, nbit, i_special)
  66. short n;
  67. short nbit;
  68. short i_special;
  69. {
  70. short i, k;
  71. while (n > 0 && pt_len[n - 1] == 0)
  72. n--;
  73. putbits(nbit, n);
  74. i = 0;
  75. while (i < n) {
  76. k = pt_len[i++];
  77. if (k <= 6)
  78. putbits(3, k);
  79. else
  80. putbits(k - 3, USHRT_MAX << 1);
  81. if (i == i_special) {
  82. while (i < 6 && pt_len[i] == 0)
  83. i++;
  84. putbits(2, i - 3);
  85. }
  86. }
  87. }
  88. /* ------------------------------------------------------------------------ */
  89. static void
  90. write_c_len(/*void*/)
  91. {
  92. short i, k, n, count;
  93. n = NC;
  94. while (n > 0 && c_len[n - 1] == 0)
  95. n--;
  96. putbits(CBIT, n);
  97. i = 0;
  98. while (i < n) {
  99. k = c_len[i++];
  100. if (k == 0) {
  101. count = 1;
  102. while (i < n && c_len[i] == 0) {
  103. i++;
  104. count++;
  105. }
  106. if (count <= 2) {
  107. for (k = 0; k < count; k++)
  108. putcode(pt_len[0], pt_code[0]);
  109. }
  110. else if (count <= 18) {
  111. putcode(pt_len[1], pt_code[1]);
  112. putbits(4, count - 3);
  113. }
  114. else if (count == 19) {
  115. putcode(pt_len[0], pt_code[0]);
  116. putcode(pt_len[1], pt_code[1]);
  117. putbits(4, 15);
  118. }
  119. else {
  120. putcode(pt_len[2], pt_code[2]);
  121. putbits(CBIT, count - 20);
  122. }
  123. }
  124. else
  125. putcode(pt_len[k + 2], pt_code[k + 2]);
  126. }
  127. }
  128. /* ------------------------------------------------------------------------ */
  129. static void
  130. encode_c(c)
  131. short c;
  132. {
  133. putcode(c_len[c], c_code[c]);
  134. }
  135. /* ------------------------------------------------------------------------ */
  136. static void
  137. encode_p(p)
  138. unsigned short p;
  139. {
  140. unsigned short c, q;
  141. c = 0;
  142. q = p;
  143. while (q) {
  144. q >>= 1;
  145. c++;
  146. }
  147. putcode(pt_len[c], pt_code[c]);
  148. if (c > 1)
  149. putbits(c - 1, p);
  150. }
  151. /* ------------------------------------------------------------------------ */
  152. static void
  153. send_block( /* void */ )
  154. {
  155. unsigned char flags;
  156. unsigned short i, k, root, pos, size;
  157. root = make_tree(NC, c_freq, c_len, c_code);
  158. size = c_freq[root];
  159. putbits(16, size);
  160. if (root >= NC) {
  161. count_t_freq();
  162. root = make_tree(NT, t_freq, pt_len, pt_code);
  163. if (root >= NT) {
  164. write_pt_len(NT, TBIT, 3);
  165. } else {
  166. putbits(TBIT, 0);
  167. putbits(TBIT, root);
  168. }
  169. write_c_len();
  170. } else {
  171. putbits(TBIT, 0);
  172. putbits(TBIT, 0);
  173. putbits(CBIT, 0);
  174. putbits(CBIT, root);
  175. }
  176. root = make_tree(np, p_freq, pt_len, pt_code);
  177. if (root >= np) {
  178. write_pt_len(np, pbit, -1);
  179. }
  180. else {
  181. putbits(pbit, 0);
  182. putbits(pbit, root);
  183. }
  184. pos = 0;
  185. for (i = 0; i < size; i++) {
  186. if (i % CHAR_BIT == 0)
  187. flags = buf[pos++];
  188. else
  189. flags <<= 1;
  190. if (flags & (1 << (CHAR_BIT - 1))) {
  191. encode_c(buf[pos++] + (1 << CHAR_BIT));
  192. k = buf[pos++] << CHAR_BIT;
  193. k += buf[pos++];
  194. encode_p(k);
  195. } else
  196. encode_c(buf[pos++]);
  197. if (unpackable)
  198. return;
  199. }
  200. for (i = 0; i < NC; i++)
  201. c_freq[i] = 0;
  202. for (i = 0; i < np; i++)
  203. p_freq[i] = 0;
  204. }
  205. /* ------------------------------------------------------------------------ */
  206. void
  207. output_st1(c, p)
  208. unsigned short c;
  209. unsigned short p;
  210. {
  211. static unsigned short cpos;
  212. output_mask >>= 1;
  213. if (output_mask == 0) {
  214. output_mask = 1 << (CHAR_BIT - 1);
  215. if (output_pos >= bufsiz - 3 * CHAR_BIT) {
  216. send_block();
  217. if (unpackable)
  218. return;
  219. output_pos = 0;
  220. }
  221. cpos = output_pos++;
  222. buf[cpos] = 0;
  223. }
  224. buf[output_pos++] = (unsigned char) c;
  225. c_freq[c]++;
  226. if (c >= (1 << CHAR_BIT)) {
  227. buf[cpos] |= output_mask;
  228. buf[output_pos++] = (unsigned char) (p >> CHAR_BIT);
  229. buf[output_pos++] = (unsigned char) p;
  230. c = 0;
  231. while (p) {
  232. p >>= 1;
  233. c++;
  234. }
  235. p_freq[c]++;
  236. }
  237. }
  238. #endif
  239. /* ------------------------------------------------------------------------ */
  240. unsigned char *alloc_buf(void)
  241. {
  242. bufsiz = 16 * 1024 *2; /* 65408U; */ /* t.okamoto */
  243. while ((buf = (unsigned char *) malloc(bufsiz)) == NULL) {
  244. bufsiz = (bufsiz / 10) * 9;
  245. if (bufsiz < 4 * 1024)
  246. break;
  247. }
  248. return buf;
  249. }
  250. /* ------------------------------------------------------------------------ */
  251. #if 0
  252. void
  253. encode_start_st1( /* void */ )
  254. {
  255. int i;
  256. #if 0
  257. if (dicbit <= (MAX_DICBIT - 2)) {
  258. pbit = 4; /* lh4,5 etc. */
  259. np = 14;
  260. } else {
  261. pbit = 5; /* lh6 */
  262. np = 16;
  263. }
  264. #endif
  265. if (dicbit <= 13) {
  266. pbit = 4; /* lh4,5 etc. */
  267. np = 14;
  268. } else {
  269. pbit = 5; /* lh6,7 */
  270. if (dicbit == 16)
  271. np = 17;
  272. else
  273. np = 16;
  274. }
  275. for (i = 0; i < NC; i++)
  276. c_freq[i] = 0;
  277. for (i = 0; i < np; i++)
  278. p_freq[i] = 0;
  279. output_pos = output_mask = 0;
  280. init_putbits();
  281. buf[0] = 0;
  282. }
  283. /* ------------------------------------------------------------------------ */
  284. void
  285. encode_end_st1( /* void */ )
  286. {
  287. if (!unpackable) {
  288. send_block();
  289. putbits(CHAR_BIT - 1, 0); /* flush remaining bits */
  290. }
  291. }
  292. #endif
  293. /* ------------------------------------------------------------------------ */
  294. /* decoding */
  295. /* ------------------------------------------------------------------------ */
  296. static void read_pt_len(short nn, short nbit, short i_special)
  297. {
  298. int i, c, n;
  299. n = getbits(nbit);
  300. if (n == 0) {
  301. c = getbits(nbit);
  302. for (i = 0; i < nn; i++)
  303. pt_len[i] = 0;
  304. for (i = 0; i < 256; i++)
  305. pt_table[i] = c;
  306. }
  307. else {
  308. i = 0;
  309. while (i < n) {
  310. c = lhabitbuf >> (16 - 3);
  311. if (c == 7) {
  312. unsigned short mask = 1 << (16 - 4);
  313. while (mask & lhabitbuf) {
  314. mask >>= 1;
  315. c++;
  316. }
  317. }
  318. fillbuf((c < 7) ? 3 : c - 3);
  319. pt_len[i++] = c;
  320. if (i == i_special) {
  321. c = getbits(2);
  322. while (--c >= 0)
  323. pt_len[i++] = 0;
  324. }
  325. }
  326. while (i < nn)
  327. pt_len[i++] = 0;
  328. lha_make_table(nn, pt_len, 8, pt_table);
  329. }
  330. }
  331. /* ------------------------------------------------------------------------ */
  332. static void read_c_len(void)
  333. {
  334. short i, c, n;
  335. n = getbits(CBIT);
  336. if (n == 0) {
  337. c = getbits(CBIT);
  338. for (i = 0; i < NC; i++)
  339. c_len[i] = 0;
  340. for (i = 0; i < 4096; i++)
  341. c_table[i] = c;
  342. } else {
  343. i = 0;
  344. while (i < n) {
  345. c = pt_table[lhabitbuf >> (16 - 8)];
  346. if (c >= NT) {
  347. unsigned short mask = 1 << (16 - 9);
  348. do {
  349. if (lhabitbuf & mask)
  350. c = h_right[c];
  351. else
  352. c = h_left[c];
  353. mask >>= 1;
  354. } while (c >= NT);
  355. }
  356. fillbuf(pt_len[c]);
  357. if (c <= 2) {
  358. if (c == 0)
  359. c = 1;
  360. else if (c == 1)
  361. c = getbits(4) + 3;
  362. else
  363. c = getbits(CBIT) + 20;
  364. while (--c >= 0)
  365. c_len[i++] = 0;
  366. }
  367. else
  368. c_len[i++] = c - 2;
  369. }
  370. while (i < NC)
  371. c_len[i++] = 0;
  372. lha_make_table(NC, c_len, 12, c_table);
  373. }
  374. }
  375. /* ------------------------------------------------------------------------ */
  376. unsigned short decode_c_st1(void)
  377. {
  378. unsigned short j, mask;
  379. if (blocksize == 0) {
  380. blocksize = getbits(16);
  381. read_pt_len(NT, TBIT, 3);
  382. read_c_len();
  383. read_pt_len(np, pbit, -1);
  384. }
  385. blocksize--;
  386. j = c_table[lhabitbuf >> 4];
  387. if (j < NC)
  388. fillbuf(c_len[j]);
  389. else {
  390. fillbuf(12);
  391. mask = 1 << (16 - 1);
  392. do {
  393. if (lhabitbuf & mask)
  394. j = h_right[j];
  395. else
  396. j = h_left[j];
  397. mask >>= 1;
  398. } while (j >= NC);
  399. fillbuf(c_len[j] - 12);
  400. }
  401. return j;
  402. }
  403. /* ------------------------------------------------------------------------ */
  404. unsigned short decode_p_st1(void)
  405. {
  406. unsigned short j, mask;
  407. j = pt_table[lhabitbuf >> (16 - 8)];
  408. if (j < np)
  409. fillbuf(pt_len[j]);
  410. else {
  411. fillbuf(8);
  412. mask = 1 << (16 - 1);
  413. do {
  414. if (lhabitbuf & mask)
  415. j = h_right[j];
  416. else
  417. j = h_left[j];
  418. mask >>= 1;
  419. } while (j >= np);
  420. fillbuf(pt_len[j] - 8);
  421. }
  422. if (j != 0)
  423. j = (1 << (j - 1)) + getbits(j - 1);
  424. return j;
  425. }
  426. /* ------------------------------------------------------------------------ */
  427. void decode_start_st1(void)
  428. {
  429. if (dicbit <= 13) {
  430. np = 14;
  431. pbit = 4;
  432. } else {
  433. if (dicbit == 16) {
  434. np = 17; /* for -lh7- */
  435. } else {
  436. np = 16;
  437. }
  438. pbit = 5;
  439. }
  440. #if 0
  441. if (dicbit <= 13) { /* 13 ... Changed N.Watazaki */
  442. np = 14;
  443. pbit = 4;
  444. } else {
  445. np = 16;
  446. pbit = 5;
  447. }
  448. #endif
  449. init_getbits();
  450. blocksize = 0;
  451. }
  452. /* Local Variables: */
  453. /* mode:c */
  454. /* tab-width:4 */
  455. /* End: */