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.
5 package org.ibex.graphics;
7 import org.ibex.util.*;
9 import org.ibex.nestedvm.*;
10 import org.ibex.nestedvm.Runtime;
13 private static byte[] image;
15 private String[] fileNames;
16 private int[] lengths;
17 private byte[][] data;
19 public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
21 public MSPack(InputStream cabIS) throws IOException {
23 Runtime vm = (Runtime)Class.forName("org.ibex.util.MIPSApps").newInstance();
24 byte[] cab = InputStreamToByteArray.convert(cabIS);
25 int cabAddr = vm.sbrk(cab.length);
26 if(cabAddr < 0) throw new MSPackException("sbrk failed");
28 vm.copyout(cab,cabAddr,cab.length);
30 vm.setUserInfo(0,cabAddr);
31 vm.setUserInfo(1,cab.length);
33 int status = vm.run(new String[]{ "mspack"} );
34 if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
40 } output_table[MAX_MEMBERS+1]; */
42 int filesTable = vm.getUserInfo(2);
44 while(vm.memRead(filesTable+count*12) != 0) count++;
46 fileNames = new String[count];
47 data = new byte[count][];
48 lengths = new int[count];
50 for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
51 int length = vm.memRead(addr+8);
52 data[i] = new byte[length];
54 fileNames[i] = vm.cstring(vm.memRead(addr));
55 System.out.println("" + fileNames[i]);
56 vm.copyin(vm.memRead(addr+4),data[i],length);
58 } catch(Runtime.ExecutionException e) {
60 throw new MSPackException("mspack.mips crashed");
61 } catch(Exception e) {
62 throw new MSPackException(e.toString());
66 public String[] getFileNames() { return fileNames; }
67 public int[] getLengths() { return lengths; }
68 public InputStream getInputStream(int index) {
69 return new KnownLength.KnownLengthInputStream(new ByteArrayInputStream(data[index]), data[index].length);
71 public InputStream getInputStream(String fileName) {
72 for(int i=0;i<fileNames.length;i++) {
73 if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
78 public static void main(String[] args) throws IOException {
79 MSPack pack = new MSPack(new FileInputStream(args[0]));
80 String[] files = pack.getFileNames();
81 for(int i=0;i<files.length;i++)
82 System.out.println(i + ": " + files[i] + ": " + pack.getLengths()[i]);
83 System.out.println("Writing " + files[files.length-1]);
84 InputStream is = pack.getInputStream(files.length-1);
85 OutputStream os = new FileOutputStream(files[files.length-1]);
87 byte[] buf = new byte[4096];
88 while((n = is.read(buf)) != -1) os.write(buf,0,n);