Patch from Konstantin Isakov <ikm@pisem.net>:
[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         xread_all(src_fd, header.raw, 8);
19
20    /* Check the compression method */
21         if (header.formated.method != 8) {
22                 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 = xread_char(src_fd);
31                 extra_short += xread_char(src_fd) << 8;
32                 while (extra_short > 0) {
33                    /* Ignore extra field */
34                         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                 char tmp;
43
44                 do {
45                         read(src_fd, &tmp, 1);
46                 } while (tmp != 0);
47         }
48
49    /* Discard file comment if any */
50         if (header.formated.flags & 0x10) {
51            /* bit 4 set: file comment present */
52                 char tmp;
53
54                 do {
55                         read(src_fd, &tmp, 1);
56                 } while (tmp != 0);
57         }
58
59    /* Read the header checksum */
60         if (header.formated.flags & 0x02) {
61                 char tmp;
62
63                 read(src_fd, &tmp, 1);
64                 read(src_fd, &tmp, 1);
65         }
66
67         return;
68 }