Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 45 additions & 11 deletions core/lib/src/header_remover.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,52 @@ String removeHeader(String filepath, {String? outputDir, bool backup = true}) {

final hdrSize = info['header_size'] as int;

final inRaf = file.openSync(mode: FileMode.read);
inRaf.setPositionSync(hdrSize);

final outSink = File(cleanPath).openSync(mode: FileMode.write);
const chunkSize = 1024 * 1024;
while (true) {
final chunk = inRaf.readSync(chunkSize);
if (chunk.isEmpty) break;
outSink.writeFromSync(chunk);
final tempPath = '$cleanPath.tmp';
final tempFile = File(tempPath);

RandomAccessFile? inRaf;
RandomAccessFile? outSink;

try {
inRaf = file.openSync(mode: FileMode.read);
inRaf.setPositionSync(hdrSize);

outSink = tempFile.openSync(mode: FileMode.write);
const chunkSize = 1024 * 1024;
while (true) {
final chunk = inRaf.readSync(chunkSize);
if (chunk.isEmpty) break;
outSink.writeFromSync(chunk);
}

inRaf.closeSync();
inRaf = null;

outSink.closeSync();
outSink = null;

try {
tempFile.renameSync(cleanPath);
} catch (_) {
tempFile.copySync(cleanPath);
if (tempFile.existsSync()) {
tempFile.deleteSync();
}
}
} catch (e) {
try {
inRaf?.closeSync();
} catch (_) {}
try {
outSink?.closeSync();
} catch (_) {}
if (tempFile.existsSync()) {
try {
tempFile.deleteSync();
} catch (_) {}
}
rethrow;
}
inRaf.closeSync();
outSink.closeSync();

return cleanPath;
}
46 changes: 46 additions & 0 deletions core/test/header_remover_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,50 @@ void main() {
tmpDir.deleteSync(recursive: true);
}
});

test('nes_in_place_header_removal_without_backup', () {
final tmpDir = Directory.systemTemp.createTempSync('hdr_inplace_test_');
try {
final romPath = p.join(tmpDir.path, 'game_inplace.nes');
final header = [
0x4E,
0x45,
0x53,
0x1A,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
];
final body = List<int>.generate(200, (i) => (i + 1) % 256);
File(romPath).writeAsBytesSync([...header, ...body]);

// Remove header in-place with backup: false (outputDir: null)
final cleanPath = removeHeader(romPath, outputDir: null, backup: false);
expect(cleanPath, equals(romPath));

final cleanFile = File(cleanPath);
expect(cleanFile.existsSync(), isTrue);

final cleanBytes = cleanFile.readAsBytesSync();
expect(cleanBytes.length, equals(200));
expect(cleanBytes.length, greaterThan(0));
expect(cleanBytes, equals(body));

// Ensure no backup file was created
expect(File('$romPath.bak').existsSync(), isFalse);
// Ensure no temp file remained
expect(File('$romPath.tmp').existsSync(), isFalse);
} finally {
tmpDir.deleteSync(recursive: true);
}
});
}
Loading