-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmempackitem.cpp
More file actions
141 lines (120 loc) · 3.11 KB
/
mempackitem.cpp
File metadata and controls
141 lines (120 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "mempackitem.h"
#include <cstring>
#include <qtextcodec.h>
// ----------------------------------------------------------------------------
bool MemPackItem::tryLoadFrom(QFile& file, unsigned offset)
{
file.seek(offset);
// try to read header
if (file.read((char*)&header, sizeof header) < sizeof header) return false;
// try to detect a valid file...
// TODO: make this able to reject normal SNES ROMs...
bool titleValid = false;
for (int i = 0; i < sizeof header.title; i++)
{
if (header.title[i] != (char)0xff)
{
titleValid = true;
break;
}
}
if (!titleValid) return false;
if (header.blocks != 0
&& (((header.checksum ^ header.checksumComp) == 0xffff && header.makerFixed == 0x33)
|| ((header.checksum | header.checksumComp) == 0x0000)
|| header.blocks == 0xFFFFFFFF))
{
data.clear();
// read blocks based on the mapping bits
quint32 blocksLeft = normalizeBlocks(header.blocks, file.size());
for (int i = 0; i < 32 && blocksLeft; i++)
{
if (blocksLeft & (1 << i))
{
blocksLeft &= ~(1 << i);
// TODO: possibly handle non-po2 file sizes here...
file.seek(i << 17);
data += file.read(1 << 17);
}
}
char tempTitle[17];
memcpy(tempTitle, header.title, 16);
tempTitle[16] = '\0';
title = QTextCodec::codecForName("Shift-JIS")->toUnicode(tempTitle);
blocks = data.size() >> 17;
if (header.starts & (1 << 15))
{
starts = countBits(header.starts & 0x7fff);
}
else
{
starts = -1;
}
deleted = header.makerFixed != 0x33;
return true;
}
return false;
}
// ----------------------------------------------------------------------------
bool MemPackItem::saveToFile(QFile& file, unsigned offset)
{
QByteArray encodedTitle = QTextCodec::codecForName("Shift-JIS")->fromUnicode(title);
memset(header.title, 0, 16);
strncpy(header.title, encodedTitle.constData(), 16);
const unsigned blockStart = offset >> 17;
unsigned newBlocks = 0;
for (unsigned i = blockStart; i < blockStart + (data.size() >> 17); i++)
{
newBlocks |= (1 << i);
}
header.blocks = newBlocks;
unsigned headerPos = 0x7fb0;
if (header.mapMode & 1)
{
headerPos |= 1 << 15;
}
memset(data.data() + headerPos, 0, sizeof header);
if (deleted)
{
header.makerFixed = 0;
}
else
{
header.makerFixed = 0x33;
quint16 checksum = 0;
for (int i = 0; i < data.size(); i++)
{
checksum += (uint8)data[i];
}
header.checksum = checksum;
header.checksumComp = ~checksum;
}
memcpy(data.data() + headerPos, &header, sizeof header);
file.seek(offset);
return file.write(data) == data.size();
}
// ----------------------------------------------------------------------------
unsigned MemPackItem::countBits(unsigned val)
{
unsigned bits = 0;
while (val)
{
bits += (val & 1);
val >>= 1;
}
return bits;
}
// ----------------------------------------------------------------------------
unsigned MemPackItem::normalizeBlocks(quint32 blocks, unsigned packSize)
{
unsigned totalBlocks = packSize >> 17;
quint32 mask = 0xffffffff;
uint shift = 16;
while (totalBlocks <= shift)
{
blocks |= (blocks >> shift);
mask >>= shift;
shift >>= 1;
}
return blocks & mask;
}