OSDN Git Service

Merge branch 'master' of github.com:jca02266/lha
[lha/lha.git] / src / pm2.c
index 67d3624..c9e2e3a 100644 (file)
--- a/src/pm2.c
+++ b/src/pm2.c
@@ -1,15 +1,36 @@
 /***********************************************************
        pm2.c -- extract pmext2 coding
 ***********************************************************/
+/*
+  Copyright (c) 1999 Maarten ter Huurne
+
+  Permission is hereby granted, free of charge, to any person
+  obtaining a copy of this software and associated documentation files
+  (the "Software"), to deal in the Software without restriction,
+  including without limitation the rights to use, copy, modify, merge,
+  publish, distribute, sublicense, and/or sell copies of the Software,
+  and to permit persons to whom the Software is furnished to do so,
+  subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be
+  included in all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+  SOFTWARE.
+*/
 #include "lha.h"
-#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,41 +60,48 @@ decode_c_pm2(void)
         hist_update(dtext[lastupdate]);
         lastupdate = (lastupdate + 1) & dicsiz1;
     }
-    while (decode_count >= nextcount)
-        /* Actually it will never loop, because count doesn't grow that fast.
+
+    while (decode_count >= nextcount) {
+        /* Actually it will never loop, because decode_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 */
+    gettree1 = tree1_get();        /* value preserved for decode_p */
+    if (gettree1 >= 29) {
+        error("Bad table");
+        exit(1);
+    }
 
     /* direct value (ret <= UCHAR_MAX) */
     if (gettree1 < 8)
@@ -97,7 +125,7 @@ decode_p_pm2(void)
         delta = 0;
     }
     else if (gettree1 < 28) {   /* n-byte repeat with offset 0..8191 */
-        gettree2 = tree_get(&tree2);
+        gettree2 = tree2_get();
         if (gettree2 == 0) {
             nbits = 6;
             delta = 0;