2003/12/28 09:45:29
[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 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 = new MIPSApps();
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             /*static struct {
35                 char *filename;
36                 char *data;
37                 int length;
38             } output_table[MAX_MEMBERS+1]; */
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(int i=0,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         }
60     }
61     
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);
66     }
67     public InputStream getInputStream(String fileName) {
68         for(int i=0;i<fileNames.length;i++) {
69             if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
70         }
71         return null;
72     }
73     
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]);
82         int n;
83         byte[] buf = new byte[4096];
84         while((n = is.read(buf)) != -1) os.write(buf,0,n);
85         os.close();
86         is.close();
87     }
88 }
89