source: trunk/icedtea-web/launcher/parse_manifest.c@ 417

Last change on this file since 417 was 348, checked in by dmik, 13 years ago

vendor: Add icedtea-web v1.1.2 to current.

File size: 19.2 KB
Line 
1/*
2 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33/*
34 * If Windows is POSIX compliant, why isn't the prototype for lseek where
35 * POSIX says it should be?
36 */
37#ifdef _WIN32
38#include <windows.h>
39#include <io.h>
40#else /* Unix */
41#include <unistd.h>
42#endif /* Unix */
43
44#include <zlib.h>
45#include "manifest_info.h"
46
47/*
48 * On Windows, str[n]casecmp() are known as str[n]icmp().
49 */
50#ifdef _WIN32
51#define strcasecmp(p1, p2) stricmp((p1), (p2))
52#define strncasecmp(p1, p2, p3) strnicmp((p1), (p2), (p3))
53#endif
54
55static char *manifest;
56
57static const char *manifest_name = "META-INF/MANIFEST.MF";
58
59/*
60 * Inflate the manifest file (or any file for that matter).
61 *
62 * fd: File descriptor of the jar file.
63 * entry: Contains the information necessary to perform the inflation
64 * (the compressed and uncompressed sizes and the offset in
65 * the file where the compressed data is located).
66 * size_out: Returns the size of the inflated file.
67 *
68 * Upon success, it returns a pointer to a NUL-terminated malloc'd buffer
69 * containing the inflated manifest file. When the caller is done with it,
70 * this buffer should be released by a call to free(). Upon failure,
71 * returns NULL.
72 */
73static char *
74inflate_file(int fd, zentry *entry, int *size_out)
75{
76 char *in;
77 char *out;
78 z_stream zs;
79
80 if (entry->csize == 0xffffffff || entry->isize == 0xffffffff)
81 return (NULL);
82 if (lseek(fd, entry->offset, SEEK_SET) < (off_t)0)
83 return (NULL);
84 if ((in = malloc(entry->csize + 1)) == NULL)
85 return (NULL);
86 if ((size_t)(read(fd, in, (unsigned int)entry->csize)) != entry->csize) {
87 free(in);
88 return (NULL);
89 }
90 if (entry->how == STORED) {
91 *(char *)((size_t)in + entry->csize) = '\0';
92 if (size_out) {
93 *size_out = entry->csize;
94 }
95 return (in);
96 } else if (entry->how == DEFLATED) {
97 zs.zalloc = (alloc_func)Z_NULL;
98 zs.zfree = (free_func)Z_NULL;
99 zs.opaque = (voidpf)Z_NULL;
100 zs.next_in = (Byte*)in;
101 zs.avail_in = (uInt)entry->csize;
102 if (inflateInit2(&zs, -MAX_WBITS) < 0) {
103 free(in);
104 return (NULL);
105 }
106 if ((out = malloc(entry->isize + 1)) == NULL) {
107 free(in);
108 return (NULL);
109 }
110 zs.next_out = (Byte*)out;
111 zs.avail_out = (uInt)entry->isize;
112 if (inflate(&zs, Z_PARTIAL_FLUSH) < 0) {
113 free(in);
114 free(out);
115 return (NULL);
116 }
117 *(char *)((size_t)out + entry->isize) = '\0';
118 free(in);
119 if (inflateEnd(&zs) < 0) {
120 free(out);
121 return (NULL);
122 }
123 if (size_out) {
124 *size_out = entry->isize;
125 }
126 return (out);
127 } else
128 return (NULL);
129}
130
131/*
132 * A very little used routine to handle the case that zip file has
133 * a comment at the end. Believe it or not, the only way to find the
134 * END record is to walk backwards, byte by bloody byte looking for
135 * the END record signature.
136 *
137 * fd: File descriptor of the jar file.
138 * eb: Pointer to a buffer to receive a copy of the END header.
139 *
140 * Returns the offset of the END record in the file on success,
141 * -1 on failure.
142 */
143static off_t
144find_end(int fd, Byte *eb)
145{
146 off_t len;
147 off_t pos;
148 off_t flen;
149 int bytes;
150 Byte *cp;
151 Byte *endpos;
152 Byte *buffer;
153
154 /*
155 * 99.44% (or more) of the time, there will be no comment at the
156 * end of the zip file. Try reading just enough to read the END
157 * record from the end of the file.
158 */
159 if ((pos = lseek(fd, -ENDHDR, SEEK_END)) < (off_t)0)
160 return (-1);
161 if ((bytes = read(fd, eb, ENDHDR)) < 0)
162 return (-1);
163 if (GETSIG(eb) == ENDSIG)
164 return (pos);
165
166 /*
167 * Shucky-Darn,... There is a comment at the end of the zip file.
168 *
169 * Allocate and fill a buffer with enough of the zip file
170 * to meet the specification for a maximal comment length.
171 */
172 if ((flen = lseek(fd, 0, SEEK_END)) < (off_t)0)
173 return (-1);
174 len = (flen < END_MAXLEN) ? flen : END_MAXLEN;
175 if (lseek(fd, -len, SEEK_END) < (off_t)0)
176 return (-1);
177 if ((buffer = malloc(END_MAXLEN)) == NULL)
178 return (-1);
179 if ((bytes = read(fd, buffer, len)) < 0) {
180 free(buffer);
181 return (-1);
182 }
183
184 /*
185 * Search backwards from the end of file stopping when the END header
186 * signature is found. (The first condition of the "if" is just a
187 * fast fail, because the GETSIG macro isn't always cheap. The
188 * final condition protects against false positives.)
189 */
190 endpos = &buffer[bytes];
191 for (cp = &buffer[bytes - ENDHDR]; cp >= &buffer[0]; cp--)
192 if ((*cp == (ENDSIG & 0xFF)) && (GETSIG(cp) == ENDSIG) &&
193 (cp + ENDHDR + ENDCOM(cp) == endpos)) {
194 (void) memcpy(eb, cp, ENDHDR);
195 free(buffer);
196 return (flen - (endpos - cp));
197 }
198 free(buffer);
199 return (-1);
200}
201
202/*
203 * Locate the manifest file with the zip/jar file.
204 *
205 * fd: File descriptor of the jar file.
206 * entry: To be populated with the information necessary to perform
207 * the inflation (the compressed and uncompressed sizes and
208 * the offset in the file where the compressed data is located).
209 *
210 * Returns zero upon success. Returns a negative value upon failure.
211 *
212 * The buffer for reading the Central Directory if the zip/jar file needs
213 * to be large enough to accommodate the largest possible single record
214 * and the signature of the next record which is:
215 *
216 * 3*2**16 + CENHDR + SIGSIZ
217 *
218 * Each of the three variable sized fields (name, comment and extension)
219 * has a maximum possible size of 64k.
220 *
221 * Typically, only a small bit of this buffer is used with bytes shuffled
222 * down to the beginning of the buffer. It is one thing to allocate such
223 * a large buffer and another thing to actually start faulting it in.
224 *
225 * In most cases, all that needs to be read are the first two entries in
226 * a typical jar file (META-INF and META-INF/MANIFEST.MF). Keep this factoid
227 * in mind when optimizing this code.
228 */
229#define BUFSIZE (3 * 65536 + CENHDR + SIGSIZ)
230#define MINREAD 1024
231
232static int
233find_file(int fd, zentry *entry, const char *file_name)
234{
235 int bytes;
236 int res;
237 int entry_size;
238 int read_size;
239 int base_offset;
240 Byte *p;
241 Byte *bp;
242 Byte buffer[BUFSIZE];
243 Byte locbuf[LOCHDR];
244
245 p = buffer;
246 bp = buffer;
247
248 /*
249 * Read the END Header, which is the starting point for ZIP files.
250 * (Clearly designed to make writing a zip file easier than reading
251 * one. Now isn't that precious...)
252 */
253 if ((base_offset = find_end(fd, bp)) == -1)
254 return (-1);
255
256 /*
257 * There is a historical, but undocumented, ability to allow for
258 * additional "stuff" to be prepended to the zip/jar file. It seems
259 * that this has been used to prepend an actual java launcher
260 * executable to the jar on Windows. Although this is just another
261 * form of statically linking a small piece of the JVM to the
262 * application, we choose to continue to support it. Note that no
263 * guarantees have been made (or should be made) to the customer that
264 * this will continue to work.
265 *
266 * Therefore, calculate the base offset of the zip file (within the
267 * expanded file) by assuming that the central directory is followed
268 * immediately by the end record.
269 */
270 base_offset = base_offset - ENDSIZ(p) - ENDOFF(p);
271
272 /*
273 * The END Header indicates the start of the Central Directory
274 * Headers. Remember that the desired Central Directory Header (CEN)
275 * will almost always be the second one and the first one is a small
276 * directory entry ("META-INF/"). Keep the code optimized for
277 * that case.
278 *
279 * Begin by seeking to the beginning of the Central Directory and
280 * reading in the first buffer full of bits.
281 */
282 if (lseek(fd, base_offset + ENDOFF(p), SEEK_SET) < (off_t)0)
283 return (-1);
284 if ((bytes = read(fd, bp, MINREAD)) < 0)
285 return (-1);
286
287 /*
288 * Loop through the Central Directory Headers. Note that a valid zip/jar
289 * must have an ENDHDR (with ENDSIG) after the Central Directory.
290 */
291 while (GETSIG(p) == CENSIG) {
292
293 /*
294 * If a complete header isn't in the buffer, shift the contents
295 * of the buffer down and refill the buffer. Note that the check
296 * for "bytes < CENHDR" must be made before the test for the entire
297 * size of the header, because if bytes is less than CENHDR, the
298 * actual size of the header can't be determined. The addition of
299 * SIGSIZ guarantees that the next signature is also in the buffer
300 * for proper loop termination.
301 */
302 if (bytes < CENHDR) {
303 p = memmove(bp, p, bytes);
304 if ((res = read(fd, bp + bytes, MINREAD)) <= 0)
305 return (-1);
306 bytes += res;
307 }
308 entry_size = CENHDR + CENNAM(p) + CENEXT(p) + CENCOM(p);
309 if (bytes < entry_size + SIGSIZ) {
310 if (p != bp)
311 p = memmove(bp, p, bytes);
312 read_size = entry_size - bytes + SIGSIZ;
313 read_size = (read_size < MINREAD) ? MINREAD : read_size;
314 if ((res = read(fd, bp + bytes, read_size)) <= 0)
315 return (-1);
316 bytes += res;
317 }
318
319 /*
320 * Check if the name is the droid we are looking for; the jar file
321 * manifest. If so, build the entry record from the data found in
322 * the header located and return success.
323 */
324 if (CENNAM(p) == strlen(file_name) &&
325 memcmp((p + CENHDR), file_name, strlen(file_name)) == 0) {
326 if (lseek(fd, base_offset + CENOFF(p), SEEK_SET) < (off_t)0)
327 return (-1);
328 if (read(fd, locbuf, LOCHDR) < 0)
329 return (-1);
330 if (GETSIG(locbuf) != LOCSIG)
331 return (-1);
332 entry->isize = CENLEN(p);
333 entry->csize = CENSIZ(p);
334 entry->offset = base_offset + CENOFF(p) + LOCHDR +
335 LOCNAM(locbuf) + LOCEXT(locbuf);
336 entry->how = CENHOW(p);
337 return (0);
338 }
339
340 /*
341 * Point to the next entry and decrement the count of valid remaining
342 * bytes.
343 */
344 bytes -= entry_size;
345 p += entry_size;
346 }
347
348 return (-1); /* Fell off the end the loop without a Manifest */
349}
350
351/*
352 * Parse a Manifest file header entry into a distinct "name" and "value".
353 * Continuation lines are joined into a single "value". The documented
354 * syntax for a header entry is:
355 *
356 * header: name ":" value
357 *
358 * name: alphanum *headerchar
359 *
360 * value: SPACE *otherchar newline *continuation
361 *
362 * continuation: SPACE *otherchar newline
363 *
364 * newline: CR LF | LF | CR (not followed by LF)
365 *
366 * alphanum: {"A"-"Z"} | {"a"-"z"} | {"0"-"9"}
367 *
368 * headerchar: alphanum | "-" | "_"
369 *
370 * otherchar: any UTF-8 character except NUL, CR and LF
371 *
372 * Note that a manifest file may be composed of multiple sections,
373 * each of which may contain multiple headers.
374 *
375 * section: *header +newline
376 *
377 * nonempty-section: +header +newline
378 *
379 * (Note that the point of "nonempty-section" is unclear, because it isn't
380 * referenced elsewhere in the full specification for the Manifest file.)
381 *
382 * Arguments:
383 * lp pointer to a character pointer which points to the start
384 * of a valid header.
385 * name pointer to a character pointer which will be set to point
386 * to the name portion of the header (nul terminated).
387 * value pointer to a character pointer which will be set to point
388 * to the value portion of the header (nul terminated).
389 *
390 * Returns:
391 * 1 Successful parsing of an NV pair. lp is updated to point to the
392 * next character after the terminating newline in the string
393 * representing the Manifest file. name and value are updated to
394 * point to the strings parsed.
395 * 0 A valid end of section indicator was encountered. lp, name, and
396 * value are not modified.
397 * -1 lp does not point to a valid header. Upon return, the values of
398 * lp, name, and value are undefined.
399 */
400static int
401parse_nv_pair(char **lp, char **name, char **value)
402{
403 char *nl;
404 char *cp;
405
406 /*
407 * End of the section - return 0. The end of section condition is
408 * indicated by either encountering a blank line or the end of the
409 * Manifest "string" (EOF).
410 */
411 if (**lp == '\0' || **lp == '\n' || **lp == '\r')
412 return (0);
413
414 /*
415 * Getting to here, indicates that *lp points to an "otherchar".
416 * Turn the "header" into a string on its own.
417 */
418 nl = strpbrk(*lp, "\n\r");
419 if (nl == NULL) {
420 nl = strchr(*lp, (int)'\0');
421 } else {
422 cp = nl; /* For merging continuation lines */
423 if (*nl == '\r' && *(nl+1) == '\n')
424 *nl++ = '\0';
425 *nl++ = '\0';
426
427 /*
428 * Process any "continuation" line(s), by making them part of the
429 * "header" line. Yes, I know that we are "undoing" the NULs we
430 * just placed here, but continuation lines are the fairly rare
431 * case, so we shouldn't unnecessarily complicate the code above.
432 *
433 * Note that an entire continuation line is processed each iteration
434 * through the outer while loop.
435 */
436 while (*nl == ' ') {
437 nl++; /* First character to be moved */
438 while (*nl != '\n' && *nl != '\r' && *nl != '\0')
439 *cp++ = *nl++; /* Shift string */
440 if (*nl == '\0')
441 return (-1); /* Error: newline required */
442 *cp = '\0';
443 if (*nl == '\r' && *(nl+1) == '\n')
444 *nl++ = '\0';
445 *nl++ = '\0';
446 }
447 }
448
449 /*
450 * Separate the name from the value;
451 */
452 cp = strchr(*lp, (int)':');
453 if (cp == NULL)
454 return (-1);
455 *cp++ = '\0'; /* The colon terminates the name */
456 if (*cp != ' ')
457 return (-1);
458 *cp++ = '\0'; /* Eat the required space */
459 *name = *lp;
460 *value = cp;
461 *lp = nl;
462 return (1);
463}
464
465/*
466 * Read the manifest from the specified jar file and fill in the manifest_info
467 * structure with the information found within.
468 *
469 * Error returns are as follows:
470 * 0 Success
471 * -1 Unable to open jarfile
472 * -2 Error accessing the manifest from within the jarfile (most likely
473 * a manifest is not present, or this isn't a valid zip/jar file).
474 */
475int
476JLI_ParseManifest(char *jarfile, manifest_info *info)
477{
478 int fd;
479 zentry entry;
480 char *lp;
481 char *name;
482 char *value;
483 int rc;
484 char *splashscreen_name = NULL;
485
486 if ((fd = open(jarfile, O_RDONLY
487#ifdef O_BINARY
488 | O_BINARY /* use binary mode on windows */
489#endif
490 )) == -1)
491 return (-1);
492
493 info->manifest_version = NULL;
494 info->main_class = NULL;
495 info->jre_version = NULL;
496 info->jre_restrict_search = 0;
497 info->splashscreen_image_file_name = NULL;
498 if (rc = find_file(fd, &entry, manifest_name) != 0) {
499 close(fd);
500 return (-2);
501 }
502 manifest = inflate_file(fd, &entry, NULL);
503 if (manifest == NULL) {
504 close(fd);
505 return (-2);
506 }
507 lp = manifest;
508 while ((rc = parse_nv_pair(&lp, &name, &value)) > 0) {
509 if (strcasecmp(name, "Manifest-Version") == 0)
510 info->manifest_version = value;
511 else if (strcasecmp(name, "Main-Class") == 0)
512 info->main_class = value;
513 else if (strcasecmp(name, "JRE-Version") == 0)
514 info->jre_version = value;
515 else if (strcasecmp(name, "JRE-Restrict-Search") == 0) {
516 if (strcasecmp(value, "true") == 0)
517 info->jre_restrict_search = 1;
518 } else if (strcasecmp(name, "Splashscreen-Image") == 0) {
519 info->splashscreen_image_file_name = value;
520 }
521 }
522 close(fd);
523 if (rc == 0)
524 return (0);
525 else
526 return (-2);
527}
528
529/*
530 * Opens the jar file and unpacks the specified file from its contents.
531 * Returns NULL on failure.
532 */
533void *
534JLI_JarUnpackFile(const char *jarfile, const char *filename, int *size) {
535 int fd;
536 zentry entry;
537 void *data = NULL;
538
539 fd = open(jarfile, O_RDONLY
540#ifdef O_BINARY
541 | O_BINARY /* use binary mode on windows */
542#endif
543 );
544 if (fd != -1 && find_file(fd, &entry, filename) == 0) {
545 data = inflate_file(fd, &entry, size);
546 }
547 close(fd);
548 return (data);
549}
550
551/*
552 * Specialized "free" function.
553 */
554void
555JLI_FreeManifest()
556{
557 if (manifest)
558 free(manifest);
559}
560
561/*
562 * Iterate over the manifest of the specified jar file and invoke the provided
563 * closure function for each attribute encountered.
564 *
565 * Error returns are as follows:
566 * 0 Success
567 * -1 Unable to open jarfile
568 * -2 Error accessing the manifest from within the jarfile (most likely
569 * this means a manifest is not present, or it isn't a valid zip/jar file).
570 */
571int
572JLI_ManifestIterate(const char *jarfile, attribute_closure ac, void *user_data)
573{
574 int fd;
575 zentry entry;
576 char *mp; /* manifest pointer */
577 char *lp; /* pointer into manifest, updated during iteration */
578 char *name;
579 char *value;
580 int rc;
581
582 if ((fd = open(jarfile, O_RDONLY
583#ifdef O_BINARY
584 | O_BINARY /* use binary mode on windows */
585#endif
586 )) == -1)
587 return (-1);
588
589 if (rc = find_file(fd, &entry, manifest_name) != 0) {
590 close(fd);
591 return (-2);
592 }
593
594 mp = inflate_file(fd, &entry, NULL);
595 if (mp == NULL) {
596 close(fd);
597 return (-2);
598 }
599
600 lp = mp;
601 while ((rc = parse_nv_pair(&lp, &name, &value)) > 0) {
602 (*ac)(name, value, user_data);
603 }
604 free(mp);
605 close(fd);
606 if (rc == 0)
607 return (0);
608 else
609 return (-2);
610}
Note: See TracBrowser for help on using the repository browser.