7 2: Addr of output_table array
19 #include <sys/fcntl.h>
23 #define MAX(a,b) (((a)>(b))?(a):(b))
24 #define MIN(a,b) (((a)<(b))?(a):(b))
25 #define MAX_MEMBERS 64
27 char *xstrdup(const char *s) {
28 char *ret = strdup(s);
29 if(ret == NULL) exit(1);
41 static mem_buf_t *cab_mem_buf = NULL;
43 static void mem_buf_grow(mem_buf_t *buf,size_t newsize) {
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);
52 buf->length = new_len;
58 } write_buf_table[MAX_MEMBERS];
64 } output_table[MAX_MEMBERS+1];
66 static struct mspack_file *my_open(struct mspack_system *sys, char *filename, int mode) {
67 mem_buf_t *buf = NULL;
69 if(strcmp(filename,"/dev/cab")==0) {
70 if(mode != MSPACK_SYS_OPEN_READ) return NULL;
73 if(mode != MSPACK_SYS_OPEN_WRITE) return NULL;
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;
85 return (struct mspack_file *) buf;
88 static void my_close(struct mspack_file *buf_) {
89 mem_buf_t *buf = (mem_buf_t*) buf_;
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);
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);
107 buf->size = MAX(buf->size,buf->pos);
111 static int my_seek(struct mspack_file *buf_, off_t off, int mode) {
112 mem_buf_t *buf = (mem_buf_t*) buf_;
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;
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);
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;
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, ...) {
139 va_start(ap, format);
140 vfprintf(stderr, format, ap);
142 fputc((int) '\n', stderr);
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); }
150 static struct mspack_system my_system = {
164 extern char *user_info[1024];
167 struct mscab_decompressor *decomp;
168 struct mscabd_cabinet *cab;
169 struct mscabd_file *file;
171 size_t size = (size_t)user_info[1];
174 mem_buf.addr = user_info[0];
175 mem_buf.pos = mem_buf.writable = 0;
179 cab_mem_buf = &mem_buf;
181 decomp = mspack_create_cab_decompressor(&my_system);
184 cab = decomp->search(decomp,"/dev/cab");
187 for(file = cab->files;file;file=file->next)
188 decomp->extract(decomp,file,file->filename);
190 decomp->close(decomp,cab);
191 mspack_destroy_cab_decompressor(decomp);
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;
199 user_info[2] = (char*) output_table;