xref: /rk3399_ARM-atf/lib/zlib/tf_gunzip.c (revision 57546074cb245d67dda53b36272835f5f871b444)
1c43d6851SMasahiro Yamada /*
2c43d6851SMasahiro Yamada  * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3c43d6851SMasahiro Yamada  *
4c43d6851SMasahiro Yamada  * SPDX-License-Identifier: BSD-3-Clause
5c43d6851SMasahiro Yamada  */
6c43d6851SMasahiro Yamada 
7c43d6851SMasahiro Yamada #include <assert.h>
8c43d6851SMasahiro Yamada #include <debug.h>
9c43d6851SMasahiro Yamada #include <errno.h>
10c43d6851SMasahiro Yamada #include <string.h>
11c43d6851SMasahiro Yamada #include <tf_gunzip.h>
12c43d6851SMasahiro Yamada #include <utils.h>
13c43d6851SMasahiro Yamada 
14c43d6851SMasahiro Yamada #include "zutil.h"
15c43d6851SMasahiro Yamada 
16c43d6851SMasahiro Yamada /*
17c43d6851SMasahiro Yamada  * memory allocated by malloc() is supposed to be aligned for any built-in type
18c43d6851SMasahiro Yamada  */
19c43d6851SMasahiro Yamada #define ZALLOC_ALIGNMENT	sizeof(void *)
20c43d6851SMasahiro Yamada 
21c43d6851SMasahiro Yamada static uintptr_t zalloc_start;
22c43d6851SMasahiro Yamada static uintptr_t zalloc_end;
23c43d6851SMasahiro Yamada static uintptr_t zalloc_current;
24c43d6851SMasahiro Yamada 
25c43d6851SMasahiro Yamada static void * ZLIB_INTERNAL zcalloc(void *opaque, unsigned int items,
26c43d6851SMasahiro Yamada 				    unsigned int size)
27c43d6851SMasahiro Yamada {
28c43d6851SMasahiro Yamada 	uintptr_t p, p_end;
29c43d6851SMasahiro Yamada 
30c43d6851SMasahiro Yamada 	size *= items;
31c43d6851SMasahiro Yamada 
32c43d6851SMasahiro Yamada 	p = round_up(zalloc_current, ZALLOC_ALIGNMENT);
33c43d6851SMasahiro Yamada 	p_end = p + size;
34c43d6851SMasahiro Yamada 
35c43d6851SMasahiro Yamada 	if (p_end > zalloc_end)
36c43d6851SMasahiro Yamada 		return NULL;
37c43d6851SMasahiro Yamada 
38c43d6851SMasahiro Yamada 	memset((void *)p, 0, size);
39c43d6851SMasahiro Yamada 
40c43d6851SMasahiro Yamada 	zalloc_current = p_end;
41c43d6851SMasahiro Yamada 
42c43d6851SMasahiro Yamada 	return (void *)p;
43c43d6851SMasahiro Yamada }
44c43d6851SMasahiro Yamada 
45c43d6851SMasahiro Yamada static void ZLIB_INTERNAL zfree(void *opaque, void *ptr)
46c43d6851SMasahiro Yamada {
47c43d6851SMasahiro Yamada }
48c43d6851SMasahiro Yamada 
49c43d6851SMasahiro Yamada /*
50c43d6851SMasahiro Yamada  * gunzip - decompress gzip data
51c43d6851SMasahiro Yamada  * @in_buf: source of compressed input. Upon exit, the end of input.
52c43d6851SMasahiro Yamada  * @in_len: length of in_buf
53c43d6851SMasahiro Yamada  * @out_buf: destination of decompressed output. Upon exit, the end of output.
54c43d6851SMasahiro Yamada  * @out_len: length of out_buf
55c43d6851SMasahiro Yamada  * @work_buf: workspace
56c43d6851SMasahiro Yamada  * @work_len: length of workspace
57c43d6851SMasahiro Yamada  */
58c43d6851SMasahiro Yamada int gunzip(uintptr_t *in_buf, size_t in_len, uintptr_t *out_buf,
59c43d6851SMasahiro Yamada 	   size_t out_len, uintptr_t work_buf, size_t work_len)
60c43d6851SMasahiro Yamada {
61c43d6851SMasahiro Yamada 	z_stream stream;
62c43d6851SMasahiro Yamada 	int zret, ret;
63c43d6851SMasahiro Yamada 
64c43d6851SMasahiro Yamada 	zalloc_start = work_buf;
65c43d6851SMasahiro Yamada 	zalloc_end = work_buf + work_len;
66c43d6851SMasahiro Yamada 	zalloc_current = zalloc_start;
67c43d6851SMasahiro Yamada 
68c43d6851SMasahiro Yamada 	stream.next_in = (typeof(stream.next_in))*in_buf;
69c43d6851SMasahiro Yamada 	stream.avail_in = in_len;
70c43d6851SMasahiro Yamada 	stream.next_out = (typeof(stream.next_out))*out_buf;
71c43d6851SMasahiro Yamada 	stream.avail_out = out_len;
72c43d6851SMasahiro Yamada 	stream.zalloc = zcalloc;
73c43d6851SMasahiro Yamada 	stream.zfree = zfree;
74c43d6851SMasahiro Yamada 	stream.opaque = (voidpf)0;
75c43d6851SMasahiro Yamada 
76c43d6851SMasahiro Yamada 	zret = inflateInit(&stream);
77c43d6851SMasahiro Yamada 	if (zret != Z_OK) {
78c43d6851SMasahiro Yamada 		ERROR("zlib: inflate init failed (ret = %d)\n", zret);
79c43d6851SMasahiro Yamada 		return (zret == Z_MEM_ERROR) ? -ENOMEM : -EIO;
80c43d6851SMasahiro Yamada 	}
81c43d6851SMasahiro Yamada 
82c43d6851SMasahiro Yamada 	zret = inflate(&stream, Z_NO_FLUSH);
83c43d6851SMasahiro Yamada 	if (zret == Z_STREAM_END) {
84c43d6851SMasahiro Yamada 		ret = 0;
85c43d6851SMasahiro Yamada 	} else {
86c43d6851SMasahiro Yamada 		if (stream.msg)
87c43d6851SMasahiro Yamada 			ERROR("%s\n", stream.msg);
88c43d6851SMasahiro Yamada 		ERROR("zlib: inflate failed (ret = %d)\n", zret);
89c43d6851SMasahiro Yamada 		ret = (zret == Z_MEM_ERROR) ? -ENOMEM : -EIO;
90c43d6851SMasahiro Yamada 	}
91c43d6851SMasahiro Yamada 
92*57546074SSandrine Bailleux 	VERBOSE("zlib: %lu byte input\n", stream.total_in);
93*57546074SSandrine Bailleux 	VERBOSE("zlib: %lu byte output\n", stream.total_out);
94c43d6851SMasahiro Yamada 
95c43d6851SMasahiro Yamada 	*in_buf = (uintptr_t)stream.next_in;
96c43d6851SMasahiro Yamada 	*out_buf = (uintptr_t)stream.next_out;
97c43d6851SMasahiro Yamada 
98c43d6851SMasahiro Yamada 	inflateEnd(&stream);
99c43d6851SMasahiro Yamada 
100c43d6851SMasahiro Yamada 	return ret;
101c43d6851SMasahiro Yamada }
102