1 package org.xwt.translators;
2
3 import org.xwt.Main;
4 import org.xwt.util.*;
5 import org.xwt.mips.*;
6 import java.io.*;
7
8 import org.xwt.mips.Runtime;
9
10 public class MSPack {
11 private static byte[] image;
12
13 private String[] fileNames;
14 private int[] lengths;
15 private byte[][] data;
16
17 public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
18
19 public MSPack(InputStream cabIS) throws IOException {
20 try {
21 Runtime vm = (Runtime)Class.forName("org.xwt.translators.MIPSApps").newInstance();
22 byte[] cab = InputStreamToByteArray.convert(cabIS);
23 int cabAddr = vm.sbrk(cab.length);
24 if(cabAddr < 0) throw new MSPackException("sbrk failed");
25
26 vm.copyout(cab,cabAddr,cab.length);
27
28 vm.setUserInfo(0,cabAddr);
29 vm.setUserInfo(1,cab.length);
30
31 int status = vm.run(new String[]{ "mspack"} );
32 if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
33
34
39
40 int filesTable = vm.getUserInfo(2);
41 int count=0;
42 while(vm.memRead(filesTable+count*12) != 0) count++;
43
44 fileNames = new String[count];
45 data = new byte[count][];
46 lengths = new int[count];
47
48 for(intint0,addr=filesTable;i<count;i++,addr+=12) {
49 int length = vm.memRead(addr+8);
50 data[i] = new byte[length];
51 lengths[i] = 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);
55 }
56 } catch(Runtime.ExecutionException e) {
57 e.printStackTrace();
58 throw new MSPackException("mspack.mips crashed");
59 } catch(Exception e) {
60 throw new MSPackException(e.toString());
61 }
62 }
63
64 public String[] getFileNames() { return fileNames; }
65 public int[] getLengths() { return lengths; }
66 public InputStream getInputStream(int index) {
67 return new KnownLength.KnownLengthInputStream(new ByteArrayInputStream(data[index]), data[index].length);
68 }
69 public InputStream getInputStream(String fileName) {
70 for(int i=0;i<fileNames.length;i++) {
71 if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
72 }
73 return null;
74 }
75
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]);
84 int n;
85 byte[] buf = new byte[4096];
86 while((n = is.read(buf)) != -1) os.write(buf,0,n);
87 os.close();
88 is.close();
89 }
90 }
91
92