resolve darcs stupidity
[org.ibex.core.git] / src / org / ibex / translators / MSPack.c
1 /*
2 UserInfo:
3     On start:
4         0: Addr of CAB/EXE
5         1: Length of CAB/EXE
6     On Edit:
7         2: Addr of output_table array
8
9 Exit codes:
10     0: Success
11     1: Internal Error
12     2: Invalid CAB
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdarg.h>
19 #include <sys/fcntl.h>
20
21 #include "mspack.h"
22
23 #define MAX(a,b) (((a)>(b))?(a):(b))
24 #define MIN(a,b) (((a)<(b))?(a):(b))
25 #define MAX_MEMBERS 64
26
27 char *xstrdup(const char *s) {
28     char *ret = strdup(s);
29     if(ret == NULL) exit(1);
30     return ret;
31 }
32
33 typedef struct {
34     char *addr;
35     int pos;
36     int size;
37     int length;
38     int writable;
39 } mem_buf_t;
40
41 static mem_buf_t *cab_mem_buf = NULL;
42
43 static void mem_buf_grow(mem_buf_t *buf,size_t newsize) {
44     size_t new_len;
45     char *p;
46     if(buf->length < 0) exit(1); 
47     if(newsize <= buf->length) return;
48     new_len = MAX(buf->length ? buf->length*2 : 65536,newsize);
49     p = realloc(buf->addr,new_len);
50     if(p == NULL) exit(1);
51     buf->addr = p;
52     buf->length = new_len;
53 }
54
55 static struct {
56     char *filename;
57     mem_buf_t buf;
58 } write_buf_table[MAX_MEMBERS];
59
60 static struct {
61     char *filename;
62     char *data;
63     int length;
64 } output_table[MAX_MEMBERS+1];
65
66 static struct mspack_file *my_open(struct mspack_system *sys, char *filename, int mode) {
67     mem_buf_t *buf = NULL;
68     int i;
69     if(strcmp(filename,"/dev/cab")==0) {    
70         if(mode != MSPACK_SYS_OPEN_READ) return NULL;
71         buf = cab_mem_buf;
72     } else {
73         if(mode != MSPACK_SYS_OPEN_WRITE) return NULL;
74         
75         for(i=0;i<MAX_MEMBERS;i++) {
76             if(write_buf_table[i].filename == NULL) {
77                 write_buf_table[i].filename = xstrdup(filename);
78                 buf = &write_buf_table[i].buf;
79                 buf->writable = 1;
80                 break;
81             }
82         }
83     }
84     
85     return (struct mspack_file *) buf;
86 }
87
88 static void my_close(struct mspack_file *buf_) {
89     mem_buf_t *buf = (mem_buf_t*) buf_;
90     /* NO OP */
91 }
92
93 static int my_read(struct mspack_file *buf_, void *out, int count) {
94     mem_buf_t *buf = (mem_buf_t*) buf_;
95     count = MIN(buf->size - buf->pos, count);
96     memcpy(out,buf->addr + buf->pos,count);
97     buf->pos += count;
98     return count;
99 }
100
101 static int my_write(struct mspack_file *buf_, void *in, int count) {
102     mem_buf_t *buf = (mem_buf_t*) buf_;
103     if(!buf->writable) return -1;
104     if(buf->length < buf->pos + count) mem_buf_grow(buf,buf->pos + count);
105     memcpy(buf->addr+buf->pos,in,count);
106     buf->pos += count;
107     buf->size = MAX(buf->size,buf->pos);
108     return count;
109 }
110
111 static int my_seek(struct mspack_file *buf_, off_t off, int mode) {
112     mem_buf_t *buf = (mem_buf_t*) buf_;
113     int newpos;
114     switch(mode) {
115         case MSPACK_SYS_SEEK_START: newpos = off; break;
116         case MSPACK_SYS_SEEK_CUR: newpos = buf->pos + off; break;
117         case MSPACK_SYS_SEEK_END: newpos = buf->size - off; break;
118         default: return -1;
119     }
120     if(newpos < 0) return -1;
121     if(newpos > buf->size) {
122         if(!buf->writable) return -1;
123         if(newpos > buf->length)
124             mem_buf_grow(buf,newpos);
125     }
126     buf->pos = newpos;
127     return 0;
128 }
129
130 static off_t my_tell(struct mspack_file *buf_) {
131     mem_buf_t *buf = (mem_buf_t*) buf_;
132     return buf ? buf->pos : 0;
133 }
134
135 // FEATURE: Remove this to possibly avoid pulling in stdio from libc 
136 // (it may be getting pulled in anyway from malloc or something)
137 static void my_message(struct mspack_file *file, char *format, ...) {
138   va_list ap;
139   va_start(ap, format);
140   vfprintf(stderr, format, ap);
141   va_end(ap);
142   fputc((int) '\n', stderr);
143   fflush(stderr);
144 }
145
146 static void *my_alloc(struct mspack_system *sys, size_t size) { return malloc(size); }
147 static void my_free(void *p) { free(p); }
148 static void my_copy(void *src, void *dest, size_t bytes) { memcpy(dest, src, bytes); }
149
150 static struct mspack_system my_system =  {
151     &my_open,
152     &my_close,
153     &my_read, 
154     &my_write,
155     &my_seek,
156     &my_tell,
157     &my_message,
158     &my_alloc,
159     &my_free,
160     &my_copy,
161     NULL
162 };
163
164 extern char *user_info[1024];
165
166 int mspack_main() {
167     struct mscab_decompressor *decomp;
168     struct mscabd_cabinet *cab;
169     struct mscabd_file *file;
170     mem_buf_t mem_buf;
171     size_t size = (size_t)user_info[1];
172     int i;
173     
174     mem_buf.addr = user_info[0];
175     mem_buf.pos = mem_buf.writable = 0;
176     mem_buf.length = -1;
177     mem_buf.size = size;
178     
179     cab_mem_buf = &mem_buf;
180                 
181     decomp = mspack_create_cab_decompressor(&my_system);
182     if(!decomp) exit(1);
183     
184     cab = decomp->search(decomp,"/dev/cab");
185     if(!cab) exit(2);
186
187     for(file = cab->files;file;file=file->next)
188         decomp->extract(decomp,file,file->filename);
189         
190     decomp->close(decomp,cab);
191     mspack_destroy_cab_decompressor(decomp);
192         
193     for(i=0;i<MAX_MEMBERS && write_buf_table[i].filename;i++) {
194         output_table[i].filename = write_buf_table[i].filename;
195         output_table[i].data = write_buf_table[i].buf.addr;
196         output_table[i].length = write_buf_table[i].buf.size;
197     }
198     
199     user_info[2] = (char*) output_table;
200     
201     return 0;
202 }