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.
7 import org.ibex.nestedvm.Runtime;
11 private String[] fileNames;
12 private int[] lengths;
13 private byte[][] data;
15 public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
17 public MSPack(InputStream cabIS) throws IOException {
18 byte[] cab = InputStreamToByteArray.convert(cabIS);
20 //Interpreter vm = new Interpreter("mspack.mips");
23 vm = (Runtime) Class.forName("tests.MSPackHelper").newInstance();
24 } catch(Exception e) {
25 throw new MSPackException("couldn't instansiate MSPackHelper");
27 int cabAddr = vm.sbrk(cab.length);
28 if(cabAddr < 0) throw new MSPackException("sbrk failed");
30 vm.copyout(cab,cabAddr,cab.length);
32 vm.setUserInfo(0,cabAddr);
33 vm.setUserInfo(1,cab.length);
35 int status = vm.run(new String[]{ "mspack.mips"} );
36 if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
42 } output_table[MAX_MEMBERS+1]; */
44 int filesTable = vm.getUserInfo(2);
46 while(vm.memRead(filesTable+count*12) != 0) count++;
48 fileNames = new String[count];
49 data = new byte[count][];
50 lengths = new int[count];
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];
56 fileNames[i] = vm.cstring(vm.memRead(addr));
57 System.out.println("" + fileNames[i]);
58 vm.copyin(vm.memRead(addr+4),data[i],length);
60 } catch(Runtime.ExecutionException e) {
62 throw new MSPackException("mspack.mips crashed");
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);
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]);
85 byte[] buf = new byte[4096];
86 while((n = is.read(buf)) != -1) os.write(buf,0,n);
90 private static class InputStreamToByteArray {
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 {
98 int numread = is.read(workspace, pos, workspace.length - pos);
99 if (numread == -1) break;
100 else if (pos + numread < workspace.length) pos += numread;
103 byte[] temp = new byte[workspace.length * 2];
104 System.arraycopy(workspace, 0, temp, 0, workspace.length);
108 byte[] ret = new byte[pos];
109 System.arraycopy(workspace, 0, ret, 0, pos);