1 package org.xwt.translators;
3 import org.ibex.util.*;
6 import org.xwt.mips.Runtime;
9 private static byte[] image;
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 {
19 Runtime vm = (Runtime)Class.forName("org.xwt.translators.MIPSApps").newInstance();
20 byte[] cab = InputStreamToByteArray.convert(cabIS);
21 int cabAddr = vm.sbrk(cab.length);
22 if(cabAddr < 0) throw new MSPackException("sbrk failed");
24 vm.copyout(cab,cabAddr,cab.length);
26 vm.setUserInfo(0,cabAddr);
27 vm.setUserInfo(1,cab.length);
29 int status = vm.run(new String[]{ "mspack"} );
30 if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
36 } output_table[MAX_MEMBERS+1]; */
38 int filesTable = vm.getUserInfo(2);
40 while(vm.memRead(filesTable+count*12) != 0) count++;
42 fileNames = new String[count];
43 data = new byte[count][];
44 lengths = new int[count];
46 for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
47 int length = vm.memRead(addr+8);
48 data[i] = new byte[length];
50 fileNames[i] = vm.cstring(vm.memRead(addr));
51 System.out.println("" + fileNames[i]);
52 vm.copyin(vm.memRead(addr+4),data[i],length);
54 } catch(Runtime.ExecutionException e) {
56 throw new MSPackException("mspack.mips crashed");
57 } catch(Exception e) {
58 throw new MSPackException(e.toString());
62 public String[] getFileNames() { return fileNames; }
63 public int[] getLengths() { return lengths; }
64 public InputStream getInputStream(int index) {
65 return new KnownLength.KnownLengthInputStream(new ByteArrayInputStream(data[index]), data[index].length);
67 public InputStream getInputStream(String fileName) {
68 for(int i=0;i<fileNames.length;i++) {
69 if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
74 public static void main(String[] args) throws IOException {
75 MSPack pack = new MSPack(new FileInputStream(args[0]));
76 String[] files = pack.getFileNames();
77 for(int i=0;i<files.length;i++)
78 System.out.println(i + ": " + files[i] + ": " + pack.getLengths()[i]);
79 System.out.println("Writing " + files[files.length-1]);
80 InputStream is = pack.getInputStream(files.length-1);
81 OutputStream os = new FileOutputStream(files[files.length-1]);
83 byte[] buf = new byte[4096];
84 while((n = is.read(buf)) != -1) os.write(buf,0,n);