From 64c45b122bf8cbfa4555fd3f4ed5d3f94b8ebced Mon Sep 17 00:00:00 2001 From: Koji Arai Date: Sun, 24 Aug 2008 04:26:55 +0900 Subject: [PATCH] refined --- src/pm2.c | 33 ++++++++++++++++--------------- src/pm2tree.c | 62 ++++++++++++++++++++++++++++++++++------------------------- 2 files changed, 54 insertions(+), 41 deletions(-) diff --git a/src/pm2.c b/src/pm2.c index 67d3624..be74f5a 100644 --- a/src/pm2.c +++ b/src/pm2.c @@ -5,11 +5,11 @@ #include "pm2hist.h" #include "pm2tree.h" -static unsigned long nextcount; -static unsigned short lastupdate; +static off_t nextcount; +static unsigned long lastupdate; /* repeated from slide.c */ -static unsigned short dicsiz1; +static unsigned int dicsiz1; #define offset (0x100 - 2) void @@ -39,38 +39,41 @@ decode_c_pm2(void) hist_update(dtext[lastupdate]); lastupdate = (lastupdate + 1) & dicsiz1; } - while (decode_count >= nextcount) + + while (decode_count >= nextcount) { /* Actually it will never loop, because count doesn't grow that fast. However, this is the way LHA does it. Probably other encoding methods can have repeats larger than 256 bytes. Note: LHA puts this code in decode_p... - */ - { - if (nextcount == 0x0000) { + */ + + switch (nextcount) { + case 0x0000: maketree1(); maketree2(5); nextcount = 0x0400; - } - else if (nextcount == 0x0400) { + break; + case 0x0400: maketree2(6); nextcount = 0x0800; - } - else if (nextcount == 0x0800) { + break; + case 0x0800: maketree2(7); nextcount = 0x1000; - } - else if (nextcount == 0x1000) { + break; + case 0x1000: if (getbits(1) != 0) maketree1(); maketree2(8); nextcount = 0x2000; - } - else { /* 0x2000, 0x3000, 0x4000, ... */ + break; + default: /* 0x2000, 0x3000, 0x4000, ... */ if (getbits(1) != 0) { maketree1(); maketree2(8); } nextcount += 0x1000; + break; } } gettree1 = tree_get(&tree1); /* value preserved for decode_p */ diff --git a/src/pm2tree.c b/src/pm2tree.c index 4891c98..cae183a 100644 --- a/src/pm2tree.c +++ b/src/pm2tree.c @@ -21,6 +21,7 @@ void maketree1() { int i, nbits, x; + tree1bound = getbits(5); mindepth = getbits(3); if (mindepth == 0) { @@ -101,7 +102,7 @@ tree_rebuild(struct tree *t, unsigned char *parentarr, d; int i, curr, empty, n; - parentarr = (unsigned char *)malloc(bound * sizeof(unsigned char)); + parentarr = (unsigned char *)xmalloc(bound); t->root = 0; for (i = 0; i < bound; i++) { t->leftarr[i] = 0; @@ -118,39 +119,48 @@ tree_rebuild(struct tree *t, empty = mindepth; for (d = mindepth; TRUE; d++) { for (i = 0; i < bound; i++) { - if (table[i] == d) { - if (t->leftarr[curr] == 0) - t->leftarr[curr] = i | 128; - else { - t->rightarr[curr] = i | 128; - n = 0; - while (t->rightarr[curr] != 0) { - if (curr == 0) { /* root? -> done */ - free(parentarr); - return; - } - curr = parentarr[curr]; - n++; - } - t->rightarr[curr] = empty; - for (;;) { - parentarr[empty] = curr; - curr = empty; - empty++; - - n--; - if (n == 0) - break; - t->leftarr[curr] = empty; - } + if (table[i] != d) + continue; + + if (t->leftarr[curr] == 0) { + t->leftarr[curr] = i | 128; + continue; + } + + t->rightarr[curr] = i | 128; + n = 0; + while (t->rightarr[curr] != 0) { + if (curr == 0) { /* root? -> done */ + free(parentarr); + return; } + curr = parentarr[curr]; + n++; + } + + t->rightarr[curr] = empty; + for (;;) { + parentarr[empty] = curr; + curr = empty; + empty++; + + n--; + if (n == 0) + break; + t->leftarr[curr] = empty; } } + if (t->leftarr[curr] == 0) t->leftarr[curr] = empty; else t->rightarr[curr] = empty; + if (empty >= bound) { + error("bad archive"); + exit(1); + } + parentarr[empty] = curr; curr = empty; empty++; -- 2.11.0