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