Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <unistd.h> |
| 3 | #include "libbb.h" |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 4 | #include "unarchive.h" /* for external decl of check_header_gzip */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 5 | |
| 6 | extern void check_header_gzip(int src_fd) |
| 7 | { |
| 8 | union { |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 9 | unsigned char raw[8]; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 10 | struct { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 11 | unsigned char method; |
| 12 | unsigned char flags; |
| 13 | unsigned int mtime; |
| 14 | unsigned char xtra_flags; |
| 15 | unsigned char os_flags; |
| 16 | } formated; |
| 17 | } header; |
| 18 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | bb_xread_all(src_fd, header.raw, 8); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 20 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 21 | /* Check the compression method */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 22 | if (header.formated.method != 8) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | bb_error_msg_and_die("Unknown compression method %d", |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 24 | header.formated.method); |
| 25 | } |
| 26 | |
| 27 | if (header.formated.flags & 0x04) { |
| 28 | /* bit 2 set: extra field present */ |
| 29 | unsigned char extra_short; |
| 30 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 31 | extra_short = bb_xread_char(src_fd) + (bb_xread_char(src_fd) << 8); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 32 | while (extra_short > 0) { |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 33 | /* Ignore extra field */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | bb_xread_char(src_fd); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 35 | extra_short--; |
| 36 | } |
| 37 | } |
| 38 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 39 | /* Discard original name if any */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 40 | if (header.formated.flags & 0x08) { |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 41 | /* bit 3 set: original file name present */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | while(bb_xread_char(src_fd) != 0); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 45 | /* Discard file comment if any */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 46 | if (header.formated.flags & 0x10) { |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 47 | /* bit 4 set: file comment present */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | while(bb_xread_char(src_fd) != 0); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Glenn L McGrath | 9c60b29 | 2002-11-03 10:57:25 +0000 | [diff] [blame] | 51 | /* Read the header checksum */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 52 | if (header.formated.flags & 0x02) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | bb_xread_char(src_fd); |
| 54 | bb_xread_char(src_fd); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return; |
| 58 | } |