1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
6 import java.util.zip.*;
9 /** Reads a CAB file structure */
12 /** reads a CAB file, parses it, and returns an InputStream representing the named file */
13 public static InputStream getFileInputStream(InputStream is, String fileName) throws IOException, EOFException {
14 return getFileInputStream(is, 0, fileName);
17 public static InputStream getFileInputStream(InputStream is, int skipHeaders, String fileName) throws IOException, EOFException {
18 DataInputStream dis = new DataInputStream(is);
19 CFHEADER h = new CFHEADER();
21 while (skipHeaders > 0) { CFHEADER.seekMSCF(dis); skipHeaders--; }
25 } catch (CFHEADER.BogusHeaderException bhe) {
26 throw new EOFException();
29 for(int i=0; i<h.folders.length; i++) {
30 CFFOLDER f = new CFFOLDER(h);
33 } catch (CFFOLDER.UnsupportedCompressionTypeException ucte) {
34 throw new IOException(ucte.toString());
38 for(int i=0; i<h.files.length; i++) {
39 CFFILE f = new CFFILE(h);
43 for(int i=0; i<h.folders.length; i++) {
44 InputStream is2 = new CFFOLDERInputStream(h.folders[i], dis);
45 for(int j=0; j<h.folders[i].files.size(); j++) {
46 CFFILE file = (CFFILE)h.folders[i].files.elementAt(j);
47 if (file.fileName.equals(fileName)) return new LimitStream(is2, file.fileSize);
48 byte[] b = new byte[file.fileSize];
49 int read = is2.read(b);
56 private static class LimitStream extends FilterInputStream {
58 public LimitStream(InputStream is, int limit) {
62 public int read() throws IOException {
63 if (limit == 0) return -1;
64 int ret = super.read();
65 if (ret != -1) limit--;
68 public int read(byte[] b, int off, int len) throws IOException {
69 if (len > limit) len = limit;
70 if (limit == 0) return -1;
71 int ret = super.read(b, off, len);
77 /** Encapsulates a CFHEADER entry */
78 public static class CFHEADER {
79 byte[] reserved1 = new byte[4]; // reserved
80 int fileSize = 0; // size of this cabinet file in bytes
81 byte[] reserved2 = new byte[4]; // reserved
82 int offsetOfFirstCFFILEEntry; // offset of the first CFFILE entry
83 byte[] reserved3 = new byte[4]; // reserved
84 byte versionMinor = 3; // cabinet file format version, minor
85 byte versionMajor = 1; // cabinet file format version, major
86 boolean prevCAB = false; // true iff there is a cabinet before this one in a sequence
87 boolean nextCAB = false; // true iff there is a cabinet after this one in a sequence
88 boolean hasReserved = false; // true iff the cab has per-{cabinet, folder, block} reserved areas
89 int setID = 0; // must be the same for all cabinets in a set
90 int indexInCabinetSet = 0; // number of this cabinet file in a set
91 byte perCFFOLDERReservedSize = 0; // (optional) size of per-folder reserved area
92 byte perDatablockReservedSize = 0; // (optional) size of per-datablock reserved area
93 byte[] perCabinetReservedArea = null; // per-cabinet reserved area
94 String previousCabinet = null; // name of previous cabinet file in a set
95 String previousDisk = null; // name of previous disk in a set
96 String nextCabinet = null; // name of next cabinet in a set
97 String nextDisk = null; // name of next disk in a set
99 CFFOLDER[] folders = new CFFOLDER[0];
100 CFFILE[] files = new CFFILE[0];
102 int readCFFOLDERs = 0; // the number of folders read in so far
103 int readCFFILEs = 0; // the number of folders read in so far
105 public CFHEADER() { }
107 public void print(PrintStream ps) {
108 ps.println("CAB CFFILE CFHEADER v" + ((int)versionMajor) + "." + ((int)versionMinor));
109 ps.println(" total file size = " + fileSize);
110 ps.println(" offset of first file = " + offsetOfFirstCFFILEEntry);
111 ps.println(" total folders = " + folders.length);
112 ps.println(" total files = " + files.length);
113 ps.println(" flags = 0x" +
114 Integer.toString((prevCAB ? 0x1 : 0x0) |
115 (nextCAB ? 0x2 : 0x0) |
116 (hasReserved ? 0x4 : 0x0), 16) + " [ " +
117 (prevCAB ? "prev " : "") +
118 (nextCAB ? "next " : "") +
119 (hasReserved ? "reserve_present " : "") + "]");
120 ps.println(" set id = " + setID);
121 ps.println(" index in set = " + indexInCabinetSet);
122 ps.println(" header reserved area #1 =" +
123 " 0x" + Integer.toString(reserved1[0], 16) +
124 " 0x" + Integer.toString(reserved1[1], 16) +
125 " 0x" + Integer.toString(reserved1[2], 16) +
126 " 0x" + Integer.toString(reserved1[3], 16));
127 ps.println(" header reserved area #2 =" +
128 " 0x" + Integer.toString(reserved2[0], 16) +
129 " 0x" + Integer.toString(reserved2[1], 16) +
130 " 0x" + Integer.toString(reserved2[2], 16) +
131 " 0x" + Integer.toString(reserved2[3], 16));
132 ps.println(" header reserved area #3 =" +
133 " 0x" + Integer.toString(reserved3[0], 16) +
134 " 0x" + Integer.toString(reserved3[1], 16) +
135 " 0x" + Integer.toString(reserved3[2], 16) +
136 " 0x" + Integer.toString(reserved3[3], 16));
138 if (perCabinetReservedArea != null) {
139 ps.print(" per-cabinet reserved area = ");
140 for(int i=0; i<perCabinetReservedArea.length; i++)
141 ps.print(((perCabinetReservedArea[i] & 0xff) < 16 ? "0" : "") +
142 Integer.toString(perCabinetReservedArea[i] & 0xff, 16) + " ");
145 ps.println(" per folder reserved area = " + perCFFOLDERReservedSize + " bytes");
146 ps.println(" per block reserved area = " + perDatablockReservedSize + " bytes");
150 public String toString() {
152 "[ CAB CFFILE CFHEADER v" +
153 ((int)versionMajor) + "." + ((int)versionMinor) + ", " +
154 fileSize + " bytes, " +
155 folders.length + " folders, " +
156 files.length + " files] ";
159 /** fills in all fields in the header and positions the stream at the first folder */
160 public void read(DataInputStream dis) throws IOException, BogusHeaderException {
162 dis.readFully(reserved1);
164 byte[] headerHashable = new byte[28];
165 dis.readFully(headerHashable);
166 DataInputStream hhis = new DataInputStream(new ByteArrayInputStream(headerHashable));
168 fileSize = readLittleInt(hhis);
169 hhis.readFully(reserved2);
170 offsetOfFirstCFFILEEntry = readLittleInt(hhis);
171 hhis.readFully(reserved3);
172 versionMinor = hhis.readByte();
173 versionMajor = hhis.readByte();
174 folders = new CFFOLDER[readLittleShort(hhis)];
175 files = new CFFILE[readLittleShort(hhis)];
176 int flags = readLittleShort(hhis);
177 prevCAB = (flags & 0x0001) != 0;
178 nextCAB = (flags & 0x0002) != 0;
179 hasReserved = (flags & 0x0004) != 0;
180 setID = readLittleShort(hhis);
181 indexInCabinetSet = readLittleShort(hhis);
183 if (offsetOfFirstCFFILEEntry < 0 || fileSize < 0) {
184 throw new BogusHeaderException();
188 perCabinetReservedArea = new byte[readLittleShort(dis)];
189 perCFFOLDERReservedSize = dis.readByte();
190 perDatablockReservedSize = dis.readByte();
191 if (perCabinetReservedArea.length > 0)
192 dis.readFully(perCabinetReservedArea);
197 previousCabinet = readZeroTerminatedString(dis);
198 previousDisk = readZeroTerminatedString(dis);
201 nextCabinet = readZeroTerminatedString(dis);
202 nextDisk = readZeroTerminatedString(dis);
204 } catch (ArrayIndexOutOfBoundsException e) {
205 throw new BogusHeaderException();
209 public static void seekMSCF(DataInputStream dis) throws EOFException, IOException
212 // skip up to and including the 'MSCF' signature
216 while (state == 0 && dis.readByte() != 0x4D) { }
219 switch (dis.readByte()) {
220 case 0x53 : state = 2; break;
221 case 0x4D : state = 1; continue;
222 default : state = 0; continue;
225 if (dis.readByte() == 0x43) { state = 3; }
226 else { state = 0; continue; }
228 if (dis.readByte() == 0x46) { state = 4; }
233 public static class BogusHeaderException extends Exception {}
236 /** Encapsulates a CFFOLDER entry */
237 public static class CFFOLDER {
238 public static final int COMPRESSION_NONE = 0;
239 public static final int COMPRESSION_MSZIP = 1;
240 public static final int COMPRESSION_QUANTUM = 2;
241 public static final int COMPRESSION_LZX = 3;
243 int firstBlockOffset = 0; // offset of first data block within this folder
244 int numBlocks = 0; // number of data blocks
245 int compressionType = 0; // compression type for this folder
246 byte[] reservedArea = null; // per-folder reserved area
247 int indexInCFHEADER = 0; // our index in CFHEADER.folders
248 Vector files = new Vector();
250 private CFHEADER header = null;
252 public CFFOLDER(CFHEADER header) { this.header = header; }
254 public String toString() {
255 return "[ CAB CFFOLDER, " + numBlocks + " data blocks, compression type " +
256 compressionName(compressionType) +
257 ", " + reservedArea.length + " bytes of reserved data ]";
260 public void read(DataInputStream dis) throws IOException, UnsupportedCompressionTypeException {
261 firstBlockOffset = readLittleInt(dis);
262 numBlocks = readLittleShort(dis);
263 compressionType = readLittleShort(dis) & 0x000F;
264 if (compressionType != COMPRESSION_MSZIP) {
265 throw new UnsupportedCompressionTypeException(compressionType);
267 reservedArea = new byte[header.perCFFOLDERReservedSize];
268 if (reservedArea.length > 0) dis.readFully(reservedArea);
269 indexInCFHEADER = header.readCFFOLDERs++;
270 header.folders[indexInCFHEADER] = this;
273 public static String compressionName(int type) {
275 case COMPRESSION_NONE:
277 case COMPRESSION_MSZIP:
279 case COMPRESSION_QUANTUM:
281 case COMPRESSION_LZX:
284 return "<Unknown type " + type + ">";
288 public static class UnsupportedCompressionTypeException extends Exception {
289 private int compressionType;
291 UnsupportedCompressionTypeException(int type) {
292 compressionType = type;
294 public String toString() {
295 return "UnsupportedCompressionTypeException: no support for compression type " + compressionName(compressionType);
300 /** Encapsulates a CFFILE entry */
301 public static class CFFILE {
302 int fileSize = 0; // size of this file
303 int uncompressedOffsetInCFFOLDER = 0; // offset of this file within the folder, not accounting for compression
304 int folderIndex = 0; // index of the CFFOLDER we belong to
305 Date date = null; // modification date
306 int attrs = 0; // attrs
307 boolean readOnly = false; // read-only flag
308 boolean hidden = false; // hidden flag
309 boolean system = false; // system flag
310 boolean arch = false; // archive flag
311 boolean runAfterExec = false; // true if file should be run during extraction
312 boolean UTFfileName = false; // true if filename is UTF-encoded
313 String fileName = null; // filename
314 int indexInCFHEADER = 0; // our index in CFHEADER.files
315 CFFOLDER folder = null; // the folder we belong to
316 private CFHEADER header = null;
319 public CFFILE(CFHEADER header) { this.header = header; }
321 public CFFILE(File f, String pathName) throws IOException {
322 fileSize = (int)f.length();
324 date = new java.util.Date(f.lastModified());
329 public String toString() {
330 return "[ CAB CFFILE: " + fileName + ", " + fileSize + " bytes [ " +
331 (readOnly ? "readonly " : "") +
332 (system ? "system " : "") +
333 (hidden ? "hidden " : "") +
334 (arch ? "arch " : "") +
335 (runAfterExec ? "run_after_exec " : "") +
336 (UTFfileName ? "UTF_filename " : "") +
340 public void read(DataInputStream dis) throws IOException {
341 fileSize = readLittleInt(dis);
342 uncompressedOffsetInCFFOLDER = readLittleInt(dis);
343 folderIndex = readLittleShort(dis);
344 readLittleShort(dis); // date
345 readLittleShort(dis); // time
346 attrs = readLittleShort(dis);
347 readOnly = (attrs & 0x1) != 0;
348 hidden = (attrs & 0x2) != 0;
349 system = (attrs & 0x4) != 0;
350 arch = (attrs & 0x20) != 0;
351 runAfterExec = (attrs & 0x40) != 0;
352 UTFfileName = (attrs & 0x80) != 0;
353 fileName = readZeroTerminatedString(dis);
355 indexInCFHEADER = header.readCFFILEs++;
356 header.files[indexInCFHEADER] = this;
357 folder = header.folders[folderIndex];
358 folder.files.addElement(this);
365 // Compressing Input and Output Streams ///////////////////////////////////////////////
367 /** an InputStream that decodes CFDATA blocks belonging to a CFFOLDER */
368 private static class CFFOLDERInputStream extends InputStream {
371 InputStream iis = null;
373 byte[] compressed = new byte[128 * 1024];
374 byte[] uncompressed = new byte[256 * 1024];
376 public CFFOLDERInputStream(CFFOLDER f, DataInputStream dis) {
381 InputStream readBlock() throws IOException {
382 int checksum = readLittleInt(dis);
383 int compressedBytes = readLittleShort(dis);
384 int unCompressedBytes = readLittleShort(dis);
385 byte[] reserved = new byte[/*folder.header.perDatablockReservedSize*/0];
386 if (reserved.length > 0) dis.readFully(reserved);
387 if (dis.readByte() != 0x43) throw new CABException("malformed block header");
388 if (dis.readByte() != 0x4B) throw new CABException("malformed block header");
390 dis.readFully(compressed, 0, compressedBytes - 2);
392 Inflater i = new Inflater(true);
393 i.setInput(compressed, 0, compressedBytes - 2);
395 if (unCompressedBytes > uncompressed.length) uncompressed = new byte[unCompressedBytes];
396 try { i.inflate(uncompressed, 0, uncompressed.length);
397 } catch (DataFormatException dfe) {
398 dfe.printStackTrace();
399 throw new CABException(dfe.toString());
401 return new ByteArrayInputStream(uncompressed, 0, unCompressedBytes);
404 public int available() throws IOException { return iis == null ? 0 : iis.available(); }
405 public void close() throws IOException { iis.close(); }
406 public void mark(int i) { }
407 public boolean markSupported() { return false; }
408 public void reset() { }
410 public long skip(long l) throws IOException {
411 if (iis == null) iis = readBlock();
414 long numread = iis.skip(l - ret);
415 if (numread == 0 || numread == -1) iis = readBlock();
421 public int read(byte[] b, int off, int len) throws IOException {
422 if (iis == null) iis = readBlock();
425 int numread = iis.read(b, off + ret, len - ret);
426 if (numread == 0 || numread == -1) iis = readBlock();
432 public int read() throws IOException {
433 if (iis == null) iis = readBlock();
434 int ret = iis.read();
445 // Misc Stuff //////////////////////////////////////////////////////////////
447 public static String readZeroTerminatedString(DataInputStream dis) throws IOException {
449 byte[] b = new byte[256];
451 byte next = dis.readByte();
452 if (next == 0x0) return new String(b, 0, numBytes);
453 b[numBytes++] = next;
457 public static int readLittleInt(DataInputStream dis) throws IOException {
458 int lowest = (int)(dis.readByte() & 0xff);
459 int low = (int)(dis.readByte() & 0xff);
460 int high = (int)(dis.readByte() & 0xff);
461 int highest = (int)(dis.readByte() & 0xff);
462 return (highest << 24) | (high << 16) | (low << 8) | lowest;
465 public static int readLittleShort(DataInputStream dis) throws IOException {
466 int low = (int)(dis.readByte() & 0xff);
467 int high = (int)(dis.readByte() & 0xff);
468 return (high << 8) | low;
471 public static class CABException extends IOException {
472 public CABException(String s) { super(s); }
476 /** scratch space for isToByteArray() */
477 static byte[] workspace = new byte[16 * 1024];
479 /** Trivial method to completely read an InputStream */
480 public static synchronized byte[] isToByteArray(InputStream is) throws IOException {
483 int numread = is.read(workspace, pos, workspace.length - pos);
484 if (numread == -1) break;
485 else if (pos + numread < workspace.length) pos += numread;
488 byte[] temp = new byte[workspace.length * 2];
489 System.arraycopy(workspace, 0, temp, 0, workspace.length);
493 byte[] ret = new byte[pos];
494 System.arraycopy(workspace, 0, ret, 0, pos);