36de110690ea734a8f0ad1126233855e8a474918
[org.ibex.core.git] / src / org / ibex / graphics / MSPack.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.graphics;
6
7 import org.ibex.core.Main;
8 import org.ibex.util.*;
9 import java.io.*;
10 import org.ibex.nestedvm.*;
11 import org.ibex.nestedvm.Runtime;
12
13 public class MSPack {
14     private static byte[] image;
15     
16     private String[] fileNames;
17     private int[] lengths;
18     private byte[][] data;
19         
20     public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
21         
22     public MSPack(InputStream cabIS) throws IOException {
23         try {
24             Runtime vm = (Runtime)Class.forName("org.ibex.util.MIPSApps").newInstance();
25             byte[] cab = InputStreamToByteArray.convert(cabIS);
26             int cabAddr = vm.sbrk(cab.length);
27             if(cabAddr < 0) throw new MSPackException("sbrk failed");
28             
29             vm.copyout(cab,cabAddr,cab.length);
30         
31             vm.setUserInfo(0,cabAddr);
32             vm.setUserInfo(1,cab.length);
33         
34             int status = vm.run(new String[]{ "mspack"} );
35             if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
36             
37             /*static struct {
38                 char *filename;
39                 char *data;
40                 int length;
41             } output_table[MAX_MEMBERS+1]; */
42
43             int filesTable = vm.getUserInfo(2);
44             int count=0;
45             while(vm.memRead(filesTable+count*12) != 0) count++;
46             
47             fileNames = new String[count];
48             data = new byte[count][];
49             lengths = new int[count];
50             
51             for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
52                 int length = vm.memRead(addr+8);
53                 data[i] = new byte[length];
54                 lengths[i] = length;
55                 fileNames[i] = vm.cstring(vm.memRead(addr));
56                 System.out.println("" + fileNames[i]);
57                 vm.copyin(vm.memRead(addr+4),data[i],length);
58             }
59         } catch(Runtime.ExecutionException e) {
60             e.printStackTrace();
61             throw new MSPackException("mspack.mips crashed");
62         } catch(Exception e) {
63             throw new MSPackException(e.toString());
64         }
65     }
66     
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);
71     }
72     public InputStream getInputStream(String fileName) {
73         for(int i=0;i<fileNames.length;i++) {
74             if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
75         }
76         return null;
77     }
78     
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]);
87         int n;
88         byte[] buf = new byte[4096];
89         while((n = is.read(buf)) != -1) os.write(buf,0,n);
90         os.close();
91         is.close();
92     }
93 }
94