]> xenbits.xensource.com Git - people/andrewcoop/xen.git/commitdiff
xen/gunzip: Move crc state into gunzip_state
authorDaniel P. Smith <dpsmith@apertussolutions.com>
Wed, 24 Apr 2024 16:34:22 +0000 (12:34 -0400)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 9 May 2024 17:19:49 +0000 (18:19 +0100)
Move the crc and its state into struct gunzip_state.  In the process, expand
the only use of CRC_VALUE as it is hides what is being compared.

Furthermore, all variables here should be uint32_t rather than unsigned long,
which halves the storage space required.  Filter the typechanges through the
logic.

Adjust the logic to hold crc in a positive form, and negate it for update in
flush_window().  This is the more normal way to write CRC algorithms, and
avoids weird-to-follow logic in gunzip().

Signed-off-by: Daniel P. Smith <dpsmith@apertussolutions.com>
Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>
xen/common/gzip/gunzip.c
xen/common/gzip/inflate.c

index 134144a184d131c270d3cd79a3d3e90b744ae597..89f45d4050ba386a35eb06e39516dcaa5438ed20 100644 (file)
@@ -21,6 +21,9 @@ struct gunzip_state {
 
     unsigned long bb;      /* bit buffer */
     unsigned int  bk;      /* bits in bit buffer */
+
+    uint32_t crc_32_tab[256];
+    uint32_t crc;
 };
 
 #define malloc(a)       xmalloc_bytes(a)
@@ -74,7 +77,7 @@ static __init void flush_window(struct gunzip_state *s)
      * The window is equal to the output buffer therefore only need to
      * compute the crc.
      */
-    unsigned long c = crc;
+    uint32_t c = ~s->crc;
     unsigned int n;
     unsigned char *in, ch;
 
@@ -82,9 +85,9 @@ static __init void flush_window(struct gunzip_state *s)
     for ( n = 0; n < s->wp; n++ )
     {
         ch = *in++;
-        c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
+        c = s->crc_32_tab[(c ^ ch) & 0xff] ^ (c >> 8);
     }
-    crc = c;
+    s->crc = ~c;
 
     s->bytes_out += s->wp;
     s->wp = 0;
@@ -121,7 +124,7 @@ __init int perform_gunzip(char *output, char *image, unsigned long image_len)
     s->inptr = 0;
     s->bytes_out = 0;
 
-    makecrc();
+    makecrc(s);
 
     if ( gunzip(s) < 0 )
     {
index 7487616442ed1a3a5769d4d355c15584e703c206..b9a2d7a23a8aa92c36066e32462430a3ec10c899 100644 (file)
@@ -1041,16 +1041,12 @@ static int __init inflate(struct gunzip_state *s)
  *
  **********************************************************************/
 
-static ulg __initdata crc_32_tab[256];
-static ulg __initdata crc;  /* initialized in makecrc() so it'll reside in bss */
-#define CRC_VALUE (crc ^ 0xffffffffUL)
-
 /*
  * Code to compute the CRC-32 table. Borrowed from
  * gzip-1.0.3/makecrc.c.
  */
 
-static void __init makecrc(void)
+static void __init makecrc(struct gunzip_state *s)
 {
 /* Not copyrighted 1990 Mark Adler */
 
@@ -1067,7 +1063,7 @@ static void __init makecrc(void)
     for (i = 0; i < sizeof(p)/sizeof(int); i++)
         e |= 1L << (31 - p[i]);
 
-    crc_32_tab[0] = 0;
+    s->crc_32_tab[0] = 0;
 
     for (i = 1; i < 256; i++)
     {
@@ -1078,11 +1074,10 @@ static void __init makecrc(void)
             if (k & 1)
                 c ^= e;
         }
-        crc_32_tab[i] = c;
+        s->crc_32_tab[i] = c;
     }
 
-    /* this is initialized here so this code could reside in ROM */
-    crc = (ulg)0xffffffffUL; /* shift register contents */
+    s->crc = 0;
 }
 
 /* gzip flag byte */
@@ -1204,7 +1199,8 @@ static int __init gunzip(struct gunzip_state *s)
     orig_len |= (ulg) NEXTBYTE(s) << 24;
 
     /* Validate decompression */
-    if (orig_crc != CRC_VALUE) {
+    if ( orig_crc != s->crc )
+    {
         error("crc error");
         return -1;
     }