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