3 import org.ibex.core.Main;
4 import org.ibex.util.*;
6 import org.ibex.nestedvm.*;
7 import org.ibex.nestedvm.Runtime;
10 private static byte[] image;
12 private String[] fileNames;
13 private int[] lengths;
14 private byte[][] data;
16 public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
18 public MSPack(InputStream cabIS) throws IOException {
20 Runtime vm = (Runtime)Class.forName("org.ibex.util.MIPSApps").newInstance();
21 byte[] cab = InputStreamToByteArray.convert(cabIS);
22 int cabAddr = vm.sbrk(cab.length);
23 if(cabAddr < 0) throw new MSPackException("sbrk failed");
25 vm.copyout(cab,cabAddr,cab.length);
27 vm.setUserInfo(0,cabAddr);
28 vm.setUserInfo(1,cab.length);
30 int status = vm.run(new String[]{ "mspack"} );
31 if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
37 } output_table[MAX_MEMBERS+1]; */
39 int filesTable = vm.getUserInfo(2);
41 while(vm.memRead(filesTable+count*12) != 0) count++;
43 fileNames = new String[count];
44 data = new byte[count][];
45 lengths = new int[count];
47 for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
48 int length = vm.memRead(addr+8);
49 data[i] = new byte[length];
51 fileNames[i] = vm.cstring(vm.memRead(addr));
52 System.out.println("" + fileNames[i]);
53 vm.copyin(vm.memRead(addr+4),data[i],length);
55 } catch(Runtime.ExecutionException e) {
57 throw new MSPackException("mspack.mips crashed");
58 } catch(Exception e) {
59 throw new MSPackException(e.toString());
63 public String[] getFileNames() { return fileNames; }
64 public int[] getLengths() { return lengths; }
65 public InputStream getInputStream(int index) {
66 return new KnownLength.KnownLengthInputStream(new ByteArrayInputStream(data[index]), data[index].length);
68 public InputStream getInputStream(String fileName) {
69 for(int i=0;i<fileNames.length;i++) {
70 if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
75 public static void main(String[] args) throws IOException {
76 MSPack pack = new MSPack(new FileInputStream(args[0]));
77 String[] files = pack.getFileNames();
78 for(int i=0;i<files.length;i++)
79 System.out.println(i + ": " + files[i] + ": " + pack.getLengths()[i]);
80 System.out.println("Writing " + files[files.length-1]);
81 InputStream is = pack.getInputStream(files.length-1);
82 OutputStream os = new FileOutputStream(files[files.length-1]);
84 byte[] buf = new byte[4096];
85 while((n = is.read(buf)) != -1) os.write(buf,0,n);