blob: 9e065c4fde5efde3a244e01bffdc1c2ad921e919 [file] [log] [blame]
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001#include <stdlib.h>
2#include <unistd.h>
3#include "libbb.h"
Eric Andersen14f5c8d2005-04-16 19:39:00 +00004#include "unarchive.h" /* for external decl of check_header_gzip */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00005
6extern void check_header_gzip(int src_fd)
7{
8 union {
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +00009 unsigned char raw[8];
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000010 struct {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000011 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
Manuel Novoa III cad53642003-03-19 09:13:01 +000019 bb_xread_all(src_fd, header.raw, 8);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000020
Glenn L McGrath9c60b292002-11-03 10:57:25 +000021 /* Check the compression method */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000022 if (header.formated.method != 8) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 bb_error_msg_and_die("Unknown compression method %d",
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000024 header.formated.method);
25 }
26
27 if (header.formated.flags & 0x04) {
28 /* bit 2 set: extra field present */
29 unsigned char extra_short;
30
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 extra_short = bb_xread_char(src_fd) + (bb_xread_char(src_fd) << 8);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000032 while (extra_short > 0) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000033 /* Ignore extra field */
Manuel Novoa III cad53642003-03-19 09:13:01 +000034 bb_xread_char(src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000035 extra_short--;
36 }
37 }
38
Glenn L McGrath9c60b292002-11-03 10:57:25 +000039 /* Discard original name if any */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000040 if (header.formated.flags & 0x08) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000041 /* bit 3 set: original file name present */
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 while(bb_xread_char(src_fd) != 0);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000043 }
44
Glenn L McGrath9c60b292002-11-03 10:57:25 +000045 /* Discard file comment if any */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000046 if (header.formated.flags & 0x10) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000047 /* bit 4 set: file comment present */
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 while(bb_xread_char(src_fd) != 0);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000049 }
50
Glenn L McGrath9c60b292002-11-03 10:57:25 +000051 /* Read the header checksum */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000052 if (header.formated.flags & 0x02) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 bb_xread_char(src_fd);
54 bb_xread_char(src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000055 }
56
57 return;
58}