more xwt -> ibex cleanup
[nestedvm.git] / src / tests / MSPack.java
1 package tests;
2
3 import org.ibex.nestedvm.Runtime;
4 import java.io.*;
5
6 public class MSPack {
7     private String[] fileNames;
8     private int[] lengths;
9     private byte[][] data;
10         
11     public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
12     
13     public MSPack(InputStream cabIS) throws IOException {
14         byte[] cab = InputStreamToByteArray.convert(cabIS);        
15         try {
16             //Interpreter vm = new Interpreter("mspack.mips");
17                         Runtime vm;
18                         try {
19                                  vm = (Runtime) Class.forName("tests.MSPackHelper").newInstance();
20                         } catch(Exception e) {
21                                 throw new MSPackException("couldn't instansiate MSPackHelper");
22                         }
23             int cabAddr = vm.sbrk(cab.length);
24             if(cabAddr < 0) throw new MSPackException("sbrk failed");
25         
26             vm.copyout(cab,cabAddr,cab.length);
27         
28             vm.setUserInfo(0,cabAddr);
29             vm.setUserInfo(1,cab.length);
30         
31             int status = vm.run(new String[]{ "mspack.mips"} );
32             if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
33             
34             /*static struct {
35                 char *filename;
36                 char *data;
37                 int length;
38             } output_table[MAX_MEMBERS+1]; */
39
40             int filesTable = vm.getUserInfo(2);
41             int count=0;
42             while(vm.memRead(filesTable+count*12) != 0) count++;
43             
44             fileNames = new String[count];
45             data = new byte[count][];
46             lengths = new int[count];
47             
48             for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
49                 int length = vm.memRead(addr+8);
50                 data[i] = new byte[length];
51                 lengths[i] = length;
52                 fileNames[i] = vm.cstring(vm.memRead(addr));
53                 System.out.println("" + fileNames[i]);
54                 vm.copyin(vm.memRead(addr+4),data[i],length);
55             }
56         } catch(Runtime.ExecutionException e) {
57             e.printStackTrace();
58             throw new MSPackException("mspack.mips crashed");
59         }
60     }
61     
62     public String[] getFileNames() { return fileNames; }
63     public int[] getLengths() { return lengths; }
64     public InputStream getInputStream(int index) { return new ByteArrayInputStream(data[index]); }
65     public InputStream getInputStream(String fileName) {
66         for(int i=0;i<fileNames.length;i++) {
67             if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
68         }
69         return null;
70     }
71     
72     public static void main(String[] args) throws IOException {
73         MSPack pack = new MSPack(new FileInputStream(args[0]));
74         String[] files = pack.getFileNames();
75         for(int i=0;i<files.length;i++)
76             System.out.println(i + ": " + files[i] + ": " + pack.getLengths()[i]);
77         System.out.println("Writing " + files[files.length-1]);
78         InputStream is = pack.getInputStream(files.length-1);
79         OutputStream os = new FileOutputStream(files[files.length-1]);
80         int n;
81         byte[] buf = new byte[4096];
82         while((n = is.read(buf)) != -1) os.write(buf,0,n);
83         os.close();
84         is.close();
85     }
86     private static class InputStreamToByteArray {
87
88         /** scratch space for isToByteArray() */
89         private static byte[] workspace = new byte[16 * 1024];
90         /** Trivial method to completely read an InputStream */
91         public static synchronized byte[] convert(InputStream is) throws IOException {
92             int pos = 0;
93             while (true) {
94                 int numread = is.read(workspace, pos, workspace.length - pos);
95                 if (numread == -1) break;
96                 else if (pos + numread < workspace.length) pos += numread;
97                 else {
98                     pos += numread;
99                     byte[] temp = new byte[workspace.length * 2];
100                     System.arraycopy(workspace, 0, temp, 0, workspace.length);
101                     workspace = temp;
102                 }
103             }
104             byte[] ret = new byte[pos];
105             System.arraycopy(workspace, 0, ret, 0, pos);
106             return ret;
107         }
108     }
109 }
110