OSDN Git Service

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