Patch from Konstantin Isakov <ikm@pisem.net>:
[oweals/busybox.git] / archival / libunarchive / check_trailer_gzip.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  busybox gunzip trailing header handler
4  *  Copyright Glenn McGrath 2002
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <signal.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include "config.h"
30 #include "busybox.h"
31 #include "unarchive.h"
32
33 extern unsigned int gunzip_crc;
34 extern unsigned int gunzip_bytes_out;
35 extern unsigned char *gunzip_in_buffer;
36 extern unsigned char gunzip_in_buffer_count;
37
38 extern void check_trailer_gzip(int src_fd)
39 {
40
41         unsigned int stored_crc = 0;
42         unsigned char count;
43
44         /* top up the input buffer with the rest of the trailer */
45         xread_all(src_fd, &gunzip_in_buffer[gunzip_in_buffer_count], 8 - gunzip_in_buffer_count);
46         for (count = 0; count != 4; count++) {
47                 stored_crc |= (gunzip_in_buffer[count] << (count * 8));
48         }
49
50         /* Validate decompression - crc */
51         if (stored_crc != (gunzip_crc ^ 0xffffffffL)) {
52                 error_msg("invalid compressed data--crc error");
53         }
54
55         /* Validate decompression - size */
56         if (gunzip_bytes_out != 
57                 (gunzip_in_buffer[4] | (gunzip_in_buffer[5] << 8) |
58                 (gunzip_in_buffer[6] << 16) | (gunzip_in_buffer[7] << 24))) {
59                 error_msg("invalid compressed data--length error");
60         }
61
62 }