3 import org.xwt.mips.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");
17 MSPackHelper vm = new MSPackHelper();
19 int cabAddr = vm.sbrk(cab.length);
20 if(cabAddr < 0) throw new MSPackException("sbrk failed");
22 vm.copyout(cab,cabAddr,cab.length);
24 vm.setUserInfo(0,cabAddr);
25 vm.setUserInfo(1,cab.length);
27 int status = vm.run(new String[]{ "mspack.mips"} );
28 if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
34 } output_table[MAX_MEMBERS+1]; */
36 int filesTable = vm.getUserInfo(2);
38 while(vm.memRead(filesTable+count*12) != 0) count++;
40 fileNames = new String[count];
41 data = new byte[count][];
42 lengths = new int[count];
44 for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
45 int length = vm.memRead(addr+8);
46 data[i] = new byte[length];
48 fileNames[i] = vm.cstring(vm.memRead(addr));
49 System.out.println("" + fileNames[i]);
50 vm.copyin(vm.memRead(addr+4),data[i],length);
52 } catch(Runtime.ExecutionException e) {
54 throw new MSPackException("mspack.mips crashed");
58 public String[] getFileNames() { return fileNames; }
59 public int[] getLengths() { return lengths; }
60 public InputStream getInputStream(int index) { return new ByteArrayInputStream(data[index]); }
61 public InputStream getInputStream(String fileName) {
62 for(int i=0;i<fileNames.length;i++) {
63 if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
68 public static void main(String[] args) throws IOException {
69 MSPack pack = new MSPack(new FileInputStream(args[0]));
70 String[] files = pack.getFileNames();
71 for(int i=0;i<files.length;i++)
72 System.out.println(i + ": " + files[i] + ": " + pack.getLengths()[i]);
73 System.out.println("Writing " + files[files.length-1]);
74 InputStream is = pack.getInputStream(files.length-1);
75 OutputStream os = new FileOutputStream(files[files.length-1]);
77 byte[] buf = new byte[4096];
78 while((n = is.read(buf)) != -1) os.write(buf,0,n);
82 private static class InputStreamToByteArray {
84 /** scratch space for isToByteArray() */
85 private static byte[] workspace = new byte[16 * 1024];
86 /** Trivial method to completely read an InputStream */
87 public static synchronized byte[] convert(InputStream is) throws IOException {
90 int numread = is.read(workspace, pos, workspace.length - pos);
91 if (numread == -1) break;
92 else if (pos + numread < workspace.length) pos += numread;
95 byte[] temp = new byte[workspace.length * 2];
96 System.arraycopy(workspace, 0, temp, 0, workspace.length);
100 byte[] ret = new byte[pos];
101 System.arraycopy(workspace, 0, ret, 0, pos);