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