]> gcc.gnu.org Git - gcc.git/blob - gcc/java/zextract.c
buffer.h: PROTO -> PARAMS.
[gcc.git] / gcc / java / zextract.c
1 /* Handle a .class file embedded in a .zip archive.
2 This extracts a member from a .zip file, but does not handle
3 uncompression (since that is not needed for classes.zip).
4
5 Copyright (C) 1996, 97-99, 2000 Free Software Foundation, Inc.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25
26 /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
27
28 #include "config.h"
29 #include "system.h"
30 #include "zipfile.h"
31
32 /* This stuff is partly based on the 28 August 1994 public release of the
33 Info-ZIP group's portable UnZip zipfile-extraction program (and related
34 utilities). */
35
36 /*************/
37 /* Defines */
38 /*************/
39
40 #define UNZIP
41 #define UNZIP_VERSION 20 /* compatible with PKUNZIP 2.0 */
42 #define VMS_UNZIP_VERSION 42 /* if OS-needed-to-extract is VMS: can do */
43
44
45 #define ZSUFX ".zip"
46 #define CENTRAL_HDR_SIG "\113\001\002" /* the infamous "PK" signature */
47 #define LOCAL_HDR_SIG "\113\003\004" /* bytes, sans "P" (so unzip */
48 #define END_CENTRAL_SIG "\113\005\006" /* executable not mistaken for */
49 #define EXTD_LOCAL_SIG "\113\007\010" /* zipfile itself) */
50
51 #define STORED 0 /* compression methods */
52 #define SHRUNK 1
53 #define REDUCED1 2
54 #define REDUCED2 3
55 #define REDUCED3 4
56 #define REDUCED4 5
57 #define IMPLODED 6
58 #define TOKENIZED 7
59 #define DEFLATED 8
60 #define NUM_METHODS 9 /* index of last method + 1 */
61 /* don't forget to update list_files() appropriately if NUM_METHODS changes */
62
63 #define PK_OK 0 /* no error */
64 #define PK_COOL 0 /* no error */
65 #define PK_GNARLY 0 /* no error */
66 #define PK_WARN 1 /* warning error */
67 #define PK_ERR 2 /* error in zipfile */
68 #define PK_BADERR 3 /* severe error in zipfile */
69 #define PK_MEM 4 /* insufficient memory */
70 #define PK_MEM2 5 /* insufficient memory */
71 #define PK_MEM3 6 /* insufficient memory */
72 #define PK_MEM4 7 /* insufficient memory */
73 #define PK_MEM5 8 /* insufficient memory */
74 #define PK_NOZIP 9 /* zipfile not found */
75 #define PK_PARAM 10 /* bad or illegal parameters specified */
76 #define PK_FIND 11 /* no files found */
77 #define PK_DISK 50 /* disk full */
78 #define PK_EOF 51 /* unexpected EOF */
79
80 /*---------------------------------------------------------------------------
81 True sizes of the various headers, as defined by PKWARE--so it is not
82 likely that these will ever change. But if they do, make sure both these
83 defines AND the typedefs below get updated accordingly.
84 ---------------------------------------------------------------------------*/
85 #define LREC_SIZE 26 /* lengths of local file headers, central */
86 #define CREC_SIZE 42 /* directory headers, and the end-of- */
87 #define ECREC_SIZE 18 /* central-dir record, respectively */
88
89
90 #ifndef SEEK_SET
91 # define SEEK_SET 0
92 # define SEEK_CUR 1
93 # define SEEK_END 2
94 #endif
95
96 /**************/
97 /* Typedefs */
98 /**************/
99
100 typedef char boolean;
101 typedef unsigned char uch; /* code assumes unsigned bytes; these type- */
102 typedef unsigned short ush; /* defs replace byte/UWORD/ULONG (which are */
103 typedef unsigned long ulg; /* predefined on some systems) & match zip */
104
105 /*---------------------------------------------------------------------------
106 Zipfile layout declarations. If these headers ever change, make sure the
107 xxREC_SIZE defines (above) change with them!
108 ---------------------------------------------------------------------------*/
109
110 typedef uch local_byte_hdr[ LREC_SIZE ];
111 # define L_VERSION_NEEDED_TO_EXTRACT_0 0
112 # define L_VERSION_NEEDED_TO_EXTRACT_1 1
113 # define L_GENERAL_PURPOSE_BIT_FLAG 2
114 # define L_COMPRESSION_METHOD 4
115 # define L_LAST_MOD_FILE_TIME 6
116 # define L_LAST_MOD_FILE_DATE 8
117 # define L_CRC32 10
118 # define L_COMPRESSED_SIZE 14
119 # define L_UNCOMPRESSED_SIZE 18
120 # define L_FILENAME_LENGTH 22
121 # define L_EXTRA_FIELD_LENGTH 24
122
123 typedef uch cdir_byte_hdr[ CREC_SIZE ];
124 # define C_VERSION_MADE_BY_0 0
125 # define C_VERSION_MADE_BY_1 1
126 # define C_VERSION_NEEDED_TO_EXTRACT_0 2
127 # define C_VERSION_NEEDED_TO_EXTRACT_1 3
128 # define C_GENERAL_PURPOSE_BIT_FLAG 4
129 # define C_COMPRESSION_METHOD 6
130 # define C_LAST_MOD_FILE_TIME 8
131 # define C_LAST_MOD_FILE_DATE 10
132 # define C_CRC32 12
133 # define C_COMPRESSED_SIZE 16
134 # define C_UNCOMPRESSED_SIZE 20
135 # define C_FILENAME_LENGTH 24
136 # define C_EXTRA_FIELD_LENGTH 26
137 # define C_FILE_COMMENT_LENGTH 28
138 # define C_DISK_NUMBER_START 30
139 # define C_INTERNAL_FILE_ATTRIBUTES 32
140 # define C_EXTERNAL_FILE_ATTRIBUTES 34
141 # define C_RELATIVE_OFFSET_LOCAL_HEADER 38
142
143 typedef uch ec_byte_rec[ ECREC_SIZE+4 ];
144 /* define SIGNATURE 0 space-holder only */
145 # define NUMBER_THIS_DISK 4
146 # define NUM_DISK_WITH_START_CENTRAL_DIR 6
147 # define NUM_ENTRIES_CENTRL_DIR_THS_DISK 8
148 # define TOTAL_ENTRIES_CENTRAL_DIR 10
149 # define SIZE_CENTRAL_DIRECTORY 12
150 # define OFFSET_START_CENTRAL_DIRECTORY 16
151 # define ZIPFILE_COMMENT_LENGTH 20
152
153
154 typedef struct local_file_header { /* LOCAL */
155 uch version_needed_to_extract[2];
156 ush general_purpose_bit_flag;
157 ush compression_method;
158 ush last_mod_file_time;
159 ush last_mod_file_date;
160 ulg crc32;
161 ulg csize;
162 ulg ucsize;
163 ush filename_length;
164 ush extra_field_length;
165 } local_file_hdr;
166
167 typedef struct central_directory_file_header { /* CENTRAL */
168 uch version_made_by[2];
169 uch version_needed_to_extract[2];
170 ush general_purpose_bit_flag;
171 ush compression_method;
172 ush last_mod_file_time;
173 ush last_mod_file_date;
174 ulg crc32;
175 ulg csize;
176 ulg ucsize;
177 ush filename_length;
178 ush extra_field_length;
179 ush file_comment_length;
180 ush disk_number_start;
181 ush internal_file_attributes;
182 ulg external_file_attributes;
183 ulg relative_offset_local_header;
184 } cdir_file_hdr;
185
186 typedef struct end_central_dir_record { /* END CENTRAL */
187 ush number_this_disk;
188 ush num_disk_with_start_central_dir;
189 ush num_entries_centrl_dir_ths_disk;
190 ush total_entries_central_dir;
191 ulg size_central_directory;
192 ulg offset_start_central_directory;
193 ush zipfile_comment_length;
194 } ecdir_rec;
195
196
197 /************/
198 /* Macros */
199 /************/
200
201 #ifndef MAX
202 # define MAX(a,b) ((a) > (b) ? (a) : (b))
203 #endif
204 #ifndef MIN
205 # define MIN(a,b) ((a) < (b) ? (a) : (b))
206 #endif
207
208
209 /***********************/
210 /* Prototypes */
211 /***********************/
212
213 static ush makeword PARAMS ((const uch *));
214 static ulg makelong PARAMS ((const uch *));
215
216 /***********************/
217 /* Function makeword() */
218 /***********************/
219
220 static ush makeword(b)
221 const uch *b;
222 {
223 /*
224 * Convert Intel style 'short' integer to non-Intel non-16-bit
225 * host format. This routine also takes care of byte-ordering.
226 */
227 return (ush)((b[1] << 8) | b[0]);
228 }
229
230
231 /***********************/
232 /* Function makelong() */
233 /***********************/
234
235 static ulg makelong(sig)
236 const uch *sig;
237 {
238 /*
239 * Convert intel style 'long' variable to non-Intel non-16-bit
240 * host format. This routine also takes care of byte-ordering.
241 */
242 return (((ulg)sig[3]) << 24)
243 + (((ulg)sig[2]) << 16)
244 + (((ulg)sig[1]) << 8)
245 + ((ulg)sig[0]);
246 }
247
248 int
249 read_zip_archive (zipf)
250 register ZipFile *zipf;
251 {
252 int i;
253 int dir_last_pad;
254 char *dir_ptr;
255 char buffer[100];
256
257 zipf->size = lseek (zipf->fd, 0L, SEEK_END);
258
259 if (zipf->size < (ECREC_SIZE+4) || lseek (zipf->fd, (long)(-(ECREC_SIZE+4)), SEEK_CUR) <= 0)
260 return -1;
261 if (read (zipf->fd, buffer, ECREC_SIZE+4) != ECREC_SIZE+4)
262 return -2;
263 zipf->count = makeword(&buffer[TOTAL_ENTRIES_CENTRAL_DIR]);
264 zipf->dir_size = makelong(&buffer[SIZE_CENTRAL_DIRECTORY]);
265 #define ALLOC xmalloc
266 /* Allocate 1 more to allow appending '\0' to last filename. */
267 zipf->central_directory = ALLOC (zipf->dir_size+1);
268 if (lseek (zipf->fd, -(zipf->dir_size+ECREC_SIZE+4), SEEK_CUR) < 0)
269 return -2;
270 if (read (zipf->fd, zipf->central_directory, zipf->dir_size) < 0)
271 return -2;
272
273 #ifdef TEST
274 printf ("number_this_disk = %d\n", makeword(&buffer[NUMBER_THIS_DISK]));
275 printf ("num_disk_with_start_central_dir = %d\n", makeword(&buffer[NUM_DISK_WITH_START_CENTRAL_DIR]));
276
277 printf ("num_entries_centrl_dir_ths_disk = %d\n",
278 makeword(&buffer[NUM_ENTRIES_CENTRL_DIR_THS_DISK]));
279 printf ("total_entries_central_dir = %d\n",
280 makeword(&buffer[TOTAL_ENTRIES_CENTRAL_DIR]));
281 printf ("size_central_directory = %d\n",
282 makelong(&buffer[SIZE_CENTRAL_DIRECTORY]));
283 printf ("offset_start_central_directory = %d\n",
284 makelong(&buffer[OFFSET_START_CENTRAL_DIRECTORY]));
285 printf ("zipfile_comment_length = %d\n",
286 makeword(&buffer[ZIPFILE_COMMENT_LENGTH]));
287 #endif
288
289 dir_last_pad = 0;
290 dir_ptr = zipf->central_directory;
291 for (i = 0; i < zipf->count; i++)
292 {
293 ZipDirectory *zipd = (ZipDirectory*)(dir_ptr + dir_last_pad);
294 long uncompressed_size = makelong (&dir_ptr[4+C_UNCOMPRESSED_SIZE]);
295 long filename_length = makeword (&dir_ptr[4+C_FILENAME_LENGTH]);
296 long extra_field_length = makeword (&dir_ptr[4+C_EXTRA_FIELD_LENGTH]);
297 long file_comment_length = makeword (&dir_ptr[4+C_FILE_COMMENT_LENGTH]);
298 int unpadded_direntry_length;
299 if ((dir_ptr-zipf->central_directory)+filename_length+CREC_SIZE+4>zipf->dir_size)
300 return -1;
301
302 zipd->filename_length = filename_length;
303 zipd->size = uncompressed_size;
304 #ifdef __GNUC__
305 #define DIR_ALIGN __alignof__(ZipDirectory)
306 #else
307 #define DIR_ALIGN sizeof(long)
308 #endif
309 zipd->filestart = makelong (&dir_ptr[4+C_RELATIVE_OFFSET_LOCAL_HEADER])
310 + (LREC_SIZE+4) + filename_length + file_comment_length +
311 + (extra_field_length ? extra_field_length+4 : 0);
312 /* About the last term of the expression above. Should the same
313 apply if file_comment_length is not zero ? I've never seen
314 the comment field uses so far. FIXME. */
315 zipd->filename_offset = CREC_SIZE+4 - dir_last_pad;
316 unpadded_direntry_length
317 = zipd->filename_offset + zipd->filename_length + extra_field_length;
318 zipd->direntry_size =
319 ((unpadded_direntry_length + DIR_ALIGN) / DIR_ALIGN) * DIR_ALIGN;
320 dir_last_pad = zipd->direntry_size - unpadded_direntry_length;
321 dir_ptr = (char*)zipd + unpadded_direntry_length;
322 *dir_ptr = '\0';
323 }
324 return 0;
325 }
326
327 #ifdef TEST
328 main ()
329 {
330 ZipFile zipf[1];
331 ZipDirectory *zipd;
332 int i;
333
334 zipf->fd = 0;
335
336 i = read_zip_archive (zipf);
337 if (i)
338 {
339 fprintf (stderr, "Bad zip file.\n");
340 exit (i);
341 }
342
343 zipd = (ZipDirectory*) zipf->central_directory;
344 for (i = 0; i < zipf->count; i++, zipd = ZIPDIR_NEXT (zipd))
345 {
346 printf ("%d: size:%d, name(#%d)%s, offset:%d\n",
347 i, zipd->size, zipd->filename_length,
348 ZIPDIR_FILENAME (zipd),
349 zipd->filestart);
350 }
351 }
352 #endif
This page took 0.050742 seconds and 5 git commands to generate.