6236df6a576b2cae07d3fc9fd75a891e2045201f
[org.ibex.core.git] / src / org / ibex / graphics / MSPack.java
1 package org.ibex.util;
2
3 import org.ibex.core.Main;
4 import org.ibex.util.*;
5 import java.io.*;
6 import org.ibex.nestedvm.*;
7 import org.ibex.nestedvm.Runtime;
8
9 public class MSPack {
10     private static byte[] image;
11     
12     private String[] fileNames;
13     private int[] lengths;
14     private byte[][] data;
15         
16     public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
17         
18     public MSPack(InputStream cabIS) throws IOException {
19         try {
20             Runtime vm = (Runtime)Class.forName("org.ibex.util.MIPSApps").newInstance();
21             byte[] cab = InputStreamToByteArray.convert(cabIS);
22             int cabAddr = vm.sbrk(cab.length);
23             if(cabAddr < 0) throw new MSPackException("sbrk failed");
24             
25             vm.copyout(cab,cabAddr,cab.length);
26         
27             vm.setUserInfo(0,cabAddr);
28             vm.setUserInfo(1,cab.length);
29         
30             int status = vm.run(new String[]{ "mspack"} );
31             if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
32             
33             /*static struct {
34                 char *filename;
35                 char *data;
36                 int length;
37             } output_table[MAX_MEMBERS+1]; */
38
39             int filesTable = vm.getUserInfo(2);
40             int count=0;
41             while(vm.memRead(filesTable+count*12) != 0) count++;
42             
43             fileNames = new String[count];
44             data = new byte[count][];
45             lengths = new int[count];
46             
47             for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
48                 int length = vm.memRead(addr+8);
49                 data[i] = new byte[length];
50                 lengths[i] = length;
51                 fileNames[i] = vm.cstring(vm.memRead(addr));
52                 System.out.println("" + fileNames[i]);
53                 vm.copyin(vm.memRead(addr+4),data[i],length);
54             }
55         } catch(Runtime.ExecutionException e) {
56             e.printStackTrace();
57             throw new MSPackException("mspack.mips crashed");
58         } catch(Exception e) {
59             throw new MSPackException(e.toString());
60         }
61     }
62     
63     public String[] getFileNames() { return fileNames; }
64     public int[] getLengths() { return lengths; }
65     public InputStream getInputStream(int index) {
66         return new KnownLength.KnownLengthInputStream(new ByteArrayInputStream(data[index]), data[index].length);
67     }
68     public InputStream getInputStream(String fileName) {
69         for(int i=0;i<fileNames.length;i++) {
70             if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
71         }
72         return null;
73     }
74     
75     public static void main(String[] args) throws IOException {
76         MSPack pack = new MSPack(new FileInputStream(args[0]));
77         String[] files = pack.getFileNames();
78         for(int i=0;i<files.length;i++)
79             System.out.println(i + ": " + files[i] + ": " + pack.getLengths()[i]);
80         System.out.println("Writing " + files[files.length-1]);
81         InputStream is = pack.getInputStream(files.length-1);
82         OutputStream os = new FileOutputStream(files[files.length-1]);
83         int n;
84         byte[] buf = new byte[4096];
85         while((n = is.read(buf)) != -1) os.write(buf,0,n);
86         os.close();
87         is.close();
88     }
89 }
90