Branch data Line data Source code
1 : : #include "asar.h"
2 : : #include "crc32.h"
3 : :
4 : : #include "platform/file-helpers.h"
5 : :
6 : : mapper_t mapper=lorom;
7 : : int sa1banks[8]={0<<20, 1<<20, -1, -1, 2<<20, 3<<20, -1, -1};
8 : : const unsigned char * romdata= nullptr; // NOTE: Changed into const to prevent direct write access - use writeromdata() functions below
9 : : int romlen;
10 : : static bool header;
11 : : static FileHandleType thisfile = InvalidFileHandle;
12 : :
13 : : asar_error_id openromerror;
14 : :
15 : : autoarray<writtenblockdata> writtenblocks;
16 : : // not immediately put into writtenblocks to allow the freespace finder to use
17 : : // the reclaimed space.
18 : : autoarray<writtenblockdata> cleared_rats_tag_blocks;
19 : :
20 : : // RPG Hacker: Uses binary search to find the insert position of our ROM write
21 : 79543 : static int findromwritepos(int snesoffset, int searchstartpos, int searchendpos)
22 : : {
23 [ + + ]: 235947 : if (searchendpos == searchstartpos)
24 : : {
25 : : return searchstartpos;
26 : : }
27 : :
28 : 156404 : int centerpos = searchstartpos + ((searchendpos - searchstartpos) / 2);
29 : :
30 [ + + ]: 156404 : if (writtenblocks[centerpos].snesoffset >= snesoffset)
31 : : {
32 : : return findromwritepos(snesoffset, searchstartpos, centerpos);
33 : : }
34 : :
35 : 91770 : return findromwritepos(snesoffset, centerpos + 1, searchendpos);
36 : : }
37 : :
38 : :
39 : 79313 : static void addromwriteforbank(int snesoffset, int numbytes)
40 : : {
41 : 79313 : int currentbank = (snesoffset & 0xFF0000);
42 : :
43 : 79313 : int insertpos = findromwritepos(snesoffset, 0, writtenblocks.count);
44 : :
45 [ + + ]: 79084 : if (insertpos > 0 && (writtenblocks[insertpos - 1].snesoffset & 0xFF0000) == currentbank
46 [ + + + + ]: 158205 : && writtenblocks[insertpos - 1].snesoffset + writtenblocks[insertpos - 1].numbytes >= snesoffset)
47 : : {
48 : : // Merge if we overlap with a preceding block
49 : 78551 : int firstend = writtenblocks[insertpos - 1].snesoffset + writtenblocks[insertpos - 1].numbytes;
50 : 78551 : int secondend = snesoffset + numbytes;
51 : :
52 : : int newend = (firstend > secondend ? firstend : secondend);
53 : :
54 : 78551 : numbytes = newend - writtenblocks[insertpos - 1].snesoffset;
55 : 78551 : snesoffset = writtenblocks[insertpos - 1].snesoffset;
56 : :
57 : 78551 : writtenblocks.remove(insertpos - 1);
58 : : insertpos -= 1;
59 : : }
60 : :
61 [ + + ]: 64001 : while (insertpos < writtenblocks.count && (writtenblocks[insertpos].snesoffset & 0xFF0000) == currentbank
62 [ + + + + ]: 110532 : && snesoffset + numbytes >= writtenblocks[insertpos].snesoffset)
63 : : {
64 : : // Merge if we overlap with a succeeding block
65 : : int firstend = snesoffset + numbytes;
66 : 409 : int secondend = writtenblocks[insertpos].snesoffset + writtenblocks[insertpos].numbytes;
67 : :
68 : : int newend = (firstend > secondend ? firstend : secondend);
69 : :
70 : 409 : numbytes = newend - snesoffset;
71 : :
72 : 409 : writtenblocks.remove(insertpos);
73 : : }
74 : :
75 : : // Insert ROM write
76 : : writtenblockdata blockdata;
77 : 79313 : blockdata.snesoffset = snesoffset;
78 : 79313 : blockdata.pcoffset = snestopc(snesoffset);
79 : 79313 : blockdata.numbytes = numbytes;
80 : :
81 : 79313 : writtenblocks.insert(insertpos, blockdata);
82 : 79313 : }
83 : :
84 : :
85 : 79279 : void addromwrite(int pcoffset, int numbytes)
86 : : {
87 : 79279 : int snesaddr = pctosnes(pcoffset);
88 : : int bytesleft = numbytes;
89 : :
90 : : // RPG Hacker: Some kind of witchcraft which I actually hope works as intended
91 : : // Basically, the purpose of this is to sort all ROM writes into banks for the sake of cleanness
92 : :
93 [ + + ]: 79313 : while (((snesaddr >> 16) & 0xFF) != (((snesaddr + bytesleft) >> 16) & 0xFF))
94 : : {
95 : 34 : int bytesinbank = ((snesaddr + 0x10000) & 0xFF0000) - snesaddr;
96 : :
97 : 34 : addromwriteforbank(snesaddr, bytesinbank);
98 : :
99 : 34 : pcoffset += bytesinbank;
100 : 34 : snesaddr = pctosnes(pcoffset);
101 : 34 : bytesleft -= bytesinbank;
102 : : }
103 : :
104 : 79279 : addromwriteforbank(snesaddr, bytesleft);
105 : 79279 : }
106 : :
107 : 112 : void writeromdata(int pcoffset, const void * indata, int numbytes)
108 : : {
109 : 112 : memcpy(const_cast<unsigned char*>(romdata) + pcoffset, indata, (size_t)numbytes);
110 : 112 : addromwrite(pcoffset, numbytes);
111 : 112 : }
112 : :
113 : 74735 : void writeromdata_byte(int pcoffset, unsigned char indata)
114 : : {
115 : 74735 : memcpy(const_cast<unsigned char*>(romdata) + pcoffset, &indata, 1);
116 : 74735 : addromwrite(pcoffset, 1);
117 : 74735 : }
118 : :
119 : 1 : void writeromdata_bytes(int pcoffset, unsigned char indata, int numbytes)
120 : : {
121 : 1 : memset(const_cast<unsigned char*>(romdata) + pcoffset, indata, (size_t)numbytes);
122 : 1 : addromwrite(pcoffset, numbytes);
123 : 1 : }
124 : :
125 : 35 : int ratsstart(int snesaddr)
126 : : {
127 : 35 : int pcaddr=snestopc(snesaddr);
128 [ + + ]: 35 : if (pcaddr<0x7FFF8) return -1;
129 : 31 : const unsigned char * start=romdata+pcaddr-0x10000;
130 [ + - ]: 514 : for (int i=0x10000;i>=0;i--)
131 : : {
132 [ + + ]: 514 : if (!strncmp((const char*)start+i, "STAR", 4) &&
133 [ + - + - ]: 31 : (start[i+4]^start[i+6])==0xFF && (start[i+5]^start[i+7])==0xFF)
134 : : {
135 [ + - ]: 31 : if ((start[i+4]|(start[i+5]<<8))>0x10000-i-8-1) return pctosnes((int)(start-romdata+i));
136 : : return -1;
137 : : }
138 : : }
139 : : return -1;
140 : : }
141 : :
142 : 3 : static void handleprot(int loc, char * name, int len, const unsigned char * contents)
143 : : {
144 : : (void)loc; // RPG Hacker: Silence "unused argument" warning.
145 : :
146 [ + - ]: 3 : if (!strncmp(name, "PROT", 4))
147 : : {
148 : 3 : memcpy(name, "NULL", 4);//to block recursion, in case someone is an idiot
149 [ + - ]: 3 : if (len%3) return;
150 : 3 : len/=3;
151 [ + + ]: 7 : for (int i=0;i<len;i++) removerats((contents[(i*3)+0] )|(contents[(i*3)+1]<<8 )|(contents[(i*3)+2]<<16), 0x00);
152 : : }
153 : : }
154 : :
155 : 8 : void removerats(int snesaddr, unsigned char clean_byte)
156 : : {
157 : 8 : int addr=ratsstart(snesaddr);
158 [ + + ]: 8 : if (addr<0) return;
159 : : // randomdude999: don't forget bank borders
160 : 7 : WalkMetadata(pctosnes(snestopc(addr)+8), handleprot);
161 : 7 : addr=snestopc(addr);
162 : : // don't use writeromdata() because this must not go to the writtenblocks list.
163 : 7 : int len = (romdata[addr+4]|(romdata[addr+5]<<8))+9;
164 : 7 : memset(const_cast<unsigned char*>(romdata) + addr, clean_byte, len);
165 : 7 : cleared_rats_tag_blocks[cleared_rats_tag_blocks.count] = writtenblockdata{addr, 0, len};
166 : : //for (int i=(romdata[addr+4]|(romdata[addr+5]<<8))+8;i>=0;i--) writeromdata_byte(addr+i, clean_byte);
167 : : }
168 : :
169 : 110 : void handle_cleared_rats_tags()
170 : : {
171 [ + + ]: 117 : for(int i = 0; i < cleared_rats_tag_blocks.count; i++) {
172 : : auto & block = cleared_rats_tag_blocks[i];
173 : 7 : addromwrite(block.pcoffset, block.numbytes);
174 : : }
175 : 110 : cleared_rats_tag_blocks.reset();
176 : 110 : }
177 : :
178 : 242 : static inline int trypcfreespace(int start, int end, int size, int banksize, int minalign, unsigned char freespacebyte, bool write_rats)
179 : : {
180 [ + + ]: 9934 : while (start+size<=end)
181 : : {
182 [ + + ]: 9919 : if (write_rats &&
183 [ + + ]: 9912 : ((start+8)&~banksize)!=((start+size-1)&~banksize&0xFFFFFF)//if the contents won't fit in this bank...
184 : 1 : &&
185 [ + - ]: 1 : (start&banksize&0xFFFFF8)!=(banksize&0xFFFFF8)//and the RATS tag can't fit in the bank either...
186 : : )
187 : : {
188 : 1 : start&=~banksize&0xFFFFFF;//round it down to the start of the bank,
189 : 1 : start|=banksize&0xFFFFF8;//then round it up to the end minus the RATS tag...
190 : 1 : continue;
191 : : }
192 : 7 : else if(!write_rats &&
193 [ - + ]: 7 : (start&~banksize) != ((start+size-1)&~banksize))
194 : : {
195 : : // go to next bank.
196 : 0 : start = (start|banksize)+1;
197 : 0 : continue;
198 : : }
199 [ + + ]: 9918 : if (minalign)
200 : : {
201 : 4 : start&=~minalign&0xFFFFFF;
202 : 4 : start|=minalign&0xFFFFF8;
203 : : }
204 [ + + ]: 9918 : if (!strncmp((const char*)romdata+start, "STAR", 4) &&
205 [ - + - + ]: 9688 : (romdata[start+4]^romdata[start+6])==0xFF && (romdata[start+5]^romdata[start+7])==0xFF)
206 : : {
207 : 9688 : start+=(romdata[start+4]|(romdata[start+5]<<8))+1+8;
208 : 9688 : continue;
209 : : }
210 : : bool bad=false;
211 [ + + ]: 2166762 : for (int i=0;i<size;i++)
212 : : {
213 [ - + ]: 2166532 : if (romdata[start+i]!=freespacebyte)
214 : : {
215 : : // TheBiob: fix freedata align freezing.
216 [ # # # # ]: 0 : if ((start & minalign) == 0x7FF8 && i < 8) i = 8;
217 : 0 : start+=i;
218 [ # # ]: 0 : if (!i) start++;//this could check for a rats tag instead, but somehow I think this will give better performance.
219 : : bad=true;
220 : : break;
221 : : }
222 : : }
223 : 0 : if (bad) continue;
224 : : // check against collisions with any written blocks.
225 : : // written blocks go through like 3x snes->pc->snes and are then sorted by snes.
226 : : // todo: this is janky..... should sort writtenblocks by pc maybe??
227 : 230 : int snesstart = pctosnes(start);
228 : : // this gives the index of the first block whose snespos is >= start.
229 : 230 : int writtenblock_pos = findromwritepos(snesstart, 0, writtenblocks.count);
230 : : // we might need to check the preceding block too.
231 : : // (but we don't need to check any older ones, as blocks are merged together)
232 [ + + ]: 230 : if(writtenblock_pos > 0) writtenblock_pos--;
233 [ + + ]: 460 : for(; writtenblock_pos < writtenblocks.count; writtenblock_pos++)
234 : : {
235 : : auto & block = writtenblocks[writtenblock_pos];
236 [ + + ]: 234 : if(snesstart < block.snesoffset+block.numbytes
237 [ + + ]: 4 : && snesstart+size > block.snesoffset)
238 : : {
239 : 3 : start = block.pcoffset + block.numbytes;
240 : : bad = true;
241 : : break;
242 : : }
243 : : // blocks are sorted by snesoffset, so if they start after our end we know there are no more that could intersect
244 [ + + ]: 231 : if(block.snesoffset > snesstart+size) break;
245 : : }
246 : 3 : if (bad) continue;
247 [ + + ]: 227 : if(write_rats)
248 : : {
249 : 222 : size-=8;
250 [ + - ]: 222 : if (size) size--;//rats tags eat one byte more than specified for some reason
251 : 222 : writeromdata_byte(start+0, 'S');
252 : 222 : writeromdata_byte(start+1, 'T');
253 : 222 : writeromdata_byte(start+2, 'A');
254 : 222 : writeromdata_byte(start+3, 'R');
255 : 222 : writeromdata_byte(start+4, (unsigned char)(size&0xFF));
256 : 222 : writeromdata_byte(start+5, (unsigned char)((size>>8)&0xFF));
257 : 222 : writeromdata_byte(start+6, (unsigned char)((size&0xFF)^0xFF));
258 : 222 : writeromdata_byte(start+7, (unsigned char)(((size>>8)&0xFF)^0xFF));
259 : 222 : return start+8;
260 : : }
261 : : else
262 : : {
263 : : // not sure if needs to be done here,
264 : : // but it needs to be done somewhere
265 : 5 : addromwrite(start, size);
266 : 5 : return start;
267 : : }
268 : : }
269 : : return -1;
270 : : }
271 : :
272 : : //This function finds a block of freespace. -1 means "no freespace found", anything else is a PC address.
273 : : //isforcode=false tells it to favor banks 40+, true tells it to avoid them entirely.
274 : : //It automatically adds a RATS tag.
275 : :
276 : 248 : int getpcfreespace(int size, int target_bank, bool autoexpand, bool respectbankborders, bool align, unsigned char freespacebyte, bool write_rats)
277 : : {
278 : : // TODO: probably should error if specifying target_bank and align, or target_bank and respectbankborders
279 [ + + ]: 248 : if (!size) return 0x1234;//in case someone protects zero bytes for some dumb reason.
280 : : //You can write zero bytes to anywhere, so I'll just return something that removerats will ignore.
281 [ - + ]: 227 : if (size>0x10000) return -1;
282 : 227 : bool isforcode = target_bank == -2;
283 [ + + ]: 227 : if(write_rats)
284 : 222 : size+=8;
285 : :
286 : 3 : auto find_for_fixed_bank = [&](int banksize, bool force_high_half = false) {
287 [ + - - + ]: 3 : if(!force_high_half && (target_bank & 0x40) == 0x40)
288 : 0 : return trypcfreespace(snestopc(target_bank<<16), min(romlen, snestopc(target_bank<<16)+0x10000), size, banksize, 0, freespacebyte, write_rats);
289 : : else
290 : 3 : return trypcfreespace(snestopc(target_bank<<16 | 0x8000), min(romlen, snestopc(target_bank<<16 | 0x8000)+0x8000), size, banksize, 0, freespacebyte, write_rats);
291 : 227 : };
292 : :
293 [ + + ]: 227 : if (mapper==lorom)
294 : : {
295 [ + + ]: 225 : if(target_bank >= 0) return find_for_fixed_bank(0x7fff);
296 [ + + - + ]: 222 : if (size>0x8008 && respectbankborders) return -1;
297 : 222 : rebootlorom:
298 [ + + + - ]: 237 : if (romlen>0x200000 && !isforcode)
299 : : {
300 [ - + - - ]: 28 : int pos=trypcfreespace(0x200000-8, (romlen<0x400000)?romlen:0x400000, size,
301 [ - + - - ]: 14 : respectbankborders?0x7FFF:0xFFFFFF, align?0x7FFF:(respectbankborders || size<32768)?0:0x7FFF, freespacebyte, write_rats);
302 [ + - ]: 14 : if (pos>=0) return pos;
303 : : }
304 [ + + + + ]: 448 : int pos=trypcfreespace(0x80000, (romlen<0x200000)?romlen:0x200000, size,
305 [ + + + - ]: 220 : respectbankborders?0x7FFF:0xFFFFFF, align?0x7FFF:(respectbankborders || size<32768)?0:0x7FFF, freespacebyte, write_rats);
306 [ + + ]: 223 : if (pos>=0) return pos;
307 [ + - ]: 15 : if (autoexpand)
308 : : {
309 : : if(0);
310 [ + + ]: 15 : else if (romlen==0x080000)
311 : : {
312 : 13 : romlen=0x100000;
313 : 13 : writeromdata_byte(snestopc(0x00FFD7), 0x0A);
314 : : }
315 [ + + ]: 2 : else if (romlen==0x100000)
316 : : {
317 : 1 : romlen=0x200000;
318 : 1 : writeromdata_byte(snestopc(0x00FFD7), 0x0B);
319 : : }
320 [ - + ]: 1 : else if (isforcode) return -1;//no point creating freespace that can't be used
321 [ - + - - ]: 1 : else if (romlen==0x200000 || romlen==0x300000)
322 : : {
323 : 1 : romlen=0x400000;
324 : 1 : writeromdata_byte(snestopc(0x00FFD7), 0x0C);
325 : : }
326 : : else return -1;
327 : : autoexpand=false;
328 : 15 : goto rebootlorom;
329 : : }
330 : : }
331 [ - + ]: 2 : if (mapper==hirom)
332 : : {
333 [ # # ]: 0 : if(target_bank >= 0) return find_for_fixed_bank(0xffff);
334 [ # # ]: 0 : if (isforcode)
335 : : {
336 [ # # ]: 0 : for(int i = 0x8000; i < min(romlen, 0x400000); i += 0x10000){
337 [ # # ]: 0 : int space = trypcfreespace(i, min(i+0x7FFF, romlen), size, 0x7FFF, align?0xFFFF:0, freespacebyte, write_rats);
338 [ # # ]: 0 : if(space != -1) return space;
339 : : }
340 : : return -1;
341 : : }
342 [ # # ]: 0 : return trypcfreespace(0, romlen, size, 0xFFFF, align?0xFFFF:0, freespacebyte, write_rats);
343 : : }
344 [ - + ]: 2 : if (mapper==exlorom)
345 : : {
346 [ # # ]: 0 : if(target_bank >= 0) return find_for_fixed_bank(0x7fff);
347 : : // RPG Hacker: Not really 100% sure what to do here, but I suppose this simplified code will do
348 : : // and we won't need all the complicated stuff from LoROM above?
349 [ # # ]: 0 : if (isforcode)
350 : : {
351 [ # # ]: 0 : trypcfreespace(0, min(romlen, 0x200000), size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats);
352 : : }
353 [ # # ]: 0 : return trypcfreespace(0, romlen, size, 0x7FFF, align ? 0x7FFF : 0, freespacebyte, write_rats);
354 : : }
355 [ - + ]: 2 : if (mapper==exhirom)
356 : : {
357 [ # # ]: 0 : if(target_bank >= 0) return find_for_fixed_bank(0xffff);
358 [ # # ]: 0 : if (isforcode)
359 : : {
360 [ # # # # ]: 0 : for(int i = 0x8000; i < romlen && i < 0x400000; i += 0x10000){
361 [ # # ]: 0 : int space = trypcfreespace(i, min(i+0x7FFF, romlen), size, 0x7FFF, align?0xFFFF:0, freespacebyte, write_rats);
362 [ # # ]: 0 : if(space != -1) return space;
363 : : }
364 : : return -1;
365 : : }
366 [ # # ]: 0 : return trypcfreespace(0, romlen, size, 0xFFFF, align?0xFFFF:0, freespacebyte, write_rats);
367 : : }
368 [ + + ]: 2 : if (mapper==sfxrom)
369 : : {
370 [ - + ]: 1 : if(target_bank >= 0) return find_for_fixed_bank(0xffff);
371 [ - + ]: 1 : if (!isforcode) return -1;
372 : : // try not to overwrite smw stuff
373 [ + - ]: 2 : return trypcfreespace(0x80000, romlen, size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats);
374 : : }
375 [ + - ]: 1 : if (mapper==sa1rom)
376 : : {
377 [ - + ]: 1 : if(target_bank >= 0) return find_for_fixed_bank(0xffff);
378 : 1 : rebootsa1rom:
379 : : int nextbank=-1;
380 [ + + ]: 10 : for (int i=0;i<8;i++)
381 : : {
382 [ + + ]: 9 : if (i&2) continue;
383 [ + + ]: 5 : if (sa1banks[i]+0x100000>romlen)
384 : : {
385 [ + + ]: 4 : if (sa1banks[i]<=romlen && sa1banks[i]+0x100000>romlen) nextbank=sa1banks[i];
386 : 4 : continue;
387 : : }
388 [ + - - + ]: 2 : int pos=trypcfreespace(sa1banks[i]?sa1banks[i]:0x80000, sa1banks[i]+0x100000, size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats);
389 [ + - ]: 1 : if (pos>=0) return pos;
390 : : }
391 [ + - ]: 1 : if (autoexpand && nextbank>=0)
392 : : {
393 : 1 : unsigned char x7FD7[]={0, 0x0A, 0x0B, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D};
394 : 1 : romlen=nextbank+0x100000;
395 : 1 : writeromdata_byte(0x7FD7, x7FD7[romlen>>20]);
396 : : autoexpand=false;
397 : 1 : goto rebootsa1rom;
398 : : }
399 : : }
400 [ # # ]: 0 : if (mapper==bigsa1rom)
401 : : {
402 [ # # ]: 0 : if(target_bank >= 0) return find_for_fixed_bank(0xffff);
403 [ # # # # ]: 0 : if(!isforcode && romlen > 0x400000)
404 : : {
405 [ # # ]: 0 : int pos=trypcfreespace(0x400000, romlen, size, 0xFFFF, align?0xFFFF:0, freespacebyte, write_rats);
406 [ # # ]: 0 : if(pos>=0) return pos;
407 : : }
408 [ # # ]: 0 : int pos=trypcfreespace(0x080000, romlen, size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats);
409 [ # # ]: 0 : if(pos>=0) return pos;
410 : : }
411 : : return -1;
412 : : }
413 : :
414 : 7 : void WalkMetadata(int loc, void(*func)(int loc, char * name, int len, const unsigned char * contents))
415 : : {
416 : 7 : int pcoff=snestopc(loc);
417 [ + - ]: 7 : if (strncmp((const char*)romdata+pcoff-8, "STAR", 4)) return;
418 : 7 : const unsigned char * metadata=romdata+pcoff;
419 [ + + + - : 10 : while (is_upper(metadata[0]) && is_upper(metadata[1]) && is_upper(metadata[2]) && is_upper(metadata[3]))
+ - + - ]
420 : : {
421 [ + + ]: 5 : if (!strncmp((const char*)metadata, "STOP", 4))
422 : : {
423 : : metadata=romdata+pcoff;
424 [ + - + - : 5 : while (is_upper(metadata[0]) && is_upper(metadata[1]) && is_upper(metadata[2]) && is_upper(metadata[3]))
+ - + - ]
425 : : {
426 [ + + ]: 5 : if (!strncmp((const char*)metadata, "STOP", 4))
427 : : {
428 : : break;
429 : : }
430 : 3 : func(pctosnes((int)(metadata-romdata)), (char*)const_cast<unsigned char*>(metadata), metadata[4], metadata+5);
431 : 3 : metadata+=5+metadata[4];
432 : : }
433 : : break;
434 : : }
435 : 3 : metadata+=5+metadata[4];
436 : : }
437 : : }
438 : :
439 : 247 : int getsnesfreespace(int size, int target_bank, bool autoexpand, bool respectbankborders, bool align, unsigned char freespacebyte, bool write_rats)
440 : : {
441 : 247 : return pctosnes(getpcfreespace(size, target_bank, autoexpand, respectbankborders, align, freespacebyte, write_rats));
442 : : }
443 : :
444 : 115 : bool openrom(const char * filename, bool confirm)
445 : : {
446 : 115 : closerom();
447 : 115 : thisfile = open_file(filename, FileOpenMode_ReadWrite);
448 [ - + ]: 115 : if (thisfile == InvalidFileHandle)
449 : : {
450 : 0 : openromerror = error_id_open_rom_failed;
451 : 0 : return false;
452 : : }
453 : 115 : header=false;
454 [ + - ]: 115 : if (strlen(filename)>4)
455 : : {
456 : 115 : const char * fnameend=strchr(filename, '\0')-4;
457 : 115 : header=(!stricmp(fnameend, ".smc"));
458 : : }
459 : 115 : romlen=(int)get_file_size(thisfile)-(header*512);
460 [ - + ]: 115 : if (romlen<0) romlen=0;
461 : 115 : set_file_pos(thisfile, header*512);
462 : 115 : romdata=(unsigned char*)malloc(sizeof(unsigned char)*16*1024*1024);
463 : 115 : int truelen=(int)read_file(thisfile, (void*)romdata, (uint32_t)romlen);
464 [ - + ]: 115 : if (truelen!=romlen)
465 : : {
466 : 0 : openromerror = error_id_open_rom_failed;
467 : 0 : free(const_cast<unsigned char*>(romdata));
468 : 0 : return false;
469 : : }
470 : 115 : memset(const_cast<unsigned char*>(romdata)+romlen, 0x00, (size_t)(16*1024*1024-romlen));
471 [ - + - - : 115 : if (confirm && snestopc(0x00FFC0)+21<(int)romlen && strncmp((const char*)romdata+snestopc(0x00FFC0), "SUPER MARIOWORLD ", 21))
- - ]
472 : : {
473 : 0 : closerom(false);
474 [ # # ]: 0 : openromerror = header ? error_id_open_rom_not_smw_extension : error_id_open_rom_not_smw_header;
475 : 0 : return false;
476 : : }
477 : :
478 : 115 : romdata_r=(unsigned char*)malloc((size_t)romlen);
479 : 115 : romlen_r=romlen;
480 : 115 : memcpy((void*)romdata_r, romdata, (size_t)romlen);//recently allocated, dead
481 : :
482 : 115 : return true;
483 : : }
484 : :
485 : 230 : uint32_t closerom(bool save)
486 : : {
487 : : uint32_t romCrc = 0;
488 [ + + + + : 230 : if (thisfile != InvalidFileHandle && save && romlen)
+ + ]
489 : : {
490 : 78 : set_file_pos(thisfile, header*512);
491 : 78 : write_file(thisfile, romdata, (uint32_t)romlen);
492 : :
493 : : // do a quick re-read of the header, and include that in the crc32 calculation if necessary
494 : : {
495 : 78 : uint8_t* filedata = (uint8_t*)malloc(sizeof(uint8_t) * (romlen + header * 512));
496 [ - + ]: 78 : if (header)
497 : : {
498 : 0 : set_file_pos(thisfile, 0u);
499 : 0 : read_file(thisfile, filedata, 512);
500 : : }
501 : 78 : memcpy(filedata + (header * 512), romdata, sizeof(uint8_t) * (size_t)romlen);
502 : 78 : romCrc = crc32(filedata, (unsigned int)(romlen + header * 512));
503 : 78 : free(filedata);
504 : : }
505 : : }
506 [ + + ]: 230 : if (thisfile != InvalidFileHandle) close_file(thisfile);
507 [ + + ]: 230 : if (romdata) free(const_cast<unsigned char*>(romdata));
508 : 230 : thisfile= InvalidFileHandle;
509 : 230 : romdata= nullptr;
510 : 230 : romdata_r = nullptr;
511 : 230 : romlen=0;
512 : 230 : return romCrc;
513 : : }
514 : :
515 : 111 : static unsigned int getchecksum()
516 : : {
517 : : unsigned int checksum=0;
518 [ + + ]: 111 : if((romlen & (romlen-1)) == 0)
519 : : {
520 : : // romlen is a power of 2, just add up all the bytes
521 [ + + ]: 29884527 : for (int i=0;i<romlen;i++) checksum+=romdata[i];
522 : : }
523 : : else
524 : : {
525 : : // assume romlen is the sum of 2 powers of 2 - i haven't seen any real rom that isn't,
526 : : // and if you make such a rom, fixing its checksum is your problem.
527 : 42 : int firstpart = bitround(romlen) >> 1;
528 : 42 : int secondpart = romlen - firstpart;
529 : 42 : int repeatcount = firstpart / secondpart;
530 : : unsigned int secondpart_sum = 0;
531 [ + + ]: 8424800 : for(int i = 0; i < firstpart; i++) checksum += romdata[i];
532 [ + + ]: 7340658 : for(int i = firstpart; i < romlen; i++) secondpart_sum += romdata[i];
533 : 42 : checksum += secondpart_sum * repeatcount;
534 : : }
535 : 111 : return checksum&0xFFFF;
536 : : }
537 : :
538 : 111 : void fixchecksum()
539 : : {
540 : : // randomdude999: clear out checksum bytes before recalculating checksum, this should make it correct on roms that don't have a checksum yet
541 : 111 : writeromdata(snestopc(0x00FFDC), "\xFF\xFF\0\0", 4);
542 : 111 : int checksum=(int)getchecksum();
543 : 111 : writeromdata_byte(snestopc(0x00FFDE), (unsigned char)(checksum&255));
544 : 111 : writeromdata_byte(snestopc(0x00FFDF), (unsigned char)((checksum>>8)&255));
545 : 111 : writeromdata_byte(snestopc(0x00FFDC), (unsigned char)((checksum&255)^255));
546 : 111 : writeromdata_byte(snestopc(0x00FFDD), (unsigned char)(((checksum>>8)&255)^255));
547 : 111 : }
|