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