📄 jar.c
字号:
/* * Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. * * Use is subject to license terms. *//*========================================================================= * SYSTEM: Verifier * SUBSYSTEM: JAR class loader. * FILE: jar.c * OVERVIEW: Routines for reading contents of a JAR file. The routines * are optimized to reduce code size and run-time memory * requirement so that they can run happily on small devices. * AUTHOR: Frank Yellin, Sun Microsystems, Inc. * Modifications for JAR support and comments, * Tasneem Sayeed, Sun Microsystems *=======================================================================*//*========================================================================= * Include files *=======================================================================*/#include <stdio.h>#include <sys/types.h>#include <stddef.h>#include <assert.h>#include <oobj.h>#include <typedefs.h>#include <jar.h>#include <jarint.h>#include <jartables.h>/*========================================================================= * Globals and extern declarations *=======================================================================*/static bool_t decodeDynamicHuffmanTables(inflaterState *state, HuffmanCodeTable **lcodesPtr, HuffmanCodeTable **dcodesPtr);static HuffmanCodeTable *makeCodeTable(inflaterState *state, unsigned char *codelen, unsigned numElems, unsigned maxQuickBits);static bool_t inflateHuffman(inflaterState *state, bool_t fixedHuffman);static bool_t inflateStored(inflaterState *state);/*=========================================================================== * FUNCTION: inflate * TYPE: jar file decoding * OVERVIEW Used for decoding JAR files given the compressed data source, * compressed data length, buffer to store decompressed data and * length of decompression buffer. * * INTERFACE: * parameters: compressed data source, compressed data length, * buffer to store decompressed data, decompression buffer size * returns: TRUE if the data was encoded in a supported <method> and the * size of the decoded data is exactly the same as <decompLen> * FALSE if an error occurs * NOTE: * The caller of this method must insure that this function can safely get * up to INFLATER_EXTRA_BYTES beyond compData + compLen without causing * any problems. * The inflater algorithm occasionally reads one more byte than it needs * to. But it double checks that it doesn't actually care what's in that * extra byte. *===========================================================================*//* Change some definitions so that this compiles nicely, even if it is * compiled as part of something that requires real malloc() and free() */#if !JAR_INFLATER_INSIDE_KVM# undef START_TEMPORARY_ROOTS# undef END_TEMPORARY_ROOTS# undef MAKE_TEMPORARY_ROOT# define START_TEMPORARY_ROOTS# define END_TEMPORARY_ROOTS# define MAKE_TEMPORARY_ROOT(x)# define mallocBytes(x) malloc(x)# define freeBytes(x) if (x == NULL) {} else free(x)#else# define freeBytes(x)#endifbool_t inflate(JarCompressedType compData, /* compressed data source */ int compLen, /* length of compressed data */ unsigned char *decompData, /* buffer to store decompressed data */ int decompLen) /* length of decompression buffer */{ inflaterState stateStruct; bool_t result;/* Temporarily define state, so that LOAD_IN, LOAD_OUT, etc. macros work */#define state (&stateStruct) stateStruct.out = decompData; stateStruct.outStart = decompData; stateStruct.outEnd = decompData + decompLen; stateStruct.inFile = compData; stateStruct.inData = 0; stateStruct.inDataSize = 0; stateStruct.inRemaining = compLen + INFLATER_EXTRA_BYTES;#ifdef JAR_DEBUG_FILE { static int length = 0; if (length == 0) { struct stat stat_buffer; stat(JAR_DEBUG_FILE, &stat_buffer); length = stat_buffer.st_size;; } if (length == decompLen) { FILE *f = fopen(JAR_DEBUG_FILE, "rb"); state->jarDebugBytes = malloc(length); fseek(f, 0, SEEK_SET); fread(state->jarDebugBytes, sizeof(char), length, f); fclose(f); } else { state->jarDebugBytes = NULL; } }#endif for(;;) { int type; DECLARE_IN_VARIABLES LOAD_IN; NEEDBITS(3); type = NEXTBITS(3); DUMPBITS(3); STORE_IN; switch (type >> 1) { default: case BTYPE_INVALID: ziperr("Invalid BTYPE"); result = FALSE; break; case BTYPE_NO_COMPRESSION: result = inflateStored(state); break; case BTYPE_FIXED_HUFFMAN: result = inflateHuffman(state, TRUE); break; case BTYPE_DYNA_HUFFMAN: START_TEMPORARY_ROOTS result = inflateHuffman(state, FALSE); END_TEMPORARY_ROOTS break; } if (!result || (type & 1)) { break; } } if (state->inRemaining + (state->inDataSize >> 3) != INFLATER_EXTRA_BYTES) { ziperr("Error on the input bits"); result = FALSE; } else if (state->out != state->outEnd) { ziperr("Error on the output bits"); result = FALSE; }#ifdef JAR_DEBUG_FILE if (state->jarDebugBytes != NULL) { free(state->jarDebugBytes); }#endif /* Remove temporary definition of state defined above */#undef state return result;}/*========================================================================= * FUNCTION: inflateStored * TYPE: Huffman code Decoding helper function * OVERVIEW: Used by inflate() for BTYPE_NO_COMPRESSION. * INTERFACE: * parameters: inflaterState: *state * * returns: boolean type *=======================================================================*/static bool_tinflateStored(inflaterState *state){ DECLARE_IN_VARIABLES DECLARE_OUT_VARIABLES unsigned len, nlen; LOAD_IN; LOAD_OUT; DUMPBITS(inDataSize & 7); /* move to byte boundary */ NEEDBITS(32) len = NEXTBITS(16); DUMPBITS(16); nlen = NEXTBITS(16); DUMPBITS(16); ASSERT(inDataSize == 0); if (len + nlen != 0xFFFF) { ziperr("Bad length field"); return FALSE; } else if ((unsigned) inRemaining < len) { ziperr("Input overflow"); return FALSE; } else if (out + len > state->outEnd) { ziperr("Output overflow"); return FALSE; } else { if (!COPY_N_BYTES(out, len)) { ziperr("Bad Read"); return FALSE; } inRemaining -= len; out += len; } STORE_IN; STORE_OUT; return TRUE;}/*========================================================================= * FUNCTION: inflateHuffman * TYPE: Huffman code Decoding helper function * OVERVIEW: Used by inflate() for BTYPE_FIXED_HUFFMAN and BTYPE_DYNA_HUFFMAN. * INTERFACE: * parameters: inflaterState: *state * bool_t: fixedHuffman * * returns: boolean type *=======================================================================*/static bool_tinflateHuffman(inflaterState *state, bool_t fixedHuffman){ DECLARE_IN_VARIABLES DECLARE_OUT_VARIABLES unsigned char *outStart = state->outStart; unsigned char *outEnd = state->outEnd; bool_t noerror = FALSE; unsigned int quickDataSize = 0, quickDistanceSize = 0; unsigned int code, litxlen; HuffmanCodeTable *lcodes, *dcodes; if (!fixedHuffman) { if (!decodeDynamicHuffmanTables(state, &lcodes, &dcodes)) { return FALSE; } quickDataSize = lcodes->h.quickBits; quickDistanceSize = dcodes->h.quickBits; } LOAD_IN; LOAD_OUT; for (;;) { if (inRemaining < 0) { goto done_loop; } NEEDBITS(MAX_BITS + MAX_ZIP_EXTRA_LENGTH_BITS); if (fixedHuffman) { /* literal (hex) * 0x100 - 0x117 7 0.0000.00 - 0.0101.11 * 0 - 8f 8 0.0110.000 - 1.0111.111 * 118 - 11f 8 1.1000.000 - 1.1000.111 * 90 - ff 9 1.1001.0000 - 1.1111.1111 */ /* Get 9 bits, and reverse them. */ code = NEXTBITS(9); code = REVERSE_9BITS(code); if (code < 0x060) { /* A 7-bit code */ DUMPBITS(7); litxlen = 0x100 + (code >> 2); } else if (code < 0x190) { DUMPBITS(8); litxlen = (code >> 1) + ((code < 0x180) ? (0x000 - 0x030) : (0x118 - 0x0c0)); } else { DUMPBITS(9); litxlen = 0x90 + code - 0x190; } } else { GET_HUFFMAN_ENTRY(lcodes, quickDataSize, litxlen, done_loop); } if (litxlen <= 255) { if (out < outEnd) {#ifdef JAR_DEBUG_FILE if (state->jarDebugBytes && state->jarDebugBytes[out - outStart] != litxlen) { ziperr("Dragon single byte"); }#endif *out++ = litxlen; } else { goto done_loop; } } else if (litxlen == 256) { /* end of block */ noerror = TRUE; goto done_loop; } else if (litxlen > 285) { ziperr("Invalid literal/length"); goto done_loop; } else { unsigned int n = litxlen - LITXLEN_BASE; unsigned int length = ll_length_base[n]; unsigned int moreBits = ll_extra_bits[n]; unsigned int d0, distance; /* The NEEDBITS(..) above took care of this */ length += NEXTBITS(moreBits); DUMPBITS(moreBits); NEEDBITS(MAX_BITS); if (fixedHuffman) { d0 = REVERSE_5BITS(NEXTBITS(5)); DUMPBITS(5); } else { GET_HUFFMAN_ENTRY(dcodes, quickDistanceSize, d0, done_loop); } if (d0 > MAX_ZIP_DISTANCE_CODE) { ziperr("Bad distance code"); goto done_loop; } NEEDBITS(MAX_ZIP_EXTRA_DISTANCE_BITS) distance = dist_base[d0]; moreBits = dist_extra_bits[d0]; distance += NEXTBITS(moreBits); DUMPBITS(moreBits);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -