support for zipped ISOs
[picodrive.git] / Pico / Cart.c
1 // This is part of Pico Library\r
2 \r
3 // (c) Copyright 2004 Dave, All rights reserved.\r
4 // (c) Copyright 2006 notaz, All rights reserved.\r
5 // Free for non-commercial use.\r
6 \r
7 // For commercial use, separate licencing terms must be obtained.\r
8 \r
9 \r
10 #include "PicoInt.h"\r
11 #include "../zlib/zlib.h"\r
12 #include "../unzip/unzip.h"\r
13 #include "../unzip/unzip_stream.h"\r
14 \r
15 static char *rom_exts[] = { "bin", "gen", "smd", "iso" };\r
16 \r
17 \r
18 pm_file *pm_open(const char *path)\r
19 {\r
20   pm_file *file = NULL;\r
21   const char *ext;\r
22   FILE *f;\r
23 \r
24   if (path == NULL) return NULL;\r
25 \r
26   if (strlen(path) < 5) ext = NULL; // no ext\r
27   else ext = path + strlen(path) - 3;\r
28 \r
29   if (ext && strcasecmp(ext, "zip") == 0)\r
30   {\r
31     struct zipent *zipentry;\r
32     gzFile gzf = NULL;\r
33     ZIP *zipfile;\r
34     int i;\r
35 \r
36     zipfile = openzip(path);\r
37 \r
38     if (zipfile != NULL)\r
39     {\r
40       /* search for suitable file (right extension or large enough file) */\r
41       while ((zipentry = readzip(zipfile)) != NULL)\r
42       {\r
43         if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;\r
44         if (strlen(zipentry->name) < 5) continue;\r
45 \r
46         ext = zipentry->name+strlen(zipentry->name)-3;\r
47         for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)\r
48           if (!strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;\r
49       }\r
50 \r
51       /* zipfile given, but nothing found suitable for us inside */\r
52       goto zip_failed;\r
53 \r
54 found_rom_zip:\r
55       /* try to convert to gzip stream, so we could use standard gzio functions from zlib */\r
56       gzf = zip2gz(zipfile, zipentry);\r
57       if (gzf == NULL)  goto zip_failed;\r
58 \r
59       file = malloc(sizeof(*file));\r
60       if (file == NULL) goto zip_failed;\r
61       file->file  = zipfile;\r
62       file->param = gzf;\r
63       file->size  = zipentry->uncompressed_size;\r
64       file->type  = PMT_ZIP;\r
65       return file;\r
66 \r
67 zip_failed:\r
68       if (gzf) {\r
69         gzclose(gzf);\r
70         zipfile->fp = NULL; // gzclose() closed it\r
71       }\r
72       closezip(zipfile);\r
73       return NULL;\r
74     }\r
75   }\r
76 \r
77   /* not a zip, treat as uncompressed file */\r
78   f = fopen(path, "rb");\r
79   if (f == NULL) return NULL;\r
80 \r
81   file = malloc(sizeof(*file));\r
82   if (file == NULL) {\r
83     fclose(f);\r
84     return NULL;\r
85   }\r
86   fseek(f, 0, SEEK_END);\r
87   file->file  = f;\r
88   file->param = NULL;\r
89   file->size  = ftell(f);\r
90   file->type  = PMT_UNCOMPRESSED;\r
91   fseek(f, 0, SEEK_SET);\r
92   return file;\r
93 }\r
94 \r
95 size_t pm_read(void *ptr, size_t bytes, pm_file *stream)\r
96 {\r
97   int ret;\r
98 \r
99   if (stream->type == PMT_UNCOMPRESSED)\r
100   {\r
101     ret = fread(ptr, 1, bytes, stream->file);\r
102   }\r
103   else if (stream->type == PMT_ZIP)\r
104   {\r
105     gzFile gf = stream->param;\r
106     int err;\r
107     ret = gzread(gf, ptr, bytes);\r
108     err = gzerror2(gf);\r
109     if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))\r
110       /* we must reset stream pointer or else next seek/read fails */\r
111       gzrewind(gf);\r
112   }\r
113   else\r
114     ret = 0;\r
115 \r
116   return ret;\r
117 }\r
118 \r
119 int pm_seek(pm_file *stream, long offset, int whence)\r
120 {\r
121   if (stream->type == PMT_UNCOMPRESSED)\r
122   {\r
123     return fseek(stream->file, offset, whence);\r
124   }\r
125   else if (stream->type == PMT_ZIP)\r
126   {\r
127     return gzseek((gzFile) stream->param, offset, whence);\r
128   }\r
129   else\r
130     return -1;\r
131 }\r
132 \r
133 int pm_close(pm_file *fp)\r
134 {\r
135   int ret = 0;\r
136 \r
137   if (fp == NULL) return EOF;\r
138 \r
139   if (fp->type == PMT_UNCOMPRESSED)\r
140   {\r
141     fclose(fp->file);\r
142   }\r
143   else if (fp->type == PMT_ZIP)\r
144   {\r
145     ZIP *zipfile = fp->file;\r
146     gzclose((gzFile) fp->param);\r
147     zipfile->fp = NULL; // gzclose() closed it\r
148     closezip(zipfile);\r
149   }\r
150   else\r
151     ret = EOF;\r
152 \r
153   free(fp);\r
154   return ret;\r
155 }\r
156 \r
157 \r
158 void Byteswap(unsigned char *data,int len)\r
159 {\r
160   int i=0;\r
161 \r
162   if (len<2) return; // Too short\r
163 \r
164   do\r
165   {\r
166     unsigned short *pd=(unsigned short *)(data+i);\r
167     int value=*pd; // Get 2 bytes\r
168 \r
169     value=(value<<8)|(value>>8); // Byteswap it\r
170     *pd=(unsigned short)value; // Put 2b ytes\r
171     i+=2;\r
172   }\r
173   while (i+2<=len);\r
174 }\r
175 \r
176 // Interleve a 16k block and byteswap\r
177 static int InterleveBlock(unsigned char *dest,unsigned char *src)\r
178 {\r
179   int i=0;\r
180   for (i=0;i<0x2000;i++) dest[(i<<1)  ]=src[       i]; // Odd\r
181   for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even\r
182   return 0;\r
183 }\r
184 \r
185 // Decode a SMD file\r
186 static int DecodeSmd(unsigned char *data,int len)\r
187 {\r
188   unsigned char *temp=NULL;\r
189   int i=0;\r
190 \r
191   temp=(unsigned char *)malloc(0x4000);\r
192   if (temp==NULL) return 1;\r
193   memset(temp,0,0x4000);\r
194 \r
195   // Interleve each 16k block and shift down by 0x200:\r
196   for (i=0; i+0x4200<=len; i+=0x4000)\r
197   {\r
198     InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer\r
199     memcpy(data+i,temp,0x4000); // Copy back in\r
200   }\r
201 \r
202   free(temp);\r
203   return 0;\r
204 }\r
205 \r
206 static unsigned char *cd_realloc(void *old, int filesize)\r
207 {\r
208   unsigned char *rom;\r
209   dprintf("sizeof(mcd_state): %i", sizeof(mcd_state));\r
210   rom=realloc(old, sizeof(mcd_state));\r
211   if (rom) memset(rom+0x20000, 0, sizeof(mcd_state)-0x20000);\r
212   return rom;\r
213 }\r
214 \r
215 static unsigned char *PicoCartAlloc(int filesize)\r
216 {\r
217   int alloc_size;\r
218   unsigned char *rom;\r
219 \r
220   if (PicoMCD & 1) return cd_realloc(NULL, filesize);\r
221 \r
222   alloc_size=filesize+0x7ffff;\r
223   if((filesize&0x3fff)==0x200) alloc_size-=0x200;\r
224   alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently\r
225   if((filesize&0x3fff)==0x200) alloc_size+=0x200;\r
226   else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection\r
227   //dprintf("alloc_size: %x\n",  alloc_size);\r
228 \r
229   // Allocate space for the rom plus padding\r
230   rom=(unsigned char *)malloc(alloc_size);\r
231   if(rom) memset(rom+alloc_size-0x80000,0,0x80000);\r
232   return rom;\r
233 }\r
234 \r
235 int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)\r
236 {\r
237   unsigned char *rom=NULL; int size;\r
238   if (f==NULL) return 1;\r
239 \r
240   size=f->size;\r
241   if (size <= 0) return 1;\r
242   size=(size+3)&~3; // Round up to a multiple of 4\r
243 \r
244   // Allocate space for the rom plus padding\r
245   rom=PicoCartAlloc(size);\r
246   if (rom==NULL) return 1; // { fclose(f); return 1; }\r
247 \r
248   pm_read(rom,size,f); // Load up the rom\r
249 \r
250   // maybe we are loading MegaCD BIOS?\r
251   if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {\r
252     PicoMCD |= 1;\r
253     rom = cd_realloc(rom, size);\r
254   }\r
255 \r
256   // Check for SMD:\r
257   if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD\r
258   else Byteswap(rom,size); // Just byteswap\r
259 \r
260   if (prom)  *prom=rom;\r
261   if (psize) *psize=size;\r
262 \r
263   return 0;\r
264 }\r
265 \r
266 // Insert/remove a cartridge:\r
267 int PicoCartInsert(unsigned char *rom,unsigned int romsize)\r
268 {\r
269   // notaz: add a 68k "jump one op back" opcode to the end of ROM.\r
270   // This will hang the emu, but will prevent nasty crashes.\r
271   // note: 4 bytes are padded to every ROM\r
272   if(rom != NULL)\r
273     *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped\r
274 \r
275   SRam.resize=1;\r
276   Pico.rom=rom;\r
277   Pico.romsize=romsize;\r
278 \r
279   return PicoReset(1);\r
280 }\r
281 \r
282 int PicoUnloadCart(unsigned char* romdata)\r
283 {\r
284   free(romdata);\r
285   return 0;\r
286 }\r
287 \r