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