Add debugging code for input buffer underrun
authorhpa <hpa>
Tue, 18 Nov 2003 19:20:20 +0000 (19:20 +0000)
committerhpa <hpa>
Tue, 18 Nov 2003 19:20:20 +0000 (19:20 +0000)
memdisk/e820test.c
memdisk/inflate.c
memdisk/unzip.c

index eefdf51..cadd2a7 100644 (file)
@@ -17,6 +17,7 @@
  * Test of INT 15:E820 canonicalization/manipulation routine
  */
 
+#include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <inttypes.h>
@@ -47,17 +48,22 @@ int main(int argc, char *argv[])
 {
   uint64_t start, len;
   uint32_t type;
+  char line[BUFSIZ], *p;
 
   e820map_init();
   printranges();
 
-  while ( scanf("%llx %llx %d", &start, &len, &type) == 3 ) {
-    putchar('\n'); 
-    printf("%016llx %016llx %d <-\n", start, len, type);
-    putchar('\n'); 
-    insertrange(start, len, type);
-    printranges(); 
- }
+  while ( fgets(line, BUFSIZ, stdin) ) {
+    p = strchr(line, ':');
+    p = p ? p+1 : line;
+    if ( sscanf(p, " %llx %llx %d", &start, &len, &type) == 3 ) {
+      putchar('\n'); 
+      printf("%016llx %016llx %d <-\n", start, len, type);
+      putchar('\n'); 
+      insertrange(start, len, type);
+      printranges(); 
+    }
+  }
 
   parse_mem();
 
index a07d6f5..6c25ee6 100644 (file)
@@ -978,7 +978,7 @@ STATIC int inflate()
    */
   while (bk >= 8) {
     bk -= 8;
-    inptr--;
+    inbuf--;
   }
 
   /* flush out slide */
@@ -1093,10 +1093,12 @@ int gunzip(void)
            error("Input has invalid flags");
            return -1;
     }
-    (ulg)get_byte();   /* Get timestamp */
-    ((ulg)get_byte()) << 8;
-    ((ulg)get_byte()) << 16;
-    ((ulg)get_byte()) << 24;
+
+    /* Ignore timestamp */
+    (void)get_byte();
+    (void)get_byte();
+    (void)get_byte();
+    (void)get_byte();
 
     (void)get_byte();  /* Ignore extra flags for the moment */
     (void)get_byte();  /* Ignore OS type for the moment */
index 94f56e4..a3a7f12 100644 (file)
@@ -28,15 +28,18 @@ typedef uint8_t  uch;
 typedef uint16_t ush;
 typedef uint32_t ulg;
 
-#define WSIZE 0x8000           /* Window size must be at least 32k, */
+#define WSIZE 0x8000           /* Window size must be at least 32k, */
                                /* and a power of two */
 
-static uch *inbuf;          /* input buffer */
-static uch window[WSIZE];    /* Sliding window buffer */
+static uch *inbuf;             /* input pointer */
+static uch window[WSIZE];      /* sliding output window buffer */
 
-static unsigned insize;      /* valid bytes in inbuf */
-static unsigned inptr;       /* index of next byte to be processed in inbuf */
-static unsigned outcnt;             /* bytes in output buffer */
+static unsigned insize;                /* total input bytes read */
+static unsigned inbytes;       /* valid bytes in inbuf */
+static unsigned outcnt;                /* bytes in output buffer */
+
+/* This is here to try to debug strange errors seen on some machines. */
+static ulg in_crc;             /* crc of input bytes as read */
 
 /* gzip flag byte */
 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ASCII text */
@@ -47,8 +50,6 @@ static unsigned outcnt;            /* bytes in output buffer */
 #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
 #define RESERVED     0xC0 /* bit 6,7:   reserved */
 
-#define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
-               
 /* Diagnostic functions */
 #ifdef DEBUG
 #  define Assert(cond,msg) {if(!(cond)) error(msg);}
@@ -71,7 +72,23 @@ static void flush_window(void);
 static void error(char *m);
 static void gzip_mark(void **);
 static void gzip_release(void **);
-  
+
+extern ulg crc_32_tab[256];
+
+/* Get byte from input buffer */
+static inline uch get_byte(void)
+{
+  if ( inbytes ) {
+    uch b = *inbuf++;
+    inbytes--;
+    in_crc = crc_32_tab[(in_crc ^ b) & 0xff] ^ (in_crc >> 8);
+    
+    return b;
+  } else {
+    return fill_inbuf();       /* Input buffer underrun */
+  }
+}
+
 static ulg bytes_out = 0;      /* Number of bytes output */
 static uch *output_data;       /* Output data pointer */
 static ulg output_size;                /* Number of output bytes expected */
@@ -123,8 +140,9 @@ static int fill_inbuf(void)
 {
   /* This should never happen.  We have already pointed the algorithm
      to all the data we have. */
-  error("ran out of input data");
-  return 0;
+  printf("failed\nDecompression error: ran out of input data, cksum = %lu %u\n",
+        in_crc^0xffffffffUL, insize);
+  die();
 }
 
 /* ===========================================================================
@@ -144,7 +162,7 @@ static void flush_window(void)
     out = output_data;
     for (n = 0; n < outcnt; n++) {
            ch = *out++ = *in++;
-           c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
+           c = crc_32_tab[(c ^ ch) & 0xff] ^ (c >> 8);
     }
     crc = c;
     output_data = out;
@@ -155,7 +173,6 @@ static void flush_window(void)
 static void error(char *x)
 {
   printf("failed\nDecompression error: %s\n", x);
-  puts(x);
   die();
 }
 
@@ -176,8 +193,9 @@ void *unzip(void *indata, unsigned long zbytes, void *target)
   free_mem_end_ptr = free_mem_ptr + 0x10000;
 
   /* Set up input buffer */
+  in_crc = 0xffffffffUL;
   inbuf  = indata;
-  insize = zbytes;
+  insize = inbytes = zbytes;
 
   /* Set up output buffer */
   outcnt = 0;