"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 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 6 | #include "libbb.h" |
| 7 | #include "unarchive.h" /* for external decl of check_header_gzip_or_die */ |
| 8 | |
| 9 | void check_header_gzip_or_die(int src_fd) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 10 | { |
| 11 | union { |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 12 | unsigned char raw[8]; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 13 | struct { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 14 | unsigned char method; |
| 15 | unsigned char flags; |
| 16 | unsigned int mtime; |
| 17 | unsigned char xtra_flags; |
| 18 | unsigned char os_flags; |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 19 | } formatted; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 20 | } header; |
| 21 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 22 | xread(src_fd, header.raw, 8); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 23 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 24 | /* Check the compression method */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 25 | if (header.formatted.method != 8) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 26 | bb_error_msg_and_die("unknown compression method %d", |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 27 | header.formatted.method); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 28 | } |
| 29 | |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 30 | if (header.formatted.flags & 0x04) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 31 | /* bit 2 set: extra field present */ |
Denis Vlasenko | 3ce293b | 2007-03-15 23:30:18 +0000 | [diff] [blame] | 32 | unsigned extra_short; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 33 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 34 | extra_short = xread_char(src_fd) + (xread_char(src_fd) << 8); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 35 | while (extra_short > 0) { |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 36 | /* Ignore extra field */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 37 | xread_char(src_fd); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 38 | extra_short--; |
| 39 | } |
| 40 | } |
| 41 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 42 | /* Discard original name if any */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 43 | if (header.formatted.flags & 0x08) { |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 44 | /* bit 3 set: original file name present */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 45 | while (xread_char(src_fd) != 0); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 48 | /* Discard file comment if any */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 49 | if (header.formatted.flags & 0x10) { |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 50 | /* bit 4 set: file comment present */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 51 | while (xread_char(src_fd) != 0); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 54 | /* Read the header checksum */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 55 | if (header.formatted.flags & 0x02) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 56 | xread_char(src_fd); |
| 57 | xread_char(src_fd); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 58 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 59 | } |