2003/11/03 02:10:52
authorbrian <brian@xwt.org>
Fri, 30 Jan 2004 07:40:47 +0000 (07:40 +0000)
committerbrian <brian@xwt.org>
Fri, 30 Jan 2004 07:40:47 +0000 (07:40 +0000)
darcs-hash:20040130074047-aa32f-e9d551f38ca60aa901e51ea0c2bc94e38c830e35.gz

Makefile
src/org/xwt/mips/VM.java
src/org/xwt/translators/MSPack.c
src/org/xwt/translators/MSPack.java

index d0361e7..d89b329 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -178,7 +178,10 @@ build/mips/%.c.o: src/%.c
        make .install_freetype-2.1.4_mips-unknown-elf target=mips-unknown-elf
        mkdir -p $(@D)
        echo -e "\n\033[1mcompiling $< -> $@ (mips)\033[0m"
-       upstream/install/bin/mips-unknown-elf-gcc -march=r3000 -I upstream/freetype-2.1.4/src/include -c -o $@ $<
+       upstream/install/bin/mips-unknown-elf-gcc -march=r3000 \
+               -Iupstream/freetype-2.1.4/src/include  \
+               -Iupstream/libmspack-20030726/libmspack/mspack/ \
+               -c -o $@ $<
 
 build/res/freetype.mips: build/mips/org/xwt/translators/Freetype.c.o build/mips/org/xwt/mips/crt0.c.o build/mips/org/xwt/mips/syscalls.c.o
        make .install_freetype-2.1.4_mips-unknown-elf target=mips-unknown-elf
index 08fee29..b0bbf7b 100644 (file)
@@ -1,7 +1,7 @@
 package org.xwt.mips;
 import java.io.*;
 
-abstract class VM implements Syscalls, Errno {
+public abstract class VM implements Syscalls, Errno {
     // Register Names
     protected final static int ZERO = 0; // Immutable, hardwired to 0
     protected final static int AT = 1;  // Reserved for assembler
@@ -125,7 +125,7 @@ abstract class VM implements Syscalls, Errno {
             int end = start + (min(PAGE_SIZE-(addr&(PAGE_SIZE-1)),(length-n)&~3) >> 2);
             int[] page = readPages[addr >>> PAGE_SHIFT];
             if(page == null) throw new ReadFaultException(addr);
-            if(page == emptyPage) { addr+=(end-start); n+=(end-start); continue; }
+            if(page == emptyPage) { addr+=(end-start)<<2; n+=(end-start)<<2; continue; }
             for(int i=start;i<end;i++,addr+=4) {
                 int word = page[i];
                 a[n++] = (byte)((word>>>24)&0xff); a[n++] = (byte)((word>>>16)&0xff);
@@ -504,7 +504,7 @@ abstract class VM implements Syscalls, Errno {
     }
     
     // Helper function to read a cstring from main memory
-    private String cstring(int addr) throws ReadFaultException {
+    public String cstring(int addr) throws ReadFaultException {
         StringBuffer sb = new StringBuffer();
         for(;;) {
             int word = memRead(addr&~3);
index 42bafcf..96cb1ac 100644 (file)
-// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
+/*
+UserInfo:
+    On start:
+        0: Addr of CAB/EXE
+        1: Length of CAB/EXE
+    On Edit:
+        2: Addr of output_table array
 
-/* NOTE: _user_info is defined in crt0.c. It points to a 4096 byte
-   block of memory that contains 1024 32-bit values that can be set
-   with the setUserInfo() method of MIPSEmu.
+Exit codes:
+    0: Success
+    1: Internal Error
+    2: Invalid CAB
+*/
 
-   The VM will pause after initialization.  When unpaused, it expects
-   that:
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <sys/fcntl.h>
 
-       FIXME
+#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;
+
+int main(int argc, char **argv) {
+    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);
 
-int main(int argc, char** argv) {
-  // FIXME: do some cool stuff here
+    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;
 }
index 00316de..2ad52d2 100644 (file)
@@ -1,40 +1,92 @@
 package org.xwt.translators;
-import org.xwt.*;
+
+import org.xwt.Main;
 import org.xwt.util.*;
+import org.xwt.mips.*;
 import java.io.*;
-import java.util.zip.*;
 
 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); } }
+    
+    private static synchronized byte[] getImage() {
+        if (image == null)
+            image = InputStreamToByteArray.convert(Main.builtin.getInputStream("libmspack.mips"));
+        return image;
+    }
+    
+    public MSPack(InputStream cabIS) throws IOException {
+        byte[] cab = InputStreamToByteArray.convert(cabIS);
+        Interpreter vm = new Interpreter(getImage());
+        
+        int cabAddr = vm.sbrk(cab.length);
+        if(cabAddr < 0) throw new MSPackException("sbrk failed");
+        
+        try {
+            vm.copyout(cab,cabAddr,cab.length);
+        
+            vm.setUserInfo(0,cabAddr);
+            vm.setUserInfo(1,cab.length);
+        
+            int status = vm.run(new String[]{ "mspack.mips"} );
+            if(status != 0) throw new MSPackException("mspack.mips failed (" + status + ")");
+            
+            /*static struct {
+                char *filename;
+                char *data;
+                int length;
+            } output_table[MAX_MEMBERS+1]; */
 
-    MSPack() { }
-
-    private static org.xwt.mips.Interpreter vm = null;
-
-    public static synchronized void unpack(InputStream theCabFile) {
-        try {           
-            if (vm == null) {
-                InputStream bis = Platform.getBuiltinInputStream();
-                ZipInputStream zis = new ZipInputStream(bis);
-                for(ZipEntry ze = zis.getNextEntry(); ze != null && !ze.getName().equals("libmspack.mips"); ze = zis.getNextEntry()) { }
-                byte[] image = InputStreamToByteArray.convert(zis);
-                vm = new org.xwt.mips.Interpreter(image);
-                vm.start(new String[]{ "libmspack.mips" });
-                vm.execute();
-            }
-
-            int CAB_RESERVED = 256*1024;
-            int baseAddr = vm.sbrk(CAB_RESERVED);
+            int filesTable = vm.getUserInfo(2);
+            int count=0;
+            while(vm.memRead(filesTable+count*12) != 0) count++;
             
-            byte[] cabstream = InputStreamToByteArray.convert(theCabFile);
-            vm.copyout(cabstream, baseAddr, cabstream.length);
-            vm.setUserInfo(0, baseAddr);
-            vm.setUserInfo(1, cabstream.length);
+            fileNames = new String[count];
+            data = new byte[count][];
+            lengths = new int[count];
             
-            vm.execute();
-            // FIXME: do more stuff here
-
-        } catch (Exception e) {
-            Log.log(MSPack.class, e);
+            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(VM.EmulationException e) {
+            e.printStackTrace();
+            throw new MSPackException("mspack.mips crashed");
+        }
+    }
+    
+    public String[] getFileNames() { return fileNames; }
+    public int[] getLengths() { return lengths; }
+    public InputStream getInputStream(int index) { return new ByteArrayInputStream(data[index]); }
+    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();
     }
 }
+