mass rename and rebranding from xwt to ibex - fixed to use ixt files
[org.ibex.core.git] / src / org / ibex / util / CAB.java
1 // Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
2 //
3 // You may modify, copy, and redistribute this code under the terms of
4 // the GNU Library Public License version 2.1, with the exception of
5 // the portion of clause 6a after the semicolon (aka the "obnoxious
6 // relink clause")
7
8 package org.ibex.util;
9
10 import java.io.*;
11 import java.util.*;
12 import java.util.zip.*;
13
14 /** Reads a CAB file structure */
15 public class CAB {
16
17     /** reads a CAB file, parses it, and returns an InputStream representing the named file */
18     public static InputStream getFileInputStream(InputStream is, String fileName) throws IOException, EOFException {
19       return getFileInputStream(is, 0, fileName);
20     }
21
22     public static InputStream getFileInputStream(InputStream is, int skipHeaders, String fileName) throws IOException, EOFException {
23         DataInputStream dis = new DataInputStream(is);
24         CFHEADER h = new CFHEADER();
25
26         while (skipHeaders > 0) { CFHEADER.seekMSCF(dis); skipHeaders--; }
27
28         try {
29          h.read(dis);
30         } catch (CFHEADER.BogusHeaderException bhe) {
31          throw new EOFException();
32         }
33
34         for(int i=0; i<h.folders.length; i++) {
35             CFFOLDER f = new CFFOLDER(h);
36             try {
37                f.read(dis);
38             } catch (CFFOLDER.UnsupportedCompressionTypeException ucte) {
39                throw ucte;
40             }
41         }
42
43         for(int i=0; i<h.files.length; i++) {
44             CFFILE f = new CFFILE(h);
45             f.read(dis);
46         }
47         
48         for(int i=0; i<h.folders.length; i++) {
49             InputStream is2 = new CFFOLDERInputStream(h.folders[i], dis);
50             for(int j=0; j<h.folders[i].files.size(); j++) {
51                 CFFILE file = (CFFILE)h.folders[i].files.elementAt(j);
52                 if (file.fileName.equals(fileName)) return new LimitStream(is2, file.fileSize);
53                 byte[] b = new byte[file.fileSize];
54                 int read = is2.read(b);
55             }
56         }
57
58         return null;
59     }
60
61     private static class LimitStream extends FilterInputStream {
62         int limit;
63         public LimitStream(InputStream is, int limit) {
64             super(is);
65             this.limit = limit;
66         }
67         public int read() throws IOException {
68             if (limit == 0) return -1;
69             int ret = super.read();
70             if (ret != -1) limit--;
71             return ret;
72         }
73         public int read(byte[] b, int off, int len) throws IOException {
74             if (len > limit) len = limit;
75             if (limit == 0) return -1;
76             int ret = super.read(b, off, len);
77             limit -= ret;
78             return ret;
79         }
80     }
81     
82     /** Encapsulates a CFHEADER entry */
83     public static class CFHEADER {
84         byte[]   reserved1 = new byte[4];       // reserved
85         int      fileSize = 0;                  // size of this cabinet file in bytes
86         byte[]   reserved2 = new byte[4];       // reserved
87         int      offsetOfFirstCFFILEEntry;      // offset of the first CFFILE entry
88         byte[]   reserved3 = new byte[4];       // reserved
89         byte     versionMinor = 3;              // cabinet file format version, minor
90         byte     versionMajor = 1;              // cabinet file format version, major
91         boolean  prevCAB = false;               // true iff there is a cabinet before this one in a sequence
92         boolean  nextCAB = false;               // true iff there is a cabinet after this one in a sequence
93         boolean  hasReserved = false;           // true iff the cab has per-{cabinet, folder, block} reserved areas
94         int      setID = 0;                     // must be the same for all cabinets in a set
95         int      indexInCabinetSet = 0;         // number of this cabinet file in a set
96         byte     perCFFOLDERReservedSize = 0;   // (optional) size of per-folder reserved area
97         byte     perDatablockReservedSize = 0;  // (optional) size of per-datablock reserved area
98         byte[]   perCabinetReservedArea = null; // per-cabinet reserved area
99         String   previousCabinet = null;        // name of previous cabinet file in a set
100         String   previousDisk = null;           // name of previous disk in a set
101         String   nextCabinet = null;            // name of next cabinet in a set
102         String   nextDisk = null;               // name of next disk in a set
103
104         CFFOLDER[] folders = new CFFOLDER[0];
105         CFFILE[] files = new CFFILE[0];
106
107         int readCFFOLDERs = 0;                    // the number of folders read in so far
108         int readCFFILEs = 0;                      // the number of folders read in so far
109
110         public CFHEADER() { }
111
112         public void print(PrintStream ps) {
113             ps.println("CAB CFFILE CFHEADER v" + ((int)versionMajor) + "." + ((int)versionMinor));
114             ps.println("    total file size               = " + fileSize);
115             ps.println("    offset of first file          = " + offsetOfFirstCFFILEEntry);
116             ps.println("    total folders                 = " + folders.length);
117             ps.println("    total files                   = " + files.length);
118             ps.println("    flags                         = 0x" +
119                        Integer.toString((prevCAB ? 0x1 : 0x0) |
120                                         (nextCAB ? 0x2 : 0x0) |
121                                         (hasReserved ? 0x4 : 0x0), 16) + " [ " +
122                        (prevCAB ? "prev " : "") + 
123                        (nextCAB ? "next " : "") + 
124                        (hasReserved ? "reserve_present " : "") + "]");
125             ps.println("    set id                        = " + setID);
126             ps.println("    index in set                  = " + indexInCabinetSet);
127             ps.println("    header reserved area #1       =" +
128                        " 0x" + Integer.toString(reserved1[0], 16) +
129                        " 0x" + Integer.toString(reserved1[1], 16) +
130                        " 0x" + Integer.toString(reserved1[2], 16) +
131                        " 0x" + Integer.toString(reserved1[3], 16));
132             ps.println("    header reserved area #2       =" +
133                        " 0x" + Integer.toString(reserved2[0], 16) +
134                        " 0x" + Integer.toString(reserved2[1], 16) +
135                        " 0x" + Integer.toString(reserved2[2], 16) +
136                        " 0x" + Integer.toString(reserved2[3], 16));
137             ps.println("    header reserved area #3       =" +
138                        " 0x" + Integer.toString(reserved3[0], 16) +
139                        " 0x" + Integer.toString(reserved3[1], 16) +
140                        " 0x" + Integer.toString(reserved3[2], 16) +
141                        " 0x" + Integer.toString(reserved3[3], 16));
142             if (hasReserved) {
143                 if (perCabinetReservedArea != null) {
144                     ps.print("    per-cabinet reserved area     = ");
145                     for(int i=0; i<perCabinetReservedArea.length; i++)
146                         ps.print(((perCabinetReservedArea[i] & 0xff) < 16 ? "0" : "") +
147                                  Integer.toString(perCabinetReservedArea[i] & 0xff, 16) + " ");
148                     ps.println();
149                 }
150                 ps.println("    per folder  reserved area     = " + perCFFOLDERReservedSize + " bytes");
151                 ps.println("    per block   reserved area     = " + perDatablockReservedSize + " bytes");
152             }
153         }
154
155         public String toString() {
156             return
157                 "[ CAB CFFILE CFHEADER v" +
158                 ((int)versionMajor) + "." + ((int)versionMinor) + ", " +
159                 fileSize + " bytes, " +
160                 folders.length + " folders, " +
161                 files.length + " files] ";
162         }
163
164         /** fills in all fields in the header and positions the stream at the first folder */
165         public void read(DataInputStream dis) throws IOException, BogusHeaderException {
166             seekMSCF(dis);
167             dis.readFully(reserved1);
168
169             byte[] headerHashable = new byte[28];
170             dis.readFully(headerHashable);
171             DataInputStream hhis = new DataInputStream(new ByteArrayInputStream(headerHashable));
172
173             fileSize = readLittleInt(hhis);
174             hhis.readFully(reserved2);
175             offsetOfFirstCFFILEEntry = readLittleInt(hhis);
176             hhis.readFully(reserved3);
177             versionMinor = hhis.readByte();
178             versionMajor = hhis.readByte();
179             folders = new CFFOLDER[readLittleShort(hhis)];
180             files = new CFFILE[readLittleShort(hhis)];
181             int flags = readLittleShort(hhis);
182             prevCAB = (flags & 0x0001) != 0;
183             nextCAB = (flags & 0x0002) != 0;
184             hasReserved = (flags & 0x0004) != 0;
185             setID = readLittleShort(hhis);
186             indexInCabinetSet = readLittleShort(hhis);
187
188             if (offsetOfFirstCFFILEEntry < 0 || fileSize < 0) {
189                throw new BogusHeaderException();
190             }
191
192             if (hasReserved) {
193                 perCabinetReservedArea = new byte[readLittleShort(dis)];
194                 perCFFOLDERReservedSize = dis.readByte();
195                 perDatablockReservedSize = dis.readByte();
196                 if (perCabinetReservedArea.length > 0)
197                     dis.readFully(perCabinetReservedArea);
198             }
199
200             try {
201                if (prevCAB) {
202                    previousCabinet = readZeroTerminatedString(dis);
203                    previousDisk = readZeroTerminatedString(dis);
204                }
205                if (nextCAB) {
206                    nextCabinet = readZeroTerminatedString(dis);
207                    nextDisk = readZeroTerminatedString(dis);
208                }
209             } catch (ArrayIndexOutOfBoundsException e) {
210                throw new BogusHeaderException();
211             }
212         }
213
214         public static void seekMSCF(DataInputStream dis) throws EOFException, IOException
215         {
216            int state;
217            // skip up to and including the 'MSCF' signature
218            state = 0;
219            while (state != 4) {
220               // M
221               while (state == 0 && dis.readByte() != 0x4D) { }
222               state = 1;
223               // S
224               switch (dis.readByte()) {
225                  case 0x53 : state = 2; break;
226                  case 0x4D : state = 1; continue;
227                  default :   state = 0; continue;
228               }
229               // C
230               if (dis.readByte() == 0x43) { state = 3; }
231               else { state = 0; continue; }
232               // F
233               if (dis.readByte() == 0x46) { state = 4; }
234               else { state = 0; }
235            }
236         }
237
238         public static class BogusHeaderException extends IOException {}
239     }
240
241     /** Encapsulates a CFFOLDER entry */
242     public static class CFFOLDER {
243         public static final int COMPRESSION_NONE      = 0;
244         public static final int COMPRESSION_MSZIP     = 1;
245         public static final int COMPRESSION_QUANTUM   = 2;
246         public static final int COMPRESSION_LZX       = 3;
247
248         int      firstBlockOffset = 0;          // offset of first data block within this folder
249         int      numBlocks = 0;                 // number of data blocks
250         int      compressionType = 0;           // compression type for this folder
251         byte[]   reservedArea = null;           // per-folder reserved area
252         int      indexInCFHEADER = 0;           // our index in CFHEADER.folders
253         Vector   files = new Vector();
254
255         private CFHEADER header = null;
256
257         public CFFOLDER(CFHEADER header) { this.header = header; }
258
259         public String toString() {
260             return "[ CAB CFFOLDER, " + numBlocks + " data blocks, compression type " +
261                 compressionName(compressionType) +
262                 ", " + reservedArea.length + " bytes of reserved data ]";
263         }
264
265         public void read(DataInputStream dis) throws IOException, UnsupportedCompressionTypeException {
266             firstBlockOffset = readLittleInt(dis);
267             numBlocks = readLittleShort(dis);
268             compressionType = readLittleShort(dis) & 0x000F;
269             if (compressionType != COMPRESSION_MSZIP) {
270                throw new UnsupportedCompressionTypeException(compressionType);
271             }
272             reservedArea = new byte[header.perCFFOLDERReservedSize];
273             if (reservedArea.length > 0) dis.readFully(reservedArea);
274             indexInCFHEADER = header.readCFFOLDERs++;
275             header.folders[indexInCFHEADER] = this;
276         }
277
278         public static String compressionName(int type) {
279             switch (type) {
280                case COMPRESSION_NONE:
281                   return "NONE";
282                case COMPRESSION_MSZIP:
283                   return "MSZIP";
284                case COMPRESSION_QUANTUM:
285                   return "QUANTUM";
286                case COMPRESSION_LZX:
287                   return "LZX";
288                default:
289                   return "<Unknown type " + type + ">";
290             }
291         }
292
293         public static class UnsupportedCompressionTypeException extends IOException {
294             private int compressionType;
295
296             UnsupportedCompressionTypeException(int type) {
297                compressionType = type;
298             }
299             public String toString() {
300                return "UnsupportedCompressionTypeException: no support for compression type " + compressionName(compressionType);
301             }
302         }
303     }
304
305     /** Encapsulates a CFFILE entry */
306     public static class CFFILE {
307         int fileSize = 0;                       // size of this file
308         int uncompressedOffsetInCFFOLDER = 0;   // offset of this file within the folder, not accounting for compression
309         int folderIndex = 0;                    // index of the CFFOLDER we belong to
310         Date date = null;                       // modification date
311         int attrs = 0;                          // attrs
312         boolean readOnly = false;               // read-only flag
313         boolean hidden = false;                 // hidden flag
314         boolean system = false;                 // system flag
315         boolean arch = false;                   // archive flag
316         boolean runAfterExec = false;           // true if file should be run during extraction
317         boolean UTFfileName = false;            // true if filename is UTF-encoded
318         String fileName = null;                 // filename
319         int indexInCFHEADER = 0;                // our index in CFHEADER.files
320         CFFOLDER folder = null;                 // the folder we belong to
321         private CFHEADER header = null;
322         File myFile;
323
324         public CFFILE(CFHEADER header) { this.header = header; }
325
326         public CFFILE(File f, String pathName) throws IOException {
327             fileSize = (int)f.length();
328             folderIndex = 0;
329             date = new java.util.Date(f.lastModified());
330             fileName = pathName;
331             myFile = f;
332         }
333
334         public String toString() {
335             return "[ CAB CFFILE: " + fileName + ", " + fileSize + " bytes [ " +
336                 (readOnly ? "readonly " : "") +
337                 (system ? "system " : "") +
338                 (hidden ? "hidden " : "") +
339                 (arch ? "arch " : "") +
340                 (runAfterExec ? "run_after_exec " : "") +
341                 (UTFfileName ? "UTF_filename " : "") +
342                 "]";
343         }
344
345         public void read(DataInputStream dis) throws IOException {
346             fileSize = readLittleInt(dis);
347             uncompressedOffsetInCFFOLDER = readLittleInt(dis);
348             folderIndex = readLittleShort(dis);
349             readLittleShort(dis);   // date
350             readLittleShort(dis);   // time
351             attrs = readLittleShort(dis);
352             readOnly = (attrs & 0x1) != 0;
353             hidden = (attrs & 0x2) != 0;
354             system = (attrs & 0x4) != 0;
355             arch = (attrs & 0x20) != 0;
356             runAfterExec = (attrs & 0x40) != 0;
357             UTFfileName = (attrs & 0x80) != 0;
358             fileName = readZeroTerminatedString(dis);
359
360             indexInCFHEADER = header.readCFFILEs++;
361             header.files[indexInCFHEADER] = this;
362             folder = header.folders[folderIndex];
363             folder.files.addElement(this);
364         }
365     }
366
367
368
369
370     // Compressing Input and Output Streams ///////////////////////////////////////////////
371
372     /** an InputStream that decodes CFDATA blocks belonging to a CFFOLDER */
373     private static class CFFOLDERInputStream extends InputStream {
374         CFFOLDER folder;
375         DataInputStream dis;
376         InputStream iis = null;
377
378         byte[] compressed = new byte[128 * 1024];
379         byte[] uncompressed = new byte[256 * 1024];
380
381         public CFFOLDERInputStream(CFFOLDER f, DataInputStream dis) {
382             this.folder = f;
383             this.dis = dis;
384         }
385
386         InputStream readBlock() throws IOException {
387             int checksum = readLittleInt(dis);
388             int compressedBytes = readLittleShort(dis);
389             int unCompressedBytes = readLittleShort(dis);
390             byte[] reserved = new byte[/*folder.header.perDatablockReservedSize*/0];
391             if (reserved.length > 0) dis.readFully(reserved);
392             if (dis.readByte() != 0x43) throw new CABException("malformed block header");
393             if (dis.readByte() != 0x4B) throw new CABException("malformed block header");
394
395             dis.readFully(compressed, 0, compressedBytes - 2);
396
397             Inflater i = new Inflater(true);
398             i.setInput(compressed, 0, compressedBytes - 2);
399             
400             if (unCompressedBytes > uncompressed.length) uncompressed = new byte[unCompressedBytes];
401             try { i.inflate(uncompressed, 0, uncompressed.length);
402             } catch (DataFormatException dfe) {
403                 dfe.printStackTrace();
404                 throw new CABException(dfe.toString());
405             }
406             return new ByteArrayInputStream(uncompressed, 0, unCompressedBytes);
407         }
408
409         public int available() throws IOException { return iis == null ? 0 : iis.available(); }
410         public void close() throws IOException { iis.close(); }
411         public void mark(int i) { }
412         public boolean markSupported() { return false; }
413         public void reset() { }
414
415         public long skip(long l) throws IOException {
416             if (iis == null) iis = readBlock();
417             int ret = 0;
418             while (l > ret) {
419                 long numread = iis.skip(l - ret);
420                 if (numread == 0 || numread == -1) iis = readBlock();
421                 else ret += numread;
422             }
423             return ret;
424         }
425         
426         public int read(byte[] b, int off, int len) throws IOException {
427             if (iis == null) iis = readBlock();
428             int ret = 0;
429             while (len > ret) {
430                 int numread = iis.read(b, off + ret, len - ret);
431                 if (numread == 0 || numread == -1) iis = readBlock();
432                 else ret += numread;
433             }
434             return ret;
435         }
436
437         public int read() throws IOException {
438             if (iis == null) iis = readBlock();
439             int ret = iis.read();
440             if (ret == -1) {
441                 iis = readBlock();
442                 ret = iis.read();
443             }
444             return ret;
445         }
446     }
447
448
449
450     // Misc Stuff //////////////////////////////////////////////////////////////
451
452     public static String readZeroTerminatedString(DataInputStream dis) throws IOException {
453         int numBytes = 0;
454         byte[] b = new byte[256];
455         while(true) {
456             byte next = dis.readByte();
457             if (next == 0x0) return new String(b, 0, numBytes);
458             b[numBytes++] = next;
459         }
460     }
461     
462     public static int readLittleInt(DataInputStream dis) throws IOException {
463         int lowest = (int)(dis.readByte() & 0xff);
464         int low = (int)(dis.readByte() & 0xff);
465         int high = (int)(dis.readByte() & 0xff);
466         int highest = (int)(dis.readByte() & 0xff);
467         return (highest << 24) | (high << 16) | (low << 8) | lowest;
468     }
469
470     public static int readLittleShort(DataInputStream dis) throws IOException {
471         int low = (int)(dis.readByte() & 0xff);
472         int high = (int)(dis.readByte() & 0xff);
473         return (high << 8) | low;
474     }
475
476     public static class CABException extends IOException {
477         public CABException(String s) { super(s); }
478     }
479
480
481     /** scratch space for isToByteArray() */
482     static byte[] workspace = new byte[16 * 1024];
483
484     /** Trivial method to completely read an InputStream */
485     public static synchronized byte[] isToByteArray(InputStream is) throws IOException {
486         int pos = 0;
487         while (true) {
488             int numread = is.read(workspace, pos, workspace.length - pos);
489             if (numread == -1) break;
490             else if (pos + numread < workspace.length) pos += numread;
491             else {
492                 pos += numread;
493                 byte[] temp = new byte[workspace.length * 2];
494                 System.arraycopy(workspace, 0, temp, 0, workspace.length);
495                 workspace = temp;
496             }
497         }
498         byte[] ret = new byte[pos];
499         System.arraycopy(workspace, 0, ret, 0, pos);
500         return ret;
501     }
502
503
504 }
505