Add one-line GPL boilerplate to numerous (but not all yet) source files.
[oweals/busybox.git] / archival / libunarchive / check_header_gzip.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4  */
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include "libbb.h"
8 #include "unarchive.h" /* for external decl of check_header_gzip */
9
10 void check_header_gzip(int src_fd)
11 {
12         union {
13                 unsigned char raw[8];
14                 struct {
15                         unsigned char method;
16                         unsigned char flags;
17                         unsigned int mtime;
18                         unsigned char xtra_flags;
19                         unsigned char os_flags;
20                 } formated;
21         } header;
22
23         bb_xread_all(src_fd, header.raw, 8);
24
25         /* Check the compression method */
26         if (header.formated.method != 8) {
27                 bb_error_msg_and_die("Unknown compression method %d",
28                                                   header.formated.method);
29         }
30
31         if (header.formated.flags & 0x04) {
32                 /* bit 2 set: extra field present */
33                 unsigned char extra_short;
34
35                 extra_short = bb_xread_char(src_fd) + (bb_xread_char(src_fd) << 8);
36                 while (extra_short > 0) {
37                         /* Ignore extra field */
38                         bb_xread_char(src_fd);
39                         extra_short--;
40                 }
41         }
42
43         /* Discard original name if any */
44         if (header.formated.flags & 0x08) {
45                 /* bit 3 set: original file name present */
46                 while(bb_xread_char(src_fd) != 0);
47         }
48
49         /* Discard file comment if any */
50         if (header.formated.flags & 0x10) {
51                 /* bit 4 set: file comment present */
52                 while(bb_xread_char(src_fd) != 0);
53         }
54
55         /* Read the header checksum */
56         if (header.formated.flags & 0x02) {
57                 bb_xread_char(src_fd);
58                 bb_xread_char(src_fd);
59         }
60
61         return;
62 }