2003/11/03 02:10:52
[org.ibex.core.git] / src / org / xwt / translators / MSPack.java
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 public class MSPack {
9     private static byte[] image;
10     
11     private String[] fileNames;
12     private int[] lengths;
13     private byte[][] data;
14         
15     public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
16     
17     private static synchronized byte[] getImage() {
18         if (image == null)
19             image = InputStreamToByteArray.convert(Main.builtin.getInputStream("libmspack.mips"));
20         return image;
21     }
22     
23     public MSPack(InputStream cabIS) throws IOException {
24         byte[] cab = InputStreamToByteArray.convert(cabIS);
25         Interpreter vm = new Interpreter(getImage());
26         
27         int cabAddr = vm.sbrk(cab.length);
28         if(cabAddr < 0) throw new MSPackException("sbrk failed");
29         
30         try {
31             vm.copyout(cab,cabAddr,cab.length);
32         
33             vm.setUserInfo(0,cabAddr);
34             vm.setUserInfo(1,cab.length);
35         
36             int status = vm.run(new String[]{ "mspack.mips"} );
37             if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
38             
39             /*static struct {
40                 char *filename;
41                 char *data;
42                 int length;
43             } output_table[MAX_MEMBERS+1]; */
44
45             int filesTable = vm.getUserInfo(2);
46             int count=0;
47             while(vm.memRead(filesTable+count*12) != 0) count++;
48             
49             fileNames = new String[count];
50             data = new byte[count][];
51             lengths = new int[count];
52             
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];
56                 lengths[i] = 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);
60             }
61         } catch(VM.EmulationException e) {
62             e.printStackTrace();
63             throw new MSPackException("mspack.mips crashed");
64         }
65     }
66     
67     public String[] getFileNames() { return fileNames; }
68     public int[] getLengths() { return lengths; }
69     public InputStream getInputStream(int index) { return new ByteArrayInputStream(data[index]); }
70     public InputStream getInputStream(String fileName) {
71         for(int i=0;i<fileNames.length;i++) {
72             if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
73         }
74         return null;
75     }
76     
77     public static void main(String[] args) throws IOException {
78         MSPack pack = new MSPack(new FileInputStream(args[0]));
79         String[] files = pack.getFileNames();
80         for(int i=0;i<files.length;i++)
81             System.out.println(i + ": " + files[i] + ": " + pack.getLengths()[i]);
82         System.out.println("Writing " + files[files.length-1]);
83         InputStream is = pack.getInputStream(files.length-1);
84         OutputStream os = new FileOutputStream(files[files.length-1]);
85         int n;
86         byte[] buf = new byte[4096];
87         while((n = is.read(buf)) != -1) os.write(buf,0,n);
88         os.close();
89         is.close();
90     }
91 }
92