blob: 67ed631e1c5c82c0772e23985f09652ac5081d9c [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00002#include <stdlib.h>
3#include <unistd.h>
4#include "libbb.h"
Eric Andersen14f5c8d2005-04-16 19:39:00 +00005#include "unarchive.h" /* for external decl of check_header_gzip */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00006
Rob Landleydfba7412006-03-06 20:47:33 +00007void check_header_gzip(int src_fd)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00008{
9 union {
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000010 unsigned char raw[8];
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000011 struct {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000012 unsigned char method;
13 unsigned char flags;
14 unsigned int mtime;
15 unsigned char xtra_flags;
16 unsigned char os_flags;
17 } formated;
18 } header;
19
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 bb_xread_all(src_fd, header.raw, 8);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000021
Glenn L McGrath9c60b292002-11-03 10:57:25 +000022 /* Check the compression method */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000023 if (header.formated.method != 8) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000024 bb_error_msg_and_die("Unknown compression method %d",
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000025 header.formated.method);
26 }
27
28 if (header.formated.flags & 0x04) {
29 /* bit 2 set: extra field present */
30 unsigned char extra_short;
31
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 extra_short = bb_xread_char(src_fd) + (bb_xread_char(src_fd) << 8);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000033 while (extra_short > 0) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000034 /* Ignore extra field */
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 bb_xread_char(src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000036 extra_short--;
37 }
38 }
39
Glenn L McGrath9c60b292002-11-03 10:57:25 +000040 /* Discard original name if any */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000041 if (header.formated.flags & 0x08) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000042 /* bit 3 set: original file name present */
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 while(bb_xread_char(src_fd) != 0);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000044 }
45
Glenn L McGrath9c60b292002-11-03 10:57:25 +000046 /* Discard file comment if any */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000047 if (header.formated.flags & 0x10) {
Glenn L McGrath9c60b292002-11-03 10:57:25 +000048 /* bit 4 set: file comment present */
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 while(bb_xread_char(src_fd) != 0);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000050 }
51
Glenn L McGrath9c60b292002-11-03 10:57:25 +000052 /* Read the header checksum */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000053 if (header.formated.flags & 0x02) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 bb_xread_char(src_fd);
55 bb_xread_char(src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000056 }
57
58 return;
59}