DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / preverifier / jar.c
1 /*
2 * Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * This software is the confidential and proprietary information of Sun
5 * Microsystems, Inc. ("Confidential Information"). You shall not
6 * disclose such Confidential Information and shall use it only in
7 * accordance with the terms of the license agreement you entered into
8 * with Sun.
9 *
10 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
11 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
13 * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
14 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
15 * THIS SOFTWARE OR ITS DERIVATIVES.
16 *
17 * Use is subject to license terms.
18 */
20 /*=========================================================================
21 * SYSTEM: Verifier
22 * SUBSYSTEM: JAR class loader.
23 * FILE: jar.c
24 * OVERVIEW: Routines for reading contents of a JAR file. The routines
25 * are optimized to reduce code size and run-time memory
26 * requirement so that they can run happily on small devices.
27 * AUTHOR: Frank Yellin, Sun Microsystems, Inc.
28 * Modifications for JAR support and comments,
29 * Tasneem Sayeed, Sun Microsystems
30 *=======================================================================*/
32 /*=========================================================================
33 * Include files
34 *=======================================================================*/
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <stddef.h>
40 #include <assert.h>
42 #include "oobj.h"
43 #include "typedefs.h"
44 #include "jar.h"
45 #include "jarint.h"
46 #include "jartables.h"
48 /*=========================================================================
49 * Globals and extern declarations
50 *=======================================================================*/
52 static bool_t decodeDynamicHuffmanTables(inflaterState *state,
53 HuffmanCodeTable **lcodesPtr,
54 HuffmanCodeTable **dcodesPtr);
56 static HuffmanCodeTable *makeCodeTable(inflaterState *state,
57 unsigned char *codelen,
58 unsigned numElems,
59 unsigned maxQuickBits);
62 static bool_t inflateHuffman(inflaterState *state, bool_t fixedHuffman);
63 static bool_t inflateStored(inflaterState *state);
66 /*===========================================================================
67 * FUNCTION: inflate
68 * TYPE: jar file decoding
69 * OVERVIEW Used for decoding JAR files given the compressed data source,
70 * compressed data length, buffer to store decompressed data and
71 * length of decompression buffer.
72 *
73 * INTERFACE:
74 * parameters: compressed data source, compressed data length,
75 * buffer to store decompressed data, decompression buffer size
76 * returns: TRUE if the data was encoded in a supported <method> and the
77 * size of the decoded data is exactly the same as <decompLen>
78 * FALSE if an error occurs
79 * NOTE:
80 * The caller of this method must insure that this function can safely get
81 * up to INFLATER_EXTRA_BYTES beyond compData + compLen without causing
82 * any problems.
83 * The inflater algorithm occasionally reads one more byte than it needs
84 * to. But it double checks that it doesn't actually care what's in that
85 * extra byte.
86 *===========================================================================*/
89 /* Change some definitions so that this compiles nicely, even if it is
90 * compiled as part of something that requires real malloc() and free()
91 */
92 #if !JAR_INFLATER_INSIDE_KVM
93 # undef START_TEMPORARY_ROOTS
94 # undef END_TEMPORARY_ROOTS
95 # undef MAKE_TEMPORARY_ROOT
96 # define START_TEMPORARY_ROOTS
97 # define END_TEMPORARY_ROOTS
98 # define MAKE_TEMPORARY_ROOT(x)
99 # define mallocBytes(x) malloc(x)
100 # define freeBytes(x) if (x == NULL) {} else free(x)
101 #else
102 # define freeBytes(x)
103 #endif
105 bool_t
106 inflate(JarCompressedType compData, /* compressed data source */
107 int compLen, /* length of compressed data */
108 unsigned char *decompData, /* buffer to store decompressed data */
109 int decompLen) /* length of decompression buffer */
111 inflaterState stateStruct;
112 bool_t result;
113 /* Temporarily define state, so that LOAD_IN, LOAD_OUT, etc. macros work */
114 #define state (&stateStruct)
115 stateStruct.out = decompData;
116 stateStruct.outStart = decompData;
117 stateStruct.outEnd = decompData + decompLen;
119 stateStruct.inFile = compData;
120 stateStruct.inData = 0;
121 stateStruct.inDataSize = 0;
122 stateStruct.inRemaining = compLen + INFLATER_EXTRA_BYTES;
124 #ifdef JAR_DEBUG_FILE
125 {
126 static int length = 0;
127 if (length == 0) {
128 struct stat stat_buffer;
129 stat(JAR_DEBUG_FILE, &stat_buffer);
130 length = stat_buffer.st_size;;
132 if (length == decompLen) {
133 FILE *f = fopen(JAR_DEBUG_FILE, "rb");
134 state->jarDebugBytes = malloc(length);
135 fseek(f, 0, SEEK_SET);
136 fread(state->jarDebugBytes, sizeof(char), length, f);
137 fclose(f);
138 } else {
139 state->jarDebugBytes = NULL;
142 #endif
144 for(;;) {
145 int type;
146 DECLARE_IN_VARIABLES
148 LOAD_IN;
149 NEEDBITS(3);
150 type = NEXTBITS(3);
151 DUMPBITS(3);
152 STORE_IN;
154 switch (type >> 1) {
155 default:
156 case BTYPE_INVALID:
157 ziperr("Invalid BTYPE");
158 result = FALSE;
159 break;
161 case BTYPE_NO_COMPRESSION:
162 result = inflateStored(state);
163 break;
165 case BTYPE_FIXED_HUFFMAN:
166 result = inflateHuffman(state, TRUE);
167 break;
169 case BTYPE_DYNA_HUFFMAN:
170 START_TEMPORARY_ROOTS
171 result = inflateHuffman(state, FALSE);
172 END_TEMPORARY_ROOTS
173 break;
176 if (!result || (type & 1)) {
177 break;
181 if (state->inRemaining + (state->inDataSize >> 3) != INFLATER_EXTRA_BYTES) {
182 ziperr("Error on the input bits");
183 result = FALSE;
184 } else if (state->out != state->outEnd) {
185 ziperr("Error on the output bits");
186 result = FALSE;
189 #ifdef JAR_DEBUG_FILE
190 if (state->jarDebugBytes != NULL) {
191 free(state->jarDebugBytes);
193 #endif
195 /* Remove temporary definition of state defined above */
196 #undef state
198 return result;
202 /*=========================================================================
203 * FUNCTION: inflateStored
204 * TYPE: Huffman code Decoding helper function
205 * OVERVIEW: Used by inflate() for BTYPE_NO_COMPRESSION.
206 * INTERFACE:
207 * parameters: inflaterState: *state
208 *
209 * returns: boolean type
210 *=======================================================================*/
211 static bool_t
212 inflateStored(inflaterState *state)
214 DECLARE_IN_VARIABLES
215 DECLARE_OUT_VARIABLES
216 unsigned len, nlen;
218 LOAD_IN; LOAD_OUT;
220 DUMPBITS(inDataSize & 7); /* move to byte boundary */
221 NEEDBITS(32)
222 len = NEXTBITS(16);
223 DUMPBITS(16);
224 nlen = NEXTBITS(16);
225 DUMPBITS(16);
227 ASSERT(inDataSize == 0);
229 if (len + nlen != 0xFFFF) {
230 ziperr("Bad length field");
231 return FALSE;
232 } else if ((unsigned) inRemaining < len) {
233 ziperr("Input overflow");
234 return FALSE;
235 } else if (out + len > state->outEnd) {
236 ziperr("Output overflow");
237 return FALSE;
238 } else {
239 if (!COPY_N_BYTES(out, len)) {
240 ziperr("Bad Read");
241 return FALSE;
243 inRemaining -= len;
244 out += len;
246 STORE_IN;
247 STORE_OUT;
248 return TRUE;
252 /*=========================================================================
253 * FUNCTION: inflateHuffman
254 * TYPE: Huffman code Decoding helper function
255 * OVERVIEW: Used by inflate() for BTYPE_FIXED_HUFFMAN and BTYPE_DYNA_HUFFMAN.
256 * INTERFACE:
257 * parameters: inflaterState: *state
258 * bool_t: fixedHuffman
259 *
260 * returns: boolean type
261 *=======================================================================*/
262 static bool_t
263 inflateHuffman(inflaterState *state, bool_t fixedHuffman)
265 DECLARE_IN_VARIABLES
266 DECLARE_OUT_VARIABLES
267 unsigned char *outStart = state->outStart;
268 unsigned char *outEnd = state->outEnd;
270 bool_t noerror = FALSE;
271 unsigned int quickDataSize = 0, quickDistanceSize = 0;
272 unsigned int code, litxlen;
273 HuffmanCodeTable *lcodes, *dcodes;
275 if (!fixedHuffman) {
276 if (!decodeDynamicHuffmanTables(state, &lcodes, &dcodes)) {
277 return FALSE;
279 quickDataSize = lcodes->h.quickBits;
280 quickDistanceSize = dcodes->h.quickBits;
283 LOAD_IN;
284 LOAD_OUT;
286 for (;;) {
287 if (inRemaining < 0) {
288 goto done_loop;
290 NEEDBITS(MAX_BITS + MAX_ZIP_EXTRA_LENGTH_BITS);
292 if (fixedHuffman) {
293 /* literal (hex)
294 * 0x100 - 0x117 7 0.0000.00 - 0.0101.11
295 * 0 - 8f 8 0.0110.000 - 1.0111.111
296 * 118 - 11f 8 1.1000.000 - 1.1000.111
297 * 90 - ff 9 1.1001.0000 - 1.1111.1111
298 */
300 /* Get 9 bits, and reverse them. */
301 code = NEXTBITS(9);
302 code = REVERSE_9BITS(code);
303 if (code < 0x060) {
304 /* A 7-bit code */
305 DUMPBITS(7);
306 litxlen = 0x100 + (code >> 2);
307 } else if (code < 0x190) {
308 DUMPBITS(8);
309 litxlen = (code >> 1) + ((code < 0x180) ? (0x000 - 0x030)
310 : (0x118 - 0x0c0));
311 } else {
312 DUMPBITS(9);
313 litxlen = 0x90 + code - 0x190;
315 } else {
316 GET_HUFFMAN_ENTRY(lcodes, quickDataSize, litxlen, done_loop);
319 if (litxlen <= 255) {
320 if (out < outEnd) {
321 #ifdef JAR_DEBUG_FILE
322 if (state->jarDebugBytes && state->jarDebugBytes[out - outStart] != litxlen) {
323 ziperr("Dragon single byte");
325 #endif
326 *out++ = litxlen;
327 } else {
328 goto done_loop;
330 } else if (litxlen == 256) { /* end of block */
331 noerror = TRUE;
332 goto done_loop;
333 } else if (litxlen > 285) {
334 ziperr("Invalid literal/length");
335 goto done_loop;
336 } else {
337 unsigned int n = litxlen - LITXLEN_BASE;
338 unsigned int length = ll_length_base[n];
339 unsigned int moreBits = ll_extra_bits[n];
340 unsigned int d0, distance;
342 /* The NEEDBITS(..) above took care of this */
343 length += NEXTBITS(moreBits);
344 DUMPBITS(moreBits);
346 NEEDBITS(MAX_BITS);
347 if (fixedHuffman) {
348 d0 = REVERSE_5BITS(NEXTBITS(5));
349 DUMPBITS(5);
350 } else {
351 GET_HUFFMAN_ENTRY(dcodes, quickDistanceSize, d0, done_loop);
354 if (d0 > MAX_ZIP_DISTANCE_CODE) {
355 ziperr("Bad distance code");
356 goto done_loop;
359 NEEDBITS(MAX_ZIP_EXTRA_DISTANCE_BITS)
360 distance = dist_base[d0];
361 moreBits = dist_extra_bits[d0];
362 distance += NEXTBITS(moreBits);
363 DUMPBITS(moreBits);
365 if (out - distance < outStart) {
366 ziperr("copy underflow");
367 goto done_loop;
368 } else if (out + length > outEnd) {
369 ziperr("Output overflow");
370 goto done_loop;
371 } else {
372 unsigned char *prev = out - distance;
373 unsigned char *end = out + length;
374 while (out != end) {
375 #ifdef JAR_DEBUG_FILE
376 if (state->jarDebugBytes
377 && state->jarDebugBytes[out - outStart] != *prev) {
378 ziperr("Dragon copy error");
380 #endif
381 *out++ = *prev++;
387 done_loop:
388 STORE_IN;
389 STORE_OUT;
391 if (!JAR_INFLATER_INSIDE_KVM && !fixedHuffman) {
392 freeBytes(lcodes);
393 freeBytes(dcodes);
395 return noerror;
399 /*=========================================================================
400 * FUNCTION: decodeDynamicHuffmanTables
401 * TYPE: Huffman code Decoding
402 * OVERVIEW: Used by inflateHuffman() for decoding dynamic Huffman tables.
403 * INTERFACE:
404 * parameters: inflaterState: *state
405 * HuffmanCodeTable: **lcodesPtr
406 * HuffmanCodeTable: **dcodesPtr
407 *
408 * returns: TRUE if successful in decoding or
409 * FALSE if an error occurs
410 *=======================================================================*/
412 static bool_t
413 decodeDynamicHuffmanTables(inflaterState *state,
414 HuffmanCodeTable **lcodesPtr,
415 HuffmanCodeTable **dcodesPtr) {
416 DECLARE_IN_VARIABLES
418 HuffmanCodeTable *ccodes = NULL;
419 HuffmanCodeTable *lcodes = NULL;
420 HuffmanCodeTable *dcodes = NULL;
422 int hlit, hdist, hclen;
423 int i;
424 unsigned int quickBits;
425 unsigned char codelen[286 + 32];
426 unsigned char *codePtr, *endCodePtr;
428 LOAD_IN;
430 /* 5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286) */
431 /* 5 Bits: HDIST, # of Distance codes - 1 (1 - 32) */
432 /* 4 Bits: HCLEN, # of Code Length codes - 4 (4 - 19) */
433 NEEDBITS(14);
434 hlit = 257 + NEXTBITS(5);
435 DUMPBITS(5);
436 hdist = 1 + NEXTBITS(5);
437 DUMPBITS(5);
438 hclen = 4 + NEXTBITS(4);
439 DUMPBITS(4);
441 /*
442 * (HCLEN + 4) x 3 bits: code lengths for the code length
443 * alphabet given just above, in the order: 16, 17, 18,
444 * 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
446 * These code lengths are interpreted as 3-bit integers
447 * (0-7); as above, a code length of 0 means the
448 * corresponding symbol (literal/length or distance code
449 * length) is not used.
450 */
451 memset(codelen, 0x0, 19);
452 for (i=0; i<hclen; i++) {
453 NEEDBITS(3); /* 3, plus 7 below */
454 if (inRemaining < 0) {
455 goto error;
457 codelen[(int)ccode_idx[i]] = (unsigned char) NEXTBITS(3);
458 DUMPBITS(3);
461 ccodes = makeCodeTable(state, codelen, 19, MAX_QUICK_CXD);
462 if (ccodes == NULL) {
463 goto error;
465 quickBits = ccodes->h.quickBits;
467 /*
468 * HLIT + 257 code lengths for the literal/length alphabet,
469 * encoded using the code length Huffman code.
471 * HDIST + 1 code lengths for the distance alphabet,
472 * encoded using the code length Huffman code.
474 * The code length repeat codes can cross from HLIT + 257 to the
475 * HDIST + 1 code lengths. In other words, all code lengths form
476 * a single sequence of HLIT + HDIST + 258 values.
477 */
479 memset(codelen, 0x0, sizeof(codelen));
480 for ( codePtr = codelen, endCodePtr = codePtr + hlit + hdist;
481 codePtr < endCodePtr; ) {
482 int val;
484 if (inRemaining < 0) {
485 goto error;
488 NEEDBITS(MAX_BITS + 7); /* 7 is max repeat bits below */
489 GET_HUFFMAN_ENTRY(ccodes, quickBits, val, error);
491 /*
492 * 0 - 15: Represent code lengths of 0 - 15
493 * 16: Copy the previous code length 3 - 6 times.
494 * 3 + (2 bits of length)
495 * 17: Repeat a code length of 0 for 3 - 10 times.
496 * 3 + (3 bits of length)
497 * 18: Repeat a code length of 0 for 11 - 138 times
498 * 11 + (7 bits of length)
499 */
500 if (val <= 15) {
501 *codePtr++ = val;
502 } else if (val <= 18) {
503 unsigned repeat = (val == 18) ? 11 : 3;
504 unsigned bits = (val == 18) ? 7 : (val - 14);
506 repeat += NEXTBITS(bits); /* The NEEDBITS is above */
507 DUMPBITS(bits);
509 if (codePtr + repeat > endCodePtr) {
510 ziperr("Bad repeat code");
513 if (val == 16) {
514 if (codePtr == codelen) {
515 ziperr("Bad repeat code");
516 goto error;
518 memset(codePtr, codePtr[-1], repeat);
519 } else {
520 /* The values have already been set to zero, above, so
521 * we don't have to do anything */
523 codePtr += repeat;
524 } else {
525 ziperr("Bad code-length code");
526 goto error;
531 lcodes = makeCodeTable(state, codelen, hlit, MAX_QUICK_LXL);
532 if (lcodes == NULL) {
533 goto error;
536 dcodes = makeCodeTable(state, codelen + hlit, hdist, MAX_QUICK_CXD);
537 if (dcodes == NULL) {
538 goto error;
541 *lcodesPtr = lcodes;
542 *dcodesPtr = dcodes;
543 STORE_IN;
544 if (!JAR_INFLATER_INSIDE_KVM) {
545 freeBytes(ccodes);
547 return TRUE;
549 error:
550 if (!JAR_INFLATER_INSIDE_KVM) {
551 freeBytes(ccodes);
552 freeBytes(dcodes);
553 freeBytes(lcodes);
555 return FALSE;
559 /*=========================================================================
560 * FUNCTION: makeCodeTable
561 * TYPE: Huffman code table creation
562 * INTERFACE:
563 * parameters: inflaterState
564 * code length
565 * number of elements of the alphabet
566 * maxQuickBits
567 * returns: Huffman code table created if successful or
568 * NULL if an error occurs
569 *=======================================================================*/
571 HuffmanCodeTable * makeCodeTable(
572 inflaterState *state,
573 unsigned char *codelen, /* Code lengths */
574 unsigned numElems, /* Number of elements of the alphabet */
575 unsigned maxQuickBits) /* If the length of a code is longer than
576 * <maxQuickBits> number of bits, the code is
577 * stored in the sequential lookup table
578 * instead of the quick lookup array. */
580 unsigned int bitLengthCount[MAX_BITS + 1];
581 unsigned int codes[MAX_BITS + 1];
582 unsigned bits, minCodeLen = 0, maxCodeLen = 0;
583 const unsigned char *endCodeLen = codelen + numElems;
584 unsigned int code, quickMask;
585 unsigned char *p;
587 HuffmanCodeTable * table;
588 int mainTableLength, longTableLength, numLongTables;
589 int tableSize;
590 int j;
592 unsigned short *nextLongTable;
594 /* Count the number of codes for each code length */
595 memset(bitLengthCount, 0, sizeof(bitLengthCount));
596 for (p = codelen; p < endCodeLen; p++) {
597 bitLengthCount[*p]++;
600 if (bitLengthCount[0] == numElems) {
601 ziperr("Bad code table -- empty");
602 return NULL;
605 /* Find the minimum and maximum. It's faster to do it in a separate
606 * loop that goes 1..MAX_BITS, than in the above loop that looks at
607 * every code element */
608 code = minCodeLen = maxCodeLen = 0;
609 for (bits = 1; bits <= MAX_BITS; bits++) {
610 codes[bits] = code;
611 if (bitLengthCount[bits] != 0) {
612 if (minCodeLen == 0) minCodeLen = bits;
613 maxCodeLen = bits;
614 code += bitLengthCount[bits] << (MAX_BITS - bits);
618 if (INCLUDEDEBUGCODE) {
619 if (code != (1 << MAX_BITS)) {
620 code += (1 << (MAX_BITS - maxCodeLen));
621 if (code != (1 << MAX_BITS)) {
622 ziperr("Unexpected bit codes");
627 /* Calculate the size of the code table and allocate it. */
628 if (maxCodeLen <= maxQuickBits) {
629 /* We don't need any subtables. We may even be able to get
630 * away with a table smaller than maxCodeLen
631 */
632 maxQuickBits = maxCodeLen;
633 mainTableLength = (1 << maxCodeLen);
634 numLongTables = longTableLength = 0;
635 } else {
636 mainTableLength = (1 << maxQuickBits);
637 numLongTables = (1 << MAX_BITS) - codes[maxQuickBits + 1];
638 numLongTables = numLongTables >> (MAX_BITS - maxQuickBits);
639 longTableLength = 1 << (maxCodeLen - maxQuickBits);
641 ASSERT(mainTableLength == 1 << maxQuickBits);
642 tableSize = sizeof(HuffmanCodeTableHeader)
643 + (mainTableLength + numLongTables * longTableLength) *
644 sizeof(table->entries[0]);
645 table = (HuffmanCodeTable*)mallocBytes(tableSize);
646 nextLongTable = &table->entries[mainTableLength];
647 MAKE_TEMPORARY_ROOT(table);
649 memset(table, 0, tableSize);
651 table->h.maxCodeLen = maxCodeLen;
652 table->h.quickBits = maxQuickBits;
654 quickMask = (1 << maxQuickBits) - 1;
656 for (p = codelen; p < endCodeLen; p++) {
657 unsigned short huff;
658 bits = *p;
659 if (bits == 0) {
660 continue;
662 /* Get the next code of the current length */
663 code = codes[bits];
664 codes[bits] += 1 << (MAX_BITS - bits);
665 code = REVERSE_15BITS(code);
666 huff = ((p - codelen) << 4) + bits;
667 if (bits <= maxQuickBits) {
668 unsigned stride = 1 << bits;
669 for (j = code; j < mainTableLength; j += stride) {
670 table->entries[j] = huff;
671 }
672 } else {
673 unsigned short *thisLongTable;
674 unsigned stride = 1 << (bits - maxQuickBits);
675 unsigned int prefixCode = code & quickMask;
676 unsigned int suffixCode = code >> maxQuickBits;
677 if (table->entries[prefixCode] == 0) {
678 /* This in the first long code with the indicated prefix.
679 * Create a pointer to the subtable */
680 long delta = (char *)nextLongTable - (char *)table;
681 table->entries[prefixCode] = (unsigned short)(HUFFINFO_LONG_MASK | delta);
682 thisLongTable = nextLongTable;
683 nextLongTable += longTableLength;
684 } else {
685 long delta = table->entries[prefixCode] & ~HUFFINFO_LONG_MASK;
686 thisLongTable = (unsigned short *)((char *)table + delta);
688 for (j = suffixCode; j < longTableLength; j += stride) {
689 thisLongTable[j] = huff;
692 if (INCLUDEDEBUGCODE && inflateVerbose > 0) {
693 putchar(' ');
695 for (j = 15; j >= 0; j--) {
696 char c = (j >= (signed)bits) ? ' '
697 : (code & (1 << j)) ? '1' : '0';
698 putchar(c);
700 fprintf(stdout,
701 " Char = %02x, Code = %4x\n", (p - codelen), code);
705 ASSERT(nextLongTable == &table->entries[mainTableLength + numLongTables * longTableLength]);
707 return table;
711 #if INCLUDEDEBUGCODE
713 /*=========================================================================
714 * FUNCTION: ziperr
715 * TYPE: zip error processing function
716 * OVERVIEW: Used by inflate() and other functions for zip error processing.
717 * INTERFACE:
718 * parameters: const char *message
719 *
720 * returns: nothing
721 *=======================================================================*/
722 static void
723 ziperr(const char *message) {
724 fprintf(stderr, "Zip Error: %s\n", message);
727 #endif