Stream->Fountain, move Scheduler to Platform, HashMap->Hash
[org.ibex.core.git] / src / org / ibex / graphics / MSPack.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.graphics;
6
7 import org.ibex.util.*;
8 import java.io.*;
9 import org.ibex.nestedvm.*;
10 import org.ibex.nestedvm.Runtime;
11
12 public class MSPack {
13     private static byte[] image;
14     
15     private String[] fileNames;
16     private int[] lengths;
17     private byte[][] data;
18         
19     public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
20         
21     public MSPack(InputStream cabIS) throws IOException {
22         try {
23             Runtime vm = (Runtime)Class.forName("org.ibex.util.MIPSApps").newInstance();
24             byte[] cab = InputStreamToByteArray.convert(cabIS);
25             int cabAddr = vm.sbrk(cab.length);
26             if(cabAddr < 0) throw new MSPackException("sbrk failed");
27             
28             vm.copyout(cab,cabAddr,cab.length);
29         
30             vm.setUserInfo(0,cabAddr);
31             vm.setUserInfo(1,cab.length);
32         
33             int status = vm.run(new String[]{ "mspack"} );
34             if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
35             
36             /*static struct {
37                 char *filename;
38                 char *data;
39                 int length;
40             } output_table[MAX_MEMBERS+1]; */
41
42             int filesTable = vm.getUserInfo(2);
43             int count=0;
44             while(vm.memRead(filesTable+count*12) != 0) count++;
45             
46             fileNames = new String[count];
47             data = new byte[count][];
48             lengths = new int[count];
49             
50             for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
51                 int length = vm.memRead(addr+8);
52                 data[i] = new byte[length];
53                 lengths[i] = length;
54                 fileNames[i] = vm.cstring(vm.memRead(addr));
55                 System.out.println("" + fileNames[i]);
56                 vm.copyin(vm.memRead(addr+4),data[i],length);
57             }
58         } catch(Runtime.ExecutionException e) {
59             e.printStackTrace();
60             throw new MSPackException("mspack.mips crashed");
61         } catch(Exception e) {
62             throw new MSPackException(e.toString());
63         }
64     }
65     
66     public String[] getFileNames() { return fileNames; }
67     public int[] getLengths() { return lengths; }
68     public InputStream getInputStream(int index) {
69         return new KnownLength.KnownLengthInputStream(new ByteArrayInputStream(data[index]), data[index].length);
70     }
71     public InputStream getInputStream(String fileName) {
72         for(int i=0;i<fileNames.length;i++) {
73             if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
74         }
75         return null;
76     }
77     
78     public static void main(String[] args) throws IOException {
79         MSPack pack = new MSPack(new FileInputStream(args[0]));
80         String[] files = pack.getFileNames();
81         for(int i=0;i<files.length;i++)
82             System.out.println(i + ": " + files[i] + ": " + pack.getLengths()[i]);
83         System.out.println("Writing " + files[files.length-1]);
84         InputStream is = pack.getInputStream(files.length-1);
85         OutputStream os = new FileOutputStream(files[files.length-1]);
86         int n;
87         byte[] buf = new byte[4096];
88         while((n = is.read(buf)) != -1) os.write(buf,0,n);
89         os.close();
90         is.close();
91     }
92 }
93