This source file includes following definitions.
- check_zipfile
- unzip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #ifndef lint
17 static char rcsid[] = "$Id: unzip.c,v 0.9 1993/02/10 16:07:22 jloup Exp $";
18 #endif
19
20
21 #include "gzip.h"
22 #include "crypt.h"
23
24 #include <stdio.h>
25
26
27 #define LOCSIG 0x04034b50L
28 #define LOCFLG 6
29 #define CRPFLG 1
30 #define EXTFLG 8
31 #define LOCHOW 8
32 #define LOCTIM 10
33 #define LOCCRC 14
34 #define LOCSIZ 18
35 #define LOCLEN 22
36 #define LOCFIL 26
37 #define LOCEXT 28
38 #define LOCHDR 30
39 #define EXTHDR 16
40
41
42
43
44 int decrypt;
45 char *key;
46 int pkzip = 0;
47 int extended = 0;
48
49
50
51
52
53 int check_zipfile(in)
54 int in;
55 {
56 uch *h = inbuf + inptr;
57
58
59
60
61 inptr += LOCHDR + SH(h + LOCFIL) + SH(h + LOCEXT);
62
63 if (inptr > insize || LG(h) != LOCSIG) {
64 error("input not a zip file or empty");
65 }
66 method = h[LOCHOW];
67 if (method != STORED && method != DEFLATED) {
68 error("first entry not deflated or stored--can't extract");
69 }
70
71
72 if ((decrypt = h[LOCFLG] & CRPFLG) != 0) {
73 error("encrypted file, not yet supported.\n");
74 exit_code = ERROR;
75 return -1;
76 }
77
78
79 extended = (h[LOCFLG] & EXTFLG) != 0;
80 pkzip = 1;
81
82
83 return 0;
84 }
85
86
87
88
89
90
91
92
93 void unzip(in, out)
94 int in, out;
95 {
96 ulg orig_crc = 0;
97 ulg orig_len = 0;
98 int n;
99 uch buf[EXTHDR];
100
101
102
103
104 updcrc(NULL, 0);
105
106 if (pkzip && !extended) {
107 orig_crc = LG(inbuf + LOCCRC);
108 orig_len = LG(inbuf + LOCLEN);
109 }
110
111
112 if (method == DEFLATED) {
113
114 int res = inflate();
115
116 if (res == 3) {
117 error("out of memory");
118 } else if (res != 0) {
119 error("invalid compressed data--format violated");
120 }
121
122 } else if (pkzip && method == STORED) {
123
124 register ulg n = LG(inbuf + LOCLEN);
125
126 if (n != LG(inbuf + LOCSIZ) - (decrypt ? RAND_HEAD_LEN : 0)) {
127
128 error("invalid compressed data--length mismatch");
129 }
130 while (n--) {
131 uch c = (uch)get_byte();
132 #ifdef CRYPT
133 if (decrypt) zdecode(c);
134 #endif
135 if (!test) put_char(c);
136 }
137 } else {
138 error("internal error, invalid method");
139 }
140
141
142 if (!pkzip) {
143
144
145
146 for (n = 0; n < 8; n++) {
147 buf[n] = (uch)get_byte();
148 }
149 orig_crc = LG(buf);
150 orig_len = LG(buf+4);
151
152 } else if (extended) {
153
154
155
156
157
158 for (n = 0; n < EXTHDR; n++) {
159 buf[n] = (uch)get_byte();
160 }
161 orig_crc = LG(buf+4);
162 orig_len = LG(buf+12);
163 }
164
165
166 if (orig_crc != updcrc(outbuf, 0)) {
167 error("invalid compressed data--crc error");
168 }
169 if (orig_len != bytes_out) {
170 error("invalid compressed data--length error");
171 }
172
173
174 if (pkzip && inptr + 4 < insize && LG(inbuf+inptr) == LOCSIG) {
175 error("zip file has more than one entry");
176 }
177 extended = pkzip = 0;
178 }