3261N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A * Inflate the manifest file (or any file for that matter). 0N/A * fd: File descriptor of the jar file. 0N/A * entry: Contains the information necessary to perform the inflation 0N/A * (the compressed and uncompressed sizes and the offset in 0N/A * the file where the compressed data is located). 0N/A * size_out: Returns the size of the inflated file. 0N/A * Upon success, it returns a pointer to a NUL-terminated malloc'd buffer 0N/A * containing the inflated manifest file. When the caller is done with it, 0N/A * this buffer should be released by a call to free(). Upon failure, 0N/A * A very little used routine to handle the case that zip file has 0N/A * a comment at the end. Believe it or not, the only way to find the 0N/A * END record is to walk backwards, byte by bloody byte looking for 0N/A * the END record signature. 0N/A * fd: File descriptor of the jar file. 0N/A * eb: Pointer to a buffer to receive a copy of the END header. 0N/A * Returns the offset of the END record in the file on success, 0N/A * 99.44% (or more) of the time, there will be no comment at the 0N/A * end of the zip file. Try reading just enough to read the END 0N/A * record from the end of the file. 0N/A * Shucky-Darn,... There is a comment at the end of the zip file. 0N/A * Allocate and fill a buffer with enough of the zip file 0N/A * to meet the specification for a maximal comment length. 0N/A * Search backwards from the end of file stopping when the END header 0N/A * signature is found. (The first condition of the "if" is just a 0N/A * fast fail, because the GETSIG macro isn't always cheap. The 0N/A * final condition protects against false positives.) 0N/A * Locate the manifest file with the zip/jar file. 0N/A * fd: File descriptor of the jar file. 0N/A * entry: To be populated with the information necessary to perform 0N/A * the inflation (the compressed and uncompressed sizes and 0N/A * the offset in the file where the compressed data is located). 0N/A * Returns zero upon success. Returns a negative value upon failure. 0N/A * The buffer for reading the Central Directory if the zip/jar file needs 0N/A * to be large enough to accommodate the largest possible single record 0N/A * and the signature of the next record which is: 0N/A * 3*2**16 + CENHDR + SIGSIZ 0N/A * Each of the three variable sized fields (name, comment and extension) 0N/A * has a maximum possible size of 64k. 0N/A * Typically, only a small bit of this buffer is used with bytes shuffled 0N/A * down to the beginning of the buffer. It is one thing to allocate such 0N/A * a large buffer and another thing to actually start faulting it in. 0N/A * In most cases, all that needs to be read are the first two entries in 0N/A * in mind when optimizing this code. 0N/A * Read the END Header, which is the starting point for ZIP files. 0N/A * (Clearly designed to make writing a zip file easier than reading 0N/A * one. Now isn't that precious...) 0N/A * There is a historical, but undocumented, ability to allow for 0N/A * additional "stuff" to be prepended to the zip/jar file. It seems 0N/A * that this has been used to prepend an actual java launcher 0N/A * executable to the jar on Windows. Although this is just another 0N/A * form of statically linking a small piece of the JVM to the 0N/A * application, we choose to continue to support it. Note that no 0N/A * guarantees have been made (or should be made) to the customer that 0N/A * this will continue to work. 0N/A * Therefore, calculate the base offset of the zip file (within the 0N/A * expanded file) by assuming that the central directory is followed 0N/A * immediately by the end record. 0N/A * The END Header indicates the start of the Central Directory 0N/A * Headers. Remember that the desired Central Directory Header (CEN) 0N/A * will almost always be the second one and the first one is a small 0N/A * directory entry ("META-INF/"). Keep the code optimized for 0N/A * Begin by seeking to the beginning of the Central Directory and 0N/A * reading in the first buffer full of bits. 0N/A * Loop through the Central Directory Headers. Note that a valid zip/jar 0N/A * must have an ENDHDR (with ENDSIG) after the Central Directory. 0N/A * If a complete header isn't in the buffer, shift the contents 0N/A * of the buffer down and refill the buffer. Note that the check 0N/A * for "bytes < CENHDR" must be made before the test for the entire 0N/A * size of the header, because if bytes is less than CENHDR, the 0N/A * actual size of the header can't be determined. The addition of 0N/A * SIGSIZ guarantees that the next signature is also in the buffer 0N/A * for proper loop termination. 0N/A * Check if the name is the droid we are looking for; the jar file 0N/A * manifest. If so, build the entry record from the data found in 0N/A * the header located and return success. 0N/A * Point to the next entry and decrement the count of valid remaining 0N/A return (-
1);
/* Fell off the end the loop without a Manifest */ 0N/A * Parse a Manifest file header entry into a distinct "name" and "value". 0N/A * Continuation lines are joined into a single "value". The documented 0N/A * syntax for a header entry is: 0N/A * header: name ":" value 0N/A * name: alphanum *headerchar 0N/A * value: SPACE *otherchar newline *continuation 0N/A * continuation: SPACE *otherchar newline 0N/A * newline: CR LF | LF | CR (not followed by LF) 0N/A * alphanum: {"A"-"Z"} | {"a"-"z"} | {"0"-"9"} 0N/A * headerchar: alphanum | "-" | "_" 0N/A * otherchar: any UTF-8 character except NUL, CR and LF 0N/A * Note that a manifest file may be composed of multiple sections, 0N/A * each of which may contain multiple headers. 0N/A * section: *header +newline 0N/A * nonempty-section: +header +newline 0N/A * (Note that the point of "nonempty-section" is unclear, because it isn't 0N/A * referenced elsewhere in the full specification for the Manifest file.) 0N/A * lp pointer to a character pointer which points to the start 0N/A * of a valid header. 0N/A * name pointer to a character pointer which will be set to point 0N/A * to the name portion of the header (nul terminated). 0N/A * value pointer to a character pointer which will be set to point 0N/A * to the value portion of the header (nul terminated). 0N/A * 1 Successful parsing of an NV pair. lp is updated to point to the 0N/A * next character after the terminating newline in the string 0N/A * representing the Manifest file. name and value are updated to 0N/A * point to the strings parsed. 0N/A * 0 A valid end of section indicator was encountered. lp, name, and 0N/A * value are not modified. 0N/A * -1 lp does not point to a valid header. Upon return, the values of 0N/A * lp, name, and value are undefined. 0N/A * End of the section - return 0. The end of section condition is 0N/A * indicated by either encountering a blank line or the end of the 0N/A * Manifest "string" (EOF). 0N/A if (**
lp ==
'\0' || **
lp ==
'\n' || **
lp ==
'\r')
0N/A * Getting to here, indicates that *lp points to an "otherchar". 0N/A * Turn the "header" into a string on its own. 0N/A cp =
nl;
/* For merging continuation lines */ 0N/A if (*
nl ==
'\r' && *(
nl+
1) ==
'\n')
0N/A * Process any "continuation" line(s), by making them part of the 0N/A * "header" line. Yes, I know that we are "undoing" the NULs we 0N/A * just placed here, but continuation lines are the fairly rare 0N/A * case, so we shouldn't unnecessarily complicate the code above. 0N/A * Note that an entire continuation line is processed each iteration 0N/A * through the outer while loop. 0N/A nl++;
/* First character to be moved */ 0N/A while (*
nl !=
'\n' && *
nl !=
'\r' && *
nl !=
'\0')
0N/A return (-
1);
/* Error: newline required */ 0N/A if (*
nl ==
'\r' && *(
nl+
1) ==
'\n')
0N/A * Separate the name from the value; 0N/A *
cp++ =
'\0';
/* The colon terminates the name */ 0N/A *
cp++ =
'\0';
/* Eat the required space */ 0N/A * Read the manifest from the specified jar file and fill in the manifest_info 0N/A * structure with the information found within. 0N/A * Error returns are as follows: 0N/A * -1 Unable to open jarfile 0N/A * -2 Error accessing the manifest from within the jarfile (most likely 0N/A * a manifest is not present, or this isn't a valid zip/jar file). 0N/A * Opens the jar file and unpacks the specified file from its contents. 0N/A * Returns NULL on failure. 0N/A * Specialized "free" function. 0N/A * Iterate over the manifest of the specified jar file and invoke the provided 0N/A * closure function for each attribute encountered. 0N/A * Error returns are as follows: 0N/A * -1 Unable to open jarfile 0N/A * -2 Error accessing the manifest from within the jarfile (most likely 0N/A * this means a manifest is not present, or it isn't a valid zip/jar file). 0N/A char *
mp;
/* manifest pointer */ 0N/A char *
lp;
/* pointer into manifest, updated during iteration */