xref: /linux/arch/xtensa/boot/lib/zmem.c (revision 40d269c000bda9fcd276a0412a9cebd3f6e344c5)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/zlib.h>
3 
4 /* bits taken from ppc */
5 
6 extern void *avail_ram, *end_avail;
7 void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp);
8 
9 static void exit(void)
10 {
11   for (;;);
12 }
13 
14 static void *zalloc(unsigned int size)
15 {
16         void *p = avail_ram;
17 
18         size = (size + 7) & -8;
19         avail_ram += size;
20         if (avail_ram > end_avail) {
21                 //puts("oops... out of memory\n");
22                 //pause();
23                 exit ();
24         }
25         return p;
26 }
27 
28 #define HEAD_CRC        2
29 #define EXTRA_FIELD     4
30 #define ORIG_NAME       8
31 #define COMMENT         0x10
32 #define RESERVED        0xe0
33 
34 #define DEFLATED        8
35 
36 void gunzip (void *dst, int dstlen, unsigned char *src, int *lenp)
37 {
38 	z_stream s;
39 	int r, i, flags;
40 
41         /* skip header */
42         i = 10;
43         flags = src[3];
44         if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
45                 //puts("bad gzipped data\n");
46                 exit();
47         }
48         if ((flags & EXTRA_FIELD) != 0)
49                 i = 12 + src[10] + (src[11] << 8);
50         if ((flags & ORIG_NAME) != 0)
51                 while (src[i++] != 0)
52                         ;
53         if ((flags & COMMENT) != 0)
54                 while (src[i++] != 0)
55                         ;
56         if ((flags & HEAD_CRC) != 0)
57                 i += 2;
58         if (i >= *lenp) {
59                 //puts("gunzip: ran out of data in header\n");
60                 exit();
61         }
62 
63 	s.workspace = zalloc(zlib_inflate_workspacesize());
64         r = zlib_inflateInit2(&s, -MAX_WBITS);
65         if (r != Z_OK) {
66                 //puts("inflateInit2 returned "); puthex(r); puts("\n");
67                 exit();
68         }
69         s.next_in = src + i;
70         s.avail_in = *lenp - i;
71         s.next_out = dst;
72         s.avail_out = dstlen;
73         r = zlib_inflate(&s, Z_FINISH);
74         if (r != Z_OK && r != Z_STREAM_END) {
75                 //puts("inflate returned "); puthex(r); puts("\n");
76                 exit();
77         }
78         *lenp = s.next_out - (unsigned char *) dst;
79         zlib_inflateEnd(&s);
80 }
81 
82