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>
48 /*=========================================================================
49 * Globals and extern declarations
50 *=======================================================================*/
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
110 {
111 inflaterState stateStruct;
112 bool_t result;
113 /* Temporarily define state, so that LOAD_IN, LOAD_OUT, etc. macros work */
114 #define state (&stateStruct)
124 #ifdef JAR_DEBUG_FILE
125 {
131 }
140 }
141 }
142 #endif
146 DECLARE_IN_VARIABLES
148 LOAD_IN;
152 STORE_IN;
170 START_TEMPORARY_ROOTS
172 END_TEMPORARY_ROOTS
174 }
178 }
179 }
187 }
189 #ifdef JAR_DEBUG_FILE
192 }
193 #endif
195 /* Remove temporary definition of state defined above */
196 #undef state
199 }
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
213 {
214 DECLARE_IN_VARIABLES
215 DECLARE_OUT_VARIABLES
242 }
245 }
246 STORE_IN;
247 STORE_OUT;
249 }
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
264 {
265 DECLARE_IN_VARIABLES
266 DECLARE_OUT_VARIABLES
278 }
281 }
283 LOAD_IN;
284 LOAD_OUT;
289 }
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. */
304 /* A 7-bit code */
314 }
317 }
321 #ifdef JAR_DEBUG_FILE
324 }
325 #endif
329 }
342 /* The NEEDBITS(..) above took care of this */
352 }
357 }
375 #ifdef JAR_DEBUG_FILE
379 }
380 #endif
382 }
383 }
384 }
385 }
387 done_loop:
388 STORE_IN;
389 STORE_OUT;
394 }
396 }
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
416 DECLARE_IN_VARIABLES
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) */
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
445 *
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 */
456 }
459 }
464 }
467 /*
468 * HLIT + 257 code lengths for the literal/length alphabet,
469 * encoded using the code length Huffman code.
470 *
471 * HDIST + 1 code lengths for the distance alphabet,
472 * encoded using the code length Huffman code.
473 *
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 */
486 }
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 */
511 }
517 }
520 /* The values have already been set to zero, above, so
521 * we don't have to do anything */
522 }
527 }
528 }
534 }
539 }
543 STORE_IN;
546 }
549 error:
554 }
556 }
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 *=======================================================================*/
576 * <maxQuickBits> number of bits, the code is
577 * stored in the sequential lookup table
578 * instead of the quick lookup array. */
579 {
594 /* Count the number of codes for each code length */
598 }
603 }
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 */
615 }
616 }
623 }
624 }
625 }
627 /* Calculate the size of the code table and allocate it. */
629 /* We don't need any subtables. We may even be able to get
630 * away with a table smaller than maxCodeLen
631 */
640 }
661 }
662 /* Get the next code of the current length */
671 }
678 /* This in the first long code with the indicated prefix.
679 * Create a pointer to the subtable */
687 }
690 }
691 }
699 }
702 }
704 }
708 }
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
725 }
727 #endif