blob: 66aa5746012e5ca51a8662ff0d91b815259b536d [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00002/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00005
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00006#include "libbb.h"
7#include "unarchive.h" /* for external decl of check_header_gzip_or_die */
8
9void check_header_gzip_or_die(int src_fd)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000010{
11 union {
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000012 unsigned char raw[8];
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000013 struct {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000014 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"eea56182006-07-20 19:02:24 +000019 } formatted;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000020 } header;
21
Rob Landley53437472006-07-16 08:14:35 +000022 xread(src_fd, header.raw, 8);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000023
Glenn L McGrath9c60b292002-11-03 10:57:25 +000024 /* Check the compression method */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000025 if (header.formatted.method != 8) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000026 bb_error_msg_and_die("unknown compression method %d",
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000027 header.formatted.method);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000028 }
29
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000030 if (header.formatted.flags & 0x04) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000031 /* bit 2 set: extra field present */
Denis Vlasenko3ce293b2007-03-15 23:30:18 +000032 unsigned extra_short;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000033
Rob Landley53437472006-07-16 08:14:35 +000034 extra_short = xread_char(src_fd) + (xread_char(src_fd) << 8);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000035 while (extra_short > 0) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000036 /* Ignore extra field */
Rob Landley53437472006-07-16 08:14:35 +000037 xread_char(src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000038 extra_short--;
39 }
40 }
41
Glenn L McGrath9c60b292002-11-03 10:57:25 +000042 /* Discard original name if any */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000043 if (header.formatted.flags & 0x08) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000044 /* bit 3 set: original file name present */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +000045 while (xread_char(src_fd) != 0);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000046 }
47
Glenn L McGrath9c60b292002-11-03 10:57:25 +000048 /* Discard file comment if any */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000049 if (header.formatted.flags & 0x10) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000050 /* bit 4 set: file comment present */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +000051 while (xread_char(src_fd) != 0);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000052 }
53
Glenn L McGrath9c60b292002-11-03 10:57:25 +000054 /* Read the header checksum */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000055 if (header.formatted.flags & 0x02) {
Rob Landley53437472006-07-16 08:14:35 +000056 xread_char(src_fd);
57 xread_char(src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000058 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000059}