-
Notifications
You must be signed in to change notification settings - Fork 12
/
Program.cs
540 lines (454 loc) · 17.5 KB
/
Program.cs
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WfcPatcher {
class Program {
public static string ProgramName {
get {
Version version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version;
#if DEBUG
string versionString = version.ToString() + " (Debug)";
#else
string versionString = version.Major + "." + version.Minor;
#endif
return "WfcPatcher " + versionString;
}
}
static void Main( string[] args ) {
Console.WriteLine( ProgramName );
Console.WriteLine();
if ( !CommandLineArguments.ParseCommandLineArguments( args ) ) {
Console.WriteLine( "Error parsing command line options!" );
PrintUsage();
return;
}
if ( CommandLineArguments.Filenames.Length == 0 ) {
PrintUsage();
return;
}
string domainFilenamePart = "NoSSL";
if ( CommandLineArguments.Domain != null ) {
domainFilenamePart = CommandLineArguments.Domain;
}
foreach ( string filename in CommandLineArguments.Filenames ) {
string newFilename = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( filename ), System.IO.Path.GetFileNameWithoutExtension( filename ) ) + " (" + domainFilenamePart + ")" + System.IO.Path.GetExtension( filename );
#if !DEBUG
try {
#endif
if ( PatchFile( filename, newFilename ) ) {
Console.WriteLine( "Patched to " + newFilename + "!" );
Console.WriteLine();
} else {
Console.WriteLine( "Found nothing to patch in " + filename + "." );
Console.WriteLine( "Are you sure this is a WFC-enabled game?" );
Console.WriteLine();
System.IO.File.Delete( newFilename );
}
#if !DEBUG
} catch ( Exception ex ) {
Console.WriteLine( "Failed patching " + filename );
Console.WriteLine( ex.ToString() );
Console.WriteLine();
System.IO.File.Delete( newFilename );
}
#endif
}
}
public static void PrintUsage() {
Console.WriteLine( "Usage: WfcPatcher [options] game1.nds [game2.nds] [game3.nds] [...]" );
Console.WriteLine();
Console.WriteLine( "Command line options:" );
Console.WriteLine( " -d, --domain example.com" );
Console.WriteLine( " Point the NWFC URLs to a different URL instead." );
}
public static string GetGamecode( System.IO.FileStream nds ) {
long pos = nds.Position;
nds.Position = 0x0C;
string gamecode = nds.ReadAscii( 4 );
nds.Position = pos;
return gamecode;
}
static bool PatchFile( string filename, string newFilename ) {
Console.WriteLine( "Reading and copying " + filename + "..." );
using ( var nds = new System.IO.FileStream( newFilename, System.IO.FileMode.Create ) ) {
using ( var ndsSrc = new System.IO.FileStream( filename, System.IO.FileMode.Open ) ) {
Util.CopyStream( ndsSrc, nds, (int)ndsSrc.Length );
ndsSrc.Close();
}
// http://dsibrew.org/wiki/DSi_Cartridge_Header
// arm
Console.WriteLine( "Patching ARM Executables..." );
nds.Position = 0x20;
uint arm9offset = nds.ReadUInt32();
uint arm9entry = nds.ReadUInt32();
uint arm9load = nds.ReadUInt32();
uint arm9size = nds.ReadUInt32();
uint arm7offset = nds.ReadUInt32();
uint arm7entry = nds.ReadUInt32();
uint arm7load = nds.ReadUInt32();
uint arm7size = nds.ReadUInt32();
bool modArm9 = PatchArm9( nds, arm9offset, arm9size );
bool modArm7 = PatchArm7( nds, arm7offset, arm7size );
// overlays
Console.WriteLine( "Patching Overlays..." );
nds.Position = 0x50;
uint arm9overlayoff = nds.ReadUInt32();
uint arm9overlaylen = nds.ReadUInt32();
uint arm7overlayoff = nds.ReadUInt32();
uint arm7overlaylen = nds.ReadUInt32();
bool modOvl9 = PatchOverlay( nds, arm9overlayoff, arm9overlaylen );
bool modOvl7 = PatchOverlay( nds, arm7overlayoff, arm7overlaylen );
nds.Close();
return modArm9 || modArm7 || modOvl9 || modOvl7;
}
}
static bool PatchArm9( System.IO.FileStream nds, uint pos, uint len ) {
nds.Position = pos;
byte[] data = new byte[len];
nds.Read( data, 0, (int)len );
// decompress size info: http://www.crackerscrap.com/docs/dsromstructure.html
// TODO: Is there a better way to figure out if an ARM9 is compressed?
nds.Position = nds.Position - 8;
uint compressedSize = nds.ReadUInt24();
byte headerLength = (byte)nds.ReadByte();
uint additionalCompressedSize = nds.ReadUInt32();
uint decompressedSize = additionalCompressedSize + len;
bool compressed = false;
byte[] decData = data;
#if DEBUG
Console.WriteLine( "ARM9 old dec size: 0x" + decompressedSize.ToString( "X6" ) );
Console.WriteLine( "ARM9 old cmp size: 0x" + compressedSize.ToString( "X6" ) );
Console.WriteLine( "ARM9 old filesize: 0x" + len.ToString( "X6" ) );
Console.WriteLine( "ARM9 old diff: 0x" + additionalCompressedSize.ToString( "X6" ) );
System.IO.File.WriteAllBytes( "arm9-raw.bin", data );
#endif
blz blz = new blz();
// if one of these isn't true then it can't be blz-compressed so don't even try
bool headerLengthValid = ( headerLength >= 8 && headerLength <= 11 );
bool compressedSizeValid = ( data.Length >= compressedSize + 0x4000 && data.Length <= compressedSize + 0x400B );
if ( headerLengthValid && compressedSizeValid ) {
try {
blz.arm9 = 1;
byte[] maybeDecData = blz.BLZ_Decode( data );
if ( maybeDecData.Length == decompressedSize ) {
compressed = true;
decData = maybeDecData;
#if DEBUG
System.IO.File.WriteAllBytes( "arm9-dec.bin", decData );
#endif
}
} catch ( blzDecodingException ) {
compressed = false;
}
}
byte[] decDataUnmodified = (byte[])decData.Clone();
if ( ReplaceInData( decData, 0x00, true ) ) {
if ( compressed ) {
Console.WriteLine( "Replacing and recompressing ARM9..." );
data = blz.BLZ_Encode( decData, 0 );
uint newCompressedSize = (uint)data.Length;
if ( newCompressedSize > len ) {
// new ARM is actually bigger, redo without the additional nullterm replacement
decData = decDataUnmodified;
ReplaceInData( decData, 0x00, false );
data = blz.BLZ_Encode( decData, 0, supressWarnings: true );
newCompressedSize = (uint)data.Length;
int arm9diff = (int)len - (int)newCompressedSize;
if ( arm9diff < 0 ) {
// still too big, remove debug strings
if ( !RemoveStringsInKnownGames( GetGamecode( nds ), decData ) ) {
RemoveDebugStrings( decData );
}
#if DEBUG
System.IO.File.WriteAllBytes( "arm9-dec-without-debug.bin", decData );
#endif
data = blz.BLZ_Encode( decData, 0, supressWarnings: true );
newCompressedSize = (uint)data.Length;
arm9diff = (int)len - (int)newCompressedSize;
if ( arm9diff < 0 ) {
Console.WriteLine( "WARNING: Recompressed ARM9 is " + -arm9diff + " bytes bigger than original!" );
Console.WriteLine( " Patched game may be corrupted!" );
#if DEBUG
System.IO.File.WriteAllBytes( "arm9-too-big-recomp.bin", data );
#endif
}
}
}
if ( newCompressedSize != len ) {
// new ARM is (still) different, attempt to find the metadata in the ARM9 secure area and replace that
bool foundSize = false;
for ( int i = 0; i < 0x4000; i += 4 ) {
uint maybeSize = BitConverter.ToUInt32( data, i );
if ( maybeSize == len + 0x02000000u || maybeSize == len + 0x02004000u ) {
foundSize = true;
byte[] newCmpSizeBytes;
if ( maybeSize == len + 0x02004000u ) {
newCmpSizeBytes = BitConverter.GetBytes( newCompressedSize + 0x02004000u );
} else {
newCmpSizeBytes = BitConverter.GetBytes( newCompressedSize + 0x02000000u );
}
data[i + 0] = newCmpSizeBytes[0];
data[i + 1] = newCmpSizeBytes[1];
data[i + 2] = newCmpSizeBytes[2];
data[i + 3] = newCmpSizeBytes[3];
break;
}
}
if ( !foundSize ) {
Console.WriteLine( "WARNING: Recompressed ARM9 is different size, and size could not be found in secure area!" );
Console.WriteLine( " Patched game will probably not boot!" );
}
}
#if DEBUG
uint newDecompressedSize = (uint)decData.Length;
uint newAdditionalCompressedSize = newDecompressedSize - newCompressedSize;
Console.WriteLine( "ARM9 new dec size: 0x" + newDecompressedSize.ToString( "X6" ) );
Console.WriteLine( "ARM9 new cmp size: 0x" + newCompressedSize.ToString( "X6" ) );
Console.WriteLine( "ARM9 new diff: 0x" + newAdditionalCompressedSize.ToString( "X6" ) );
#endif
} else {
Console.WriteLine( "Replacing ARM9..." );
data = decData;
}
#if DEBUG
System.IO.File.WriteAllBytes( "arm9-new.bin", data );
#endif
nds.Position = pos;
nds.Write( data, 0, data.Length );
int newSize = data.Length;
int diff = (int)len - newSize;
// copy back footer
if ( diff > 0 ) {
List<byte> footer = new List<byte>();
nds.Position = pos + len;
if ( nds.PeekUInt32() == 0xDEC00621 ) {
for ( int j = 0; j < 12; ++j ) {
footer.Add( (byte)nds.ReadByte() );
}
nds.Position = pos + newSize;
nds.Write( footer.ToArray(), 0, footer.Count );
}
// padding
for ( int j = 0; j < diff; ++j ) {
nds.WriteByte( 0xFF );
}
}
// write new size
byte[] newSizeBytes = BitConverter.GetBytes( newSize );
nds.Position = 0x2C;
nds.Write( newSizeBytes, 0, 4 );
// recalculate checksums
nds.Position = pos;
ushort secureChecksum = new Crc16().ComputeChecksum( nds, 0x4000, 0xFFFF );
nds.Position = 0x6C;
nds.Write( BitConverter.GetBytes( secureChecksum ), 0, 2 );
nds.Position = 0;
ushort headerChecksum = new Crc16().ComputeChecksum( nds, 0x15E, 0xFFFF );
nds.Write( BitConverter.GetBytes( headerChecksum ), 0, 2 );
return true;
}
return false;
}
static bool PatchArm7( System.IO.FileStream nds, uint pos, uint len ) {
nds.Position = pos;
byte[] data = new byte[len];
nds.Read( data, 0, (int)len );
if ( ReplaceInData( data ) ) {
Console.WriteLine( "Replacing ARM7..." );
nds.Position = pos;
nds.Write( data, 0, data.Length );
return true;
}
return false;
}
static bool PatchOverlay( System.IO.FileStream nds, uint pos, uint len ) {
// http://sourceforge.net/p/devkitpro/ndstool/ci/master/tree/source/ndsextract.cpp
// http://sourceforge.net/p/devkitpro/ndstool/ci/master/tree/source/overlay.h
// header compression info from http://gbatemp.net/threads/recompressing-an-overlay-file.329576/
nds.Position = 0x048;
uint fatOffset = nds.ReadUInt32();
bool modified = false;
for ( uint i = 0; i < len; i += 0x20 ) {
nds.Position = pos + i;
uint id = nds.ReadUInt32();
uint ramAddr = nds.ReadUInt32();
uint ramSize = nds.ReadUInt32();
uint bssSize = nds.ReadUInt32();
uint sinitInit = nds.ReadUInt32();
uint sinitInitEnd = nds.ReadUInt32();
uint fileId = nds.ReadUInt32();
uint compressedSize = nds.ReadUInt24();
byte compressedBitmask = (byte)nds.ReadByte();
nds.Position = fatOffset + 8 * id;
uint overlayPositionStart = nds.ReadUInt32();
uint overlayPositionEnd = nds.ReadUInt32();
uint overlaySize = overlayPositionEnd - overlayPositionStart;
if ( overlaySize == 0 ) { continue; }
nds.Position = overlayPositionStart;
byte[] data = new byte[overlaySize];
nds.Read( data, 0, (int)overlaySize );
blz blz = new blz();
byte[] decData;
bool compressed = ( compressedBitmask & 0x01 ) == 0x01;
if ( compressed ) {
try {
decData = blz.BLZ_Decode( data );
} catch ( blzDecodingException ) {
Console.WriteLine( "WARNING: Decompression of Overlay " + ( i / 0x20 ) + " failed!" );
decData = data;
compressed = false;
}
} else {
decData = data;
}
#if DEBUG
System.IO.File.WriteAllBytes( "overlay" + ( i / 0x20 ) + "-dec.bin", decData );
#endif
if ( ReplaceInData( decData ) ) {
modified = true;
int newOverlaySize;
int diff;
// if something was replaced, put it back into the ROM
if ( compressed ) {
Console.WriteLine( "Replacing and recompressing overlay " + id + "..." );
uint newCompressedSize = 0;
data = blz.BLZ_Encode( decData, 0 );
newCompressedSize = (uint)data.Length;
newOverlaySize = data.Length;
diff = (int)overlaySize - newOverlaySize;
if ( diff < 0 ) {
Console.WriteLine( "Removing known debug strings and recompressing overlay " + id + "..." );
RemoveDebugStrings( decData );
data = blz.BLZ_Encode( decData, 0, supressWarnings: true );
newCompressedSize = (uint)data.Length;
newOverlaySize = data.Length;
diff = (int)overlaySize - newOverlaySize;
if ( diff < 0 ) {
Console.WriteLine( "WARNING: Recompressed overlay is " + -diff + " bytes bigger than original!" );
Console.WriteLine( " Patched game may be corrupted!" );
}
}
// replace compressed size, if it was used before
if ( compressedSize == overlaySize ) {
byte[] newCompressedSizeBytes = BitConverter.GetBytes( newCompressedSize );
nds.Position = pos + i + 0x1C;
nds.Write( newCompressedSizeBytes, 0, 3 );
}
} else {
Console.WriteLine( "Replacing overlay " + id + "..." );
data = decData;
}
newOverlaySize = data.Length;
diff = (int)overlaySize - newOverlaySize;
nds.Position = overlayPositionStart;
nds.Write( data, 0, data.Length );
overlayPositionEnd = (uint)nds.Position;
// padding
for ( int j = 0; j < diff; ++j ) {
nds.WriteByte( 0xFF );
}
// new file end offset
byte[] newPosEndData = BitConverter.GetBytes( overlayPositionEnd );
nds.Position = fatOffset + 8 * id + 4;
nds.Write( newPosEndData, 0, 4 );
}
}
return modified;
}
static void RemoveDebugStrings( byte[] data ) {
string[] debugStrings = new string[] {
"recv buffer size",
"send buffer size",
"unknown connect mode",
"Split packet parse error",
"NULL byte expected!",
"Processing adderror packet",
"Out of memory.",
" buf->buffer",
};
foreach ( string s in debugStrings ) {
byte[] searchBytes = Encoding.ASCII.GetBytes( s );
var results = data.Locate( searchBytes );
foreach ( int result in results ) {
for ( int i = 0; i < searchBytes.Length; ++i ) {
data[result + i] = 0x20;
}
}
}
}
class KnownGamedata {
public string Gamecode;
public uint Position;
public uint Length;
public KnownGamedata( string gamecode, uint position, uint length ) {
this.Gamecode = gamecode;
this.Position = position;
this.Length = length;
}
}
static bool RemoveStringsInKnownGames( string gamecode, byte[] data ) {
KnownGamedata[] knownData = new KnownGamedata[] {
new KnownGamedata( "VI2J", 0x1047C8, 0x4A4 ), // FE12 English Patch
};
bool knownGame = false;
foreach ( KnownGamedata d in knownData ) {
if ( gamecode == d.Gamecode ) {
knownGame = true;
for ( uint i = d.Position; i < d.Position + d.Length; ++i ) {
if ( data[i] != 0 ) {
data[i] = 0x20;
}
}
}
}
return knownGame;
}
static bool ReplaceInData( byte[] data, byte paddingByte = 0x00, bool writeAdditionalBytePostString = false ) {
bool replaced = ReplaceInData( data, "https://", "http://", paddingByte, writeAdditionalBytePostString );
if ( CommandLineArguments.Domain != null ) {
replaced = ReplaceInData( data, "nintendowifi.net", CommandLineArguments.Domain, paddingByte, writeAdditionalBytePostString ) || replaced;
}
return replaced;
}
static bool ReplaceInData( byte[] data, string search, string replace, byte paddingByte = 0x00, bool writeAdditionalBytePostString = false ) {
bool replacedData = false;
byte[] searchBytes = Encoding.ASCII.GetBytes( search );
byte[] replaceBytes = Encoding.ASCII.GetBytes( replace );
int requiredPadding = searchBytes.Length - replaceBytes.Length;
var results = data.Locate( searchBytes );
if ( results.Length == 0 ) {
return false;
}
foreach ( int result in results ) {
string originalString = Util.GetTextAscii( data, result );
#if DEBUG
Console.WriteLine( originalString );
#endif
if ( originalString == "https://" ) { continue; } // don't replace lone https, probably used for strcmp to figure out if an URL is SSL or not
string replacedString = originalString.Replace( search, replace );
byte[] replacedStringBytes = Encoding.ASCII.GetBytes( replacedString );
replacedData = true;
int i = 0;
for ( ; i < replacedStringBytes.Length; ++i ) {
data[result + i] = replacedStringBytes[i];
}
for ( ; i < replacedStringBytes.Length + requiredPadding; ++i ) {
data[result + i] = paddingByte;
}
// Alright, this might require some explaination.
// This is putting a byte in the location that previously held the NULL terminator at the end of the string.
// Thanks to "http" being one byte shorter than "https", the new NULL terminator was just placed in the
// padding loop above, and the byte below, at [result + i], is unused. Thus, we can just place anything in
// there without affecting the program. Now, the actual *reason* we're putting a byte in here is to reduce
// the chance of the recompressed binary becoming smaller than the original one. We want it to remain the
// exact same size. Now, of course, this is not always going to happen, but this should improve the chance
// significantly.
if ( writeAdditionalBytePostString ) {
data[result + i] = 0x7F;
}
}
return replacedData;
}
}
}