diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2012-03-06 16:23:50 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2012-03-06 16:23:50 +0100 |
commit | 59655077c5bf176f01d8d277665ebb92263704ed (patch) | |
tree | 0d4393ea09ebe90e35866d27041faf6372f6e87e /libbb | |
parent | 17eedcad9406c43beddab3906c8c693626c351fb (diff) | |
download | busybox-w32-59655077c5bf176f01d8d277665ebb92263704ed.tar.gz busybox-w32-59655077c5bf176f01d8d277665ebb92263704ed.tar.bz2 busybox-w32-59655077c5bf176f01d8d277665ebb92263704ed.zip |
preparatory cleanups for seamless uncompression improvements
unpack_gz_stream_with_info: fix buggy error check
man: fix possible accesses past the end of a string
move seamless uncompression helpers from read_printf.c to open_transformer.c
function old new delta
show_manpage 153 212 +59
unpack_gz_stream_with_info 520 539 +19
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/read_printf.c | 139 |
1 files changed, 0 insertions, 139 deletions
diff --git a/libbb/read_printf.c b/libbb/read_printf.c index 0bbf7802a..5ed6e3632 100644 --- a/libbb/read_printf.c +++ b/libbb/read_printf.c | |||
@@ -8,16 +8,6 @@ | |||
8 | */ | 8 | */ |
9 | #include "libbb.h" | 9 | #include "libbb.h" |
10 | 10 | ||
11 | #define ZIPPED (ENABLE_FEATURE_SEAMLESS_LZMA \ | ||
12 | || ENABLE_FEATURE_SEAMLESS_BZ2 \ | ||
13 | || ENABLE_FEATURE_SEAMLESS_GZ \ | ||
14 | /* || ENABLE_FEATURE_SEAMLESS_Z */ \ | ||
15 | ) | ||
16 | |||
17 | #if ZIPPED | ||
18 | # include "bb_archive.h" | ||
19 | #endif | ||
20 | |||
21 | 11 | ||
22 | /* Suppose that you are a shell. You start child processes. | 12 | /* Suppose that you are a shell. You start child processes. |
23 | * They work and eventually exit. You want to get user input. | 13 | * They work and eventually exit. You want to get user input. |
@@ -244,132 +234,3 @@ void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) | |||
244 | bb_perror_msg_and_die("can't read '%s'", filename); | 234 | bb_perror_msg_and_die("can't read '%s'", filename); |
245 | return buf; | 235 | return buf; |
246 | } | 236 | } |
247 | |||
248 | /* Used by e.g. rpm which gives us a fd without filename, | ||
249 | * thus we can't guess the format from filename's extension. | ||
250 | */ | ||
251 | #if ZIPPED | ||
252 | void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) | ||
253 | { | ||
254 | const int fail_if_not_detected = 1; | ||
255 | union { | ||
256 | uint8_t b[4]; | ||
257 | uint16_t b16[2]; | ||
258 | uint32_t b32[1]; | ||
259 | } magic; | ||
260 | int offset = -2; | ||
261 | # if BB_MMU | ||
262 | IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd); | ||
263 | enum { xformer_prog = 0 }; | ||
264 | # else | ||
265 | enum { xformer = 0 }; | ||
266 | const char *xformer_prog; | ||
267 | # endif | ||
268 | |||
269 | /* .gz and .bz2 both have 2-byte signature, and their | ||
270 | * unpack_XXX_stream wants this header skipped. */ | ||
271 | xread(fd, magic.b16, sizeof(magic.b16[0])); | ||
272 | if (ENABLE_FEATURE_SEAMLESS_GZ | ||
273 | && magic.b16[0] == GZIP_MAGIC | ||
274 | ) { | ||
275 | # if BB_MMU | ||
276 | xformer = unpack_gz_stream; | ||
277 | # else | ||
278 | xformer_prog = "gunzip"; | ||
279 | # endif | ||
280 | goto found_magic; | ||
281 | } | ||
282 | if (ENABLE_FEATURE_SEAMLESS_BZ2 | ||
283 | && magic.b16[0] == BZIP2_MAGIC | ||
284 | ) { | ||
285 | # if BB_MMU | ||
286 | xformer = unpack_bz2_stream; | ||
287 | # else | ||
288 | xformer_prog = "bunzip2"; | ||
289 | # endif | ||
290 | goto found_magic; | ||
291 | } | ||
292 | if (ENABLE_FEATURE_SEAMLESS_XZ | ||
293 | && magic.b16[0] == XZ_MAGIC1 | ||
294 | ) { | ||
295 | offset = -6; | ||
296 | xread(fd, magic.b32, sizeof(magic.b32[0])); | ||
297 | if (magic.b32[0] == XZ_MAGIC2) { | ||
298 | # if BB_MMU | ||
299 | xformer = unpack_xz_stream; | ||
300 | /* unpack_xz_stream wants fd at position 6, no need to seek */ | ||
301 | //xlseek(fd, offset, SEEK_CUR); | ||
302 | # else | ||
303 | xformer_prog = "unxz"; | ||
304 | # endif | ||
305 | goto found_magic; | ||
306 | } | ||
307 | } | ||
308 | |||
309 | /* No known magic seen */ | ||
310 | if (fail_if_not_detected) | ||
311 | bb_error_msg_and_die("no gzip" | ||
312 | IF_FEATURE_SEAMLESS_BZ2("/bzip2") | ||
313 | IF_FEATURE_SEAMLESS_XZ("/xz") | ||
314 | " magic"); | ||
315 | xlseek(fd, offset, SEEK_CUR); | ||
316 | return; | ||
317 | |||
318 | found_magic: | ||
319 | # if !BB_MMU | ||
320 | /* NOMMU version of open_transformer execs | ||
321 | * an external unzipper that wants | ||
322 | * file position at the start of the file */ | ||
323 | xlseek(fd, offset, SEEK_CUR); | ||
324 | # endif | ||
325 | open_transformer(fd, xformer, xformer_prog); | ||
326 | } | ||
327 | #endif /* ZIPPED */ | ||
328 | |||
329 | int FAST_FUNC open_zipped(const char *fname) | ||
330 | { | ||
331 | #if !ZIPPED | ||
332 | return open(fname, O_RDONLY); | ||
333 | #else | ||
334 | char *sfx; | ||
335 | int fd; | ||
336 | |||
337 | fd = open(fname, O_RDONLY); | ||
338 | if (fd < 0) | ||
339 | return fd; | ||
340 | |||
341 | sfx = strrchr(fname, '.'); | ||
342 | if (sfx) { | ||
343 | sfx++; | ||
344 | if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0) | ||
345 | /* .lzma has no header/signature, just trust it */ | ||
346 | open_transformer(fd, unpack_lzma_stream, "unlzma"); | ||
347 | else | ||
348 | if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0) | ||
349 | || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0) | ||
350 | || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0) | ||
351 | ) { | ||
352 | setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/); | ||
353 | } | ||
354 | } | ||
355 | |||
356 | return fd; | ||
357 | #endif | ||
358 | } | ||
359 | |||
360 | void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) | ||
361 | { | ||
362 | int fd; | ||
363 | char *image; | ||
364 | |||
365 | fd = open_zipped(fname); | ||
366 | if (fd < 0) | ||
367 | return NULL; | ||
368 | |||
369 | image = xmalloc_read(fd, maxsz_p); | ||
370 | if (!image) | ||
371 | bb_perror_msg("read error from '%s'", fname); | ||
372 | close(fd); | ||
373 | |||
374 | return image; | ||
375 | } | ||