DEADSOFTWARE

Remove exception catching in main module
[mp3cc.git] / mpc / preverifier / jar.h
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: KVM
22 * SUBSYSTEM: JAR file reader.
23 * FILE: jar.h
24 * OVERVIEW: Public header file for the JAR file reader module.
25 * The JAR DataStream API is used by the Pre-verifier for loading
26 * JAR files.
27 * AUTHOR: Tasneem Sayeed
28 *=======================================================================*/
30 #ifndef _JAR_H_
31 #define _JAR_H_
33 #include "typedefs.h"
34 #include "stdio.h"
35 #include "sys/types.h"
36 #include "stddef.h"
39 /*
40 * Debug flag for JAR support
41 */
42 #define JAR_DEBUG 0
44 /*=========================================================================
45 * JAR file reader defines and macros
46 *=======================================================================*/
48 /*
49 * Supported compression types
50 */
51 #define STORED 0
52 #define DEFLATED 8
54 #define MAX_BITS 15 /* Maximum number of codes in Huffman Code Table */
56 /*
57 * Header sizes including signatures
58 */
59 #define LOCHDRSIZ 30
60 #define CENHDRSIZ 46
61 #define ENDHDRSIZ 22
63 /*
64 * Header field access macros
65 */
66 #define CH(b, n) ((long)(((unsigned char *)(b))[n]))
67 #define SH(b, n) ((long)(CH(b, n) | (CH(b, n+1) << 8)))
68 #define LG(b, n) ((long)(SH(b, n) | (SH(b, n+2) << 16)))
70 #define GETSIG(b) LG(b, 0) /* signature */
72 #define LOCSIG (('P' << 0) + ('K' << 8) + (3 << 16) + (4 << 24))
73 #define CENSIG (('P' << 0) + ('K' << 8) + (1 << 16) + (2 << 24))
74 #define ENDSIG (('P' << 0) + ('K' << 8) + (5 << 16) + (6 << 24))
76 #define freeBytes(x) if (x == NULL) {} else free(x)
79 /*
80 * Macros for getting local file header (LOC) fields
81 */
82 #define LOCVER(b) SH(b, 4) /* version needed to extract */
83 #define LOCFLG(b) SH(b, 6) /* encrypt flags */
84 #define LOCHOW(b) SH(b, 8) /* compression method */
85 #define LOCTIM(b) LG(b, 10) /* modification time */
86 #define LOCCRC(b) LG(b, 14) /* uncompressed file crc-32 value */
87 #define LOCSIZ(b) LG(b, 18) /* compressed size */
88 #define LOCLEN(b) LG(b, 22) /* uncompressed size */
89 #define LOCNAM(b) SH(b, 26) /* filename size */
90 #define LOCEXT(b) SH(b, 28) /* extra field size */
92 /*
93 * Macros for getting central directory header (CEN) fields
94 */
95 #define CENVEM(b) SH(b, 4) /* version made by */
96 #define CENVER(b) SH(b, 6) /* version needed to extract */
97 #define CENFLG(b) SH(b, 8) /* general purpose bit flags */
98 #define CENHOW(b) SH(b, 10) /* compression method */
99 #define CENTIM(b) LG(b, 12) /* file modification time (DOS format) */
100 #define CENCRC(b) LG(b, 16) /* crc of uncompressed data */
101 #define CENSIZ(b) LG(b, 20) /* compressed size */
102 #define CENLEN(b) LG(b, 24) /* uncompressed size */
103 #define CENNAM(b) SH(b, 28) /* length of filename */
104 #define CENEXT(b) SH(b, 30) /* length of extra field */
105 #define CENCOM(b) SH(b, 32) /* file comment length */
106 #define CENDSK(b) SH(b, 34) /* disk number start */
107 #define CENATT(b) SH(b, 36) /* internal file attributes */
108 #define CENATX(b) LG(b, 38) /* external file attributes */
109 #define CENOFF(b) LG(b, 42) /* offset of local header */
111 /*
112 * Macros for getting end of central directory header (END) fields
113 */
114 #define ENDSUB(b) SH(b, 8) /* number of entries on this disk */
115 #define ENDTOT(b) SH(b, 10) /* total number of entries */
116 #define ENDSIZ(b) LG(b, 12) /* central directory size */
117 #define ENDOFF(b) LG(b, 16) /* central directory offset */
118 #define ENDCOM(b) SH(b, 20) /* size of zip file comment */
120 /*=========================================================================
121 * Macros for Huffman Codes used by the JAR file reader
122 *=======================================================================*/
124 /*
125 * This is the algorithm for decoding Huffman-encoded
126 * data.
128 * loop (until end of block code recognized)
129 * decode literal/length value from input stream
130 * if value < 256
131 * copy value (literal byte) to output stream
132 * otherwise
133 * if value = end of block (256)
134 * break from loop
135 * otherwise (value = 257..285)
136 * decode distance from input stream
138 * move backwards distance bytes in the output
139 * stream, and copy length bytes from this
140 * position to the output stream.
141 * end loop
142 */
143 #define BTYPE_NO_COMPRESSION 0x00
144 #define BTYPE_FIXED_HUFFMAN 0x01 /* Fixed Huffman Code */
145 #define BTYPE_DYNA_HUFFMAN 0x02 /* Dynamic Huffman code */
146 #define BTYPE_INVALID 0x03 /* Invalid code */
148 #define LITXLEN_BASE 257
151 /*=========================================================================
152 * JAR DataStream API for reading and writing to/from JAR files
153 *=======================================================================*/
155 #define JAR_READ 1 /* Mode for reading from a JAR data stream */
156 #define JAR_WRITE 2 /* Mode for writing from a JAR data stream */
157 #define JAR_RESOURCE 1 /* type of resource (see JAR_DataStream) */
159 /*=========================================================================
160 * JAR Data Stream structure
161 *=======================================================================*/
162 typedef struct JAR_DataStream {
163 unsigned char *data; /* data stream for reading/writing */
164 int type; /* indicates type of resource */
165 int dataLen; /* length of data stream */
166 int dataIndex; /* current position for reading */
167 int mode; /* mode for reading or writing */
168 /* mode must be either JAR_READ or JAR_WRITE */
169 } JAR_DataStream;
171 typedef struct JAR_DataStream* JAR_DataStreamPtr;
173 /*=========================================================================
174 * Forward declarations for JAR DataStream API
175 *=======================================================================*/
176 int JAR_ReadBytes(JAR_DataStream *ds, char *buf, int len);
177 int JAR_WriteBytes(JAR_DataStream *ds, char *buf, int len);
178 int JAR_SkipBytes(JAR_DataStream *ds, int len);
180 /*=========================================================================
181 * Forward declarations for JAR file reader
182 *=======================================================================*/
186 typedef FILE *JarCompressedType;
187 /*
188 typedef const unsigned char *JarCompressedType;
189 */
191 bool_t inflate(JarCompressedType data, int compLen,
192 unsigned char *decompData, int decompLen);
194 /* Any caller to inflate must ensure that it is safe to read at least
195 * this many bytes beyond compData + compLen
196 */
197 #define INFLATER_EXTRA_BYTES 4
200 /*
201 * Indicates whether JAR inflater is executed from KVM or a stand-alone
202 * program.
203 */
204 #define JAR_INFLATER_INSIDE_KVM 0
206 #endif /* _JAR_H_ */