Re: [PATCH v3 3/8] gzip: refactor the gunzip window into common state

2024-04-24 Thread Daniel P. Smith

On 4/24/24 12:34, Daniel P. Smith wrote:

Begin moving core state, in this case the gunzip window, into struct
gunzip_state to allow a per decompression instance. In doing so, drop the
define aliasing of window to slide.

Signed-off-by: Daniel P. Smith 
---
  xen/common/gzip/gunzip.c  | 21 
  xen/common/gzip/inflate.c | 68 +++
  2 files changed, 48 insertions(+), 41 deletions(-)

diff --git a/xen/common/gzip/gunzip.c b/xen/common/gzip/gunzip.c
index b7cadadcca8b..e47f10ae19ad 100644
--- a/xen/common/gzip/gunzip.c
+++ b/xen/common/gzip/gunzip.c
@@ -4,10 +4,12 @@
  #include 
  #include 
  
-static unsigned char *__initdata window;

-
  #define WSIZE   0x8000U
  
+struct gunzip_state {

+unsigned char *window;
+};
+
  static unsigned char *__initdata inbuf;
  static unsigned int __initdata insize;
  
@@ -43,7 +45,7 @@ typedef unsigned long   ulg;

  #endif
  
  static long __initdata bytes_out;

-static void flush_window(void);
+static void flush_window(struct gunzip_state *s);
  
  static __init void error(const char *x)

  {
@@ -62,7 +64,7 @@ static __init uch get_byte(void) {
  
  #include "inflate.c"
  
-static __init void flush_window(void)

+static __init void flush_window(struct gunzip_state *s)
  {
  /*
   * The window is equal to the output buffer therefore only need to
@@ -72,7 +74,7 @@ static __init void flush_window(void)
  unsigned int n;
  unsigned char *in, ch;
  
-in = window;

+in = s->window;
  for ( n = 0; n < outcnt; n++ )
  {
  ch = *in++;
@@ -99,12 +101,17 @@ __init int gzip_check(char *image, unsigned long image_len)
  
  __init int perform_gunzip(char *output, char *image, unsigned long image_len)

  {
+struct gunzip_state *s;
  int rc;
  
  if ( !gzip_check(image, image_len) )

  return 1;
  
-window = (unsigned char *)output;

+s = (struct gunzip_state *)malloc(sizeof(struct gunzip_state));


Looks like I inadvertently dropped the corresponding free when breaking 
up the monolithic patch.


v/r,
dps



[PATCH v3 3/8] gzip: refactor the gunzip window into common state

2024-04-24 Thread Daniel P. Smith
Begin moving core state, in this case the gunzip window, into struct
gunzip_state to allow a per decompression instance. In doing so, drop the
define aliasing of window to slide.

Signed-off-by: Daniel P. Smith 
---
 xen/common/gzip/gunzip.c  | 21 
 xen/common/gzip/inflate.c | 68 +++
 2 files changed, 48 insertions(+), 41 deletions(-)

diff --git a/xen/common/gzip/gunzip.c b/xen/common/gzip/gunzip.c
index b7cadadcca8b..e47f10ae19ad 100644
--- a/xen/common/gzip/gunzip.c
+++ b/xen/common/gzip/gunzip.c
@@ -4,10 +4,12 @@
 #include 
 #include 
 
-static unsigned char *__initdata window;
-
 #define WSIZE   0x8000U
 
+struct gunzip_state {
+unsigned char *window;
+};
+
 static unsigned char *__initdata inbuf;
 static unsigned int __initdata insize;
 
@@ -43,7 +45,7 @@ typedef unsigned long   ulg;
 #endif
 
 static long __initdata bytes_out;
-static void flush_window(void);
+static void flush_window(struct gunzip_state *s);
 
 static __init void error(const char *x)
 {
@@ -62,7 +64,7 @@ static __init uch get_byte(void) {
 
 #include "inflate.c"
 
-static __init void flush_window(void)
+static __init void flush_window(struct gunzip_state *s)
 {
 /*
  * The window is equal to the output buffer therefore only need to
@@ -72,7 +74,7 @@ static __init void flush_window(void)
 unsigned int n;
 unsigned char *in, ch;
 
-in = window;
+in = s->window;
 for ( n = 0; n < outcnt; n++ )
 {
 ch = *in++;
@@ -99,12 +101,17 @@ __init int gzip_check(char *image, unsigned long image_len)
 
 __init int perform_gunzip(char *output, char *image, unsigned long image_len)
 {
+struct gunzip_state *s;
 int rc;
 
 if ( !gzip_check(image, image_len) )
 return 1;
 
-window = (unsigned char *)output;
+s = (struct gunzip_state *)malloc(sizeof(struct gunzip_state));
+if ( !s )
+return -ENOMEM;
+
+s->window = (unsigned char *)output;
 inbuf = (unsigned char *)image;
 insize = image_len;
 inptr = 0;
@@ -112,7 +119,7 @@ __init int perform_gunzip(char *output, char *image, 
unsigned long image_len)
 
 makecrc();
 
-if ( gunzip() < 0 )
+if ( gunzip(s) < 0 )
 {
 rc = -EINVAL;
 }
diff --git a/xen/common/gzip/inflate.c b/xen/common/gzip/inflate.c
index 02a395aeb86a..5fa5c039c6d1 100644
--- a/xen/common/gzip/inflate.c
+++ b/xen/common/gzip/inflate.c
@@ -126,12 +126,11 @@ static char rcsid[] = "#Id: inflate.c,v 0.14 1993/06/10 
13:27:04 jloup Exp #";
  * ANDing with 0x7fff (32K-1).
  *
  * It is left to other modules to supply the 32 K area.  It is assumed
- * to be usable as if it were declared "uch slide[32768];" or as just
- * "uch *slide;" and then malloc'ed in the latter case.  The definition
+ * to be usable as if it were declared "uch window[32768];" or as just
+ * "uch *window;" and then malloc'ed in the latter case.  The definition
  * must be in unzip.h, included above.
  */
 #define wp outcnt
-#define slide window
 
 /*
  * Huffman code lookup table entry--this entry is four bytes for machines
@@ -155,12 +154,13 @@ struct huft {
 static int huft_build(unsigned *, unsigned, unsigned,
   const ush *, const ush *, struct huft **, int *);
 static int huft_free(struct huft *);
-static int inflate_codes(struct huft *, struct huft *, int, int);
-static int inflate_stored(void);
-static int inflate_fixed(void);
-static int inflate_dynamic(void);
-static int inflate_block(int *);
-static int inflate(void);
+static int inflate_codes(
+struct gunzip_state *, struct huft *, struct huft *, int, int);
+static int inflate_stored(struct gunzip_state *s);
+static int inflate_fixed(struct gunzip_state *s);
+static int inflate_dynamic(struct gunzip_state *s);
+static int inflate_block(struct gunzip_state *, int *);
+static int inflate(struct gunzip_state *s);
 
 /* Tables for deflate from PKZIP's appnote.txt. */
 static const unsigned border[] = {/* Order of the bit length code lengths 
*/
@@ -542,7 +542,7 @@ static int __init huft_free(struct huft *t)
  * @param bd  Number of bits decoded by td[]
  */
 static int __init inflate_codes(
-struct huft *tl, struct huft *td, int bl, int bd)
+struct gunzip_state *s, struct huft *tl, struct huft *td, int bl, int bd)
 {
 register unsigned e;  /* table entry flag/number of extra bits */
 unsigned n, d;/* length and index for copy */
@@ -575,12 +575,12 @@ static int __init inflate_codes(
 DUMPBITS(t->b);
 if (e == 16)/* then it's a literal */
 {
-slide[w++] = (uch)t->v.n;
-Tracevv((stderr, "%c", slide[w-1]));
+s->window[w++] = (uch)t->v.n;
+Tracevv((stderr, "%c", s->window[w-1]));
 if (w == WSIZE)
 {
 wp = w;
-flush_window();
+flush_window(s);
 w = 0;
 }
 }
@@ -616,19 +616,19 @@ static int