From 2b669a033cb25e2a66e5e99ac96e1fe4d167cfb7 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Wed, 30 Sep 2026 22:03:38 +0000 Subject: [PATCH] wad 29/40 (fuzzy 92 -> 97) (worker 2a411d12) --- README.md | 4 +- libs/RVL_SDK/src/wad/wad.c | 526 +++++++++++++++------------- tools/decomp-assist/wad-attempts.md | 268 ++++++++++++++ 3 files changed, 557 insertions(+), 241 deletions(-) create mode 100644 tools/decomp-assist/wad-attempts.md diff --git a/README.md b/README.md index 25fc9c71..8b0147ea 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ Progress | Decompiled | Matched | Linked | Data | |:---:|:---:|:---:|:---:| -| 98.70% | 85.95% | 59.99% | 90.87% | +| 98.73% | 86.01% | 59.99% | 90.87% | -units 871/1027 complete, functions 12116/12563 matched. +units 871/1027 complete, functions 12118/12563 matched. decompiled = code with a C/C++ implementation (objdiff fuzzy), matched = byte-exact code, linked = code actually linked into the DOL, data = byte-exact data diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 2a49fd3f..d3aaa04b 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -18,7 +18,7 @@ typedef union WADStreamHandle { void* memoryBase; } WADStreamHandle; -typedef struct __attribute__((aligned(32))) WADStream { +typedef struct WADStream { WADLocation location; WADStreamHandle handle; } WADStream; @@ -167,7 +167,7 @@ typedef struct __attribute__((aligned(32))) WADImportWorkspace { WADFileHeader fileHeader; u8 wadHeader[0x80]; WADUnpackInfo unpackInfo; - WADStream stream; + WADStream stream ALIGN32; } WADImportWorkspace; typedef struct WADImportTransfer { @@ -536,30 +536,28 @@ static s32 WAD_815BFFA8(WADImportLoopArgs* args) { s32 fd = args->fd; s32 result = 0; u32 remaining = args->size; - u32 bufferIndex = 0; WADImportTransfer* transfer = args->transfer; + u32 bufferIndex = 0; + u32 size; - while ((remaining != 0) && (result == 0)) { - u32 size = remaining; - OSMutex* mutex; + for (; (remaining != 0) && (result == 0); bufferIndex ^= 1) { + size = remaining; if (remaining > transfer->chunkSize) { size = transfer->chunkSize; } - mutex = &transfer->mutex[bufferIndex]; - OSLockMutex(mutex); + OSLockMutex(&transfer->mutex[bufferIndex]); while (transfer->ready[bufferIndex] == 0) { - OSWaitCond(&transfer->signalCond[bufferIndex], mutex); + OSWaitCond(&transfer->signalCond[bufferIndex], &transfer->mutex[bufferIndex]); } result = ES_ImportContentData(fd, transfer->buffers[bufferIndex], size); transfer->ready[bufferIndex] = 0; if (result != 0) { transfer->error = 1; } - OSUnlockMutex(mutex); + OSUnlockMutex(&transfer->mutex[bufferIndex]); OSSignalCond(&transfer->waitCond[bufferIndex]); remaining -= size; - bufferIndex ^= 1; } return result; } @@ -567,13 +565,15 @@ static s32 WAD_815BFFA8(WADImportLoopArgs* args) { s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 offset, u32 flags, WADProcessCallback processCallback) { const char* functionName = "WADImportEx"; + void* firstBuffer = 0; + ESTitleMeta* titleMeta; + void* secondBuffer = 0; + s32 result = 0; + u32 importedBytes = 0; s32 streamOpened = FALSE; s32 backupFileOpened = FALSE; s32 importExistingTitle = TRUE; - s32 contentFd = 0; - u32 importedBytes = 0; - void* firstBuffer = 0; - void* secondBuffer = 0; + s32 contentFd; s32 titleImportStarted = FALSE; ESTitleMeta* installedTitleMeta = 0; ESContentId* installedContentIds = 0; @@ -581,18 +581,16 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o ESContentMeta* matchingContents = 0; ESHash* sharedContentHashes = 0; WADThreadStack* threadStack = 0; - s32 threadCreated = FALSE; - WADStream stream ALIGN32; + WADStream stream; WADUnpackInfo unpackInfo; WADSaveDataHeader wadHeader ALIGN32; - WADFileHeader backupHeader ALIGN32; WADImportTransfer transfer; + WADFileHeader backupHeader ALIGN32; WADImportLoopArgs threadArgs; OSThread importThread; NANDFileInfo backupFile; NANDStatus fileStatus; NANDStatus savedataStatus; - s32 result = 0; void* wadHeaderBuffer = &wadHeader; void* readBuffer = 0; u32 installedTmdSize; @@ -602,32 +600,21 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o WADFileHeader* fileHeaderBuffer; u8 transferId[0x20] ALIGN32; u32 contentCount; - s32 contentIndex; + u32 contentIndex; u32 listIndex; u32 sizeRemaining; u32 transferIdValue; - s32 existingTmdAvailable = FALSE; s32 fileReadResult; - u32 importedContentCount = 0; - u32 bufferIndex = 0; - u32 installedContentIndex = 0; + u32 bufferIndex; + u32 installedContentIndex; u32 sharedContentIndex; - u32 contentFileOffset; u32 pathFileIndex; s32 matchFound; - s32 listResult; - s32 titleVersion; u32 contentSize; u32 chunkSize; u32 readSize; u32 bootSize; - ESTitleMeta* titleMeta; u32 alignedSize; - u32 callbackDone; - u32 currentTitleId; - u32 currentTitleVersion; - u32 currentContentId; - u32 importedFileCount; u32 savedataFileIndex; s32 importResult; u32 threadPriority; @@ -641,6 +628,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o result = -3007; goto cleanup; } + result = 0; if (processCallback != 0) { processCallback(0, 0, FALSE); } @@ -659,8 +647,8 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o if (result != 0) { goto cleanup; } - importedBytes = unpackInfo.contentOffset; titleMeta = unpackInfo.titleMeta; + importedBytes = unpackInfo.contentOffset; if (((flags & 8) != 0) && (titleMeta != 0)) { ticketViewCount = 0; if (unpackInfo.headerInfo != 2) { @@ -687,12 +675,12 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o goto cleanup; } readBuffer = firstBuffer; - result = WADReadStream(&stream, &readBuffer, alignedSize, - offset + importedBytes); - if ((u32)result != alignedSize) { + if ((u32)WADReadStream(&stream, &readBuffer, (bootSize + 0x1F) & ~0x1F, + offset + importedBytes) != ((bootSize + 0x1F) & ~0x1F)) { result = -3005; goto cleanup; } + result = 0; result = ES_ImportBoot(unpackInfo.buffer_0x34, unpackInfo.buffer_0x14, unpackInfo.size_0x0c, @@ -780,7 +768,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o if (result == 0) { alignedSize = (sharedContentCount * sizeof(ESHash) + 0x1F) & ~0x1F; sharedContentHashes = _WADMemAlloc(allocator, alignedSize); - if ((sharedContentHashes == 0) && (sharedContentCount != 0)) { + if (sharedContentHashes == 0) { result = -3003; goto cleanup; } @@ -804,17 +792,20 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o if ((flags & 2) == 0) { for (contentIndex = 0; contentIndex < contentCount; contentIndex++) { ESContentMeta* content; - s32 titleMetaIndex = contentIndex; + s32 titleMetaIndex; matchFound = FALSE; - if (unpackInfo.cidxMode != 0) { + if (unpackInfo.cidxMode >= 1) { titleMetaIndex = _WADGetCidx(unpackInfo.contentIndex, contentIndex); if ((titleMetaIndex < 0) || ((s32)titleMeta->head.numContents <= titleMetaIndex)) { result = -3001; goto cleanup; } + result = 0; + content = &titleMeta->contents[titleMetaIndex]; + } else { + content = &titleMeta->contents[contentIndex]; } - content = &titleMeta->contents[titleMetaIndex]; if (!importExistingTitle) { for (listIndex = 0; listIndex < installedContentCount; listIndex++) { ESContentMeta* installedContent = &matchingContents[listIndex]; @@ -835,13 +826,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o } } } - if (matchFound) { - importedBytes += (content->size + 0x0F) & ~0x0F; - if (processCallback != 0) { - processCallback(importedBytes - unpackInfo.contentOffset, - unpackInfo.contentSize + unpackInfo.fileListSize, FALSE); - } - } else { + if (!matchFound) { if (firstBuffer == 0) { firstBuffer = _WADMemAlloc(allocator, 0x10000); if (firstBuffer == 0) { @@ -867,36 +852,38 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o result = -3003; goto cleanup; } + result = 0; contentFd = ES_ImportContentBegin( titleMeta->head.titleId, content->cid); result = contentFd; if (contentFd < 0) { goto cleanup; } + result = 0; contentImportStarted = TRUE; - sizeRemaining = (content->size + 0x0F) & ~0x0F; + sizeRemaining = ((u32)content->size + 0x0F) & ~0x0F; WAD_815C4A2C(&transfer, firstBuffer, secondBuffer, 0x10000); threadArgs.fd = contentFd; threadArgs.transfer = &transfer; threadArgs.size = sizeRemaining; threadPriority = OSGetThreadPriority(OSGetCurrentThread()); - threadCreated = OSCreateThread(&importThread, - (void* (*)(void*))WAD_815BFFA8, - &threadArgs, - &threadStack->bytes[sizeof(threadStack->bytes)], - sizeof(threadStack->bytes), - threadPriority, 0); - if (!threadCreated) { + importResult = OSCreateThread(&importThread, + (void* (*)(void*))WAD_815BFFA8, + &threadArgs, + &threadStack->bytes[sizeof(threadStack->bytes)], + sizeof(threadStack->bytes), + threadPriority, 0); + if (!importResult) { result = -3009; goto cleanup; } OSResumeThread(&importThread); - transfer.error = 0; bufferIndex = 0; while ((sizeRemaining != 0) && (transfer.error == 0)) { - chunkSize = sizeRemaining; - if (transfer.chunkSize < chunkSize) { + if (sizeRemaining > transfer.chunkSize) { chunkSize = transfer.chunkSize; + } else { + chunkSize = sizeRemaining; } OSLockMutex(&transfer.mutex[bufferIndex]); while ((transfer.ready[bufferIndex] != 0) && @@ -905,6 +892,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o &transfer.mutex[bufferIndex]); } if (transfer.error != 0) { + OSUnlockMutex(&transfer.mutex[bufferIndex]); break; } readSize = (chunkSize + 0x1F) & ~0x1F; @@ -912,7 +900,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o result = WADReadStream(&stream, &readBuffer, readSize, offset + importedBytes); - if ((u32)result != readSize) { + if ((u32)result != ((chunkSize + 0x1F) & ~0x1F)) { OSLockMutex(&transfer.mutex[bufferIndex ^ 1]); OSCancelThread(&importThread); OSJoinThread(&importThread, 0); @@ -932,15 +920,10 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o } bufferIndex ^= 1; } - if (threadCreated) { - importResult = OSJoinThread(&importThread, (void**)&result); - if (!importResult) { - result = -3009; - goto cleanup; - } - threadCreated = FALSE; + if (!OSJoinThread(&importThread, (void**)&result)) { + return -3009; } - if ((result != 0) || (transfer.error != 0)) { + if (result != 0) { goto cleanup; } result = ES_ImportContentEnd(contentFd); @@ -948,6 +931,12 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o goto cleanup; } contentImportStarted = FALSE; + } else { + importedBytes += ((u32)content->size + 0x0F) & ~0x0F; + if (processCallback != 0) { + processCallback(importedBytes - unpackInfo.contentOffset, + unpackInfo.contentSize + unpackInfo.fileListSize, FALSE); + } } if (unpackInfo.type == 2) { importedBytes = (importedBytes + 0x3F) & ~0x3F; @@ -989,12 +978,14 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o goto cleanup; } for (pathFileIndex = 0; pathFileIndex < unpackInfo.fileCount; pathFileIndex++) { + fileHeaderBuffer = &backupHeader; result = WADReadStream(&stream, (void**)&fileHeaderBuffer, sizeof(backupHeader), offset + importedBytes); if (result != sizeof(backupHeader)) { result = -3005; goto cleanup; } + result = 0; importedBytes = (importedBytes + 0xBF) & ~0x3F; if (fileHeaderBuffer->magic != 0x3ADF17E) { result = -3000; @@ -1032,10 +1023,11 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o (chunkSize + 0x1F) & ~0x1F, offset + importedBytes); readSize = (chunkSize + 0x1F) & ~0x1F; - if ((u32)result != readSize) { + if ((u32)result != ((chunkSize + 0x1F) & ~0x1F)) { result = -3005; goto cleanup; } + result = 0; result = ES_Decrypt(6, fileHeaderBuffer->iv, readBuffer, readSize, secondBuffer); if (result != 0) { @@ -1046,11 +1038,12 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o result = -3006; goto cleanup; } + result = 0; importedBytes += chunkSize; sizeRemaining -= chunkSize; if (processCallback != 0) { processCallback(unpackInfo.contentSize + - importedBytes - unpackInfo.fileOffset, + (importedBytes - unpackInfo.fileOffset), unpackInfo.contentSize + unpackInfo.fileListSize, FALSE); } } @@ -1098,13 +1091,14 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o importedBytes = (importedBytes + 0x3F) & ~0x3F; if ((fileHeaderBuffer->fileSize == 0) && (processCallback != 0)) { processCallback(unpackInfo.contentSize + - importedBytes - unpackInfo.fileOffset, + (importedBytes - unpackInfo.fileOffset), unpackInfo.contentSize + unpackInfo.fileListSize, FALSE); } } importedBytes = unpackInfo.fileOffset; for (savedataFileIndex = 0; savedataFileIndex < unpackInfo.fileCount; savedataFileIndex++) { + fileHeaderBuffer = &backupHeader; fileReadResult = WADReadStream(&stream, (void**)&fileHeaderBuffer, sizeof(backupHeader), offset + importedBytes); @@ -1114,15 +1108,19 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o } result = 0; importedBytes = (importedBytes + 0xBF) & ~0x3F; - if ((_WADCanImportFile(fileHeaderBuffer, transferIdValue, - unpackInfo.fileNames, transferId) != 0) && - (((result = NANDPrivateGetStatus(fileHeaderBuffer->name, &savedataStatus)) != - 0) || - ((fileHeaderBuffer->flags[0] & 0x20) == 0 && - ((savedataStatus.permission = fileHeaderBuffer->flags[0]), - (result = NANDPrivateSetStatus(fileHeaderBuffer->name, &savedataStatus)) != - 0)))) { - break; + if (_WADCanImportFile(fileHeaderBuffer, transferIdValue, + unpackInfo.fileNames, transferId) != 0) { + result = NANDPrivateGetStatus(fileHeaderBuffer->name, &savedataStatus); + if (result != 0) { + goto cleanup; + } + if ((fileHeaderBuffer->flags[0] & 0x20) == 0) { + savedataStatus.permission = fileHeaderBuffer->flags[0]; + result = NANDPrivateSetStatus(fileHeaderBuffer->name, &savedataStatus); + if (result != 0) { + goto cleanup; + } + } } importedBytes = (importedBytes + fileHeaderBuffer->fileSize + 0x3F) & ~0x3F; } @@ -1166,16 +1164,16 @@ cleanup: OSReport("%s:%d Cancel importing the title.\n", functionName, 0x8E6); } if (processCallback != 0) { - if (importedBytes == 0) { - processCallback(0, 0, TRUE); - } else if (((unpackInfo.contentSize + unpackInfo.fileListSize) == 0) || - (unpackInfo.fileCount == 0)) { - processCallback(importedBytes - unpackInfo.contentOffset, - unpackInfo.contentSize + unpackInfo.fileListSize, TRUE); + if (importedBytes != 0) { + if ((unpackInfo.fileListSize != 0) && (unpackInfo.fileCount != 0)) { + processCallback(unpackInfo.contentSize + (importedBytes - unpackInfo.fileOffset), + unpackInfo.contentSize + unpackInfo.fileListSize, TRUE); + } else { + processCallback(importedBytes - unpackInfo.contentOffset, + unpackInfo.contentSize + unpackInfo.fileListSize, TRUE); + } } else { - processCallback(unpackInfo.contentSize + - importedBytes - unpackInfo.fileOffset, - unpackInfo.contentSize + unpackInfo.fileListSize, TRUE); + processCallback(0, 0, TRUE); } } return result; @@ -1184,13 +1182,14 @@ cleanup: static s32 WAD_815C1288(WADExportLoopArgs* args) { ESFd fd = args->fd; u32 remaining = args->size; + u32 size; u32 bufferIndex = 0; - s32 result = 0; WADImportTransfer* transfer = args->transfer; + s32 result = 0; - while ((remaining != 0) && (result == 0)) { - u32 size = remaining; + for (; (remaining != 0) && (result == 0); bufferIndex ^= 1) { OSMutex* mutex; + size = remaining; if (remaining > transfer->chunkSize) { size = transfer->chunkSize; @@ -1208,30 +1207,30 @@ static s32 WAD_815C1288(WADExportLoopArgs* args) { OSUnlockMutex(mutex); OSSignalCond(&transfer->signalCond[bufferIndex]); remaining -= size; - bufferIndex ^= 1; } return result; } s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32* sizeOut, WADLocation location, u32 offset, WADProcessCallback processCallback) { + WADBackupFileHeader* files = 0; s32 result; + OSThread exportThread; + NANDFileInfo savedFile; + WADStream stream; + WADBackupHeaderBlock headerBlock ALIGN64; + u8 hashContext[0x60] ALIGN32; + WADFileHeader fileHeader ALIGN32; + WADImportTransfer transfer; + char currentDirectory[NAND_MAX_PATH]; + u8 signature[0x40] ALIGN32; + ESContentMask existingContentMask ALIGN32; + u8 digest[0x40] ALIGN32; + u8 transferMac[0x20] ALIGN32; + WADExportLoopArgs threadArgs; u32 totalSize; WADBackupTitleMetaBuffer* titleMetaBuffer = 0; ESTmdView* titleMeta = 0; - WADBackupFileHeader* files = 0; - WADStream stream ALIGN32; - WADImportTransfer transfer; - WADExportLoopArgs threadArgs; - OSThread exportThread; - NANDFileInfo savedFile ALIGN32; - WADBackupHeaderBlock headerBlock ALIGN64; - char currentDirectory[NAND_MAX_PATH]; - WADFileHeader fileHeader ALIGN32; - ESContentMask existingContentMask ALIGN32; - u8 hashContext[0x60] ALIGN32; - u8 digest[0x40] ALIGN32; - u8 signature[0x40] ALIGN32; struct WADExportCertificates { ESCertSignature signer; u8 device[0x180]; @@ -1242,15 +1241,15 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 void* encryptionBuffer = 0; WADThreadStack* threadStack = 0; u32 titleMetaSize = 0; + u32 titleMetaViewSize; u32 contentDataSize = 0; - u32 fileDataSize = 0; u32 fileCount = 0; + u32 fileDataSize = 0; u32 installedContentCount = 0; u32 fileIndex; u32 contentIndex; u32 importedSize = 0; u32 paddedSize; - u8 transferMac[0x20] ALIGN32; u32 priority; u32 sizeRemaining; u32 bufferIndex; @@ -1262,7 +1261,6 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 s32 threadCreated = FALSE; s32 fileOpened = FALSE; s32 fileFd; - u32 callbackDone = 0; if (flags == 0) { flags = 0x0F; @@ -1288,7 +1286,7 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 processCallback(0, 0, FALSE); } if (((flags & 1) != 0) || ((flags & 0x20) != 0)) { - result = ES_GetTmdView(titleId, 0, &titleMetaSize); + result = ES_GetTmdView(titleId, 0, &titleMetaViewSize); if (result != 0) { goto cleanup; } @@ -1297,8 +1295,14 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 result = -3003; goto cleanup; } + if (titleMetaBuffer == 0) { + result = -3000; + goto cleanup; + } + result = 0; titleMeta = &titleMetaBuffer->view; - result = ES_GetTmdView(titleId, titleMeta, &titleMetaSize); + result = 0; + result = ES_GetTmdView(titleId, titleMeta, &titleMetaViewSize); if (result != 0) { goto cleanup; } @@ -1309,10 +1313,12 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 result = -3002; goto cleanup; } - if ((result != 0) || (installedContentCount == 0)) { + result = 0; + if (installedContentCount == 0) { result = -3002; goto cleanup; } + result = 0; result = _WADCheckContents(titleMeta, &existingContentMask); if (result != 0) { goto cleanup; @@ -1327,12 +1333,19 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 if (result != 0) { goto cleanup; } - if (fileCount != 0) { + if (fileCount == 0) { + files = 0; + } else { files = _WADMemAlloc(allocator, fileCount * sizeof(WADBackupFileHeader)); if (files == 0) { result = -3003; goto cleanup; } + if (files == 0) { + result = -3003; + goto cleanup; + } + result = 0; result = _WADBackupGetFiles(0, flags, allocator, &fileCount, files); if (result != 0) { goto cleanup; @@ -1348,6 +1361,7 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 result = -3002; goto cleanup; } + result = 0; if (path == 0) { *sizeOut = totalSize + 0x340; result = 0; @@ -1358,6 +1372,11 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 result = -3003; goto cleanup; } + if (outputBuffer == 0) { + result = -3000; + goto cleanup; + } + result = 0; if (processCallback != 0) { processCallback(0, contentDataSize + fileDataSize, FALSE); } @@ -1367,8 +1386,7 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 goto cleanup; } memset(&headerBlock, 0, sizeof(headerBlock)); - result = WADWriteStream(&stream, &headerBlock, sizeof(headerBlock)); - if (result != sizeof(headerBlock)) { + if ((u32)WADWriteStream(&stream, &headerBlock, sizeof(headerBlock)) != sizeof(headerBlock)) { result = -3006; goto cleanup; } @@ -1390,13 +1408,20 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 memcpy(&headerBlock.header.cidx, &existingContentMask, sizeof(existingContentMask)); } if ((flags & 2) != 0) { - u32 currentTitleHigh = 0; - u32 currentTitleLow = 0; + u32 currentTitleHigh; + u32 currentTitleLow; encryptionBuffer = _WADMemAlloc(allocator, 0x10000); if (encryptionBuffer == 0) { result = -3003; goto cleanup; } + if (encryptionBuffer == 0) { + result = -3003; + goto cleanup; + } + result = 0; + currentTitleHigh = 0; + currentTitleLow = 0; result = NANDGetCurrentDir(encryptionBuffer); if (strncmp(encryptionBuffer, "/title/", 7) == 0) { sscanf(encryptionBuffer, "/title/%x/%x", ¤tTitleHigh, ¤tTitleLow); @@ -1427,11 +1452,11 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 if (result != 0) { goto cleanup; } - result = WADWriteStream(&stream, titleMeta, paddedSize); - if ((u32)result != paddedSize) { + if ((u32)WADWriteStream(&stream, titleMeta, paddedSize) != paddedSize) { result = -3006; goto cleanup; } + result = 0; } if ((flags & 1) != 0) { if (encryptionBuffer == 0) { @@ -1440,12 +1465,22 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 result = -3003; goto cleanup; } + if (encryptionBuffer == 0) { + result = -3003; + goto cleanup; + } + result = 0; } threadStack = _WADMemAlloc(allocator, sizeof(WADThreadStack)); if (threadStack == 0) { result = -3003; goto cleanup; } + if (threadStack == 0) { + result = -3003; + goto cleanup; + } + result = 0; installedContentCount = _WADGetCidxCount(&existingContentMask); for (contentIndex = 0; contentIndex < installedContentCount; contentIndex++) { s32 tmdIndex = _WADGetCidx(&existingContentMask, contentIndex); @@ -1462,7 +1497,7 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 } result = 0; contentExportStarted = TRUE; - sizeRemaining = (content->size + 0x0F) & ~0x0F; + sizeRemaining = ((u32)content->size + 0x0F) & ~0x0F; WAD_815C4A2C(&transfer, outputBuffer, encryptionBuffer, 0x10000); threadArgs.fd = fileFd; threadArgs.size = sizeRemaining; @@ -1482,13 +1517,14 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 sizeRemaining += paddedSize; bufferIndex = 0; while ((sizeRemaining != 0) && (transfer.error == 0)) { - chunkSize = sizeRemaining; - if (transfer.chunkSize < chunkSize) { + if (sizeRemaining > transfer.chunkSize) { chunkSize = transfer.chunkSize; + } else { + chunkSize = sizeRemaining; } OSLockMutex(&transfer.mutex[bufferIndex]); while ((transfer.ready[bufferIndex] == 0) && (transfer.error == 0)) { - OSWaitCond(&transfer.waitCond[bufferIndex], &transfer.mutex[bufferIndex]); + OSWaitCond(&transfer.signalCond[bufferIndex], &transfer.mutex[bufferIndex]); } if (transfer.error != 0) { OSUnlockMutex(&transfer.mutex[bufferIndex]); @@ -1512,9 +1548,10 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 result = -3006; goto cleanup; } + result = 0; transfer.ready[bufferIndex] = 0; OSUnlockMutex(&transfer.mutex[bufferIndex]); - OSSignalCond(&transfer.signalCond[bufferIndex]); + OSSignalCond(&transfer.waitCond[bufferIndex]); importedSize += chunkSize; sizeRemaining -= chunkSize; if (processCallback != 0) { @@ -1543,33 +1580,34 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 } } if ((flags & 2) != 0) { + WADFileHeader* fileHeaderBuffer = &fileHeader; for (fileIndex = 0; fileIndex < fileCount; fileIndex++) { WADBackupFileHeader* file = &files[fileIndex]; s32 filePosition = WADSeekStream(&stream, 0, 1); size_t nameLength = strlen(file->path); _WADRandPad(file->path + nameLength + 1, 0x75 - (nameLength + 1)); - memcpy(&fileHeader, file, sizeof(fileHeader)); - result = SHA1Input(hashContext, &fileHeader, sizeof(WADBackupHeaderBlock)); + memcpy(fileHeaderBuffer, file, sizeof(fileHeader)); + result = SHA1Input(hashContext, fileHeaderBuffer, sizeof(WADBackupHeaderBlock)); if (result != 0) { goto file_done; } - result = WADWriteStream(&stream, &fileHeader, sizeof(WADBackupHeaderBlock)); - if (result != sizeof(WADBackupHeaderBlock)) { + if ((u32)WADWriteStream(&stream, fileHeaderBuffer, sizeof(WADBackupHeaderBlock)) != sizeof(WADBackupHeaderBlock)) { result = -3006; goto file_done; } + result = 0; importedSize += sizeof(WADBackupHeaderBlock); if (processCallback != 0) { processCallback(importedSize, contentDataSize + fileDataSize, FALSE); } - if ((fileHeader.flags[2] == 1) && (fileHeader.fileSize != 0)) { - result = NANDPrivateOpen(fileHeader.name, &savedFile, 1); + if ((fileHeaderBuffer->flags[2] == 1) && (fileHeaderBuffer->fileSize != 0)) { + result = NANDPrivateOpen(fileHeaderBuffer->name, &savedFile, 1); fileOpened = TRUE; if (result != 0) { goto file_done; } - sizeRemaining = (fileHeader.fileSize + 0x1F) & ~0x1F; + sizeRemaining = (fileHeaderBuffer->fileSize + 0x1F) & ~0x1F; while (sizeRemaining != 0) { chunkSize = sizeRemaining; if (chunkSize > 0x10000) { @@ -1580,7 +1618,8 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 result = -3005; goto file_done; } - result = ES_Encrypt(6, fileHeader.iv, encryptionBuffer, chunkSize, + result = 0; + result = ES_Encrypt(6, fileHeaderBuffer->iv, encryptionBuffer, chunkSize, outputBuffer); if (result != 0) { goto file_done; @@ -1589,11 +1628,11 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 if (result != 0) { goto cleanup; } - result = WADWriteStream(&stream, outputBuffer, chunkSize); - if ((u32)result != chunkSize) { + if ((u32)WADWriteStream(&stream, outputBuffer, chunkSize) != chunkSize) { result = -3006; goto file_done; } + result = 0; importedSize += chunkSize; sizeRemaining -= chunkSize; if (processCallback != 0) { @@ -1605,7 +1644,7 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 if (result != 0) { goto file_done; } - paddedSize = ((fileHeader.fileSize + 0x1F) & ~0x1F); + paddedSize = ((fileHeaderBuffer->fileSize + 0x1F) & ~0x1F); readSize = ((paddedSize + 0x3F) & ~0x3F) - paddedSize; if (readSize != 0) { _WADRandPad(outputBuffer, readSize); @@ -1614,12 +1653,12 @@ s32 WADBackupEx(u64 titleId, u32 flags, MEMAllocator* allocator, char* path, u32 if (result != 0) { goto cleanup; } - result = WADWriteStream(&stream, outputBuffer, - readSize); - if ((u32)result != readSize) { + if ((u32)WADWriteStream(&stream, outputBuffer, + readSize) != readSize) { result = -3006; goto file_done; } + result = 0; importedSize += readSize; result = 0; if (processCallback != 0) { @@ -1632,7 +1671,7 @@ file_done: NANDClose(&savedFile); } if (result != 0) { - readSize = (fileHeader.fileSize + 0x3F) & ~0x3F; + readSize = (fileHeaderBuffer->fileSize + 0x3F) & ~0x3F; totalSize -= readSize; headerBlock.header.fileSize -= readSize; headerBlock.header.numFiles--; @@ -1644,26 +1683,26 @@ file_done: } } } - result = WADSeekStream(&stream, 0, 1); - if (result < 0) { - goto cleanup; - } - signatureOffset = result; - if ((u32)(signatureOffset - offset) != totalSize || - headerBlock.header.backupAreaLen != totalSize + 0x340) { + signatureOffset = WADSeekStream(&stream, 0, 1); + if (signatureOffset < 0 || (u32)(signatureOffset - offset) != totalSize) { result = -3006; goto cleanup; } + if (headerBlock.header.backupAreaLen != totalSize + 0x340) { + result = -3006; + goto cleanup; + } + result = 0; result = SHA1Result(hashContext, digest); if (result != 0) { goto cleanup; } - certificates = (struct WADExportCertificates*)ROUNDUP((u32)outputBuffer, 64); - result = ES_Sign(digest, 0x14, signature, &certificates->signer); + certificates = (struct WADExportCertificates*)outputBuffer; + result = ES_Sign(digest, 0x14, signature, (ESCertSignature*)ROUNDUP((u32)&certificates->signer, 64)); if (result != 0) { goto cleanup; } - result = ES_GetDeviceCert(certificates->device); + result = ES_GetDeviceCert((ESCertSignature*)ROUNDUP((u32)certificates->device, 64)); if (result != 0) { goto cleanup; } @@ -1671,32 +1710,33 @@ file_done: result = -3006; goto cleanup; } - result = WADWriteStream(&stream, signature, sizeof(signature)); - if (result != sizeof(signature)) { + if ((s32)WADWriteStream(&stream, signature, sizeof(signature)) != (s32)sizeof(signature)) { result = -3006; goto cleanup; } - result = WADWriteStream(&stream, certificates->device, sizeof(certificates->device)); - if (result != sizeof(certificates->device)) { - result = -3006; - goto cleanup; - } - result = WADWriteStream(&stream, &certificates->signer, sizeof(certificates->signer)); - if (result != sizeof(certificates->signer)) { - result = -3006; - goto cleanup; - } - if (WADSeekStream(&stream, offset, 0) != (s32)offset) { - result = -3006; - goto cleanup; - } - result = WADWriteStream(&stream, &headerBlock, sizeof(headerBlock)); - if (result != sizeof(headerBlock)) { - result = -3006; - goto cleanup; - } - *sizeOut = headerBlock.header.backupAreaLen; result = 0; + if ((s32)WADWriteStream(&stream, (void*)ROUNDUP((u32)certificates->device, 64), sizeof(certificates->device)) != (s32)sizeof(certificates->device)) { + result = -3006; + goto cleanup; + } + result = 0; + if ((s32)WADWriteStream(&stream, (ESCertSignature*)ROUNDUP((u32)&certificates->signer, 64), sizeof(certificates->signer)) != (s32)sizeof(certificates->signer)) { + result = -3006; + goto cleanup; + } + result = 0; + if ((u32)WADSeekStream(&stream, offset, 0) != offset) { + result = -3006; + goto cleanup; + } + if ((u32)WADWriteStream(&stream, &headerBlock, sizeof(headerBlock)) != sizeof(headerBlock)) { + result = -3006; + goto cleanup; + } + result = 0; + if (sizeOut != 0) { + *sizeOut = headerBlock.header.backupAreaLen; + } cleanup: if (titleMetaBuffer != 0) { @@ -1708,14 +1748,11 @@ cleanup: if (encryptionBuffer != 0) { _WADMemFree(allocator, encryptionBuffer); } - if (threadStack != 0) { - _WADMemFree(allocator, threadStack); - } if (files != 0) { _WADMemFree(allocator, files); } - if (fileOpened) { - NANDClose(&savedFile); + if (threadStack != 0) { + _WADMemFree(allocator, threadStack); } if (streamOpened) { WADCloseStream(&stream); @@ -1902,51 +1939,48 @@ s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32 case WAD_LOCATION_NAND: { s32 result; - if (write == 0) { - goto openNandRead; - } - result = NANDPrivateOpen(path, &stream->handle.nand, 3); - if (result == -12) { - result = NANDPrivateCreate(path, 0x3F, 0); + if (write != 0) { + result = NANDPrivateOpen(path, &stream->handle.nand, 3); + if (result == -12) { + result = NANDPrivateCreate(path, 0x3F, 0); + if (result != 0) { + return -3004; + } + result = NANDPrivateOpen(path, &stream->handle.nand, 3); + } if (result != 0) { return -3004; } - result = NANDPrivateOpen(path, &stream->handle.nand, 3); - } - if (result != 0) { - return -3004; - } - result = NANDSeek(&stream->handle.nand, offset, 0); - if (result == -8) { - u8 zeroes[0x4000] ALIGN32; - u32 fileSize; + result = NANDSeek(&stream->handle.nand, offset, 0); + if (result == -8) { + u8 zeroes[0x4000] ALIGN32; + u32 fileSize; - fileSize = NANDSeek(&stream->handle.nand, 0, 2); - if (fileSize >= offset) { - return -3004; - } - memset(zeroes, 0, sizeof(zeroes)); - fileSize = offset - fileSize; - while (fileSize != 0) { - u32 writeSize; - writeSize = fileSize > sizeof(zeroes) ? sizeof(zeroes) : fileSize; - if (NANDWrite(&stream->handle.nand, zeroes, writeSize) != writeSize) { + fileSize = NANDSeek(&stream->handle.nand, 0, 2); + if (fileSize >= offset) { return -3004; } - fileSize -= writeSize; + memset(zeroes, 0, sizeof(zeroes)); + fileSize = offset - fileSize; + while (fileSize != 0) { + u32 writeSize; + writeSize = fileSize > sizeof(zeroes) ? sizeof(zeroes) : fileSize; + result = NANDWrite(&stream->handle.nand, zeroes, writeSize); + if ((u32)result != writeSize) { + return -3004; + } + fileSize -= writeSize; + } + result = NANDSeek(&stream->handle.nand, offset, 0); } - result = NANDSeek(&stream->handle.nand, offset, 0); + return result != offset ? -3004 : 0; } - return result != offset ? -3004 : 0; -openNandRead: result = NANDPrivateOpen(path, &stream->handle.nand, 1); - if (result != 0) { - return -3004; - } - return result; + return result != 0 ? -3004 : result; } case WAD_LOCATION_SD_CARD: { + s32 result; if (write != 0) { stream->handle.fa = FAFopen(path, "r+"); if (stream->handle.fa == 0) { @@ -1955,12 +1989,19 @@ openNandRead: if (stream->handle.fa == 0) { return -3004; } - if ((offset != 0) && (WADSeekStream(stream, offset, 0) != offset)) { - return -3004; + if (offset != 0) { + result = WADSeekStream(stream, offset, 0); + if ((u32)result == offset) { + goto checkSdHandle; + } else { + return -3004; + } + goto checkSdHandle; } } else { stream->handle.fa = FAFopen(path, "r"); } +checkSdHandle: return stream->handle.fa == 0 ? -3004 : 0; } @@ -2465,10 +2506,11 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* info->contentIndex = &backupHeader->cidx; } info->fileNames = backupHeader->deviceMac; + titleMetaSize = backupHeader->tmdSize; sectionOffset = (backupHeader->hdrSize + WAD_STREAM_ALIGNMENT - 1) & ~(WAD_STREAM_ALIGNMENT - 1); - titleMetaSize = backupHeader->tmdSize; + result = 0; if (titleMetaSize != 0) { if ((flags & 4) == 0) { info->sectionSize = titleMetaSize; @@ -2486,7 +2528,6 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* } result = 0; } - result = 0; sectionOffset += (backupHeader->tmdSize + WAD_STREAM_ALIGNMENT - 1) & ~(WAD_STREAM_ALIGNMENT - 1); if ((flags & 2) != 0) { @@ -2594,11 +2635,11 @@ static s32 _WADBackupGetFiles(const char* directoryPath, u32 flags, MEMAllocator u32 fileSize; u32 childFileCount; u8 fileType; - char* nameList = 0; - char* name; WADBackupFileHeader* file = 0; + char* nameList = 0; u32 totalFiles = 0; - u32 nameIndex; + s32 nameIndex; + char* name; BOOL absolutePaths = FALSE; if (allocator == 0 || fileCount == 0) { @@ -2625,6 +2666,10 @@ static s32 _WADBackupGetFiles(const char* directoryPath, u32 flags, MEMAllocator result = -3003; goto cleanup; } + if (nameList == 0) { + result = -3003; + goto cleanup; + } result = NANDPrivateReadDir(directoryPath, nameList, &nameCount); if (result != 0) { goto cleanup; @@ -2636,7 +2681,7 @@ static s32 _WADBackupGetFiles(const char* directoryPath, u32 flags, MEMAllocator file = files; } name = nameList; - for (nameIndex = 0; nameIndex < nameCount; nameIndex++) { + for (nameIndex = 0; nameIndex < nameCount;) { BOOL closeFile = FALSE; if (absolutePaths) { @@ -2717,8 +2762,9 @@ static s32 _WADBackupGetFiles(const char* directoryPath, u32 flags, MEMAllocator file++; } if (fileType == 2) { - WADBackupFileHeader* children = 0; + WADBackupFileHeader* children; childFileCount = 0; + children = 0; if (files != 0) { children = file; } @@ -2741,6 +2787,7 @@ static s32 _WADBackupGetFiles(const char* directoryPath, u32 flags, MEMAllocator } } nextName: + nameIndex++; name += strlen(name) + 1; if (closeFile) { NANDClose(&fileInfo); @@ -2761,9 +2808,9 @@ static s32 _WADBackupGetSize(u32 flags, ESTmdView* titleMeta, ESContentMask* con u32* contentDataSize, u32* fileDataSize, u32* totalSize) { u32 titleSize = 0x80; u32 contentSize = 0; - u32 filesSize; u32 contentIndex; u32 selectedCount; + u32 filesSize; if ((titleMeta != 0) && (titleMetaSize != 0)) { if (((flags & 1) != 0) || ((flags & 0x20) != 0)) { @@ -2829,7 +2876,7 @@ static void _WADFreeMemory(WADUnpackInfo* info, MEMAllocator* allocator) { } static void _WADRandPad(void* buffer, u32 size) { - u32* words = buffer; + u8* words = buffer; u32 wordCount; u32 remainder; u32 wordIndex; @@ -2840,16 +2887,18 @@ static void _WADRandPad(void* buffer, u32 size) { wordCount = size >> 2; remainder = size & 3; for (wordIndex = 0; wordIndex < wordCount; wordIndex++) { - *words++ = (((u32)rand() << 17) | ((u32)rand() << 2)) | (rand() & 3); + *(u32*)words = (((u32)rand() << 17) | ((u32)rand() << 2)) | (rand() & 3); + words += sizeof(u32); } if (remainder != 0) { u8* tail = (u8*)words; - u8* randomBytes = (u8*)&wordCount; + u8* randomBytes; u32 byteIndex; wordCount = (((u32)rand() << 17) | ((u32)rand() << 2)) | (rand() & 3); for (byteIndex = 0; byteIndex < remainder; byteIndex++) { - *tail++ = *randomBytes++; + randomBytes = (u8*)&wordCount; + *tail++ = randomBytes[byteIndex]; } } } @@ -2858,30 +2907,28 @@ static s32 WAD_815C43E0(WADHashThreadArgs* args) { void* context = args->context; s32 result = 0; u32 remaining = args->size; - u32 bufferIndex = 0; WADImportTransfer* transfer = args->transfer; + u32 bufferIndex = 0; + u32 size; - while ((remaining != 0) && (result == 0)) { - u32 size = remaining; - OSMutex* mutex; + for (; (remaining != 0) && (result == 0); bufferIndex ^= 1) { + size = remaining; if (remaining > transfer->chunkSize) { size = transfer->chunkSize; } - mutex = &transfer->mutex[bufferIndex]; - OSLockMutex(mutex); + OSLockMutex(&transfer->mutex[bufferIndex]); while (transfer->ready[bufferIndex] == 0) { - OSWaitCond(&transfer->signalCond[bufferIndex], mutex); + OSWaitCond(&transfer->signalCond[bufferIndex], &transfer->mutex[bufferIndex]); } result = SHA1Input(context, transfer->buffers[bufferIndex], size); transfer->ready[bufferIndex] = 0; if (result != 0) { transfer->error = 1; } - OSUnlockMutex(mutex); + OSUnlockMutex(&transfer->mutex[bufferIndex]); OSSignalCond(&transfer->waitCond[bufferIndex]); remaining -= size; - bufferIndex ^= 1; } return result; } @@ -2889,8 +2936,8 @@ static s32 WAD_815C43E0(WADHashThreadArgs* args) { static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void* buffer, u32 chunkSize, void* secondBuffer, void* threadStack, u32 threadStackSize) { s32 result; - void* readBuffer; u32 completed = 0; + void* readBuffer; u32 readSize; if (stream == 0 || context == 0 || buffer == 0) { @@ -2917,7 +2964,8 @@ static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void readSize = chunkSize; } readBuffer = buffer; - if (WADReadStream(stream, &readBuffer, readSize, offset + completed) != readSize) { + result = WADReadStream(stream, &readBuffer, readSize, offset + completed); + if ((u32)result != readSize) { result = -3005; goto done; } @@ -3409,12 +3457,12 @@ s32 WADImportDVDExForBS(const char* path, void* buffer, u32 bufferSize) { void* titleMeta; } parts; u8* readBuffer = buffer; - u8* contentBuffer; ESTitleMeta* titleMeta; ESContentMeta* contentMeta; s32 sectionOffset; - u32 remainingBufferSize; + u8* contentBuffer; u32 contentCount; + u32 remainingBufferSize; u32 contentIndex; s32 contentFd; s32 result; @@ -3430,7 +3478,7 @@ s32 WADImportDVDExForBS(const char* path, void* buffer, u32 bufferSize) { return -3004; } fileOpened = TRUE; - if ((u32)buffer == 0) { + if (buffer == 0) { result = -3003; goto cleanup; } @@ -3543,8 +3591,8 @@ s32 WADImportDVDExForBS(const char* path, void* buffer, u32 bufferSize) { remainingContentSize = ((u32)contentMeta->size + 0xF) & ~0xF; while (remainingContentSize != 0) { - u32 chunkSize = remainingContentSize < remainingBufferSize ? - remainingContentSize : remainingBufferSize; + u32 chunkSize = remainingContentSize > remainingBufferSize ? + remainingBufferSize : remainingContentSize; u32 readSize; readResult = DVDReadPrio(&fileInfo, contentBuffer, (chunkSize + 0x1F) & ~0x1F, sectionOffset, 2); diff --git a/tools/decomp-assist/wad-attempts.md b/tools/decomp-assist/wad-attempts.md new file mode 100644 index 00000000..36b66ac8 --- /dev/null +++ b/tools/decomp-assist/wad-attempts.md @@ -0,0 +1,268 @@ +# WAD matching attempts, round 2 + +Baseline: merged main f1590c83, 27/40 instruction-exact, code 8392/24500, data 64/528. + +| Function | Target bytes | Objdiff before -> after | Source/target instructions | Positional nonregister differences | +| --- | ---: | --- | --- | ---: | +| WADImportGetBlocks | 1192 | 94.24161 -> 94.22147 | 286/298 | 260 | +| WAD_815BFFA8 | 248 | 97.17742 -> 99.35484 | 62/62 | 0 | +| WADImportEx | 4584 | 79.80366 -> 91.91536 | 1157/1146 | 1045 | +| WAD_815C1288 | 244 | 96.39344 -> 96.39344 | 61/61 | 0 | +| WADBackupEx | 4248 | 82.28342 -> 93.37382 | 1044/1062 | 994 | +| WADOpenStream | 812 | 98.89162 -> 99.50739 | 202/203 | 47 | +| _WADUnpackBackup | 452 | 97.34513 -> 99.02655 | 114/113 | 73 | +| _WADBackupGetFiles | 1268 | 97.19243 -> 100.00000 | 317/317 | 0 | +| _WADBackupGetSize | 620 | 99.90323 -> 99.90323 | 155/155 | 0 | +| _WADRandPad | 364 | 93.78022 -> 100.00000 | 91/91 | 0 | +| WAD_815C43E0 | 248 | 97.17742 -> 99.35484 | 62/62 | 0 | +| _WADHash | 776 | 98.53093 -> 98.55670 | 194/194 | 0 | +| WADImportDVDExForBS | 1052 | 96.04562 -> 96.17871 | 260/263 | 233 | + +String pools: identical, 18 entries. All 458 emitted .data bytes equal the target prefix; the target section has six trailing zero bytes. Both sections have alignment 8. .sdata is identical at 64 bytes. No padding or forced data placement added. + +Repeated null-validation branches were translated directly from target assembly: files 815C3B08/815C3B14; backup 815C1504/815C1514, 815C162C/815C163C, 815C170C/815C171C, 815C1868/815C1878, 815C1A48/815C1A58, 815C1A84/815C1A94. + +Positional difference counts include shifted instructions. Five remaining functions have equal instruction counts and zero nonregister differences. Seven instruction-count discrepancies were investigated; _WADRandPad is now exact after recomputing its byte-source view inside the tail loop. Six functions still differ beyond registers. Every initially open function has at least three fresh distinct logged attempts. + +Positional difference counts include shifted instructions. Five remaining functions have equal instruction counts and zero nonregister differences. Seven instruction-count discrepancies were investigated; _WADRandPad is now exact after recomputing its byte-source view inside the tail loop. Six functions still differ beyond registers. Every initially open function has at least three fresh distinct logged attempts. + +Positional difference counts include shifted instructions. Five remaining functions have equal instruction counts and zero nonregister differences. Seven instruction-count discrepancies were investigated; _WADRandPad is now exact after recomputing its byte-source view inside the tail loop. Six functions still differ beyond registers. Every initially open function has at least three fresh distinct logged attempts. + +Attempts: +```text +_WADBackupGetSize: r2 final backup size has its own lifetime rather than updating metadata header size; insns/diffs/nonregister (155, 155, 2, 0) -> (155, 155, 3, 0); objdiff 99.83871; retained False +r2 baseline: 27/40 exact, code 8392/24500, data 64/528, fuzzy 92.233955; merged source clean. Data audit pending; remaining function attempts start fresh. +_WADBackupGetSize: r2 lifetime: declare file byte count after selected content count; insns/diffs/nonregister (155, 155, 2, 0) -> (155, 155, 2, 0); objdiff 99.90323; retained True +_WADBackupGetSize: r2 write total through the output expression without overwriting header size; insns/diffs/nonregister (155, 155, 2, 0) -> (155, 155, 10, 9); objdiff 97.3871; retained False +_WADUnpackBackup: r2 retranslate success reset before metadata load and move shared success to entry; insns/diffs/nonregister (113, 113, 12, 4) -> (114, 113, 78, 73); objdiff 99.02655; retained True +WADOpenStream: r2 translate NAND-read success as switch break; reverse extent and SD seek operand order; insns/diffs/nonregister (203, 203, 30, 28) -> (204, 203, 51, 48); objdiff 98.349754; retained False +WADOpenStream: r2 translate nested SD seek failure with explicit seek result; insns/diffs/nonregister (203, 203, 30, 28) -> (203, 203, 30, 28); objdiff 98.891624; retained True +WADOpenStream: r2 translate NAND cases into structured write/read switch branches; compilation failed; restored +_WADUnpackBackup: r2 assign success once after either metadata branch; no early extra reset; insns/diffs/nonregister (114, 113, 78, 73) -> (113, 113, 12, 4); objdiff 97.34513; retained False +_WADUnpackBackup: r2 load metadata size before header alignment into its longer-lived temporary; insns/diffs/nonregister (114, 113, 78, 73) -> (114, 113, 78, 73); objdiff 99.02655; retained True +_WADUnpackBackup: r2 reset success in metadata-read and skip-read branches with no extra prologue instruction; insns/diffs/nonregister (114, 113, 78, 73) -> (115, 113, 54, 45); objdiff 97.831856; retained False +_WADUnpackBackup: r2 original branch direction: skip read before metadata allocation arm; insns/diffs/nonregister (114, 113, 78, 73) -> (115, 113, 75, 67); objdiff 97.30089; retained False +_WADRandPad: r2 do not hold the tail source address across random calls; insns/diffs/nonregister (89, 91, 52, 35) -> (89, 91, 26, 25); objdiff 94.60439; retained True +_WADRandPad: r2 index the random source bytes with the copied-byte counter; insns/diffs/nonregister (89, 91, 26, 25) -> (84, 91, 80, 80); objdiff 75.48351; retained False +_WADRandPad: r2 one byte cursor for word output and remaining tail; insns/diffs/nonregister (89, 91, 26, 25) -> (89, 91, 26, 25); objdiff 94.60439; retained True +data audit .data: source 458, target 464, overlapping differences 0, target remainder 000000000000 +data audit .sdata: source 64, target 64, overlapping differences 0, target remainder +_WADRandPad: r2 inline copy helper caused original function to disappear through automatic inlining; restored immediately; candidate rejected. +WADImportEx: r2 stack/prologue: result declaration and zero initialization before all ownership locals; insns/diffs/nonregister (1155, 1146, 1130, 1105) -> (1155, 1146, 1121, 1096); objdiff 79.70506; retained False +WADImportEx: r2 stack/prologue: parsed metadata local before progress and owned allocations; insns/diffs/nonregister (1155, 1146, 1130, 1105) -> (1155, 1146, 1130, 1105); objdiff 79.803665; retained True +WADImportEx: r2 opening block: reuse boot size for initial header read length as target r25; insns/diffs/nonregister (1155, 1146, 1130, 1105) -> (1156, 1146, 1129, 1101); objdiff 79.373474; retained False +WADImportEx: unlock current mutex on transfer failure; recompute aligned read comparison; insns/diffs/nonregister (1155, 1146, 1130, 1105) -> (1160, 1146, 1126, 1104); objdiff 80.374344; retained True +WADImportEx: unconditional join after successful create; return failed join immediately; insns/diffs/nonregister (1160, 1146, 1126, 1104) -> (1155, 1146, 1126, 1100); objdiff 80.92234; retained True +WADImportEx: remove transfer reset and postjoin error test absent from target; insns/diffs/nonregister (1155, 1146, 1126, 1100) -> (1150, 1146, 1126, 1095); objdiff 81.25305; retained True +_WADBackupGetSize: sum title and content through local before files-first final addition; insns/diffs/nonregister (155, 155, 2, 0) -> (155, 155, 3, 0); objdiff 99.83871; retained False +WAD_815BFFA8: move result initialization after transfer pointer load; insns/diffs/nonregister (62, 62, 30, 0) -> (62, 62, 36, 0); objdiff 96.29032; retained False +WAD_815BFFA8: use direct mutex expressions across wait and unlock; insns/diffs/nonregister (62, 62, 30, 0) -> (62, 62, 19, 0); objdiff 98.30645; retained True +WAD_815BFFA8: make transfer loop a for loop with index advance; insns/diffs/nonregister (62, 62, 19, 0) -> (62, 62, 19, 0); objdiff 98.30645; retained True +WAD_815C1288: move result initialization after transfer pointer load; insns/diffs/nonregister (61, 61, 34, 0) -> (61, 61, 34, 0); objdiff 96.39344; retained True +WAD_815C1288: use direct mutex expressions across wait and unlock; insns/diffs/nonregister (61, 61, 34, 0) -> (61, 61, 35, 0); objdiff 96.31148; retained False +WAD_815C1288: make transfer loop a for loop with index advance; insns/diffs/nonregister (61, 61, 34, 0) -> (61, 61, 34, 0); objdiff 96.39344; retained True +WAD_815C43E0: move result initialization after transfer pointer load; insns/diffs/nonregister (62, 62, 30, 0) -> (62, 62, 36, 0); objdiff 96.29032; retained False +WAD_815C43E0: use direct mutex expressions across wait and unlock; insns/diffs/nonregister (62, 62, 30, 0) -> (62, 62, 19, 0); objdiff 98.30645; retained True +WAD_815C43E0: make transfer loop a for loop with index advance; insns/diffs/nonregister (62, 62, 19, 0) -> (62, 62, 19, 0); objdiff 98.30645; retained True +WAD_815BFFA8: declare chunk size with loop state; assign at each iteration; insns/diffs/nonregister (62, 62, 19, 0) -> (62, 62, 14, 0); objdiff 98.79032; retained True +WAD_815BFFA8: declare transfer pointer before index but initialize in target order; insns/diffs/nonregister (62, 62, 14, 0) -> (62, 62, 19, 0); objdiff 98.30645; retained False +WAD_815C1288: declare chunk size with loop state; assign at each iteration; compilation failed; restored +WAD_815C1288: declare transfer pointer before index but initialize in target order; compilation failed; restored +WAD_815C43E0: declare chunk size with loop state; assign at each iteration; insns/diffs/nonregister (62, 62, 19, 0) -> (62, 62, 14, 0); objdiff 98.79032; retained True +WAD_815C43E0: declare transfer pointer before index but initialize in target order; insns/diffs/nonregister (62, 62, 14, 0) -> (62, 62, 19, 0); objdiff 98.30645; retained False +WAD_815BFFA8: declare chunk after transfer state; insns/diffs/nonregister (62, 62, 14, 0) -> (62, 62, 19, 0); objdiff 98.30645; retained False +WAD_815BFFA8: declare chunk before result but load input state first; insns/diffs/nonregister (62, 62, 14, 0) -> (62, 62, 17, 0); objdiff 98.30645; retained False +WAD_815BFFA8: assign transfer before zero index initialization; insns/diffs/nonregister (62, 62, 14, 0) -> (62, 62, 19, 0); objdiff 98.30645; retained False +WAD_815C43E0: declare chunk after transfer state; insns/diffs/nonregister (62, 62, 14, 0) -> (62, 62, 19, 0); objdiff 98.30645; retained False +WAD_815C43E0: declare chunk before result but load input state first; insns/diffs/nonregister (62, 62, 14, 0) -> (62, 62, 17, 0); objdiff 98.30645; retained False +WAD_815C43E0: assign transfer before zero index initialization; insns/diffs/nonregister (62, 62, 14, 0) -> (62, 62, 19, 0); objdiff 98.30645; retained False +WAD_815C1288: loop chunk declaration before its assignment; insns/diffs/nonregister (61, 61, 34, 0) -> (61, 61, 34, 0); objdiff 96.39344; retained True +WAD_815C1288: direct mutex use with outer chunk local; insns/diffs/nonregister (61, 61, 34, 0) -> (61, 61, 35, 0); objdiff 96.31148; retained False +_WADBackupGetFiles: translate allocation status before validating the name-list pointer; insns/diffs/nonregister (314, 317, 266, 257) -> (321, 317, 274, 265); objdiff 96.8265; retained False +_WADBackupGetFiles: allocation failure flows to shared status test before directory read; insns/diffs/nonregister (314, 317, 266, 257) -> (315, 317, 272, 264); objdiff 97.28706; retained True +_WADBackupGetFiles: load output iterator immediately before traversal; insns/diffs/nonregister (315, 317, 272, 264) -> (315, 317, 272, 264); objdiff 96.6877; retained False +_WADHash: keep read status in the result local before length comparison; insns/diffs/nonregister (194, 194, 44, 0) -> (194, 194, 44, 0); objdiff 98.5567; retained True +_WADHash: declare completed count before read pointer; insns/diffs/nonregister (194, 194, 44, 0) -> (194, 194, 44, 0); objdiff 98.5567; retained True +_WADHash: declare chunk length inside each transfer arm; insns/diffs/nonregister (194, 194, 44, 0) -> (194, 194, 62, 0); objdiff 97.78351; retained False +WADImportEx: reset status after argument validation as in target opening block; insns/diffs/nonregister (1150, 1146, 1126, 1095) -> (1152, 1146, 1123, 1095); objdiff 81.24869; retained False +WADImportEx: load parsed metadata before setting progress cursor; insns/diffs/nonregister (1150, 1146, 1126, 1095) -> (1150, 1146, 1126, 1095); objdiff 81.25305; retained True +WADImportEx: reuse aligned boot length expression after stream read; insns/diffs/nonregister (1150, 1146, 1126, 1095) -> (1152, 1146, 1123, 1094); objdiff 81.53054; retained True +WADBackupEx: retranslate stack: reverse declaration order; NAND file has natural alignment; insns/diffs/nonregister (996, 1062, 970, 950) -> (996, 1062, 968, 948); objdiff 82.33804; retained True +WADBackupEx: retranslate content listing: reset status after only missing-title error; insns/diffs/nonregister (996, 1062, 968, 948) -> (995, 1062, 975, 953); objdiff 82.43126; retained True +WADBackupEx: retranslate metadata view: reset status after selecting allocated view; insns/diffs/nonregister (995, 1062, 975, 953) -> (997, 1062, 971, 949); objdiff 83.12429; retained True +WADImportGetBlocks: load file-header pointer once at traversal entry; insns/diffs/nonregister (286, 298, 274, 259) -> (284, 298, 271, 251); objdiff 93.067116; retained False +WADImportGetBlocks: give file-list entry a pointer during field population; insns/diffs/nonregister (286, 298, 274, 259) -> (272, 298, 260, 246); objdiff 88.06376; retained False +WADImportGetBlocks: load parsed metadata before clearing block counters; insns/diffs/nonregister (286, 298, 274, 259) -> (286, 298, 274, 260); objdiff 93.58725; retained False +WADImportDVDExForBS: load title metadata after ticket import into a narrower block lifetime; insns/diffs/nonregister (260, 263, 243, 233) -> (260, 263, 243, 233); objdiff 96.00761; retained False +WADImportDVDExForBS: declare section cursor before content pointer; insns/diffs/nonregister (260, 263, 243, 233) -> (260, 263, 243, 233); objdiff 96.08365; retained True +WADImportDVDExForBS: express chunk minimum with target compare branch direction; insns/diffs/nonregister (260, 263, 243, 233) -> (259, 263, 238, 224); objdiff 95.39924; retained False +WADImportEx: correct stream type natural size; keep local/member alignment; (1152, 1146, 1123, 1094); objdiff 81.53054 +WADBackupEx: correct stream type natural size; keep local/member alignment; (997, 1062, 971, 949); objdiff 83.12429 +WADImportGetBlocks: correct stream type natural size; keep local/member alignment; (286, 298, 275, 260); objdiff 94.22147 +WADImportEx: align enclosing frame while stream local keeps natural extent; insns/diffs/nonregister (1152, 1146, 1123, 1094) -> (1152, 1146, 1123, 1094); objdiff 81.53839; retained True +WADBackupEx: align enclosing frame while stream local keeps natural extent; insns/diffs/nonregister (997, 1062, 971, 949) -> (997, 1062, 970, 948); objdiff 83.13936; retained True +WADImportEx: initialize status immediately after report name; insns/diffs/nonregister (1152, 1146, 1123, 1094) -> (1152, 1146, 1113, 1084); objdiff 82.50175; retained True +WADImportEx: declare metadata after owned buffers to shorten allocation pressure; insns/diffs/nonregister (1152, 1146, 1113, 1084) -> (1152, 1146, 1113, 1084); objdiff 82.50175; retained True +WADImportEx: defer status initialization until all frame and ownership declarations; insns/diffs/nonregister (1152, 1146, 1113, 1084) -> (1152, 1146, 1121, 1092); objdiff 81.363; retained False +_WADBackupGetSize: use signed header accumulator before total-size output; insns/diffs/nonregister (155, 155, 2, 0) -> (155, 155, 2, 0); objdiff 99.90323; retained True +_WADBackupGetSize: compute file-inclusive size before updating metadata accumulator; insns/diffs/nonregister (155, 155, 2, 0) -> (158, 155, 18, 14); objdiff 96.67742; retained False +_WADBackupGetSize: preserve header-plus-content subtotal through content output; insns/diffs/nonregister (155, 155, 2, 0) -> (155, 155, 6, 4); objdiff 98.645164; retained False +WADBackupEx: form certificate addresses from their struct fields and align each API buffer; insns/diffs/nonregister (997, 1062, 970, 948) -> (999, 1062, 973, 952); objdiff 83.17609; retained True +WADBackupEx: retranslate writes: validate byte count then set successful status; insns/diffs/nonregister (999, 1062, 973, 952) -> (1003, 1062, 979, 959); objdiff 85.32957; retained True +WADBackupEx: retranslate closing size checks: failed seek is backup write error; clear status before digest; insns/diffs/nonregister (1003, 1062, 979, 959) -> (1006, 1062, 985, 965); objdiff 85.70904; retained True +_WADBackupGetSize: single left-associated total expression; insns/diffs/nonregister (155, 155, 2, 0) -> (155, 155, 3, 0); objdiff 99.83871; retained False +_WADBackupGetSize: single sum with file total as first operand; insns/diffs/nonregister (155, 155, 2, 0) -> (155, 155, 3, 0); objdiff 99.83871; retained False +WADOpenStream: nested SD offset and seek-status branches with explicit result; write extent compare order; insns/diffs/nonregister (203, 203, 30, 28) -> (203, 203, 30, 28); objdiff 98.94089; retained True +WADOpenStream: structured NAND write/read arms without goto label; insns/diffs/nonregister (203, 203, 30, 28) -> (203, 203, 30, 28); objdiff 98.94089; retained True +WADOpenStream: NAND write arm returns before independent read path; insns/diffs/nonregister (203, 203, 30, 28) -> (203, 203, 30, 28); objdiff 98.94089; retained True +WADOpenStream: SD seek guards write arm directly with short-circuit test; insns/diffs/nonregister (203, 203, 30, 28) -> (203, 203, 30, 28); objdiff 98.891624; retained False +WADOpenStream: NAND read status mapped to return expression; insns/diffs/nonregister (203, 203, 30, 28) -> (202, 203, 48, 47); objdiff 99.45813; retained True +WADOpenStream: join successful seek with common SD handle validation; insns/diffs/nonregister (202, 203, 48, 47) -> (202, 203, 48, 47); objdiff 99.45813; retained True +WADOpenStream: seek success branches directly to handle validation; failure has explicit else; insns/diffs/nonregister (202, 203, 48, 47) -> (202, 203, 48, 47); objdiff 99.45813; retained True +WADOpenStream: validate NAND write count through explicit status local; insns/diffs/nonregister (202, 203, 48, 47) -> (202, 203, 47, 47); objdiff 99.50739; retained True +WADOpenStream: SD write arm returns validated handle; read arm shares validation expression; insns/diffs/nonregister (202, 203, 47, 47) -> (213, 203, 49, 49); objdiff 92.38916; retained False +WADOpenStream: map SD seek byte offset to an error status before shared handle check; insns/diffs/nonregister (202, 203, 47, 47) -> (206, 203, 48, 48); objdiff 98.44827; retained False +_WADBackupGetFiles: translate both allocation-result validation branches present at 815C3B08 and 815C3B14; insns/diffs/nonregister (315, 317, 272, 264) -> (317, 317, 41, 11); objdiff 98.1388; retained True +_WADBackupGetFiles: advance traversal index before name-length call and cleanup; insns/diffs/nonregister (317, 317, 41, 11) -> (317, 317, 36, 3); objdiff 98.73817; retained True +_WADBackupGetFiles: initialize recursive count before selecting child output pointer; insns/diffs/nonregister (317, 317, 36, 3) -> (317, 317, 33, 0); objdiff 99.38486; retained True +WADBackupEx: translate the six repeated allocation checks and success resets visible in target blocks; insns/diffs/nonregister (1006, 1062, 985, 965) -> (1038, 1062, 1018, 997); objdiff 88.28719; retained True +WADBackupEx: reset status before path-size query and progress callback; insns/diffs/nonregister (1038, 1062, 1018, 997) -> (1038, 1062, 1017, 997); objdiff 88.75612; retained True +_WADBackupGetFiles: declare total file count before output iterator; insns/diffs/nonregister (317, 317, 33, 0) -> (317, 317, 30, 0); objdiff 99.46372; retained True +_WADBackupGetFiles: declare name cursor after loop index state; insns/diffs/nonregister (317, 317, 30, 0) -> (317, 317, 19, 0); objdiff 99.63722; retained True +_WADBackupGetFiles: use signed directory traversal index like NAND count comparisons; insns/diffs/nonregister (317, 317, 19, 0) -> (317, 317, 19, 0); objdiff 99.63722; retained True +WADBackupEx: separate TMD view length from exported TMD length; correct output-size stack declaration order; insns/diffs/nonregister (1038, 1062, 1017, 997) -> (1038, 1062, 1013, 986); objdiff 88.859695; retained True +WADBackupEx: translate empty file list branch with explicit null output owner; insns/diffs/nonregister (1038, 1062, 1013, 986) -> (1041, 1062, 1002, 969); objdiff 89.0951; retained True +WADBackupEx: initialize owned buffers and size outputs in target prologue order after declarations; insns/diffs/nonregister (1041, 1062, 1002, 969) -> (1042, 1062, 967, 931); objdiff 88.86441; retained False +WADImportEx: align the 32-bit content length rather than the 64-bit metadata member; insns/diffs/nonregister (1152, 1146, 1113, 1084) -> (1142, 1146, 1113, 1089); objdiff 82.56806; retained True +WADBackupEx: align the 32-bit content length rather than the 64-bit metadata member; insns/diffs/nonregister (1041, 1062, 1002, 969) -> (1039, 1062, 1011, 985); objdiff 89.391716; retained True +WADBackupEx: use export consumer condition queues from target wait and signal blocks; insns/diffs/nonregister (1039, 1062, 1011, 985) -> (1039, 1062, 1011, 985); objdiff 89.3936; retained True +WADBackupEx: keep export stream write count in joined-thread status slot as target does; insns/diffs/nonregister (1039, 1062, 1011, 985) -> (1040, 1062, 1015, 987); objdiff 89.49247; retained True +_WADBackupGetFiles: declare output iterator before directory owner and file count; insns/diffs/nonregister (317, 317, 19, 0) -> (317, 317, 0, 0); objdiff 100.0; retained True +_WADBackupGetFiles: initialize directory owner and counter before optional output iterator; insns/diffs/nonregister (317, 317, 0, 0) -> (317, 317, 4, 0); objdiff 99.936905; retained False +_WADRandPad: copy remaining random bytes with standard memcpy; insns/diffs/nonregister (89, 91, 26, 25) -> (52, 91, 22, 14); objdiff 52.307693; retained False +_WADRandPad: index source and destination bytes from their original cursors; insns/diffs/nonregister (89, 91, 26, 25) -> (85, 91, 82, 80); objdiff 68.73627; retained False +_WADRandPad: use immutable source pointer with an indexed read and advancing destination; insns/diffs/nonregister (89, 91, 26, 25) -> (84, 91, 80, 80); objdiff 75.48351; retained False +WADImportEx: declare report-name pointer after transfer state to reduce its register priority; insns/diffs/nonregister (1142, 1146, 1113, 1089) -> (1142, 1146, 1113, 1089); objdiff 82.56806; retained True +WADImportEx: place metadata pointer after all owner declarations; insns/diffs/nonregister (1142, 1146, 1113, 1089) -> (1142, 1146, 1113, 1089); objdiff 82.56806; retained True +WADImportEx: keep progress cursor declaration before status state; insns/diffs/nonregister (1142, 1146, 1113, 1089) -> (1141, 1146, 1053, 1013); objdiff 83.095116; retained True +WAD_815BFFA8: declare transfer, index, then chunk in register-lifetime order; insns/diffs/nonregister (62, 62, 14, 0) -> (62, 62, 8, 0); objdiff 99.354836; retained True +WAD_815C43E0: declare transfer, index, then chunk in register-lifetime order; insns/diffs/nonregister (62, 62, 14, 0) -> (62, 62, 8, 0); objdiff 99.354836; retained True +WADImportEx: choose transfer chunk in target greater-than branch order; insns/diffs/nonregister (1141, 1146, 1053, 1013) -> (1141, 1146, 1053, 1013); objdiff 83.25131; retained True +WADBackupEx: choose transfer chunk in target greater-than branch order; insns/diffs/nonregister (1040, 1062, 1015, 987) -> (1040, 1062, 1015, 987); objdiff 89.69021; retained True +WADBackupEx: clear successful file-read status before encryption API block; insns/diffs/nonregister (1040, 1062, 1015, 987) -> (1043, 1062, 1012, 985); objdiff 90.01224; retained True +WAD_815BFFA8: name ready and failure status constants before the chunk local; insns/diffs/nonregister (62, 62, 8, 0) -> (62, 62, 8, 0); objdiff 99.354836; retained True +WAD_815C43E0: name ready and failure status constants before the chunk local; insns/diffs/nonregister (62, 62, 8, 0) -> (62, 62, 8, 0); objdiff 99.354836; retained True +WADOpenStream: join SD handle validation with a labelled seek failure block; insns/diffs/nonregister (202, 203, 47, 47) -> (202, 203, 47, 47); objdiff 99.50739; retained True +WADBackupEx: declare size outputs before ownership locals to match target zero stores; insns/diffs/nonregister (1043, 1062, 1012, 985) -> (1044, 1062, 972, 933); objdiff 89.77683; retained False +WADBackupEx: free file descriptors before export thread stack in target cleanup order; insns/diffs/nonregister (1043, 1062, 1012, 985) -> (1043, 1062, 1012, 985); objdiff 90.01224; retained True +WADBackupEx: guard final backup-size output pointer as target does; insns/diffs/nonregister (1043, 1062, 1012, 985) -> (1046, 1062, 1009, 983); objdiff 90.33616; retained True +WADBackupEx: limit saved-file closing to file_done; target cleanup has no second NANDClose; insns/diffs/nonregister (1046, 1062, 1009, 983) -> (1031, 1062, 1001, 978); objdiff 89.44633; retained False +WADBackupEx: retranslate signature seek and fixed-size writes with target signed status comparisons; insns/diffs/nonregister (1046, 1062, 1009, 983) -> (1046, 1062, 1008, 982); objdiff 91.4162; retained True +WADBackupEx: retain target-faithful cleanup without second NANDClose despite lower fuzzy score; original cleanup 815C232C-815C23F4 has no NANDClose; (1031, 1062, 1001, 978); objdiff 90.23258 +WADBackupEx: keep file-header pointer across file export calls as in target loop; insns/diffs/nonregister (1031, 1062, 1001, 978) -> (1033, 1062, 1010, 989); objdiff 91.35123; retained True +WADImportEx: initialize parsed metadata pointer to null in the target prologue; insns/diffs/nonregister (1141, 1146, 1053, 1013) -> (1141, 1146, 1053, 1013); objdiff 83.25131; retained True +WADImportEx: initialize metadata pointer before ownership and progress state; insns/diffs/nonregister (1141, 1146, 1053, 1013) -> (1144, 1146, 1118, 1097); objdiff 82.42583; retained False +WADImportEx: defer content descriptor assignment until content import begins; cancellation is guarded; insns/diffs/nonregister (1141, 1146, 1053, 1013) -> (1139, 1146, 1083, 1045); objdiff 83.356895; retained True +WADImportEx: reset import status after validation before initial progress callback; insns/diffs/nonregister (1139, 1146, 1083, 1045) -> (1141, 1146, 1038, 992); objdiff 83.35253; retained False +_WADRandPad: inline byte copier with indexed source; move existing non-inlining boundary past real padding function to preserve it; (52, 91, 22, 14); objdiff 52.307693 +_WADRandPad: helper candidate rejected; not instruction-exact or registers-only +_WADRandPad: form indexed byte view through the SDK 32-bit address representation; insns/diffs/nonregister (89, 91, 26, 25) -> (84, 91, 80, 80); objdiff 75.48351; retained False +Final remaining audit: WADImportGetBlocks 286/298; target initializes two unused zero owners and conditionally frees them; no dummy owners added. WADImportEx 1139/1146: frame 0x760 matches; metadata spill and branch/status ordering remain. WADBackupEx 1033/1062: frame 0x8c0 matches; genuine transfer/size/signature/cleanup blocks retranslated; remaining prologue, metadata addressing, and branch layout. WADOpenStream 202/203: target has an unreachable branch after SD seek failure. _WADUnpackBackup 114/113: defined success initialization adds one instruction on skipped-metadata path. _WADRandPad 89/91: scalar tail source address reused rather than recomputed. WADImportDVDExForBS 260/263: target retains empty content-size comparison and null/status setup. All open functions have at least three fresh distinct source attempts. Five open functions have equal counts and only register differences. +Cleanup: remove consecutive duplicate success assignment; restore equal-scoring structured SD seek block rather than retaining extra labels. +_WADRandPad: tail byte source recomputed inside each iteration; insns/diffs/nonregister (89, 91, 26, 25) -> (91, 91, 0, 0); objdiff 100.0; retained True +_WADRandPad: tail source uses void byte view with loop index; insns/diffs/nonregister (91, 91, 0, 0) -> (91, 91, 35, 32); objdiff 96.15385; retained False +_WADRandPad: tail count and byte cursor established before random word assignment; insns/diffs/nonregister (91, 91, 0, 0) -> (89, 91, 43, 35); objdiff 93.72527; retained False +WADImportEx: metadata owner declared first before progress and status; insns/diffs/nonregister (1139, 1146, 1083, 1045) -> (1142, 1146, 1123, 1099); objdiff 82.47819; retained False +WADImportEx: metadata owner and function report declared before progress state; insns/diffs/nonregister (1139, 1146, 1083, 1045) -> (1142, 1146, 1123, 1099); objdiff 82.47819; retained False +WADImportEx: report name then metadata owner precede status and progress; insns/diffs/nonregister (1139, 1146, 1083, 1045) -> (1142, 1146, 1123, 1099); objdiff 82.47819; retained False +WADImportDVDExForBS: keep allocation buffer null validation as pointer comparison; insns/diffs/nonregister (260, 263, 243, 233) -> (260, 263, 243, 233); objdiff 96.08365; retained True +WADImportDVDExForBS: chunk minimum uses remaining-size greater-than branch with destination retained; insns/diffs/nonregister (260, 263, 243, 233) -> (260, 263, 243, 233); objdiff 96.12167; retained True +WADImportDVDExForBS: declare remaining buffer length after content count to match pointer lifetime; insns/diffs/nonregister (260, 263, 243, 233) -> (260, 263, 243, 233); objdiff 96.17871; retained True +WADImportEx: stack first: place transfer before saved file header to match target 0x120 and 0xa0; insns/diffs/nonregister (1139, 1146, 1083, 1045) -> (1139, 1146, 1082, 1044); objdiff 83.37086; retained True +WADImportEx: retranslate shared hash allocation failure exactly, including zero-count allocation result; insns/diffs/nonregister (1139, 1146, 1082, 1044) -> (1136, 1146, 1095, 1063); objdiff 83.84032; retained True +WADImportEx: retranslate indexed content lookup: unsigned mode threshold, record pointer in each branch, successful status clear; insns/diffs/nonregister (1136, 1146, 1095, 1063) -> (1148, 1146, 1083, 1042); objdiff 85.16143; retained True +WADImportEx: clear import status before beginning content and after obtaining a valid descriptor; insns/diffs/nonregister (1148, 1146, 1083, 1042) -> (1152, 1146, 1103, 1068); objdiff 85.3089; retained True +WADImportEx: use unsigned traversal count for metadata array indices; insns/diffs/nonregister (1152, 1146, 1103, 1068) -> (1152, 1146, 1103, 1068); objdiff 85.34468; retained True +WADImportEx: emit missing-content import before matched-content skip as target address order; insns/diffs/nonregister (1152, 1146, 1103, 1068) -> (1152, 1146, 1097, 1059); objdiff 88.05061; retained True +WADImportEx: retranslate final callback: file list size and count select backup progress; zero progress last; insns/diffs/nonregister (1152, 1146, 1097, 1059) -> (1153, 1146, 1096, 1058); objdiff 89.56806; retained True +WADBackupEx: validate system-title prefix with explicit title ID mask as target; insns/diffs/nonregister (1033, 1062, 1010, 989) -> (1035, 1062, 1016, 993); objdiff 91.14595; retained False +WADBackupEx: preserve flags mask comparison width in title import validation; insns/diffs/nonregister (1033, 1062, 1010, 989) -> (1034, 1062, 1014, 985); objdiff 91.25047; retained False +WADBackupEx: initialize size accumulators immediately before title metadata query; insns/diffs/nonregister (1033, 1062, 1010, 989) -> (1033, 1062, 1010, 989); objdiff 91.35123; retained True +WADImportEx: selected content pointer declared with metadata state rather than inside loop; insns/diffs/nonregister (1153, 1146, 1096, 1058) -> (1153, 1146, 1096, 1058); objdiff 89.53316; retained False +WADImportEx: loop cursor initialization occurs at traversal entry rather than function entry; insns/diffs/nonregister (1153, 1146, 1096, 1058) -> (1153, 1146, 1096, 1058); objdiff 89.56806; retained True +WADImportEx: assign successful argument-validation status through progress cursor before callback; insns/diffs/nonregister (1153, 1146, 1096, 1058) -> (1154, 1146, 1062, 1012); objdiff 89.74084; retained True +WADBackupEx: mask the 32-bit title prefix before comparing to system title class; insns/diffs/nonregister (1033, 1062, 1010, 989) -> (1032, 1062, 999, 980); objdiff 91.14877; retained False +WADBackupEx: initialize parsed title components after successful encryption-buffer allocation; insns/diffs/nonregister (1033, 1062, 1010, 989) -> (1033, 1062, 1010, 988); objdiff 91.53484; retained True +WADBackupEx: keep header block address through initial write and explicit count local; insns/diffs/nonregister (1033, 1062, 1010, 988) -> (1035, 1062, 1010, 980); objdiff 91.32768; retained False +WADBackupEx: restore size initialization at declaration to preserve argument spill offsets; insns/diffs/nonregister (1033, 1062, 1010, 988) -> (1033, 1062, 1010, 988); objdiff 91.53484; retained True +WADBackupEx: declare saved-file descriptor owner before other function locals; insns/diffs/nonregister (1033, 1062, 1010, 988) -> (1044, 1062, 1022, 994); objdiff 93.373825; retained True +WADBackupEx: normalize backup option flags through a local with argument lifetime ending at initialization; insns/diffs/nonregister (1044, 1062, 1022, 994) -> (1045, 1062, 1018, 994); objdiff 92.80038; retained False +WADImportEx: keep parsed metadata owner alongside stream arguments at function entry; insns/diffs/nonregister (1154, 1146, 1062, 1012) -> (1154, 1146, 1062, 1012); objdiff 89.74084; retained True +WADImportEx: remove unused scalar declarations left by earlier translations; insns/diffs/nonregister (1154, 1146, 1062, 1012) -> (1154, 1146, 1062, 1012); objdiff 89.74084; retained True +WADBackupEx: remove unused scalar declarations left by earlier translations; insns/diffs/nonregister (1044, 1062, 1022, 994) -> (1044, 1062, 1022, 994); objdiff 93.373825; retained True +WADImportEx: initial header read keeps requested byte count for returned-count comparison; insns/diffs/nonregister (1154, 1146, 1062, 1012) -> (1156, 1146, 1063, 1023); objdiff 89.47557; retained False +WADImportEx: boot read validates byte count without storing it as import status, then resets successful status; insns/diffs/nonregister (1154, 1146, 1062, 1012) -> (1155, 1146, 1078, 1045); objdiff 90.37522; retained True +WADBackupEx: reuse backup header byte count across initial and per-file hash/write checks; insns/diffs/nonregister (1044, 1062, 1022, 994) -> (1051, 1062, 1013, 980); objdiff 92.655365; retained False +WADImportEx: retranslate first savedata traversal: reset writable header pointer each read and clear successful read status; insns/diffs/nonregister (1155, 1146, 1078, 1045) -> (1156, 1146, 1072, 1034); objdiff 90.59773; retained True +WADImportEx: retranslate final permissions pass with explicit status branches and per-read header reset; insns/diffs/nonregister (1156, 1146, 1072, 1034) -> (1156, 1146, 1071, 1034); objdiff 91.15968; retained True +WADImportEx: clear successful file read/write status and compute file progress relative to its section first; insns/diffs/nonregister (1156, 1146, 1071, 1034) -> (1156, 1146, 1072, 1036); objdiff 92.03316; retained True +Progress after additional block translations: _WADRandPad now 91/91 with ctxdiff zero; WADImportEx 92.03316 after savedata pointer/status/permissions and callback corrections; WADBackupEx 93.373825 after moving the real file owner before other locals. Full gate running before committing these changes. Six instruction-count discrepancies remain; no artificial zero owners, unreachable branch padding, or undefined return values introduced. +WADImportEx: declare first buffer owner before progress and parsed metadata state; insns/diffs/nonregister (1156, 1146, 1072, 1036) -> (1156, 1146, 1072, 1036); objdiff 92.03316; retained True +WADImportEx: declare both buffer owners before progress and parsed metadata state; insns/diffs/nonregister (1156, 1146, 1072, 1036) -> (1156, 1146, 1072, 1036); objdiff 92.1815; retained True +WADBackupEx: initialize saved-file owner after declarations while preserving its early variable lifetime; insns/diffs/nonregister (1044, 1062, 1022, 994) -> (1033, 1062, 1011, 989); objdiff 91.53484; retained False +WADImportEx: represent successful validation directly as zero status; insns/diffs/nonregister (1156, 1146, 1072, 1036) -> (1157, 1146, 1083, 1045); objdiff 91.91536; retained False +WADImportEx: retain direct zero success status despite 92.1815 -> 91.91536 fuzzy change; assigning status from a progress counter obscures its purpose. No matching functions regressed. +WADImportEx: metadata declaration between first and second buffer owners; insns/diffs/nonregister (1157, 1146, 1083, 1045) -> (1157, 1146, 1083, 1045); objdiff 91.91536; retained True +WADImportEx: status declared before progress with buffer owners already first; insns/diffs/nonregister (1157, 1146, 1083, 1045) -> (1157, 1146, 1083, 1045); objdiff 91.91536; retained True +WADImportEx: report-name declaration first followed by both buffer owners; insns/diffs/nonregister (1157, 1146, 1083, 1045) -> (1157, 1146, 1083, 1045); objdiff 91.91536; retained True +Final attempt-count audit: +WADImportGetBlocks: 4 logged attempts; 1192 B; 94.24161 -> 94.22147; source/target 286/298; positional nonregister 260 +WAD_815BFFA8: 10 logged attempts; 248 B; 97.17742 -> 99.354836; source/target 62/62; positional nonregister 0 +WADImportEx: 50 logged attempts; 4584 B; 79.803665 -> 91.91536; source/target 1157/1146; positional nonregister 1045 +WAD_815C1288: 7 logged attempts; 244 B; 96.39344 -> 96.39344; source/target 61/61; positional nonregister 0 +WADBackupEx: 37 logged attempts; 4248 B; 82.283424 -> 93.373825; source/target 1044/1062; positional nonregister 994 +WADOpenStream: 14 logged attempts; 812 B; 98.891624 -> 99.50739; source/target 202/203; positional nonregister 47 +_WADUnpackBackup: 5 logged attempts; 452 B; 97.34513 -> 99.02655; source/target 114/113; positional nonregister 73 +_WADBackupGetFiles: 11 logged attempts; 1268 B; 97.19243 -> 100.0; source/target 317/317; positional nonregister 0 +_WADBackupGetSize: 9 logged attempts; 620 B; 99.90323 -> 99.90323; source/target 155/155; positional nonregister 0 +_WADRandPad: 13 logged attempts; 364 B; 93.78022 -> 100.0; source/target 91/91; positional nonregister 0 +WAD_815C43E0: 10 logged attempts; 248 B; 97.17742 -> 99.354836; source/target 62/62; positional nonregister 0 +_WADHash: 3 logged attempts; 776 B; 98.53093 -> 98.5567; source/target 194/194; positional nonregister 0 +WADImportDVDExForBS: 6 logged attempts; 1052 B; 96.04562 -> 96.17871; source/target 260/263; positional nonregister 233 +data final audit .data: source 458, target 464, overlapping differences 0, target remainder 000000000000; alignments 8/8 +data final audit .sdata: source 64, target 64, overlapping differences 0, target remainder ; alignments 8/8 +Final limitations: WADImportEx 1157/1146 retains metadata and pool-pointer spill differences plus success-store scheduling; WADBackupEx 1044/1062 retains prologue zero ordering, scalar offsets, header address folding and branch shape. WADImportGetBlocks 286/298 lacks two target-only unused allocation owners; WADOpenStream 202/203 lacks target unreachable SD branch; _WADUnpackBackup 114/113 keeps defined success initialization for skipped metadata; WADImportDVDExForBS 260/263 lacks target empty comparison and null/status setup. Remaining five functions are registers-only. Both newly exact functions have ctxdiff zero. Data tail gap remains uncertain; no dummy padding added. +``` + +Full gate: +```text +full build: ok +main.dol sha1: 26116613f624061ba99c8d1a299aaa6efa85670d +[libs/RVL_SDK/src/wad/wad] pool: IDENTICAL +[libs/RVL_SDK/src/wad/wad] objdiff: code 10024/24500 data 64/528 functions 29/40 fuzzy 96.7618 linked code 0 +[libs/RVL_SDK/src/wad/wad] instruction-exact functions: 29/40 +[libs/RVL_SDK/src/wad/wad] section .data size 464 match 99.34924 +[libs/RVL_SDK/src/wad/wad] section .sdata size 64 match 100.0 +[libs/RVL_SDK/src/wad/wad] section .text size 24500 match 96.761795 +[libs/RVL_SDK/src/wad/wad] below 100: WADImportGetBlocks 94.22147 +[libs/RVL_SDK/src/wad/wad] below 100: WAD_815BFFA8 99.354836 +[libs/RVL_SDK/src/wad/wad] below 100: WADImportEx 91.91536 +[libs/RVL_SDK/src/wad/wad] below 100: WAD_815C1288 96.39344 +[libs/RVL_SDK/src/wad/wad] below 100: WADBackupEx 93.373825 +[libs/RVL_SDK/src/wad/wad] below 100: WADOpenStream 99.50739 +[libs/RVL_SDK/src/wad/wad] below 100: _WADUnpackBackup 99.02655 +[libs/RVL_SDK/src/wad/wad] below 100: _WADBackupGetSize 99.90323 +[libs/RVL_SDK/src/wad/wad] below 100: WAD_815C43E0 99.354836 +[libs/RVL_SDK/src/wad/wad] below 100: _WADHash 98.5567 +[libs/RVL_SDK/src/wad/wad] below 100: WADImportDVDExForBS 96.17871 +[libs/RVL_SDK/src/wad/wad] baseline: code 8392/24500 data 64 functions 27 fuzzy 92.2340 +regressions vs baseline: 0 +global matched_code_percent: 85.63862 -> 85.69311 +global fuzzy_match_percent: 98.59054 -> 98.62757 +global complete_code_percent: 59.81479 -> 59.81479 +global matched_data_percent: 90.85581 -> 90.85581 +forbidden patterns added (net, per file): 0 +readability warnings (net, per file; must be 0 in the final result): 0 +review note: libs/RVL_SDK/src/wad/wad.c: possible pointer+offset into a blob (orchestrator reviews) (+4 net), e.g. if ((u32)WADReadStream(&stream, &readBuffer, (bootSize + 0x1F) & ~0x1F, +GATE PASS +``` -- 2.49.1