xen/gunzip: Introduce struct gunzip_state and refactor window

Introduce struct gunzip_state so the state can be per-instance rather than
global.  Allocate and free the structure in perform_gunzip().

Move the window (output) pointer into gunzip_state first, which involves
plumbing the state pointer all the way down into flush_window().

Drop the 'slide' alias, and the flush_output() macro too as it hides at least
one "wp = wp" assignment.

Signed-off-by: Daniel P. Smith <dpsmith@apertussolutions.com>
Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>
This commit is contained in:
Daniel P. Smith authored and Andrew Cooper committed 2024-05-09 18:19:49 +01:00
1 parent 473156eec6
commit 02e32883f3
2 files changed
+51 -41

No files matched your search

+16 -7
View File
@@ -4,10 +4,12 @@
#include <xen/lib.h>
#include <xen/mm.h>
static unsigned char *__initdata window;
#define WSIZE 0x80000000U
struct gunzip_state {
unsigned char *window;
};
static unsigned char *__initdata inbuf;
static unsigned int __initdata insize;
@@ -45,7 +47,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)
{
@@ -60,7 +62,7 @@ static __init int fill_inbuf(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
@@ -70,7 +72,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++;
@@ -97,12 +99,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 = malloc(sizeof(struct gunzip_state));
if ( !s )
return -ENOMEM;
s->window = (unsigned char *)output;
inbuf = (unsigned char *)image;
insize = image_len;
inptr = 0;
@@ -110,7 +117,7 @@ __init int perform_gunzip(char *output, char *image, unsigned long image_len)
makecrc();
if ( gunzip() < 0 )
if ( gunzip(s) < 0 )
{
rc = -EINVAL;
}
@@ -119,6 +126,8 @@ __init int perform_gunzip(char *output, char *image, unsigned long image_len)
rc = 0;
}
free(s);
return rc;
}
+35 -34
View File
@@ -119,8 +119,6 @@ static char rcsid[] = "#Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp #";
#endif /* !__XEN__ */
#define slide window
/*
* Huffman code lookup table entry--this entry is four bytes for machines
* that have 16-bit pointers (e.g. PC's in the small or medium model).
@@ -143,12 +141,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 *s, struct huft *tl, struct huft *td, int bl, int bd);
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 *s, int *e);
static int inflate(struct gunzip_state *s);
/*
* The inflate algorithm uses a sliding 32 K byte window on the uncompressed
@@ -163,7 +162,6 @@ static int inflate(void);
*/
/* unsigned wp; current position in slide */
#define wp outcnt
#define flush_output(w) (wp=(w),flush_window())
/* Tables for deflate from PKZIP's appnote.txt. */
static const unsigned border[] = { /* Order of the bit length code lengths */
@@ -545,7 +543,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 */
@@ -578,11 +576,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)
{
flush_output(w);
wp = w;
flush_window(s);
w = 0;
}
}
@@ -618,18 +617,19 @@ static int __init inflate_codes(
n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
if (w - d >= e) /* (this test assumes unsigned comparison) */
{
memcpy(slide + w, slide + d, e);
memcpy(s->window + w, s->window + d, e);
w += e;
d += e;
}
else /* do it slow to avoid memcpy() overlap */
do {
slide[w++] = slide[d++];
Tracevv((stderr, "%c", slide[w-1]));
s->window[w++] = s->window[d++];
Tracevv((stderr, "%c", s->window[w-1]));
} while (--e);
if (w == WSIZE)
{
flush_output(w);
wp = w;
flush_window(s);
w = 0;
}
} while (n);
@@ -649,7 +649,7 @@ static int __init inflate_codes(
}
/* "decompress" an inflated type 0 (stored) block. */
static int __init inflate_stored(void)
static int __init inflate_stored(struct gunzip_state *s)
{
unsigned n; /* number of bytes in block */
unsigned w; /* current window position */
@@ -682,10 +682,11 @@ static int __init inflate_stored(void)
while (n--)
{
NEEDBITS(8);
slide[w++] = (uch)b;
s->window[w++] = (uch)b;
if (w == WSIZE)
{
flush_output(w);
wp = w;
flush_window(s);
w = 0;
}
DUMPBITS(8);
@@ -713,7 +714,7 @@ static int __init inflate_stored(void)
* either replace this with a custom decoder, or at least precompute the
* Huffman tables.
*/
static int noinline __init inflate_fixed(void)
static int noinline __init inflate_fixed(struct gunzip_state *s)
{
int i; /* temporary variable */
struct huft *tl; /* literal/length code table */
@@ -757,7 +758,7 @@ static int noinline __init inflate_fixed(void)
}
/* decompress until an end-of-block code */
i = inflate_codes(tl, td, bl, bd);
i = inflate_codes(s, tl, td, bl, bd);
/* free the decoding tables, return */
free(l);
@@ -772,7 +773,7 @@ static int noinline __init inflate_fixed(void)
*/
/* decompress an inflated type 2 (dynamic Huffman codes) block. */
static int noinline __init inflate_dynamic(void)
static int noinline __init inflate_dynamic(struct gunzip_state *s)
{
int i; /* temporary variables */
unsigned j;
@@ -938,7 +939,7 @@ static int noinline __init inflate_dynamic(void)
DEBG("dyn6 ");
/* decompress until an end-of-block code */
ret = !!inflate_codes(tl, td, bl, bd);
ret = !!inflate_codes(s, tl, td, bl, bd);
if ( !ret )
DEBG("dyn7 ");
@@ -963,7 +964,7 @@ static int noinline __init inflate_dynamic(void)
*
* @param e Last block flag
*/
static int __init inflate_block(int *e)
static int __init inflate_block(struct gunzip_state *s, int *e)
{
unsigned t; /* block type */
register ulg b; /* bit buffer */
@@ -991,11 +992,11 @@ static int __init inflate_block(int *e)
/* inflate that block type */
if (t == 2)
return inflate_dynamic();
return inflate_dynamic(s);
if (t == 0)
return inflate_stored();
return inflate_stored(s);
if (t == 1)
return inflate_fixed();
return inflate_fixed(s);
DEBG(">");
@@ -1007,7 +1008,7 @@ static int __init inflate_block(int *e)
}
/* decompress an inflated entry */
static int __init inflate(void)
static int __init inflate(struct gunzip_state *s)
{
int e; /* last block flag */
int r; /* result code */
@@ -1019,7 +1020,7 @@ static int __init inflate(void)
/* decompress until the last block */
do {
r = inflate_block(&e);
r = inflate_block(s, &e);
if (r)
return r;
} while (!e);
@@ -1032,8 +1033,7 @@ static int __init inflate(void)
inptr--;
}
/* flush out slide */
flush_output(wp);
flush_window(s);
/* return success */
return 0;
@@ -1101,7 +1101,7 @@ static void __init makecrc(void)
/*
* Do the uncompression!
*/
static int __init gunzip(void)
static int __init gunzip(struct gunzip_state *s)
{
uch flags;
unsigned char magic[2]; /* magic header */
@@ -1165,7 +1165,8 @@ static int __init gunzip(void)
}
/* Decompress */
if ((res = inflate())) {
if ( (res = inflate(s)) )
{
switch (res) {
case 0:
break;
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.