9e065c4fde5efde3a244e01bffdc1c2ad921e919
[oweals/busybox.git] / archival / libunarchive / check_header_gzip.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include "libbb.h"
4 #include "unarchive.h" /* for external decl of check_header_gzip */
5
6 extern void check_header_gzip(int src_fd)
7 {
8         union {
9                 unsigned char raw[8];
10                 struct {
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
19         bb_xread_all(src_fd, header.raw, 8);
20
21         /* Check the compression method */
22         if (header.formated.method != 8) {
23                 bb_error_msg_and_die("Unknown compression method %d",
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
31                 extra_short = bb_xread_char(src_fd) + (bb_xread_char(src_fd) << 8);
32                 while (extra_short > 0) {
33                         /* Ignore extra field */
34                         bb_xread_char(src_fd);
35                         extra_short--;
36                 }
37         }
38
39         /* Discard original name if any */
40         if (header.formated.flags & 0x08) {
41                 /* bit 3 set: original file name present */
42                 while(bb_xread_char(src_fd) != 0);
43         }
44
45         /* Discard file comment if any */
46         if (header.formated.flags & 0x10) {
47                 /* bit 4 set: file comment present */
48                 while(bb_xread_char(src_fd) != 0);
49         }
50
51         /* Read the header checksum */
52         if (header.formated.flags & 0x02) {
53                 bb_xread_char(src_fd);
54                 bb_xread_char(src_fd);
55         }
56
57         return;
58 }