mass rename and rebranding from xwt to ibex - fixed to use ixt files
[org.ibex.core.git] / src / org / xwt / translators / MSPack.java
1 package org.xwt.translators;
2
3 import org.ibex.util.*;
4 import java.io.*;
5
6 import org.xwt.mips.Runtime;
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     public MSPack(InputStream cabIS) throws IOException {
18         try {
19             Runtime vm = (Runtime)Class.forName("org.xwt.translators.MIPSApps").newInstance();
20             byte[] cab = InputStreamToByteArray.convert(cabIS);
21             int cabAddr = vm.sbrk(cab.length);
22             if(cabAddr < 0) throw new MSPackException("sbrk failed");
23             
24             vm.copyout(cab,cabAddr,cab.length);
25         
26             vm.setUserInfo(0,cabAddr);
27             vm.setUserInfo(1,cab.length);
28         
29             int status = vm.run(new String[]{ "mspack"} );
30             if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
31             
32             /*static struct {
33                 char *filename;
34                 char *data;
35                 int length;
36             } output_table[MAX_MEMBERS+1]; */
37
38             int filesTable = vm.getUserInfo(2);
39             int count=0;
40             while(vm.memRead(filesTable+count*12) != 0) count++;
41             
42             fileNames = new String[count];
43             data = new byte[count][];
44             lengths = new int[count];
45             
46             for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
47                 int length = vm.memRead(addr+8);
48                 data[i] = new byte[length];
49                 lengths[i] = length;
50                 fileNames[i] = vm.cstring(vm.memRead(addr));
51                 System.out.println("" + fileNames[i]);
52                 vm.copyin(vm.memRead(addr+4),data[i],length);
53             }
54         } catch(Runtime.ExecutionException e) {
55             e.printStackTrace();
56             throw new MSPackException("mspack.mips crashed");
57         } catch(Exception e) {
58             throw new MSPackException(e.toString());
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