forked from silicontrip/SkyReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileio.cpp
290 lines (221 loc) · 5.64 KB
/
fileio.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "fileio.h"
void SkylanderIO::fprinthex(FILE *f, unsigned char *c, unsigned int n) {
unsigned int h,i;
unsigned char j;
for (h=0; h<n; h+=16) {
fprintf (f,"%04x: ",h);
for (i=0; i<16; i++) {
if (i+h < n)
fprintf (f,"%02x ",*(c+i+h) & 0xff);
else
fprintf (f," ");
}
for (i=0; i<16; i++) {
if (i+h < n) {
j = *(c+i+h);
if (j<32) j='.';
if (j>=127) j='.';
fprintf (f,"%c",j);
} else
fprintf(f," ");
}
fprintf(f,"\n");
}
}
SkylanderIO::SkylanderIO ()
{
sky = NULL;
buffer=new unsigned char [1025];
}
SkylanderIO::~SkylanderIO ()
{
delete buffer;
}
void SkylanderIO::initWithUnencryptedFile(char * name) throw (int)
{
if (!sky) {
ReadSkylanderFile(name);
sky = new Skylander(buffer);
}
}
void SkylanderIO::initWithEncryptedFile(char * name) throw (int)
{
if (!sky) {
ReadSkylanderFile(name);
DecryptBuffer(buffer);
sky = new Skylander(buffer);
}
}
void SkylanderIO::initWithPortal(int number) throw (int) {
if (!sky) {
//printf("Reading Skylander from portal.\n");
ReadPortal(buffer,number);
DecryptBuffer(buffer);
sky = new Skylander(buffer);
// printf("\nSkylander read from portal.\n");
}
}
void SkylanderIO::ReadPortal(unsigned char *s, int number) throw (int)
{
unsigned char data[0x10];
unsigned char *ptr;
PortalIO *port;
port = new PortalIO();
// must start with a read of block zero
port->ReadBlock(0, data, number);
// I don't know that we need this, but the web driver sets the color when reading the data
port->SetPortalColor(0xC8, 0xC8, 0xC8);
ptr = s;
memcpy(ptr, data, sizeof(data));
for(int block=1; block < 0x40; ++block) {
ptr += 0x10;
port->ReadBlock(block, data, number);
memcpy(ptr, data, sizeof(data));
}
delete port;
}
bool SkylanderIO::writeSkylanderToPortal(int number) throw (int)
{
bool bResult;
bool bNewSkylander = false;
unsigned char data[0x10];
unsigned char old[1024];
unsigned char skydata[1024];
Crypt crypt;
if (sky) {
PortalIO *port;
ReadPortal(old,number);
memcpy (skydata,sky->getData(),1024);
EncryptBuffer(skydata);
printf("\nWriting Skylander to portal.\n");
port = new PortalIO();
for(int i=0; i<2; i++) {
// two pass write
// write the access control blocks first
bool selectAccessControlBlock;
if(i == 0) {
selectAccessControlBlock = 1;
} else {
selectAccessControlBlock = 0;
}
for(int block=0; block < 0x40; ++block) {
bool changed, OK;
int offset = block * BLOCK_SIZE;
if(crypt.IsAccessControlBlock(block) == selectAccessControlBlock) {
changed = (memcmp(old + offset, skydata+offset,BLOCK_SIZE) != 0);
if(changed) {
port->WriteBlock( block, skydata+offset, bNewSkylander);
}
}
}
}
return true;
}
return false;
}
bool SkylanderIO::writeSkylanderToUnencryptedFile(char *name) throw (int)
{
if (sky) {
WriteSkylanderFile(name,sky->getData());
}
}
bool SkylanderIO::writeSkylanderToEncryptedFile(char *name) throw (int)
{
if (sky) {
unsigned char skydata[1024];
memcpy (skydata,sky->getData(),1024);
EncryptBuffer(skydata);
WriteSkylanderFile(name,skydata);
}
}
// Encrypt entire buffer of character data
// Buffer is entire 1024 byte block of character data.
void SkylanderIO::EncryptBuffer(unsigned char* buffer) {
unsigned int blockIndex;
unsigned char* blockData;
unsigned char const* tagBlocks0and1;
tagBlocks0and1 = buffer;
for(blockIndex = 0x08; blockIndex < 0x40; blockIndex++) {
blockData = buffer + blockIndex * 0x10;
crypt.EncryptTagBlock(blockData, blockIndex, tagBlocks0and1);
}
}
// Decrypt entire buffer of character data
// Buffer is entire 1024 byte block of character data.
void SkylanderIO::DecryptBuffer(unsigned char* buffer) {
unsigned int blockIndex;
unsigned char* blockData;
unsigned char const* tagBlocks0and1;
tagBlocks0and1 = buffer;
for(blockIndex = 0x08; blockIndex < 0x40; blockIndex++) {
blockData = buffer + blockIndex * 0x10;
crypt.DecryptTagBlock(blockData, blockIndex, tagBlocks0and1);
}
}
void SkylanderIO::ReadSkylanderFile(char *name) throw (int)
{
FILE *file;
unsigned long fileLen;
//Open file
file = fopen(name, "rb");
if (!file)
{
throw 1;
// fprintf(stderr, "Unable to open file %s\n", name);
// return NULL;
}
//Get file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
if(fileLen != 1024){
throw 2;
}
//Read file contents into buffer
fread(buffer, fileLen, 1, file);
fclose(file);
}
bool SkylanderIO::WriteSkylanderFile(char *name, unsigned char * filedata) throw (int)
{
FILE *file;
bool OK = true;
int count;
file = fopen(name, "wb");
if (!file)
{
throw 1;
// fprintf(stderr, "Unable to open file %s for writing.\n", name);
// return false;
}
count = fwrite(filedata, 1024, 1, file);
if(count < 1) {
fclose(file);
throw 3;
OK = false;
}
fclose(file);
return OK;
}
void SkylanderIO::listSkylanders() {
PortalIO *port;
Skylander *sky;
unsigned char data[0x10];
port = new PortalIO();
sky = new Skylander(buffer);
try {
for (int s=0; s<3; s++)
{
memset(data,0,0x10);
// must start with a read of block zero
port->ReadBlock(1, data, s);
printf("%0d: %s\n",s,sky->toyName(data[0] + data[1] * 0x100));
}
delete sky;
delete port;
} catch (int e) {
delete sky;
delete port;
if (e != 8)
throw e;
}
}