"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 4 | */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <unistd.h> |
| 7 | #include "libbb.h" |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 8 | #include "unarchive.h" /* for external decl of check_header_gzip */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 9 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 10 | void check_header_gzip(int src_fd) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 11 | { |
| 12 | union { |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 13 | unsigned char raw[8]; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 14 | struct { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 15 | unsigned char method; |
| 16 | unsigned char flags; |
| 17 | unsigned int mtime; |
| 18 | unsigned char xtra_flags; |
| 19 | unsigned char os_flags; |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 20 | } formatted; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 21 | } header; |
| 22 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 23 | xread(src_fd, header.raw, 8); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 24 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 25 | /* Check the compression method */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 26 | if (header.formatted.method != 8) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | bb_error_msg_and_die("Unknown compression method %d", |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 28 | header.formatted.method); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 29 | } |
| 30 | |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 31 | if (header.formatted.flags & 0x04) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 32 | /* bit 2 set: extra field present */ |
| 33 | unsigned char extra_short; |
| 34 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 35 | extra_short = xread_char(src_fd) + (xread_char(src_fd) << 8); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 36 | while (extra_short > 0) { |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 37 | /* Ignore extra field */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 38 | xread_char(src_fd); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 39 | extra_short--; |
| 40 | } |
| 41 | } |
| 42 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 43 | /* Discard original name if any */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 44 | if (header.formatted.flags & 0x08) { |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 45 | /* bit 3 set: original file name present */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 46 | while(xread_char(src_fd) != 0); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 49 | /* Discard file comment if any */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 50 | if (header.formatted.flags & 0x10) { |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 51 | /* bit 4 set: file comment present */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 52 | while(xread_char(src_fd) != 0); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 55 | /* Read the header checksum */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 56 | if (header.formatted.flags & 0x02) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 57 | xread_char(src_fd); |
| 58 | xread_char(src_fd); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return; |
| 62 | } |