asar coverage - build #72


src/asar/
File: src/asar/libsmw.cpp
Date: 2024-01-18 08:25:45
Lines:
296/333
88.9%
Functions:
21/21
100.0%
Branches:
275/430
64.0%

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