blob: 13832c240e8dddf383ad4279126ec6413bf3f8ab [file] [log] [blame]
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001#include <stdlib.h>
2#include <unistd.h>
3#include "libbb.h"
4
5extern void check_header_gzip(int src_fd)
6{
7 union {
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +00008 unsigned char raw[8];
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00009 struct {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000010 unsigned char method;
11 unsigned char flags;
12 unsigned int mtime;
13 unsigned char xtra_flags;
14 unsigned char os_flags;
15 } formated;
16 } header;
17
Manuel Novoa III cad53642003-03-19 09:13:01 +000018 bb_xread_all(src_fd, header.raw, 8);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000019
Glenn L McGrath9c60b292002-11-03 10:57:25 +000020 /* Check the compression method */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000021 if (header.formated.method != 8) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000022 bb_error_msg_and_die("Unknown compression method %d",
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000023 header.formated.method);
24 }
25
26 if (header.formated.flags & 0x04) {
27 /* bit 2 set: extra field present */
28 unsigned char extra_short;
29
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 extra_short = bb_xread_char(src_fd) + (bb_xread_char(src_fd) << 8);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000031 while (extra_short > 0) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000032 /* Ignore extra field */
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 bb_xread_char(src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000034 extra_short--;
35 }
36 }
37
Glenn L McGrath9c60b292002-11-03 10:57:25 +000038 /* Discard original name if any */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000039 if (header.formated.flags & 0x08) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000040 /* bit 3 set: original file name present */
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 while(bb_xread_char(src_fd) != 0);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000042 }
43
Glenn L McGrath9c60b292002-11-03 10:57:25 +000044 /* Discard file comment if any */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000045 if (header.formated.flags & 0x10) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000046 /* bit 4 set: file comment present */
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 while(bb_xread_char(src_fd) != 0);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000048 }
49
Glenn L McGrath9c60b292002-11-03 10:57:25 +000050 /* Read the header checksum */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000051 if (header.formated.flags & 0x02) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 bb_xread_char(src_fd);
53 bb_xread_char(src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000054 }
55
56 return;
57}