~lattis/muon

ref: f9a8cbad39b665a9496cd3adb4756e85ee5ec97c muon/src/external/zlib.c -rw-r--r-- 2.0 KiB
f9a8cbadStone Tickle print stdout/stderr of failing test 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "posix.h"

#include <limits.h>
#include <stdbool.h>
#include <string.h>

#include <zlib.h>

#include "external/zlib.h"
#include "formats/tar.h"
#include "log.h"
#include "platform/filesystem.h"
#include "platform/mem.h"

const bool have_zlib = true;

#define CHUNK_SIZE 1048576ul

static const char *
archive_z_strerror(int err)
{
	switch (err) {
	case Z_OK: return "ok";
	case Z_STREAM_END: return "stream end";
	case Z_NEED_DICT: return "need dict";
	case Z_ERRNO: return "errno";
	case Z_STREAM_ERROR: return "stream";
	case Z_DATA_ERROR: return "data";
	case Z_MEM_ERROR: return "memory";
	case Z_BUF_ERROR: return "buffer";
	case Z_VERSION_ERROR: return "version";
	default: return "unknown";
	}
}

static bool
unzip(uint8_t *data, uint64_t len, uint8_t **out, uint64_t *out_len)
{
	int err;
	uint8_t *buf = z_malloc(CHUNK_SIZE);
	uint64_t buf_len = CHUNK_SIZE;

	z_stream stream = {
		.next_in = data,
		.avail_in = len,

		.next_out = buf,
		.avail_out = CHUNK_SIZE
	};

	if ((err = inflateInit2(&stream, 16 + MAX_WBITS)) != Z_OK) {
		LOG_E("failed inflateInit2: %s", archive_z_strerror(err));
		return false;
	}

	while ((err = inflate(&stream, Z_SYNC_FLUSH)) == Z_OK) {
		buf_len += CHUNK_SIZE;
		buf = z_realloc(buf, buf_len);

		stream.next_out = &buf[buf_len - CHUNK_SIZE];
		stream.avail_out = CHUNK_SIZE;
	}
	*out_len = stream.total_out;

	if (err != Z_STREAM_END) {
		LOG_E("failed to inflate: %s", archive_z_strerror(err));
		if ((err = inflateEnd(&stream)) != Z_OK) {
			LOG_E("failed inflateEnd: %s", archive_z_strerror(err));
		}
		z_free(buf);
		return false;
	}

	if ((err = inflateEnd(&stream)) != Z_OK) {
		LOG_E("failed inflateEnd: %s", archive_z_strerror(err));
		z_free(buf);
		return false;
	}

	*out = buf;
	return true;
}

bool
muon_zlib_extract(uint8_t *data, uint64_t len, const char *destdir)
{
	uint8_t *unzipped;
	uint64_t unzipped_len;

	if (!unzip(data, len, &unzipped, &unzipped_len)) {
		return false;
	} else if (!untar(unzipped, unzipped_len, destdir)) {
		z_free(unzipped);
		return false;
	}

	z_free(unzipped);
	return true;
}