OSDN Git Service

extract.c and decode.c was refined.
authorKoji Arai <jca02266@gmail.com>
Mon, 23 Jun 2008 10:24:49 +0000 (19:24 +0900)
committerKoji Arai <jca02266@gmail.com>
Mon, 23 Jun 2008 10:25:21 +0000 (19:25 +0900)
decode.c
extract.c
prototypes.h

index d9c77d7..df78170 100644 (file)
--- a/decode.c
+++ b/decode.c
@@ -6,46 +6,55 @@
 extern struct lha_opts opts;
 
 void
-decode_start(struct lzh_istream *rp)
+decode(struct lzh_istream *rp, FILE *outfile, unsigned long remainder, unsigned int *crc)
 {
-    huf_decode_start(rp, opts.method);
-}
+    char buf[MAXDICSIZ];
+    struct huf_t huf;
+    unsigned int r, n;
 
-void
-decode(struct huf_t *huf, struct lzh_istream *rp, uint count,
-       char *buf, unsigned int *slide_off, int *slide_len)
-        /* The calling function must keep the number of
-           bytes to be processed.  This function decodes
-           either 'count' bytes or 'DICSIZ' bytes, whichever
-           is smaller, into the array 'buffer[]' of size
-           'DICSIZ' or more.
-           Call decode_start() once for each new file
-           before calling this function. */
-{
-    uint r, c;
+    huf_decode_start(rp, opts.method);
 
     r = 0;
-    while (--*slide_len >= 0) {
-        buf[r] = buf[*slide_off];
-        *slide_off = (*slide_off+1) & (MAXDICSIZ - 1);
-        if (++r == count)
-            return;
-    }
+    n = (uint)MIN(remainder, MAXDICSIZ);
+
     for (;;) {
-        c = decode_c(huf, rp);
+        uint c = decode_c(&huf, rp);
         if (c <= UCHAR_MAX) {
             buf[r] = c;
-            if (++r == count)
-                return;
+            if (++r == n) {
+                fwrite_crc(buf, n, outfile, crc);
+                if (outfile != stdout && opts.quiet < 1) {
+                    putc('.', stdout);
+                }
+
+                remainder -= n;
+                if (remainder <= 0) return;
+
+                r = 0;
+                n = (uint)MIN(remainder, MAXDICSIZ);
+            }
         }
         else {
-            *slide_len = c - (UCHAR_MAX + 1 - THRESHOLD);
-            *slide_off = (r - decode_p(huf, rp) - 1) & (MAXDICSIZ - 1);
-            while (--*slide_len >= 0) {
-                buf[r] = buf[*slide_off];
-                *slide_off = (*slide_off+1) & (MAXDICSIZ - 1);
-                if (++r == count)
-                    return;
+            unsigned int slide_off = 0;
+            int slide_len = 0;
+
+            slide_len = c - (UCHAR_MAX + 1 - THRESHOLD);
+            slide_off = (r - decode_p(&huf, rp) - 1) & (MAXDICSIZ - 1);
+            while (--slide_len >= 0) {
+                buf[r] = buf[slide_off];
+                slide_off = (slide_off+1) & (MAXDICSIZ - 1);
+                if (++r == n) {
+                    fwrite_crc(buf, n, outfile, crc);
+                    if (outfile != stdout && opts.quiet < 1) {
+                        putc('.', stdout);
+                    }
+
+                    remainder -= n;
+                    if (remainder <= 0) return;
+
+                    r = 0;
+                    n = (uint)MIN(remainder, MAXDICSIZ);
+                }
             }
         }
     }
index cc70fb1..751babe 100644 (file)
--- a/extract.c
+++ b/extract.c
@@ -53,29 +53,28 @@ extract(struct lzh_istream *rp, int to_file, struct lzh_header *h)
         skip(rp->fp, h);
     }
     else {
-        char buf[MAXDICSIZ];
-        unsigned int slide_off = 0;
-        int slide_len = 0;
-        unsigned long remainder = h->origsize;
-        struct huf_t huf;
-
         crc = INIT_CRC;
-        if (opts.method->dicbit != 0)
-            decode_start(rp);
-        while (remainder != 0) {
-            uint n = (uint)MIN(remainder, MAXDICSIZ);
-            if (opts.method->dicbit != 0)
-                decode(&huf, rp, n, buf, &slide_off, &slide_len);
-            else {
+
+        if (opts.method->dicbit != 0) {
+            decode(rp, outfile, h->origsize, &crc);
+        }
+        else {
+            unsigned long remainder = h->origsize;
+
+            char buf[BUFSIZ];
+
+            while (remainder != 0) {
+                uint n = (uint)MIN(remainder, BUFSIZ);
+
                 /* no compress */
                 if (fread(buf, 1, n, rp->fp) != n)
                     error("Can't read");
+                fwrite_crc(buf, n, outfile, &crc);
+                if (outfile != stdout && opts.quiet < 1) {
+                    putc('.', stdout);
+                }
+                remainder -= n;
             }
-            fwrite_crc(buf, n, outfile, &crc);
-            if (outfile != stdout && opts.quiet < 1) {
-                putc('.', stdout);
-            }
-            remainder -= n;
         }
     }
 
index 771d775..b2caeb7 100644 (file)
@@ -24,10 +24,9 @@ void init_putbits P_((struct lzh_ostream *wp));
 /* encode.c */
 void encode P_((struct lzh_ostream *wp, FILE *rfp));
 /* decode.c */
-void decode_start P_((struct lzh_istream *rp));
-void decode P_((struct huf_t *huf, struct lzh_istream *rp, uint count, char *buf, unsigned int *slide_off, int *slide_len));
+void decode P_((struct lzh_istream *rp, FILE *outfile, unsigned long remainder, unsigned int *crc));
 /* maketree.c */
-int make_tree P_((uint16_t nparm, uint16_t *freqparm, uchar *lenparm, uint16_t *codeparm));
+int make_tree P_((uint16_t nparm, uint16_t *freqparm, uint8_t *lenparm, uint16_t *codeparm));
 /* maketbl.c */
 void make_table P_((struct huf_t *huf, int nchar, uchar bitlen[], int tablebits, ushort table[]));
 /* huf.c */