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