--- /dev/null
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.ibex.util;
+
+import java.io.IOException;
+
+import org.ibex.js.*;
+import org.ibex.util.*;
+import org.ibex.graphics.*;
+import org.ibex.plat.*;
+
+/** Implements cooperative multitasking */
+public class Scheduler {
+
+ // Public API Exposed to org.ibex /////////////////////////////////////////////////
+
+ private static Scheduler singleton;
+ public static void add(Task t) { Log.debug(Scheduler.class, "scheduling " + t); Scheduler.runnable.append(t); }
+ public static void init() { if (singleton == null) (singleton = Platform.getScheduler()).run(); }
+
+ private static Task current = null;
+
+ private static volatile boolean rendering = false;
+ private static volatile boolean again = false;
+
+ /** synchronizd so that we can safely call it from an event-delivery thread, in-context */
+ public static void renderAll() {
+ if (rendering) { again = true; return; }
+ synchronized(Scheduler.class) {
+ try {
+ rendering = true;
+ do {
+ // FEATURE: this could be cleaner
+ again = false;
+ for(int i=0; i<Surface.allSurfaces.size(); i++) {
+ Surface s = ((Surface)Surface.allSurfaces.elementAt(i));
+ do { s.render(); } while(s.abort);
+ }
+ } while(again);
+ } finally {
+ rendering = false;
+ }
+ }
+ }
+
+
+
+ // API which must be supported by subclasses /////////////////////////////////////
+
+ /**
+ * SCHEDULER INVARIANT: all scheduler implementations MUST invoke
+ * Surface.renderAll() after performing a Task if no tasks remain
+ * in the queue. A scheduler may choose to invoke
+ * Surface.renderAll() more often than that if it so chooses.
+ */
+ public void run() { defaultRun(); }
+ public Scheduler() { }
+
+
+ // Default Implementation //////////////////////////////////////////////////////
+
+ protected static Queue runnable = new Queue(50);
+ public void defaultRun() {
+ while(true) {
+ current = (Task)runnable.remove(true);
+ try {
+ // FIXME hideous
+ synchronized(this) {
+ for(int i=0; i<Surface.allSurfaces.size(); i++) {
+ Surface s = (Surface)Surface.allSurfaces.elementAt(i);
+ if (current instanceof JS) {
+ s._mousex = Integer.MAX_VALUE;
+ s._mousey = Integer.MAX_VALUE;
+ } else {
+ s._mousex = s.mousex;
+ s._mousey = s.mousey;
+ }
+ }
+ Log.debug(Scheduler.class, "performing " + current);
+ current.perform();
+ }
+ renderAll();
+ } catch (JSExn e) {
+ Log.info(Scheduler.class, "a JavaScript thread spawned with ibex.thread() threw an exception:");
+ Log.info(Scheduler.class,e);
+ } catch (Exception e) {
+ Log.info(Scheduler.class, "a Task threw an exception which was caught by the scheduler:");
+ Log.info(Scheduler.class, e);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ // if an Error is thrown it will cause the engine to quit
+ }
+ }
+}
--- /dev/null
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.ibex.js;
+
+import java.io.*;
+import java.util.zip.*;
+import org.ibex.util.*;
+//import org.ibex.plat.*;
+import org.ibex.net.*;
+
+/**
+ * Essentiall an InputStream "factory". You can repeatedly ask a
+ * Stream for an InputStream, and each InputStream you get back will
+ * be totally independent of the others (ie separate stream position
+ * and state) although they draw from the same data source.
+ */
+public abstract class Stream extends JS.Cloneable {
+
+ // Public Interface //////////////////////////////////////////////////////////////////////////////
+
+ public static InputStream getInputStream(Object js) throws IOException { return ((Stream)((JS)js).unclone()).getInputStream();}
+ public static class NotCacheableException extends Exception { }
+
+ // streams are "sealed" by default to prevent accidental object leakage
+ public void put(Object key, Object val) { }
+ private Cache getCache = new Cache(100);
+ protected Object _get(Object key) { return null; }
+ public final Object get(Object key) {
+ Object ret = getCache.get(key);
+ if (ret == null) getCache.put(key, ret = _get(key));
+ return ret;
+ }
+
+ // Private Interface //////////////////////////////////////////////////////////////////////////////
+
+ public abstract InputStream getInputStream() throws IOException;
+ protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
+
+ /** HTTP or HTTPS resource */
+ public static class HTTP extends Stream {
+ private String url;
+ public String toString() { return "Stream.HTTP:" + url; }
+ public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
+ public Object _get(Object key) { return new HTTP(url + "/" + (String)key); }
+ public String getCacheKey(Vec path) throws NotCacheableException { return url; }
+ public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(); }
+ }
+
+ /** byte arrays */
+ public static class ByteArray extends Stream {
+ private byte[] bytes;
+ private String cacheKey;
+ public ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
+ public String getCacheKey() throws NotCacheableException {
+ if (cacheKey == null) throw new NotCacheableException(); return cacheKey; }
+ public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
+ }
+
+ /** a file */
+ public static class File extends Stream {
+ private String path;
+ public File(String path) { this.path = path; }
+ public String toString() { return "file:" + path; }
+ public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
+ public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
+ public Object _get(Object key) { return new File(path + java.io.File.separatorChar + (String)key); }
+ }
+
+ /** "unwrap" a Zip archive */
+ public static class Zip extends Stream {
+ private Stream parent;
+ private String path;
+ public Zip(Stream parent) { this(parent, null); }
+ public Zip(Stream parent, String path) {
+ while(path != null && path.startsWith("/")) path = path.substring(1);
+ this.parent = parent;
+ this.path = path;
+ }
+ public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
+ public Object _get(Object key) { return new Zip(parent, path==null?(String)key:path+'/'+(String)key); }
+ public InputStream getInputStream() throws IOException {
+ InputStream pis = parent.getInputStream();
+ ZipInputStream zis = new ZipInputStream(pis);
+ ZipEntry ze = zis.getNextEntry();
+ while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
+ if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
+ return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
+ }
+ }
+
+ /** "unwrap" a Cab archive */
+ public static class Cab extends Stream {
+ private Stream parent;
+ private String path;
+ public Cab(Stream parent) { this(parent, null); }
+ public Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
+ public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
+ public Object _get(Object key) { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
+ public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
+ }
+
+ /** the Builtin resource */
+ public static class Builtin extends Stream {
+ public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
+ public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
+ }
+
+ /** shadow resource which replaces the graft */
+ public static class ProgressWatcher extends Stream {
+ final Stream watchee;
+ JS callback;
+ public ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
+ public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
+ public InputStream getInputStream() throws IOException {
+ final InputStream is = watchee.getInputStream();
+ return new FilterInputStream(is) {
+ int bytesDownloaded = 0;
+ public int read() throws IOException {
+ int ret = super.read();
+ if (ret != -1) bytesDownloaded++;
+ return ret;
+ }
+ public int read(byte[] b, int off, int len) throws IOException {
+ int ret = super.read(b, off, len);
+ if (ret != 1) bytesDownloaded += ret;
+ Scheduler.add(new Task() { public void perform() throws IOException, JSExn {
+ callback.call(N(bytesDownloaded),
+ N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
+ } });
+ return ret;
+ }
+ };
+ }
+ }
+
+ /** subclass from this if you want a CachedInputStream for each path */
+ public static class CachedStream extends Stream {
+ private Stream parent;
+ private boolean disk = false;
+ private String key;
+ public String getCacheKey() throws NotCacheableException { return key; }
+ CachedInputStream cis = null;
+ public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
+ this.parent = p; this.disk = d; this.key = p.getCacheKey();
+ }
+ public InputStream getInputStream() throws IOException {
+ if (cis != null) return cis.getInputStream();
+ if (!disk) {
+ cis = new CachedInputStream(parent.getInputStream());
+ } else {
+ java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
+ if (f.exists()) return new FileInputStream(f);
+ cis = new CachedInputStream(parent.getInputStream(), f);
+ }
+ return cis.getInputStream();
+ }
+ }
+}
--- /dev/null
+/*
+UserInfo:
+ On start:
+ 0: Addr of CAB/EXE
+ 1: Length of CAB/EXE
+ On Edit:
+ 2: Addr of output_table array
+
+Exit codes:
+ 0: Success
+ 1: Internal Error
+ 2: Invalid CAB
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <sys/fcntl.h>
+
+#include "mspack.h"
+
+#define MAX(a,b) (((a)>(b))?(a):(b))
+#define MIN(a,b) (((a)<(b))?(a):(b))
+#define MAX_MEMBERS 64
+
+char *xstrdup(const char *s) {
+ char *ret = strdup(s);
+ if(ret == NULL) exit(1);
+ return ret;
+}
+
+typedef struct {
+ char *addr;
+ int pos;
+ int size;
+ int length;
+ int writable;
+} mem_buf_t;
+
+static mem_buf_t *cab_mem_buf = NULL;
+
+static void mem_buf_grow(mem_buf_t *buf,size_t newsize) {
+ size_t new_len;
+ char *p;
+ if(buf->length < 0) exit(1);
+ if(newsize <= buf->length) return;
+ new_len = MAX(buf->length ? buf->length*2 : 65536,newsize);
+ p = realloc(buf->addr,new_len);
+ if(p == NULL) exit(1);
+ buf->addr = p;
+ buf->length = new_len;
+}
+
+static struct {
+ char *filename;
+ mem_buf_t buf;
+} write_buf_table[MAX_MEMBERS];
+
+static struct {
+ char *filename;
+ char *data;
+ int length;
+} output_table[MAX_MEMBERS+1];
+
+static struct mspack_file *my_open(struct mspack_system *sys, char *filename, int mode) {
+ mem_buf_t *buf = NULL;
+ int i;
+ if(strcmp(filename,"/dev/cab")==0) {
+ if(mode != MSPACK_SYS_OPEN_READ) return NULL;
+ buf = cab_mem_buf;
+ } else {
+ if(mode != MSPACK_SYS_OPEN_WRITE) return NULL;
+
+ for(i=0;i<MAX_MEMBERS;i++) {
+ if(write_buf_table[i].filename == NULL) {
+ write_buf_table[i].filename = xstrdup(filename);
+ buf = &write_buf_table[i].buf;
+ buf->writable = 1;
+ break;
+ }
+ }
+ }
+
+ return (struct mspack_file *) buf;
+}
+
+static void my_close(struct mspack_file *buf_) {
+ mem_buf_t *buf = (mem_buf_t*) buf_;
+ /* NO OP */
+}
+
+static int my_read(struct mspack_file *buf_, void *out, int count) {
+ mem_buf_t *buf = (mem_buf_t*) buf_;
+ count = MIN(buf->size - buf->pos, count);
+ memcpy(out,buf->addr + buf->pos,count);
+ buf->pos += count;
+ return count;
+}
+
+static int my_write(struct mspack_file *buf_, void *in, int count) {
+ mem_buf_t *buf = (mem_buf_t*) buf_;
+ if(!buf->writable) return -1;
+ if(buf->length < buf->pos + count) mem_buf_grow(buf,buf->pos + count);
+ memcpy(buf->addr+buf->pos,in,count);
+ buf->pos += count;
+ buf->size = MAX(buf->size,buf->pos);
+ return count;
+}
+
+static int my_seek(struct mspack_file *buf_, off_t off, int mode) {
+ mem_buf_t *buf = (mem_buf_t*) buf_;
+ int newpos;
+ switch(mode) {
+ case MSPACK_SYS_SEEK_START: newpos = off; break;
+ case MSPACK_SYS_SEEK_CUR: newpos = buf->pos + off; break;
+ case MSPACK_SYS_SEEK_END: newpos = buf->size - off; break;
+ default: return -1;
+ }
+ if(newpos < 0) return -1;
+ if(newpos > buf->size) {
+ if(!buf->writable) return -1;
+ if(newpos > buf->length)
+ mem_buf_grow(buf,newpos);
+ }
+ buf->pos = newpos;
+ return 0;
+}
+
+static off_t my_tell(struct mspack_file *buf_) {
+ mem_buf_t *buf = (mem_buf_t*) buf_;
+ return buf ? buf->pos : 0;
+}
+
+// FEATURE: Remove this to possibly avoid pulling in stdio from libc
+// (it may be getting pulled in anyway from malloc or something)
+static void my_message(struct mspack_file *file, char *format, ...) {
+ va_list ap;
+ va_start(ap, format);
+ vfprintf(stderr, format, ap);
+ va_end(ap);
+ fputc((int) '\n', stderr);
+ fflush(stderr);
+}
+
+static void *my_alloc(struct mspack_system *sys, size_t size) { return malloc(size); }
+static void my_free(void *p) { free(p); }
+static void my_copy(void *src, void *dest, size_t bytes) { memcpy(dest, src, bytes); }
+
+static struct mspack_system my_system = {
+ &my_open,
+ &my_close,
+ &my_read,
+ &my_write,
+ &my_seek,
+ &my_tell,
+ &my_message,
+ &my_alloc,
+ &my_free,
+ &my_copy,
+ NULL
+};
+
+extern char *user_info[1024];
+
+int mspack_main() {
+ struct mscab_decompressor *decomp;
+ struct mscabd_cabinet *cab;
+ struct mscabd_file *file;
+ mem_buf_t mem_buf;
+ size_t size = (size_t)user_info[1];
+ int i;
+
+ mem_buf.addr = user_info[0];
+ mem_buf.pos = mem_buf.writable = 0;
+ mem_buf.length = -1;
+ mem_buf.size = size;
+
+ cab_mem_buf = &mem_buf;
+
+ decomp = mspack_create_cab_decompressor(&my_system);
+ if(!decomp) exit(1);
+
+ cab = decomp->search(decomp,"/dev/cab");
+ if(!cab) exit(2);
+
+ for(file = cab->files;file;file=file->next)
+ decomp->extract(decomp,file,file->filename);
+
+ decomp->close(decomp,cab);
+ mspack_destroy_cab_decompressor(decomp);
+
+ for(i=0;i<MAX_MEMBERS && write_buf_table[i].filename;i++) {
+ output_table[i].filename = write_buf_table[i].filename;
+ output_table[i].data = write_buf_table[i].buf.addr;
+ output_table[i].length = write_buf_table[i].buf.size;
+ }
+
+ user_info[2] = (char*) output_table;
+
+ return 0;
+}
--- /dev/null
+package org.ibex.util;
+
+import org.ibex.core.Main;
+import org.ibex.util.*;
+import java.io.*;
+import org.ibex.nestedvm.*;
+import org.ibex.nestedvm.Runtime;
+
+public class MSPack {
+ private static byte[] image;
+
+ private String[] fileNames;
+ private int[] lengths;
+ private byte[][] data;
+
+ public static class MSPackException extends IOException { public MSPackException(String s) { super(s); } }
+
+ public MSPack(InputStream cabIS) throws IOException {
+ try {
+ Runtime vm = (Runtime)Class.forName("org.ibex.util.MIPSApps").newInstance();
+ byte[] cab = InputStreamToByteArray.convert(cabIS);
+ int cabAddr = vm.sbrk(cab.length);
+ if(cabAddr < 0) throw new MSPackException("sbrk failed");
+
+ vm.copyout(cab,cabAddr,cab.length);
+
+ vm.setUserInfo(0,cabAddr);
+ vm.setUserInfo(1,cab.length);
+
+ int status = vm.run(new String[]{ "mspack"} );
+ if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
+
+ /*static struct {
+ char *filename;
+ char *data;
+ int length;
+ } output_table[MAX_MEMBERS+1]; */
+
+ int filesTable = vm.getUserInfo(2);
+ int count=0;
+ while(vm.memRead(filesTable+count*12) != 0) count++;
+
+ fileNames = new String[count];
+ data = new byte[count][];
+ lengths = new int[count];
+
+ for(int i=0,addr=filesTable;i<count;i++,addr+=12) {
+ int length = vm.memRead(addr+8);
+ data[i] = new byte[length];
+ lengths[i] = length;
+ fileNames[i] = vm.cstring(vm.memRead(addr));
+ System.out.println("" + fileNames[i]);
+ vm.copyin(vm.memRead(addr+4),data[i],length);
+ }
+ } catch(Runtime.ExecutionException e) {
+ e.printStackTrace();
+ throw new MSPackException("mspack.mips crashed");
+ } catch(Exception e) {
+ throw new MSPackException(e.toString());
+ }
+ }
+
+ public String[] getFileNames() { return fileNames; }
+ public int[] getLengths() { return lengths; }
+ public InputStream getInputStream(int index) {
+ return new KnownLength.KnownLengthInputStream(new ByteArrayInputStream(data[index]), data[index].length);
+ }
+ public InputStream getInputStream(String fileName) {
+ for(int i=0;i<fileNames.length;i++) {
+ if(fileName.equalsIgnoreCase(fileNames[i])) return getInputStream(i);
+ }
+ return null;
+ }
+
+ public static void main(String[] args) throws IOException {
+ MSPack pack = new MSPack(new FileInputStream(args[0]));
+ String[] files = pack.getFileNames();
+ for(int i=0;i<files.length;i++)
+ System.out.println(i + ": " + files[i] + ": " + pack.getLengths()[i]);
+ System.out.println("Writing " + files[files.length-1]);
+ InputStream is = pack.getInputStream(files.length-1);
+ OutputStream os = new FileOutputStream(files[files.length-1]);
+ int n;
+ byte[] buf = new byte[4096];
+ while((n = is.read(buf)) != -1) os.write(buf,0,n);
+ os.close();
+ is.close();
+ }
+}
+