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.core.Main;
8 import org.ibex.util.*;
10 import org.ibex.nestedvm.*;
11 import org.ibex.nestedvm.Runtime;
14 private static byte[] image;
16 private String[] fileNames;
17 private int[] lengths;
18 private byte[][] data;
20 public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
22 public MSPack(InputStream cabIS) throws IOException {
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");
29 vm.copyout(cab,cabAddr,cab.length);
31 vm.setUserInfo(0,cabAddr);
32 vm.setUserInfo(1,cab.length);
34 int status = vm.run(new String[]{ "mspack"} );
35 if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
41 } output_table[MAX_MEMBERS+1]; */
43 int filesTable = vm.getUserInfo(2);
45 while(vm.memRead(filesTable+count*12) != 0) count++;
47 fileNames = new String[count];
48 data = new byte[count][];
49 lengths = new int[count];
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];
55 fileNames[i] = vm.cstring(vm.memRead(addr));
56 System.out.println("" + fileNames[i]);
57 vm.copyin(vm.memRead(addr+4),data[i],length);
59 } catch(Runtime.ExecutionException e) {
61 throw new MSPackException("mspack.mips crashed");
62 } catch(Exception e) {
63 throw new MSPackException(e.toString());
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);
72 public InputStream getInputStream(String fileName) {
73 for(int i=0;i<fileNames.length;i++) {
74 if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
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]);
88 byte[] buf = new byte[4096];
89 while((n = is.read(buf)) != -1) os.write(buf,0,n);