• R/O
  • HTTP
  • SSH
  • HTTPS

olha: Commit


Commit MetaInfo

Revision98706404653b523090ebe92bf776ad90ddf8d68d (tree)
Time2008-06-23 14:45:55
AuthorKoji Arai <jca02266@gmai...>
CommiterKoji Arai

Log Message

huf.c was refined.

Change Summary

Incremental Difference

--- a/ar.h
+++ b/ar.h
@@ -6,6 +6,22 @@
66 #include <limits.h>
77 #include <sys/types.h>
88
9+#define MAXDICBIT 16 /* 12(-lh4-) or 13(-lh5-) */
10+#define MAXDICSIZ (1U << MAXDICBIT)
11+#define MATCHBIT 8 /* bits for MAXMATCH - THRESHOLD */
12+#define MAXMATCH 256 /* formerly F (not more than UCHAR_MAX + 1) */
13+#define THRESHOLD 3 /* choose optimal value */
14+
15+#define NC (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
16+ /* alphabet = {0, 1, 2, ..., NC - 1} */
17+#define CBIT 9 /* $\lfloor \log_2 NC \rfloor + 1$ */
18+#define CODE_BIT 16 /* codeword length */
19+
20+#define NP (MAXDICBIT + 1)
21+#define NT (CODE_BIT + 3)
22+#define PBIT 4 /* smallest integer such that (1U << PBIT) > NP */
23+#define TBIT 5 /* smallest integer such that (1U << TBIT) > NT */
24+
925 typedef unsigned char uchar; /* 8 bits or more */
1026 typedef unsigned int uint; /* 16 bits or more */
1127 typedef unsigned short ushort; /* 16 bits or more */
@@ -21,6 +37,11 @@ struct lzh_istream {
2137
2238 /* huf.c */
2339 unsigned int blocksize;
40+
41+ unsigned char c_len[NC];
42+ unsigned short c_table[4096];
43+ unsigned char p_len[NP];
44+ unsigned short p_table[256];
2445 };
2546
2647 struct lzh_ostream {
@@ -41,6 +62,9 @@ struct lzh_ostream {
4162 /* huf.c:output() */
4263 unsigned int output_pos, output_mask;
4364 unsigned int cpos;
65+
66+ unsigned short c_freq[2 * NC - 1];
67+ unsigned short p_freq[2 * NP - 1];
4468 };
4569
4670 struct lzh_header {
@@ -101,21 +125,9 @@ extern struct lha_opts opts;
101125
102126 #define BITBUFSIZ (CHAR_BIT * sizeof(unsigned short)) /* size of bitbuf */
103127
104-/* encode.c and decode.c */
105-
106-#define MAXDICBIT 16 /* 12(-lh4-) or 13(-lh5-) */
107-#define MAXDICSIZ (1U << MAXDICBIT)
108-#define MATCHBIT 8 /* bits for MAXMATCH - THRESHOLD */
109-#define MAXMATCH 256 /* formerly F (not more than UCHAR_MAX + 1) */
110-#define THRESHOLD 3 /* choose optimal value */
111-
112128 /* huf.c */
113129
114-#define NC (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
115- /* alphabet = {0, 1, 2, ..., NC - 1} */
116-#define CBIT 9 /* $\lfloor \log_2 NC \rfloor + 1$ */
117-#define CODE_BIT 16 /* codeword length */
118-
130+/* decoding */
119131 struct huf_t {
120132 ushort left[2 * NC - 1], right[2 * NC - 1];
121133 };
--- a/extract.c
+++ b/extract.c
@@ -10,7 +10,6 @@ extract(struct lzh_istream *rp, int to_file, struct lzh_header *h)
1010 {
1111 FILE *outfile = NULL;
1212 unsigned int crc;
13- struct huf_t huf;
1413
1514 if (to_file) {
1615 if (memcmp(h->method, "-lhd-", sizeof(h->method)) == 0) {
@@ -58,6 +57,7 @@ extract(struct lzh_istream *rp, int to_file, struct lzh_header *h)
5857 unsigned int slide_off = 0;
5958 int slide_len = 0;
6059 unsigned long remainder = h->origsize;
60+ struct huf_t huf;
6161
6262 crc = INIT_CRC;
6363 if (opts.method->dicbit != 0)
--- a/huf.c
+++ b/huf.c
@@ -4,38 +4,9 @@
44 #include <stdlib.h>
55 #include "ar.h"
66
7-#define NP (MAXDICBIT + 1)
8-#define NT (CODE_BIT + 3)
9-#define PBIT 4 /* smallest integer such that (1U << PBIT) > NP */
10-#define TBIT 5 /* smallest integer such that (1U << TBIT) > NT */
11-#if NT > NP
12-#define NPT NT
13-#else
14-#define NPT NP
15-#endif
16-
177 static int np;
188 static int pbit;
199
20-/* encoding/decoding */
21-static uchar c_len[NC], pt_len[NPT];
22-
23-/* encoding */
24-static ushort c_freq[2 * NC - 1], c_code[NC];
25-static ushort p_freq[2 * NP - 1], pt_code[NPT];
26-static ushort t_freq[2 * NT - 1];
27-
28-/*
29- size frequency bitlength Huffman coding
30- -----------------------------------------------------------
31- NC c_freq c_len c_code
32- NT t_freq pt_len pt_code
33- np p_freq pt_len pt_code
34- */
35-
36-/* decoding */
37-static ushort c_table[4096], pt_table[256];
38-
3910 static void
4011 init_parameter(struct lha_method *m)
4112 {
@@ -46,7 +17,7 @@ init_parameter(struct lha_method *m)
4617 /***** encoding *****/
4718
4819 static void
49-count_t_freq(void)
20+count_t_freq(struct lzh_ostream *wp, uchar *c_len, ushort *t_freq)
5021 {
5122 int i, k, n, count;
5223
@@ -81,7 +52,8 @@ count_t_freq(void)
8152 }
8253
8354 static void
84-write_pt_len(struct lzh_ostream *wp, int n, int nbit, int i_special)
55+write_pt_len(struct lzh_ostream *wp, int n, int nbit, int i_special,
56+ uchar *pt_len)
8557 {
8658 int i, k;
8759
@@ -104,7 +76,7 @@ write_pt_len(struct lzh_ostream *wp, int n, int nbit, int i_special)
10476 }
10577
10678 static void
107-write_c_len(struct lzh_ostream *wp)
79+write_c_len(struct lzh_ostream *wp, uchar *c_len, uchar *t_len, ushort *t_code)
10880 {
10981 int i, k, n, count;
11082
@@ -123,35 +95,35 @@ write_c_len(struct lzh_ostream *wp)
12395 }
12496 if (count <= 2) {
12597 for (k = 0; k < count; k++)
126- putbits(wp, pt_len[0], pt_code[0]);
98+ putbits(wp, t_len[0], t_code[0]);
12799 }
128100 else if (count <= 18) {
129- putbits(wp, pt_len[1], pt_code[1]);
101+ putbits(wp, t_len[1], t_code[1]);
130102 putbits(wp, 4, count - 3);
131103 }
132104 else if (count == 19) {
133- putbits(wp, pt_len[0], pt_code[0]);
134- putbits(wp, pt_len[1], pt_code[1]);
105+ putbits(wp, t_len[0], t_code[0]);
106+ putbits(wp, t_len[1], t_code[1]);
135107 putbits(wp, 4, 15);
136108 }
137109 else {
138- putbits(wp, pt_len[2], pt_code[2]);
110+ putbits(wp, t_len[2], t_code[2]);
139111 putbits(wp, CBIT, count - 20);
140112 }
141113 }
142114 else
143- putbits(wp, pt_len[k + 2], pt_code[k + 2]);
115+ putbits(wp, t_len[k + 2], t_code[k + 2]);
144116 }
145117 }
146118
147119 static void
148-encode_c(struct lzh_ostream *wp, int c)
120+encode_c(struct lzh_ostream *wp, int c, uchar *c_len, ushort *c_code)
149121 {
150122 putbits(wp, c_len[c], c_code[c]);
151123 }
152124
153125 static void
154-encode_p(struct lzh_ostream *wp, uint p)
126+encode_p(struct lzh_ostream *wp, uint p, uchar *p_len, ushort *p_code)
155127 {
156128 uint c, q;
157129
@@ -161,12 +133,18 @@ encode_p(struct lzh_ostream *wp, uint p)
161133 q >>= 1;
162134 c++;
163135 }
164- putbits(wp, pt_len[c], pt_code[c]);
136+ putbits(wp, p_len[c], p_code[c]);
165137 if (c > 1)
166138 putbits(wp, c - 1, p & (0xFFFFU >> (17 - c)));
167139 }
168140
169141 /*
142+ size frequency bitlength Huffman coding
143+ -----------------------------------------------------------
144+ NC c_freq c_len c_code
145+ NT t_freq t_len t_code
146+ np p_freq p_len p_code
147+
170148 output format for a block.
171149
172150 +-----------+
@@ -175,7 +153,7 @@ encode_p(struct lzh_ostream *wp, uint p)
175153 16bit
176154
177155 +-----+--------------------+
178- | len | pt_len | Huffman tree for c_len[]
156+ | len | t_len | Huffman tree for c_len[]
179157 +-----+--------------------+
180158 5bit ?? bit
181159 TBIT
@@ -187,7 +165,7 @@ encode_p(struct lzh_ostream *wp, uint p)
187165 CBIT
188166
189167 +---------+--------------------+
190- | len | pt_len | Huffman tree for offset
168+ | len | p_len | Huffman tree for offset
191169 +---------+--------------------+
192170 pbit ?? bit
193171 (pbit=4bit(lh4,5) or 5bit(lh6,7))
@@ -232,23 +210,34 @@ send_block(struct lzh_ostream *wp)
232210 {
233211 uint i, k, flags, root, pos, size;
234212
213+ uchar c_len[NC];
214+ ushort c_code[NC];
215+
216+ uchar t_len[NT];
217+ ushort t_code[NT];
218+
219+ uchar p_len[NP];
220+ ushort p_code[NP];
221+
235222 /* make Huffman tree for characters and length */
236- root = make_tree(NC, c_freq, c_len, c_code);
237- size = c_freq[root];
223+ root = make_tree(NC, wp->c_freq, c_len, c_code);
224+ size = wp->c_freq[root];
238225 putbits(wp, 16, size);
239226 if (root >= NC) {
227+ ushort t_freq[2 * NT - 1];
228+
240229 /* make Huffman tree for c_len */
241- count_t_freq();
242- root = make_tree(NT, t_freq, pt_len, pt_code);
230+ count_t_freq(wp, c_len, t_freq);
231+ root = make_tree(NT, t_freq, t_len, t_code);
243232 if (root >= NT) {
244- write_pt_len(wp, NT, TBIT, 3);
233+ write_pt_len(wp, NT, TBIT, 3, t_len);
245234 }
246235 else {
247236 /* only one kind */
248237 putbits(wp, TBIT, 0);
249238 putbits(wp, TBIT, root);
250239 }
251- write_c_len(wp);
240+ write_c_len(wp, c_len, t_len, t_code);
252241 }
253242 else {
254243 /* only one kind */
@@ -259,9 +248,9 @@ send_block(struct lzh_ostream *wp)
259248 }
260249
261250 /* make Huffman tree for offset */
262- root = make_tree(np, p_freq, pt_len, pt_code);
251+ root = make_tree(np, wp->p_freq, p_len, p_code);
263252 if (root >= np) {
264- write_pt_len(wp, np, pbit, -1);
253+ write_pt_len(wp, np, pbit, -1, p_len);
265254 }
266255 else {
267256 putbits(wp, pbit, 0);
@@ -277,15 +266,15 @@ send_block(struct lzh_ostream *wp)
277266 flags <<= 1;
278267 if (flags & (1U << (CHAR_BIT - 1))) {
279268 /* write length */
280- encode_c(wp, wp->buf[pos++] + (1U << CHAR_BIT));
269+ encode_c(wp, wp->buf[pos++] + (1U << CHAR_BIT), c_len, c_code);
281270 /* write offset */
282271 k = wp->buf[pos++] << CHAR_BIT;
283272 k += wp->buf[pos++];
284- encode_p(wp, k);
273+ encode_p(wp, k, p_len, p_code);
285274 }
286275 else {
287276 /* write character */
288- encode_c(wp, wp->buf[pos++]);
277+ encode_c(wp, wp->buf[pos++], c_len, c_code);
289278 }
290279 if (wp->unpackable)
291280 return;
@@ -293,9 +282,9 @@ send_block(struct lzh_ostream *wp)
293282
294283 /* clear frequency table */
295284 for (i = 0; i < NC; i++)
296- c_freq[i] = 0;
285+ wp->c_freq[i] = 0;
297286 for (i = 0; i < np; i++)
298- p_freq[i] = 0;
287+ wp->p_freq[i] = 0;
299288 }
300289
301290 /*
@@ -330,7 +319,7 @@ output(struct lzh_ostream *wp, uint c, uint p)
330319 wp->buf[wp->cpos] = 0;
331320 }
332321 wp->buf[wp->output_pos++] = (uchar) c;
333- c_freq[c]++;
322+ wp->c_freq[c]++;
334323
335324 if (c >= (1U << CHAR_BIT)) {
336325 /* c is length, p is offset */
@@ -345,7 +334,7 @@ output(struct lzh_ostream *wp, uint c, uint p)
345334 p >>= 1;
346335 n++;
347336 }
348- p_freq[n]++;
337+ wp->p_freq[n]++;
349338 }
350339 }
351340 }
@@ -368,9 +357,9 @@ huf_encode_start(struct lzh_ostream *wp, struct lha_method *m)
368357 }
369358 wp->buf[0] = 0;
370359 for (i = 0; i < NC; i++)
371- c_freq[i] = 0;
360+ wp->c_freq[i] = 0;
372361 for (i = 0; i < np; i++)
373- p_freq[i] = 0;
362+ wp->p_freq[i] = 0;
374363
375364 init_putbits(wp);
376365 }
@@ -392,7 +381,8 @@ huf_encode_end(struct lzh_ostream *wp)
392381 /***** decoding *****/
393382
394383 static void
395-read_pt_len(struct huf_t *huf, struct lzh_istream *rp, int nn, int nbit, int i_special)
384+read_pt_len(struct huf_t *huf, struct lzh_istream *rp, int nn, int nbit,
385+ int i_special, uchar *pt_len, ushort *pt_table)
396386 {
397387 int i, c, n;
398388 uint mask;
@@ -431,7 +421,7 @@ read_pt_len(struct huf_t *huf, struct lzh_istream *rp, int nn, int nbit, int i_s
431421 }
432422
433423 static void
434-read_c_len(struct huf_t *huf, struct lzh_istream *rp)
424+read_c_len(struct huf_t *huf, struct lzh_istream *rp, uchar *t_len, ushort *t_table)
435425 {
436426 int i, c, n;
437427 uint mask;
@@ -440,14 +430,14 @@ read_c_len(struct huf_t *huf, struct lzh_istream *rp)
440430 if (n == 0) {
441431 c = getbits(rp, CBIT);
442432 for (i = 0; i < NC; i++)
443- c_len[i] = 0;
433+ rp->c_len[i] = 0;
444434 for (i = 0; i < 4096; i++)
445- c_table[i] = c;
435+ rp->c_table[i] = c;
446436 }
447437 else {
448438 i = 0;
449439 while (i < n) {
450- c = pt_table[rp->bitbuf >> (BITBUFSIZ - 8)];
440+ c = t_table[rp->bitbuf >> (BITBUFSIZ - 8)];
451441 if (c >= NT) {
452442 mask = 1U << (BITBUFSIZ - 1 - 8);
453443 do {
@@ -458,7 +448,7 @@ read_c_len(struct huf_t *huf, struct lzh_istream *rp)
458448 mask >>= 1;
459449 } while (c >= NT);
460450 }
461- fillbuf(rp, pt_len[c]);
451+ fillbuf(rp, t_len[c]);
462452 if (c <= 2) {
463453 if (c == 0)
464454 c = 1;
@@ -467,14 +457,14 @@ read_c_len(struct huf_t *huf, struct lzh_istream *rp)
467457 else
468458 c = getbits(rp, CBIT) + 20;
469459 while (--c >= 0)
470- c_len[i++] = 0;
460+ rp->c_len[i++] = 0;
471461 }
472462 else
473- c_len[i++] = c - 2;
463+ rp->c_len[i++] = c - 2;
474464 }
475465 while (i < NC)
476- c_len[i++] = 0;
477- make_table(huf, NC, c_len, 12, c_table);
466+ rp->c_len[i++] = 0;
467+ make_table(huf, NC, rp->c_len, 12, rp->c_table);
478468 }
479469 }
480470
@@ -484,13 +474,16 @@ decode_c(struct huf_t *huf, struct lzh_istream *rp)
484474 uint j, mask;
485475
486476 if (rp->blocksize == 0) {
477+ uchar t_len[NT];
478+ unsigned short t_table[256];
479+
487480 rp->blocksize = getbits(rp, 16);
488- read_pt_len(huf, rp, NT, TBIT, 3);
489- read_c_len(huf, rp);
490- read_pt_len(huf, rp, np, pbit, -1);
481+ read_pt_len(huf, rp, NT, TBIT, 3, t_len, t_table);
482+ read_c_len(huf, rp, t_len, t_table);
483+ read_pt_len(huf, rp, np, pbit, -1, rp->p_len, rp->p_table);
491484 }
492485 rp->blocksize--;
493- j = c_table[rp->bitbuf >> (BITBUFSIZ - 12)];
486+ j = rp->c_table[rp->bitbuf >> (BITBUFSIZ - 12)];
494487 if (j >= NC) {
495488 mask = 1U << (BITBUFSIZ - 1 - 12);
496489 do {
@@ -501,7 +494,7 @@ decode_c(struct huf_t *huf, struct lzh_istream *rp)
501494 mask >>= 1;
502495 } while (j >= NC);
503496 }
504- fillbuf(rp, c_len[j]);
497+ fillbuf(rp, rp->c_len[j]);
505498 return j;
506499 }
507500
@@ -510,7 +503,7 @@ decode_p(struct huf_t *huf, struct lzh_istream *rp)
510503 {
511504 uint j, mask;
512505
513- j = pt_table[rp->bitbuf >> (BITBUFSIZ - 8)];
506+ j = rp->p_table[rp->bitbuf >> (BITBUFSIZ - 8)];
514507 if (j >= np) {
515508 mask = 1U << (BITBUFSIZ - 1 - 8);
516509 do {
@@ -521,7 +514,7 @@ decode_p(struct huf_t *huf, struct lzh_istream *rp)
521514 mask >>= 1;
522515 } while (j >= np);
523516 }
524- fillbuf(rp, pt_len[j]);
517+ fillbuf(rp, rp->p_len[j]);
525518 if (j != 0)
526519 j = (1U << (j - 1)) + getbits(rp, j - 1);
527520 return j;
Show on old repository browser