+++ /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();
- }
-}
-