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