summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/zlibsupport.c
blob: 2b171e72db055e6df6e60124e667cc330e0707df (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* Support for compressed modules.  Willy Tarreau <willy@meta-x.org>
 * did the support for modutils, Andrey Borzenkov <arvidjaar@mail.ru>
 * ported it to module-init-tools, and I said it was too ugly to live
 * and rewrote it 8).
 *
 * (C) 2003 Rusty Russell, IBM Corporation.
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>
#include <errno.h>

#include "zlibsupport.h"

#define CONFIG_USE_LIBLZMA
#ifdef CONFIG_USE_ZLIB
#include <zlib.h>
#ifdef CONFIG_USE_LIBLZMA
#include <lzma.h>

typedef struct lzma_file {
	uint8_t buf[1<<14];
	lzma_stream strm;
	FILE *fp;
	lzma_bool eof;
} lzma_FILE;
#else
typedef unsigned char lzma_bool;
typedef	int lzma_ret;
#define	LZMA_OK 0
#endif

typedef enum xFile_e {
	XF_NONE,
	XF_GZIP,
	XF_XZ,
	XF_FAIL
} xFile_t;

typedef struct xFile_s {
	xFile_t type;
	lzma_bool eof;
	union {
		gzFile gz;
#ifdef CONFIG_USE_LIBLZMA
		lzma_FILE *xz;
#endif
	} f;
	FILE *fp;
} xFile;

#ifdef CONFIG_USE_LIBLZMA
static lzma_FILE *lzma_open(lzma_ret *lzma_error, FILE *fp)
{
	lzma_ret *ret = lzma_error;
	lzma_FILE *lzma_file;
	lzma_stream tmp = LZMA_STREAM_INIT;

	lzma_file = calloc(1, sizeof(*lzma_file));

	lzma_file->fp = fp;
	lzma_file->eof = 0;
	lzma_file->strm = tmp;

	*ret = lzma_auto_decoder(&lzma_file->strm, -1, 0);

	if (*ret != LZMA_OK) {
		(void) fclose(lzma_file->fp);
		free(lzma_file);
		return NULL;
	}
	return lzma_file;
}

static ssize_t lzma_read(lzma_ret *lzma_error, lzma_FILE *lzma_file, void *buf, size_t len)
{
	lzma_ret *ret = lzma_error;
	lzma_bool eof = 0;

	if (!lzma_file)
		return -1;
	if (lzma_file->eof)
		return 0;

	lzma_file->strm.next_out = buf;
	lzma_file->strm.avail_out = len;
	for (;;) {
		if (!lzma_file->strm.avail_in) {
			lzma_file->strm.next_in = (uint8_t *)lzma_file->buf;
			lzma_file->strm.avail_in = fread(lzma_file->buf, 1, sizeof(lzma_file->buf), lzma_file->fp);
			if (!lzma_file->strm.avail_in)
				eof = 1;
		}
		*ret = lzma_code(&lzma_file->strm, LZMA_RUN);
		if (*ret == LZMA_STREAM_END) {
			lzma_file->eof = 1;
			return len - lzma_file->strm.avail_out;
		}
		if (*ret != LZMA_OK)
			return -1;
		if (!lzma_file->strm.avail_out)
			return len;
		if (eof)
			return -1;
	}
}
#endif

static xFile xOpen(int fd, const char *filename) {
	xFile xF = {XF_FAIL, 0, {NULL}, NULL};
	lzma_ret ret = LZMA_OK;
	unsigned char buf[8];

	if (fd == -1 && filename != NULL)
		if ((fd = open(filename, O_RDONLY)) < 0)
			return xF;
	if (read(fd, buf, sizeof(buf)) < 0)
		return xF;
	lseek(fd, 0, SEEK_SET);
	if (filename != NULL) {
		close(fd);
		fd = -1;
	}
	if (buf[0] == 0xFD && buf[1] == '7' && buf[2] == 'z' &&
			buf[3] == 'X' && buf[4] == 'Z' && buf[5] == 0x00)
		xF.type = XF_XZ;
	else if (buf[0] == 0x1F && buf[1] == 0x8B)
		xF.type = XF_GZIP;
	else
		xF.type = XF_NONE;

	switch(xF.type) {
		case XF_GZIP:
			xF.f.gz = (fd == -1 && filename != NULL) ? gzopen(filename, "rb") : gzdopen(fd, "rb");
			if(xF.f.gz == NULL)
				xF.type = XF_FAIL;
			break;
		case XF_NONE:
			xF.fp = (fd == -1 && filename != NULL) ? fopen(filename, "rb") : fdopen(fd, "rb");
			break;
#ifdef CONFIG_USE_LIBLZMA
		case XF_XZ:
			xF.fp = (fd == -1 && filename != NULL) ? fopen(filename, "rb") : fdopen(fd, "rb");
			if(xF.fp == NULL)
				xF.type = XF_FAIL;
			if(xF.type == XF_NONE || xF.type == XF_FAIL) break;
			xF.f.xz = lzma_open(&ret, xF.fp);
			if(ret != LZMA_OK)
				xF.type = XF_FAIL;
			break;
#endif
		default:
			break;
	}

	return xF;
}

static int xClose(xFile *xF) {
	int ret = -1;
	switch(xF->type) {
		case XF_GZIP:
			ret = gzclose(xF->f.gz);
			break;
#ifdef CONFIG_USE_LIBLZMA
		case XF_XZ:
			lzma_end(&xF->f.xz->strm);
			free(xF->f.xz);
#endif
		case XF_NONE:
			ret = fclose(xF->fp);
			break;
		default:
			break;
	}
	return ret;
}

static ssize_t xRead(xFile *xF, lzma_ret *ret, void *buf, size_t len) {
	ssize_t sz;
	switch(xF->type) {
		case XF_GZIP:
			sz = gzread(xF->f.gz, buf, len);
			xF->eof = gzeof(xF->f.gz);
			break;
#ifdef CONFIG_USE_LIBLZMA
		case XF_XZ:
			sz = lzma_read(ret, xF->f.xz, buf, len);
			xF->eof = xF->f.xz->eof;
			break;
#endif
		case XF_NONE:
			sz = fread(buf, 1, len, xF->fp);
			xF->eof = feof(xF->fp);
			break;
		default:
			sz = -1;
			break;
	}
	return sz;
}

void *grab_contents(xFile *xF, unsigned long *size)
{
	unsigned int max = 16384;
	void *buffer = calloc(1, max);
	lzma_ret ret;

	if (!buffer)
		return NULL;

	*size = 0;
	while ((ret = xRead(xF, &ret, buffer + *size, max - *size)) > 0) {
		*size += ret;
		if (*size == max) {
			void *p;

			p = realloc(buffer, max *= 2);
			if (!p)
				goto out_err;

			buffer = p;
		}
	}
	if (ret < 0)
		goto out_err;

	return buffer;

out_err:
	free(buffer);
	return NULL;
}

/* gzopen handles uncompressed files transparently. */
void *grab_file(const char *filename, unsigned long *size)
{
	xFile xF;
	void *buffer;

	xF = xOpen(-1, filename);
	if (xF.type == XF_FAIL)
		return NULL;
	buffer = grab_contents(&xF, size);
	xClose(&xF);
	return buffer;
}

void release_file(void *data, unsigned long size)
{
	free(data);
}
#else /* ... !CONFIG_USE_ZLIB */

void *grab_fd(int fd, unsigned long *size)
{
	struct stat st;
	void *map;
	int ret;

	ret = fstat(fd, &st);
	if (ret < 0)
		return NULL;
	*size = st.st_size;
	map = mmap(0, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
	if (map == MAP_FAILED)
		map = NULL;
	return map;
}

void *grab_file(const char *filename, unsigned long *size)
{
	int fd;
	void *map;

	fd = open(filename, O_RDONLY, 0);
	if (fd < 0)
		return NULL;
	map = grab_fd(fd, size);
	close(fd);
	return map;
}

void release_file(void *data, unsigned long size)
{
	munmap(data, size);
}
#endif