OSDN Git Service

refined
authorKoji Arai <jca02266@gmail.com>
Sat, 23 Aug 2008 19:26:55 +0000 (04:26 +0900)
committerKoji Arai <jca02266@gmail.com>
Sat, 23 Aug 2008 19:26:55 +0000 (04:26 +0900)
src/pm2.c
src/pm2tree.c

index 67d3624..be74f5a 100644 (file)
--- 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 */
index 4891c98..cae183a 100644 (file)
@@ -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++;