1 package org.xwt.translators;
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 private static synchronized byte[] getImage() throws IOException {
19 image = InputStreamToByteArray.convert(Main.builtin.getInputStream("libmspack.mips"));
23 public MSPack(InputStream cabIS) throws IOException {
24 byte[] cab = InputStreamToByteArray.convert(cabIS);
25 Interpreter vm = new Interpreter(getImage());
27 int cabAddr = vm.sbrk(cab.length);
28 if(cabAddr < 0) throw new MSPackException("sbrk failed");
31 vm.copyout(cab,cabAddr,cab.length);
33 vm.setUserInfo(0,cabAddr);
34 vm.setUserInfo(1,cab.length);
36 int status = vm.run(new String[]{ "mspack.mips"} );
37 if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
43 } output_table[MAX_MEMBERS+1]; */
45 int filesTable = vm.getUserInfo(2);
47 while(vm.memRead(filesTable+count*12) != 0) count++;
49 fileNames = new String[count];
50 data = new byte[count][];
51 lengths = new int[count];
53 for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
54 int length = vm.memRead(addr+8);
55 data[i] = new byte[length];
57 fileNames[i] = vm.cstring(vm.memRead(addr));
58 System.out.println("" + fileNames[i]);
59 vm.copyin(vm.memRead(addr+4),data[i],length);
61 } catch(Exception e) {
63 throw new MSPackException("mspack.mips crashed");
67 public String[] getFileNames() { return fileNames; }
68 public int[] getLengths() { return lengths; }
69 public InputStream getInputStream(int index) {
70 return new KnownLength.KnownLengthInputStream(new ByteArrayInputStream(data[index]), data[index].length);
72 public InputStream getInputStream(String fileName) {
73 for(int i=0;i<fileNames.length;i++) {
74 if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
79 public static void main(String[] args) throws IOException {
80 MSPack pack = new MSPack(new FileInputStream(args[0]));
81 String[] files = pack.getFileNames();
82 for(int i=0;i<files.length;i++)
83 System.out.println(i + ": " + files[i] + ": " + pack.getLengths()[i]);
84 System.out.println("Writing " + files[files.length-1]);
85 InputStream is = pack.getInputStream(files.length-1);
86 OutputStream os = new FileOutputStream(files[files.length-1]);
88 byte[] buf = new byte[4096];
89 while((n = is.read(buf)) != -1) os.write(buf,0,n);