1/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
6#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)
10{
11	union {
12		unsigned char raw[8];
13		struct {
14			unsigned char method;
15			unsigned char flags;
16			unsigned int mtime;
17			unsigned char xtra_flags;
18			unsigned char os_flags;
19		} formatted;
20	} header;
21
22	xread(src_fd, header.raw, 8);
23
24	/* Check the compression method */
25	if (header.formatted.method != 8) {
26		bb_error_msg_and_die("unknown compression method %d",
27						  header.formatted.method);
28	}
29
30	if (header.formatted.flags & 0x04) {
31		/* bit 2 set: extra field present */
32		unsigned extra_short;
33
34		extra_short = xread_char(src_fd) + (xread_char(src_fd) << 8);
35		while (extra_short > 0) {
36			/* Ignore extra field */
37			xread_char(src_fd);
38			extra_short--;
39		}
40	}
41
42	/* Discard original name if any */
43	if (header.formatted.flags & 0x08) {
44		/* bit 3 set: original file name present */
45		while (xread_char(src_fd) != 0);
46	}
47
48	/* Discard file comment if any */
49	if (header.formatted.flags & 0x10) {
50		/* bit 4 set: file comment present */
51		while (xread_char(src_fd) != 0);
52	}
53
54	/* Read the header checksum */
55	if (header.formatted.flags & 0x02) {
56		xread_char(src_fd);
57		xread_char(src_fd);
58	}
59}
60