Révision | 1fda245771a2952d1333311c1d1db84e2c434651 (tree) |
---|---|
l'heure | 2008-08-25 22:16:49 |
Auteur | Koji Arai <jca02266@gmai...> |
Commiter | Koji Arai |
struct huf_t was discarded.
use function instead of PUTBUF() macro.
@@ -37,8 +37,11 @@ struct lzh_istream { | ||
37 | 37 | |
38 | 38 | uint8_t c_len[NC]; |
39 | 39 | uint16_t c_table[4096]; |
40 | + uint16_t c_left[2 * NC - 1], c_right[2 * NC - 1]; | |
41 | + | |
40 | 42 | uint8_t p_len[NP]; |
41 | 43 | uint16_t p_table[256]; |
44 | + uint16_t p_left[2 * NP - 1], p_right[2 * NP - 1]; | |
42 | 45 | |
43 | 46 | int np; |
44 | 47 | int pbit; |
@@ -132,13 +135,6 @@ extern struct lha_opts opts; | ||
132 | 135 | |
133 | 136 | #define BITBUFSIZ (CHAR_BIT * sizeof(unsigned short)) /* size of bitbuf */ |
134 | 137 | |
135 | -/* huf.c */ | |
136 | - | |
137 | -/* decoding */ | |
138 | -struct huf_t { | |
139 | - ushort left[2 * NC - 1], right[2 * NC - 1]; | |
140 | -}; | |
141 | - | |
142 | 138 | /* utilities */ |
143 | 139 | #define MIN(a, b) (((a) > (b)) ? (b) : (a)) |
144 | 140 |
@@ -5,50 +5,61 @@ | ||
5 | 5 | |
6 | 6 | extern struct lha_opts opts; |
7 | 7 | |
8 | -#define PUTBUF(c) do { \ | |
9 | - buf[pos++] = (c); \ | |
10 | - if (pos == n) { \ | |
11 | - fwrite_crc(buf, n, outfile, crc); \ | |
12 | - if (outfile != stdout && opts.quiet < 1) { \ | |
13 | - putc('.', stdout); \ | |
14 | - } \ | |
15 | - \ | |
16 | - remainder -= n; \ | |
17 | - if (remainder == 0) return; \ | |
18 | - \ | |
19 | - pos = 0; \ | |
20 | - n = (size_t)MIN(remainder, MAXDICSIZ); \ | |
21 | - } \ | |
22 | -} while (0) | |
8 | +static off_t | |
9 | +put_decode_buf(char *buf, size_t size, | |
10 | + uint32_t *pos, uint32_t c, size_t *limit, | |
11 | + FILE *outfile, off_t remainder, uint16_t *crc) | |
12 | +{ | |
13 | + buf[(*pos)++] = c; | |
14 | + | |
15 | + if (*pos == *limit) { | |
16 | + fwrite_crc(buf, *limit, outfile, crc); | |
17 | + if (outfile != stdout && opts.quiet < 1) { | |
18 | + putc('.', stdout); | |
19 | + } | |
20 | + | |
21 | + remainder -= *limit; | |
22 | + if (remainder == 0) return 0; | |
23 | 23 | |
24 | + *pos = 0; | |
25 | + *limit = (uint)MIN(remainder, size); | |
26 | + return remainder; | |
27 | + } | |
28 | + | |
29 | + return remainder; | |
30 | +} | |
24 | 31 | |
25 | 32 | void |
26 | 33 | decode(struct lzh_istream *rp, FILE *outfile, off_t remainder, uint16_t *crc) |
27 | 34 | { |
28 | 35 | char buf[MAXDICSIZ]; |
29 | - struct huf_t huf; | |
30 | 36 | uint32_t pos; |
31 | - size_t n; | |
37 | + size_t limit; | |
32 | 38 | |
33 | 39 | huf_decode_start(rp, opts.method); |
34 | 40 | |
35 | 41 | pos = 0; |
36 | - n = (size_t)MIN(remainder, MAXDICSIZ); | |
42 | + limit = (size_t)MIN(remainder, MAXDICSIZ); | |
37 | 43 | |
38 | 44 | for (;;) { |
39 | - uint16_t c = decode_c(&huf, rp); | |
45 | + uint16_t c = decode_c(rp); | |
40 | 46 | if (c <= UCHAR_MAX) { |
41 | - PUTBUF(c); | |
47 | + remainder = put_decode_buf(buf, sizeof(buf), &pos, c, &limit, | |
48 | + outfile, remainder, crc); | |
49 | + if (remainder == 0) return; | |
42 | 50 | } |
43 | 51 | else { |
44 | 52 | uint32_t off; |
45 | 53 | uint16_t len, i; |
46 | 54 | |
47 | 55 | len = c - (UCHAR_MAX + 1 - THRESHOLD); |
48 | - off = (pos - decode_p(&huf, rp) - 1) & (MAXDICSIZ - 1); | |
56 | + off = (pos - decode_p(rp) - 1) & (MAXDICSIZ - 1); | |
49 | 57 | |
50 | 58 | for (i = 0; i < len; i++) { |
51 | - PUTBUF(buf[off]); | |
59 | + remainder = put_decode_buf(buf, sizeof(buf), &pos, buf[off], | |
60 | + &limit, | |
61 | + outfile, remainder, crc); | |
62 | + if (remainder == 0) return; | |
52 | 63 | off = (off+1) & (MAXDICSIZ - 1); |
53 | 64 | } |
54 | 65 | } |
@@ -384,8 +384,9 @@ huf_encode_end(struct lzh_ostream *wp) | ||
384 | 384 | /***** decoding *****/ |
385 | 385 | |
386 | 386 | static void |
387 | -read_pt_len(struct huf_t *huf, struct lzh_istream *rp, int nn, int nbit, | |
388 | - int i_special, uint8_t *pt_len, uint16_t *pt_table) | |
387 | +read_pt_len(struct lzh_istream *rp, int nn, int nbit, | |
388 | + int i_special, uint8_t *pt_len, uint16_t *pt_table, | |
389 | + uint16_t *left, uint16_t *right) | |
389 | 390 | { |
390 | 391 | int i, c, n; |
391 | 392 | unsigned int mask; |
@@ -419,12 +420,13 @@ read_pt_len(struct huf_t *huf, struct lzh_istream *rp, int nn, int nbit, | ||
419 | 420 | } |
420 | 421 | while (i < nn) |
421 | 422 | pt_len[i++] = 0; |
422 | - make_table(huf, nn, pt_len, 8, pt_table); | |
423 | + make_table(nn, pt_len, 8, pt_table, left, right); | |
423 | 424 | } |
424 | 425 | } |
425 | 426 | |
426 | 427 | static void |
427 | -read_c_len(struct huf_t *huf, struct lzh_istream *rp, uint8_t *t_len, uint16_t *t_table) | |
428 | +read_c_len(struct lzh_istream *rp, uint8_t *t_len, uint16_t *t_table, | |
429 | + uint16_t *t_left, uint16_t *t_right) | |
428 | 430 | { |
429 | 431 | int i, c, n; |
430 | 432 | unsigned int mask; |
@@ -445,9 +447,9 @@ read_c_len(struct huf_t *huf, struct lzh_istream *rp, uint8_t *t_len, uint16_t * | ||
445 | 447 | mask = 1U << (BITBUFSIZ - 1 - 8); |
446 | 448 | do { |
447 | 449 | if (rp->bitbuf & mask) |
448 | - c = huf->right[c]; | |
450 | + c = t_right[c]; | |
449 | 451 | else |
450 | - c = huf->left[c]; | |
452 | + c = t_left[c]; | |
451 | 453 | mask >>= 1; |
452 | 454 | } while (c >= NT); |
453 | 455 | } |
@@ -467,23 +469,25 @@ read_c_len(struct huf_t *huf, struct lzh_istream *rp, uint8_t *t_len, uint16_t * | ||
467 | 469 | } |
468 | 470 | while (i < NC) |
469 | 471 | rp->c_len[i++] = 0; |
470 | - make_table(huf, NC, rp->c_len, 12, rp->c_table); | |
472 | + make_table(NC, rp->c_len, 12, rp->c_table, rp->c_left, rp->c_right); | |
471 | 473 | } |
472 | 474 | } |
473 | 475 | |
474 | 476 | uint16_t |
475 | -decode_c(struct huf_t *huf, struct lzh_istream *rp) | |
477 | +decode_c(struct lzh_istream *rp) | |
476 | 478 | { |
477 | 479 | uint16_t j, mask; |
478 | 480 | |
479 | 481 | if (rp->blocksize == 0) { |
480 | 482 | uint8_t t_len[NT]; |
481 | 483 | uint16_t t_table[256]; |
484 | + uint16_t t_left[2*NT-1], t_right[2*NT-1]; | |
482 | 485 | |
483 | 486 | rp->blocksize = getbits(rp, 16); |
484 | - read_pt_len(huf, rp, NT, TBIT, 3, t_len, t_table); | |
485 | - read_c_len(huf, rp, t_len, t_table); | |
486 | - read_pt_len(huf, rp, rp->np, rp->pbit, -1, rp->p_len, rp->p_table); | |
487 | + read_pt_len(rp, NT, TBIT, 3, t_len, t_table, t_left, t_right); | |
488 | + read_c_len(rp, t_len, t_table, t_left, t_right); | |
489 | + read_pt_len(rp, rp->np, rp->pbit, -1, rp->p_len, rp->p_table, | |
490 | + rp->p_left, rp->p_right); | |
487 | 491 | } |
488 | 492 | rp->blocksize--; |
489 | 493 | j = rp->c_table[rp->bitbuf >> (BITBUFSIZ - 12)]; |
@@ -491,9 +495,9 @@ decode_c(struct huf_t *huf, struct lzh_istream *rp) | ||
491 | 495 | mask = 1U << (BITBUFSIZ - 1 - 12); |
492 | 496 | do { |
493 | 497 | if (rp->bitbuf & mask) |
494 | - j = huf->right[j]; | |
498 | + j = rp->c_right[j]; | |
495 | 499 | else |
496 | - j = huf->left[j]; | |
500 | + j = rp->c_left[j]; | |
497 | 501 | mask >>= 1; |
498 | 502 | } while (j >= NC); |
499 | 503 | } |
@@ -502,7 +506,7 @@ decode_c(struct huf_t *huf, struct lzh_istream *rp) | ||
502 | 506 | } |
503 | 507 | |
504 | 508 | uint16_t |
505 | -decode_p(struct huf_t *huf, struct lzh_istream *rp) | |
509 | +decode_p(struct lzh_istream *rp) | |
506 | 510 | { |
507 | 511 | uint16_t j, mask; |
508 | 512 |
@@ -511,9 +515,9 @@ decode_p(struct huf_t *huf, struct lzh_istream *rp) | ||
511 | 515 | mask = 1U << (BITBUFSIZ - 1 - 8); |
512 | 516 | do { |
513 | 517 | if (rp->bitbuf & mask) |
514 | - j = huf->right[j]; | |
518 | + j = rp->p_right[j]; | |
515 | 519 | else |
516 | - j = huf->left[j]; | |
520 | + j = rp->p_left[j]; | |
517 | 521 | mask >>= 1; |
518 | 522 | } while (j >= rp->np); |
519 | 523 | } |
@@ -54,12 +54,18 @@ void | ||
54 | 54 | list(struct lzh_header *h) |
55 | 55 | { |
56 | 56 | unsigned int r; |
57 | + char *fmt; | |
57 | 58 | |
58 | 59 | printf("%-14.*s", h->namelen, h->filename); |
59 | 60 | if (h->namelen > 14) |
60 | 61 | printf("\n "); |
61 | 62 | r = ratio(h->compsize, h->origsize); |
62 | - printf(" %-7s %10lu %10lu %u.%03u %04x %-6.5s [%d]\n", | |
63 | + if (sizeof(off_t) == 8) | |
64 | + fmt = " %-7s %10llu %10llu %u.%03u %04x %-6.5s [%d]\n"; | |
65 | + else | |
66 | + fmt = " %-7s %10lu %10lu %u.%03u %04x %-6.5s [%d]\n"; | |
67 | + | |
68 | + printf(fmt, | |
63 | 69 | os_string(h->os_id), |
64 | 70 | h->origsize, |
65 | 71 | h->compsize, |
@@ -4,7 +4,8 @@ | ||
4 | 4 | #include "ar.h" |
5 | 5 | |
6 | 6 | void |
7 | -make_table(struct huf_t *huf, int nchar, uint8_t *bitlen, int tablebits, uint16_t *table) | |
7 | +make_table(int nchar, uint8_t *bitlen, int tablebits, uint16_t *table, | |
8 | + uint16_t *left, uint16_t *right) | |
8 | 9 | { |
9 | 10 | uint16_t count[17], weight[17], start[18], *p; |
10 | 11 | unsigned int i, k, len, ch, jutbits, avail, nextcode, mask; /* XXX variable type is vague */ |
@@ -53,13 +54,13 @@ make_table(struct huf_t *huf, int nchar, uint8_t *bitlen, int tablebits, uint16_ | ||
53 | 54 | i = len - tablebits; |
54 | 55 | while (i != 0) { |
55 | 56 | if (*p == 0) { |
56 | - huf->right[avail] = huf->left[avail] = 0; | |
57 | + right[avail] = left[avail] = 0; | |
57 | 58 | *p = avail++; |
58 | 59 | } |
59 | 60 | if (k & mask) |
60 | - p = &huf->right[*p]; | |
61 | + p = &right[*p]; | |
61 | 62 | else |
62 | - p = &huf->left[*p]; | |
63 | + p = &left[*p]; | |
63 | 64 | k <<= 1; |
64 | 65 | i--; |
65 | 66 | } |
@@ -28,13 +28,13 @@ void decode P_((struct lzh_istream *rp, FILE *outfile, off_t remainder, uint16_t | ||
28 | 28 | /* maketree.c */ |
29 | 29 | int make_tree P_((uint16_t nparm, uint16_t *freqparm, uint8_t *lenparm, uint16_t *codeparm)); |
30 | 30 | /* maketbl.c */ |
31 | -void make_table P_((struct huf_t *huf, int nchar, uint8_t *bitlen, int tablebits, uint16_t *table)); | |
31 | +void make_table P_((int nchar, uint8_t *bitlen, int tablebits, uint16_t *table, uint16_t *left, uint16_t *right)); | |
32 | 32 | /* huf.c */ |
33 | 33 | void output P_((struct lzh_ostream *wp, uint16_t c, uint32_t p)); |
34 | 34 | void huf_encode_start P_((struct lzh_ostream *wp, struct lha_method *m)); |
35 | 35 | void huf_encode_end P_((struct lzh_ostream *wp)); |
36 | -uint16_t decode_c P_((struct huf_t *huf, struct lzh_istream *rp)); | |
37 | -uint16_t decode_p P_((struct huf_t *huf, struct lzh_istream *rp)); | |
36 | +uint16_t decode_c P_((struct lzh_istream *rp)); | |
37 | +uint16_t decode_p P_((struct lzh_istream *rp)); | |
38 | 38 | void huf_decode_start P_((struct lzh_istream *rp, struct lha_method *m)); |
39 | 39 | /* strlib.c */ |
40 | 40 | int string_equal P_((char *str1, char *str2)); |