3 import org.ibex.nestedvm.Runtime;
7 private String[] fileNames;
11 public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
13 public MSPack(InputStream cabIS) throws IOException {
14 byte[] cab = InputStreamToByteArray.convert(cabIS);
16 //Interpreter vm = new Interpreter("mspack.mips");
19 vm = (Runtime) Class.forName("tests.MSPackHelper").newInstance();
20 } catch(Exception e) {
21 throw new MSPackException("couldn't instansiate MSPackHelper");
23 int cabAddr = vm.sbrk(cab.length);
24 if(cabAddr < 0) throw new MSPackException("sbrk failed");
26 vm.copyout(cab,cabAddr,cab.length);
28 vm.setUserInfo(0,cabAddr);
29 vm.setUserInfo(1,cab.length);
31 int status = vm.run(new String[]{ "mspack.mips"} );
32 if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
38 } output_table[MAX_MEMBERS+1]; */
40 int filesTable = vm.getUserInfo(2);
42 while(vm.memRead(filesTable+count*12) != 0) count++;
44 fileNames = new String[count];
45 data = new byte[count][];
46 lengths = new int[count];
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];
52 fileNames[i] = vm.cstring(vm.memRead(addr));
53 System.out.println("" + fileNames[i]);
54 vm.copyin(vm.memRead(addr+4),data[i],length);
56 } catch(Runtime.ExecutionException e) {
58 throw new MSPackException("mspack.mips crashed");
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);
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]);
81 byte[] buf = new byte[4096];
82 while((n = is.read(buf)) != -1) os.write(buf,0,n);
86 private static class InputStreamToByteArray {
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 {
94 int numread = is.read(workspace, pos, workspace.length - pos);
95 if (numread == -1) break;
96 else if (pos + numread < workspace.length) pos += numread;
99 byte[] temp = new byte[workspace.length * 2];
100 System.arraycopy(workspace, 0, temp, 0, workspace.length);
104 byte[] ret = new byte[pos];
105 System.arraycopy(workspace, 0, ret, 0, pos);