once more into the breach...
[debian/gzip] / unlzw.c
diff --git a/unlzw.c b/unlzw.c
index 58aaaf205be839ab6b9112b96160c1ee61cf9371..63f941c6213533bf3140ce642bc16d5bfe3e02a9 100644 (file)
--- a/unlzw.c
+++ b/unlzw.c
@@ -7,19 +7,11 @@
  * to accommodate in-memory decompression.
  */
 
-#ifdef RCSID
-static char rcsid[] = "$Id: unlzw.c,v 0.15 1993/06/10 13:28:35 jloup Exp $";
-#endif
-
 #include <config.h>
 #include "tailor.h"
 
-#ifdef HAVE_UNISTD_H
-#  include <unistd.h>
-#endif
-#ifdef HAVE_FCNTL_H
-#  include <fcntl.h>
-#endif
+#include <unistd.h>
+#include <fcntl.h>
 
 #include "gzip.h"
 #include "lzw.h"
@@ -31,22 +23,22 @@ typedef unsigned short      count_short;
 typedef unsigned long  cmp_code_int;
 
 #define MAXCODE(n)     (1L << (n))
-    
+
 #ifndef        REGISTERS
 #      define  REGISTERS       2
 #endif
-#define        REG1    
-#define        REG2    
-#define        REG3    
-#define        REG4    
-#define        REG5    
-#define        REG6    
-#define        REG7    
-#define        REG8    
-#define        REG9    
+#define        REG1
+#define        REG2
+#define        REG3
+#define        REG4
+#define        REG5
+#define        REG6
+#define        REG7
+#define        REG8
+#define        REG9
 #define        REG10
-#define        REG11   
-#define        REG12   
+#define        REG11
+#define        REG12
 #define        REG13
 #define        REG14
 #define        REG15
@@ -115,11 +107,11 @@ typedef unsigned long     cmp_code_int;
 #      undef   REG16
 #      define  REG16   register
 #endif
-    
+
 #ifndef        BYTEORDER
 #      define  BYTEORDER       0000
 #endif
-       
+
 #ifndef        NOALLIGN
 #      define  NOALLIGN        0
 #endif
@@ -129,19 +121,19 @@ union     bytes {
     long  word;
     struct {
 #if BYTEORDER == 4321
-       char_type       b1;
-       char_type       b2;
-       char_type       b3;
-       char_type       b4;
+        char_type      b1;
+        char_type      b2;
+        char_type      b3;
+        char_type      b4;
 #else
 #if BYTEORDER == 1234
-       char_type       b4;
-       char_type       b3;
-       char_type       b2;
-       char_type       b1;
+        char_type      b4;
+        char_type      b3;
+        char_type      b2;
+        char_type      b1;
 #else
 #      undef   BYTEORDER
-       int  dummy;
+        int  dummy;
 #endif
 #endif
     } bytes;
@@ -188,7 +180,7 @@ int block_mode = BLOCK_MODE; /* block compress mode -C compatible with 2.0 */
  *   The magic header has already been checked and skipped.
  *   bytes_in and bytes_out have been initialized.
  */
-int unlzw(in, out) 
+int unlzw(in, out)
     int in, out;    /* input and output file descriptors */
 {
     REG2   char_type  *stackp;
@@ -206,7 +198,7 @@ int unlzw(in, out)
     REG14  code_int   maxmaxcode;
     REG15  int        n_bits;
     REG16  int        rsize;
-    
+
 #ifdef MAXSEG_64K
     tab_prefix[0] = tab_prefix0;
     tab_prefix[1] = tab_prefix1;
@@ -214,18 +206,18 @@ int unlzw(in, out)
     maxbits = get_byte();
     block_mode = maxbits & BLOCK_MODE;
     if ((maxbits & LZW_RESERVED) != 0) {
-       WARN((stderr, "\n%s: %s: warning, unknown flags 0x%x\n",
-             progname, ifname, maxbits & LZW_RESERVED));
+        WARN((stderr, "\n%s: %s: warning, unknown flags 0x%x\n",
+              program_name, ifname, maxbits & LZW_RESERVED));
     }
     maxbits &= BIT_MASK;
     maxmaxcode = MAXCODE(maxbits);
-    
+
     if (maxbits > BITS) {
-       fprintf(stderr,
-               "\n%s: %s: compressed with %d bits, can only handle %d bits\n",
-               progname, ifname, maxbits, BITS);
-       exit_code = ERROR;
-       return ERROR;
+        fprintf(stderr,
+                "\n%s: %s: compressed with %d bits, can only handle %d bits\n",
+                program_name, ifname, maxbits, BITS);
+        exit_code = ERROR;
+        return ERROR;
     }
     rsize = insize;
     maxcode = MAXCODE(n_bits = INIT_BITS)-1;
@@ -236,141 +228,145 @@ int unlzw(in, out)
     posbits = inptr<<3;
 
     free_ent = ((block_mode) ? FIRST : 256);
-    
+
     clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
-    
+
     for (code = 255 ; code >= 0 ; --code) {
-       tab_suffixof(code) = (char_type)code;
+        tab_suffixof(code) = (char_type)code;
     }
     do {
-       REG1 int i;
-       int  e;
-       int  o;
-       
+        REG1 int i;
+        int  e;
+        int  o;
+
     resetbuf:
-       e = insize-(o = (posbits>>3));
-       
-       for (i = 0 ; i < e ; ++i) {
-           inbuf[i] = inbuf[i+o];
-       }
-       insize = e;
-       posbits = 0;
-       
-       if (insize < INBUF_EXTRA) {
-           if ((rsize = read(in, (char*)inbuf+insize, INBUFSIZ)) == -1) {
-               read_error();
-           }
-           insize += rsize;
-           bytes_in += (off_t)rsize;
-       }
-       inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 : 
-                 ((long)insize<<3)-(n_bits-1));
-       
-       while (inbits > posbits) {
-           if (free_ent > maxcode) {
-               posbits = ((posbits-1) +
-                          ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
-               ++n_bits;
-               if (n_bits == maxbits) {
-                   maxcode = maxmaxcode;
-               } else {
-                   maxcode = MAXCODE(n_bits)-1;
-               }
-               bitmask = (1<<n_bits)-1;
-               goto resetbuf;
-           }
-           input(inbuf,posbits,code,n_bits,bitmask);
-           Tracev((stderr, "%d ", code));
-
-           if (oldcode == -1) {
-               if (code >= 256) error("corrupt input.");
-               outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code));
-               continue;
-           }
-           if (code == CLEAR && block_mode) {
-               clear_tab_prefixof();
-               free_ent = FIRST - 1;
-               posbits = ((posbits-1) +
-                          ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
-               maxcode = MAXCODE(n_bits = INIT_BITS)-1;
-               bitmask = (1<<n_bits)-1;
-               goto resetbuf;
-           }
-           incode = code;
-           stackp = de_stack;
-           
-           if (code >= free_ent) { /* Special case for KwKwK string. */
-               if (code > free_ent) {
-#ifdef DEBUG               
-                   char_type *p;
-
-                   posbits -= n_bits;
-                   p = &inbuf[posbits>>3];
-                   fprintf(stderr,
-                           "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
-                           code, free_ent, n_bits, insize);
-                   fprintf(stderr,
-                           "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
-                           posbits, p[-1],p[0],p[1],p[2],p[3]);
+        o = posbits >> 3;
+        e = o <= insize ? insize - o : 0;
+
+        for (i = 0 ; i < e ; ++i) {
+            inbuf[i] = inbuf[i+o];
+        }
+        insize = e;
+        posbits = 0;
+
+        if (insize < INBUF_EXTRA) {
+            rsize = read_buffer (in, (char *) inbuf + insize, INBUFSIZ);
+            if (rsize == -1) {
+                read_error();
+            }
+            insize += rsize;
+            bytes_in += (off_t)rsize;
+        }
+        inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 :
+                  ((long)insize<<3)-(n_bits-1));
+
+        while (inbits > posbits) {
+            if (free_ent > maxcode) {
+                posbits = ((posbits-1) +
+                           ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
+                ++n_bits;
+                if (n_bits == maxbits) {
+                    maxcode = maxmaxcode;
+                } else {
+                    maxcode = MAXCODE(n_bits)-1;
+                }
+                bitmask = (1<<n_bits)-1;
+                goto resetbuf;
+            }
+            input(inbuf,posbits,code,n_bits,bitmask);
+            Tracev((stderr, "%d ", code));
+
+            if (oldcode == -1) {
+                if (256 <= code)
+                  gzip_error ("corrupt input.");
+                outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code));
+                continue;
+            }
+            if (code == CLEAR && block_mode) {
+                clear_tab_prefixof();
+                free_ent = FIRST - 1;
+                posbits = ((posbits-1) +
+                           ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
+                maxcode = MAXCODE(n_bits = INIT_BITS)-1;
+                bitmask = (1<<n_bits)-1;
+                goto resetbuf;
+            }
+            incode = code;
+            stackp = de_stack;
+
+            if (code >= free_ent) { /* Special case for KwKwK string. */
+                if (code > free_ent) {
+#ifdef DEBUG
+                    char_type *p;
+
+                    posbits -= n_bits;
+                    p = &inbuf[posbits>>3];
+                    fprintf(stderr,
+                            "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
+                            code, free_ent, n_bits, insize);
+                    fprintf(stderr,
+                            "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
+                            posbits, p[-1],p[0],p[1],p[2],p[3]);
 #endif
-                   if (!test && outpos > 0) {
-                       write_buf(out, (char*)outbuf, outpos);
-                       bytes_out += (off_t)outpos;
-                   }
-                   error(to_stdout ? "corrupt input." :
-                         "corrupt input. Use zcat to recover some data.");
-               }
-               *--stackp = (char_type)finchar;
-               code = oldcode;
-           }
-
-           while ((cmp_code_int)code >= (cmp_code_int)256) {
-               /* Generate output characters in reverse order */
-               *--stackp = tab_suffixof(code);
-               code = tab_prefixof(code);
-           }
-           *--stackp = (char_type)(finchar = tab_suffixof(code));
-           
-           /* And put them out in forward order */
-           {
-               REG1 int        i;
-           
-               if (outpos+(i = (de_stack-stackp)) >= OUTBUFSIZ) {
-                   do {
-                       if (i > OUTBUFSIZ-outpos) i = OUTBUFSIZ-outpos;
-
-                       if (i > 0) {
-                           memcpy(outbuf+outpos, stackp, i);
-                           outpos += i;
-                       }
-                       if (outpos >= OUTBUFSIZ) {
-                           if (!test) {
-                               write_buf(out, (char*)outbuf, outpos);
-                               bytes_out += (off_t)outpos;
-                           }
-                           outpos = 0;
-                       }
-                       stackp+= i;
-                   } while ((i = (de_stack-stackp)) > 0);
-               } else {
-                   memcpy(outbuf+outpos, stackp, i);
-                   outpos += i;
-               }
-           }
-
-           if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
-
-               tab_prefixof(code) = (unsigned short)oldcode;
-               tab_suffixof(code) = (char_type)finchar;
-               free_ent = code+1;
-           } 
-           oldcode = incode;   /* Remember previous code.      */
-       }
+                    if (!test && outpos > 0) {
+                        write_buf(out, (char*)outbuf, outpos);
+                        bytes_out += (off_t)outpos;
+                    }
+                    gzip_error (to_stdout
+                                ? "corrupt input."
+                                : "corrupt input. Use zcat to recover some data.");
+                }
+                *--stackp = (char_type)finchar;
+                code = oldcode;
+            }
+
+            while ((cmp_code_int)code >= (cmp_code_int)256) {
+                /* Generate output characters in reverse order */
+                *--stackp = tab_suffixof(code);
+                code = tab_prefixof(code);
+            }
+            *--stackp =        (char_type)(finchar = tab_suffixof(code));
+
+            /* And put them out in forward order */
+            {
+                REG1 int       i;
+
+                if (outpos+(i = (de_stack-stackp)) >= OUTBUFSIZ) {
+                    do {
+                        if (i > OUTBUFSIZ-outpos) i = OUTBUFSIZ-outpos;
+
+                        if (i > 0) {
+                            memcpy(outbuf+outpos, stackp, i);
+                            outpos += i;
+                        }
+                        if (outpos >= OUTBUFSIZ) {
+                            if (!test) {
+                                write_buf(out, (char*)outbuf, outpos);
+                                bytes_out += (off_t)outpos;
+                            }
+                            outpos = 0;
+                        }
+                        stackp+= i;
+                    } while ((i = (de_stack-stackp)) > 0);
+                } else {
+                    memcpy(outbuf+outpos, stackp, i);
+                    outpos += i;
+                }
+            }
+
+            if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
+
+                tab_prefixof(code) = (unsigned short)oldcode;
+                tab_suffixof(code) = (char_type)finchar;
+                free_ent = code+1;
+            }
+            oldcode = incode;  /* Remember previous code.      */
+        }
     } while (rsize != 0);
-    
+
     if (!test && outpos > 0) {
-       write_buf(out, (char*)outbuf, outpos);
-       bytes_out += (off_t)outpos;
+        write_buf(out, (char*)outbuf, outpos);
+        bytes_out += (off_t)outpos;
     }
     return OK;
 }