]> git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-file.c
Support for loading Xz-compressed modules
[thirdparty/kmod.git] / libkmod / libkmod-file.c
1 /*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011 ProFUSION embedded systems
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/mman.h>
29 #include <unistd.h>
30
31 #include "libkmod.h"
32 #include "libkmod-private.h"
33
34 #ifdef ENABLE_XZ
35 #include <lzma.h>
36 #endif
37 #ifdef ENABLE_ZLIB
38 #include <zlib.h>
39 #endif
40
41 struct kmod_file {
42 #ifdef ENABLE_XZ
43 bool xz_used;
44 #endif
45 #ifdef ENABLE_ZLIB
46 gzFile gzf;
47 #endif
48 int fd;
49 off_t size;
50 void *memory;
51 };
52
53 #ifdef ENABLE_XZ
54 static bool xz_detect(struct kmod_file *file)
55 {
56 ssize_t ret;
57 char buf[6];
58
59 ret = read(file->fd, buf, sizeof(buf));
60 if (ret < 0 || lseek(file->fd, 0, SEEK_SET))
61 return -errno;
62 return ret == sizeof(buf) &&
63 memcmp(buf, "\xFD""7zXZ\x00", sizeof(buf)) == 0;
64 }
65
66 static void xz_uncompress_belch(lzma_ret ret)
67 {
68 switch (ret) {
69 case LZMA_MEM_ERROR:
70 fprintf(stderr, "xz: %s\n", strerror(ENOMEM));
71 break;
72 case LZMA_FORMAT_ERROR:
73 fprintf(stderr, "xz: File format not recognized\n");
74 break;
75 case LZMA_OPTIONS_ERROR:
76 fprintf(stderr, "xz: Unsupported compression options\n");
77 break;
78 case LZMA_DATA_ERROR:
79 fprintf(stderr, "xz: File is corrupt\n");
80 break;
81 case LZMA_BUF_ERROR:
82 fprintf(stderr, "xz: Unexpected end of input\n");
83 break;
84 default:
85 fprintf(stderr, "xz: Internal error (bug)\n");
86 break;
87 }
88 }
89
90 static int xz_uncompress(lzma_stream *strm, struct kmod_file *file)
91 {
92 uint8_t in_buf[BUFSIZ], out_buf[BUFSIZ];
93 lzma_action action = LZMA_RUN;
94 lzma_ret ret;
95 void *p = NULL;
96 size_t total = 0;
97
98 strm->avail_in = 0;
99 strm->next_out = out_buf;
100 strm->avail_out = sizeof(out_buf);
101
102 while (true) {
103 if (strm->avail_in == 0) {
104 ssize_t rdret = read(file->fd, in_buf, sizeof(in_buf));
105 if (rdret < 0) {
106 ret = -errno;
107 goto out;
108 }
109 strm->next_in = in_buf;
110 strm->avail_in = rdret;
111 if (rdret == 0)
112 action = LZMA_FINISH;
113 }
114 ret = lzma_code(strm, action);
115 if (strm->avail_out == 0 || ret != LZMA_OK) {
116 size_t write_size = BUFSIZ - strm->avail_out;
117 char *tmp = realloc(p, total + write_size);
118 if (tmp == NULL) {
119 ret = -errno;
120 goto out;
121 }
122 memcpy(tmp + total, out_buf, write_size);
123 total += write_size;
124 p = tmp;
125 strm->next_out = out_buf;
126 strm->avail_out = BUFSIZ;
127 }
128 if (ret == LZMA_STREAM_END)
129 break;
130 if (ret != LZMA_OK) {
131 xz_uncompress_belch(ret);
132 ret = -EINVAL;
133 goto out;
134 }
135 }
136 file->memory = p;
137 file->size = total;
138 return 0;
139 out:
140 free(p);
141 close(file->fd);
142 return ret;
143 }
144
145 static int xz_file_open(struct kmod_file *file)
146 {
147 lzma_stream strm = LZMA_STREAM_INIT;
148 lzma_ret lzret;
149 int ret;
150
151 lzret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
152 if (lzret == LZMA_MEM_ERROR) {
153 fprintf(stderr, "xz: %s\n", strerror(ENOMEM));
154 close(file->fd);
155 return -ENOMEM;
156 } else if (lzret != LZMA_OK) {
157 fprintf(stderr, "xz: Internal error (bug)\n");
158 close(file->fd);
159 return -EINVAL;
160 }
161 ret = xz_uncompress(&strm, file);
162 lzma_end(&strm);
163 return ret;
164 }
165 #endif
166
167 #ifdef ENABLE_ZLIB
168 #define READ_STEP (4 * 1024 * 1024)
169
170 static bool check_zlib(struct kmod_file *file)
171 {
172 uint8_t magic[2] = {0, 0}, zlibmagic[2] = {0x1f, 0x8b};
173 size_t done = 0, todo = 2;
174 while (todo > 0) {
175 ssize_t r = read(file->fd, magic + done, todo);
176 if (r > 0){
177 todo -= r;
178 done += r;
179 } else if (r == 0)
180 goto error;
181 else if (errno == EAGAIN || errno == EINTR)
182 continue;
183 else
184 goto error;
185 }
186 lseek(file->fd, 0, SEEK_SET);
187 return memcmp(magic, zlibmagic, 2) == 0;
188 error:
189 lseek(file->fd, 0, SEEK_SET);
190 return false;
191 }
192
193 static int zlib_file_open(struct kmod_file *file)
194 {
195 int err = 0;
196 off_t did = 0, total = 0;
197 unsigned char *p = NULL;
198
199 errno = 0;
200 file->gzf = gzdopen(file->fd, "rb");
201 if (file->gzf == NULL) {
202 close(file->fd);
203 return -errno;
204 }
205
206 for (;;) {
207 int r;
208
209 if (did == total) {
210 void *tmp = realloc(p, total + READ_STEP);
211 if (tmp == NULL) {
212 err = -errno;
213 goto error;
214 }
215 total += READ_STEP;
216 p = tmp;
217 }
218
219 r = gzread(file->gzf, p + did, total - did);
220 if (r == 0)
221 break;
222 else if (r < 0) {
223 err = -errno;
224 goto error;
225 }
226 did += r;
227 }
228
229 file->memory = p;
230 file->size = did;
231 return 0;
232 error:
233 free(p);
234 gzclose(file->gzf);
235 return err;
236 }
237 #endif
238
239 static int reg_file_open(struct kmod_file *file)
240 {
241 struct stat st;
242 int err = 0;
243
244 if (fstat(file->fd, &st) < 0) {
245 err = -errno;
246 goto error;
247 }
248
249 file->size = st.st_size;
250 file->memory = mmap(0, file->size, PROT_READ, MAP_PRIVATE, file->fd, 0);
251 if (file->memory == MAP_FAILED) {
252 err = -errno;
253 goto error;
254 }
255
256 return 0;
257 error:
258 close(file->fd);
259 return err;
260 }
261
262 struct kmod_file *kmod_file_open(const char *filename)
263 {
264 struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
265 int err;
266
267 if (file == NULL)
268 return NULL;
269
270 file->fd = open(filename, O_RDONLY|O_CLOEXEC);
271 if (file->fd < 0) {
272 err = -errno;
273 goto error;
274 }
275
276 #ifdef ENABLE_XZ
277 if (xz_detect(file))
278 err = xz_file_open(file);
279 else
280 #endif
281 #ifdef ENABLE_ZLIB
282 if (check_zlib(file))
283 err = zlib_file_open(file);
284 else
285 #endif
286 err = reg_file_open(file);
287
288 error:
289 if (err < 0) {
290 free(file);
291 errno = -err;
292 return NULL;
293 }
294
295 return file;
296 }
297
298 void *kmod_file_get_contents(const struct kmod_file *file)
299 {
300 return file->memory;
301 }
302
303 off_t kmod_file_get_size(const struct kmod_file *file)
304 {
305 return file->size;
306 }
307
308 void kmod_file_unref(struct kmod_file *file)
309 {
310 #ifdef ENABLE_XZ
311 if (file->xz_used) {
312 free(file->memory);
313 close(file->fd);
314 } else
315 #endif
316 #ifdef ENABLE_ZLIB
317 if (file->gzf != NULL) {
318 free(file->memory);
319 gzclose(file->gzf);
320 } else {
321 #endif
322 munmap(file->memory, file->size);
323 close(file->fd);
324 #ifdef ENABLE_ZLIB
325 }
326 #endif
327 free(file);
328 }