From 65fcc26610934c9a20458426c27bfb41ebd71c2b Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Wed, 30 Sep 2026 02:55:37 +0000 Subject: [PATCH] NWC24MsgCommit: 15/18 exact (worker fc42b29c) --- README.md | 4 +- libs/RevoEX/src/nwc24/NWC24MsgCommit.c | 154 ++++++++++-------- .../decomp-assist/NWC24MsgCommit.attempts.md | 71 ++++++++ 3 files changed, 159 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index dd1b8c7c..f7bd6703 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ Progress | Decompiled | Matched | Linked | Data | |:---:|:---:|:---:|:---:| -| 84.89% | 75.24% | 56.76% | 86.70% | +| 84.90% | 75.26% | 56.76% | 86.70% | -units 748/1027 complete, functions 10976/12563 matched. +units 748/1027 complete, functions 10978/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/RevoEX/src/nwc24/NWC24MsgCommit.c b/libs/RevoEX/src/nwc24/NWC24MsgCommit.c index 66fb628c..b33d1b2b 100644 --- a/libs/RevoEX/src/nwc24/NWC24MsgCommit.c +++ b/libs/RevoEX/src/nwc24/NWC24MsgCommit.c @@ -34,8 +34,10 @@ NWC24Err NWC24iDateToMinutes(u32* minutes, const NWC24Date* date); NWC24Err NWC24iMinutesToDate(NWC24Date* date, u32 minutes); static inline NWC24Err WriteString(NWC24MsgObjPrivate* msg, char* buffer) { - s32 length = Mail_strlen(buffer); - NWC24Err err = NWC24FWrite(buffer, length, m_pFile); + NWC24Err err; + s32 length; + length = Mail_strlen(buffer); + err = NWC24FWrite(buffer, length, m_pFile); if (err == NWC24_OK) msg->length += length; return err; @@ -81,11 +83,12 @@ static inline NWC24Err WriteSMTPData(NWC24MsgObjPrivate* msg, const char* text) } static inline NWC24Err WriteCmdField(NWC24MsgObjPrivate* msg) { - char* buffer = NWC24WorkP->stringWork; + char* buffer; s32 length; NWC24Err err; if (msg->ledPattern == 0) return NWC24_OK; + buffer = NWC24WorkP->stringWork; Mail_memset(buffer, 0, 1024); Mail_sprintf(buffer, "X-Wii-Cmd: %08X\r\n", msg->ledPattern); length = Mail_strlen(buffer); @@ -96,11 +99,12 @@ static inline NWC24Err WriteCmdField(NWC24MsgObjPrivate* msg) { } static inline NWC24Err WriteTagField(NWC24MsgObjPrivate* msg) { - char* buffer = NWC24WorkP->stringWork; + char* buffer; s32 length; NWC24Err err; if (msg->tag == 0) return NWC24_OK; + buffer = NWC24WorkP->stringWork; Mail_memset(buffer, 0, 1024); Mail_sprintf(buffer, "X-Wii-Tag: %08X\r\n", msg->tag); length = Mail_strlen(buffer); @@ -111,11 +115,12 @@ static inline NWC24Err WriteTagField(NWC24MsgObjPrivate* msg) { } static inline NWC24Err WriteDWCIdField(NWC24MsgObjPrivate* msg) { - char* buffer = NWC24WorkP->stringWork; + char* buffer; s32 length; NWC24Err err; if (!(msg->type & 0x2000)) return NWC24_OK; + buffer = NWC24WorkP->stringWork; Mail_memset(buffer, 0, 1024); Mail_sprintf(buffer, "X-Wii-DWCId: %08X\r\n", msg->dwcId); length = Mail_strlen(buffer); @@ -126,11 +131,12 @@ static inline NWC24Err WriteDWCIdField(NWC24MsgObjPrivate* msg) { } static inline NWC24Err WriteIconNewField(NWC24MsgObjPrivate* msg) { - char* buffer = NWC24WorkP->stringWork; + char* buffer; s32 length; NWC24Err err; if (msg->iconNew == 0 || msg->iconNew == 0x80000000) return NWC24_OK; + buffer = NWC24WorkP->stringWork; Mail_memset(buffer, 0, 1024); Mail_sprintf(buffer, "X-Wii-IconNew: %08X\r\n", msg->iconNew); length = Mail_strlen(buffer); @@ -145,8 +151,8 @@ static inline NWC24Err WriteMessageIdField(NWC24MsgObjPrivate* msg) { const char* domain; NWC24UserId myId; char idText[32]; - s32 length; NWC24Err err; + s32 length; domain = NWC24GetAccountDomain(); NWC24GetMyUserId(&myId); Mail_memset(buffer, 0, 1024); @@ -179,6 +185,65 @@ NWC24Err NWC24CommitMsg(NWC24MsgObj* object) { return NWC24CommitMsgInternal(msg, loopback); } +static inline NWC24Err WriteMBNoReplyField(NWC24MsgObjPrivate* msg) { + u32 flags = msg->msgBoardFlags.raw & 0x80000000; + char* buffer; + s32 length; + NWC24Err err; + if (!flags) + return NWC24_OK; + buffer = NWC24WorkP->stringWork; + Mail_memset(buffer, 0, 1024); + Mail_strcpy(buffer, "X-Wii-MB-NoReply: -\r\n"); + length = 21; + err = NWC24FWrite(buffer, length, m_pFile); + if (err == NWC24_OK) + msg->length += length; + return err; +} + +static inline NWC24Err WriteMBRegDateField(NWC24MsgObjPrivate* msg, u32 flags) { + NWC24Err err; + s32 length; + char* buffer; + if (!flags) + return NWC24_OK; + buffer = NWC24WorkP->stringWork; + Mail_memset(buffer, 0, 1024); + Mail_sprintf(buffer, "X-Wii-MB-RegDate: %04X\r\n", flags); + length = STD_strnlen(buffer, 1024); + err = NWC24FWrite(buffer, length, m_pFile); + if (err == NWC24_OK) + msg->length += length; + return err; +} + +static inline NWC24Err WriteMBDelayField(NWC24MsgObjPrivate* msg, u32 flags) { + NWC24Err err; + s32 length; + char* buffer; + if (!flags) + return NWC24_OK; + buffer = NWC24WorkP->stringWork; + Mail_memset(buffer, 0, 1024); + Mail_sprintf(buffer, "X-Wii-MB-Delay: %02X\r\n", flags >> 16); + length = STD_strnlen(buffer, 1024); + err = NWC24FWrite(buffer, length, m_pFile); + if (err == NWC24_OK) + msg->length += length; + return err; +} + +static inline NWC24Err WriteExtraFields(NWC24MsgObjPrivate* msg) { + NWC24Err err; + if (msg->unk_0xD0.size == 0) + return NWC24_OK; + err = NWC24FWrite(msg->unk_0xD0.ptr, msg->unk_0xD0.size, m_pFile); + if (err == NWC24_OK) + msg->length += msg->unk_0xD0.size; + return err; +} + static NWC24Err NWC24CommitMsgInternal(NWC24MsgObjPrivate* msg, NWC24MBoxType type) { NWC24File file; NWC24Data attached[2]; @@ -188,7 +253,6 @@ static NWC24Err NWC24CommitMsgInternal(NWC24MsgObjPrivate* msg, NWC24MBoxType ty s32 initIndex; u32 index; char* buffer; - s32 length; NWC24Err result, err; NWC24Data_Init(&subject); NWC24Data_Init(&text); @@ -241,59 +305,12 @@ static NWC24Err NWC24CommitMsgInternal(NWC24MsgObjPrivate* msg, NWC24MBoxType ty CHECK_WRITE(WriteXWiiFaceField(msg)); CHECK_WRITE(WriteIconNewField(msg)); if (msg->msgBoardFlags.raw != 0) { - if (msg->msgBoardFlags.raw & 0x80000000) { - buffer = NWC24WorkP->stringWork; - Mail_memset(buffer, 0, 1024); - Mail_strcpy(buffer, "X-Wii-MB-NoReply: -\r\n"); - err = NWC24FWrite(buffer, 21, m_pFile); - if (err == NWC24_OK) - msg->length += 21; - if (err != NWC24_OK) { - NWC24FClose(&file); - result = err; - goto finish; - } - } - if (msg->msgBoardFlags.raw & 0xFFFF) { - buffer = NWC24WorkP->stringWork; - Mail_memset(buffer, 0, 1024); - Mail_sprintf(buffer, "X-Wii-MB-RegDate: %04X\r\n", msg->msgBoardFlags.raw & 0xFFFF); - length = STD_strnlen(buffer, 1024); - err = NWC24FWrite(buffer, length, m_pFile); - if (err == NWC24_OK) - msg->length += length; - if (err != NWC24_OK) { - NWC24FClose(&file); - result = err; - goto finish; - } - } - if (msg->msgBoardFlags.raw & 0xFF0000) { - buffer = NWC24WorkP->stringWork; - Mail_memset(buffer, 0, 1024); - Mail_sprintf(buffer, "X-Wii-MB-Delay: %02X\r\n", (msg->msgBoardFlags.raw >> 16) & 0xFF); - length = STD_strnlen(buffer, 1024); - err = NWC24FWrite(buffer, length, m_pFile); - if (err == NWC24_OK) - msg->length += length; - if (err != NWC24_OK) { - NWC24FClose(&file); - result = err; - goto finish; - } - } - } - } - if (msg->unk_0xD0.size != 0) { - err = NWC24FWrite(msg->unk_0xD0.ptr, msg->unk_0xD0.size, m_pFile); - if (err == NWC24_OK) - msg->length += msg->unk_0xD0.size; - if (err != NWC24_OK) { - NWC24FClose(&file); - result = err; - goto finish; + CHECK_WRITE(WriteMBNoReplyField(msg)); + CHECK_WRITE(WriteMBRegDateField(msg, msg->msgBoardFlags.raw & 0xFFFF)); + CHECK_WRITE(WriteMBDelayField(msg, msg->msgBoardFlags.raw & 0xFF0000)); } } + CHECK_WRITE(WriteExtraFields(msg)); Mail_memset(MultiPartDivider, 0, 40); Mail_sprintf(MultiPartDivider, "Boundary-NWC24-%08X%05X", msg->unk_0x28, msg->msgId); buffer = NWC24WorkP->stringWork; @@ -335,14 +352,15 @@ static NWC24Err NWC24CommitMsgInternal(NWC24MsgObjPrivate* msg, NWC24MBoxType ty CHECK_WRITE(WriteMIMEAttachHeader(msg, index)); attached[index].ptr = (const void*)msg->length; err = WriteBase64Data(msg->attached[index].ptr, msg->attached[index].size, &encodedLength); - msg->length += encodedLength; + if (err == NWC24_OK) + msg->length += encodedLength; if (err != NWC24_OK) { NWC24FClose(&file); result = err; goto finish; } attached[index].size = msg->length - (u32)attached[index].ptr; - if (index == msg->numAttached - 1) { + if ((s32)index == msg->numAttached - 1) { buffer = NWC24WorkP->stringWork; Mail_memset(buffer, 0, 1024); Mail_sprintf(buffer, "\r\n--%s", MultiPartDivider); @@ -576,14 +594,13 @@ static NWC24Err WriteFromField(NWC24MsgObjPrivate* msg) { } static NWC24Err WriteToField(NWC24MsgObjPrivate* msg) { - char* buffer = NWC24WorkP->stringWork; - char* cursor; + char* cursor = NWC24WorkP->stringWork; s32 index, total, remaining; u32 length; NWC24Err err = NWC24_OK; - Mail_memset(buffer, 0, 1024); - Mail_strncat(buffer, "To: ", 1022); - cursor = buffer + 4; + Mail_memset(cursor, 0, 1024); + Mail_strncat(cursor, "To: ", 1022); + cursor += 4; index = 0; total = 4; msg->unk_0x38.ptr = (const void*)(msg->length + 4); @@ -755,7 +772,8 @@ static NWC24Err WriteMIMEAttachHeader(NWC24MsgObjPrivate* msg, u32 index) { dispositionLength = Mail_strlen(ContentDispA); typeLength += encodingLength; mimeLength = Mail_strlen(mime); - if (mimeLength + (dispositionLength + typeLength) + 4 >= 1024) + mimeLength = (dispositionLength + typeLength) + mimeLength + 4; + if (mimeLength >= 1024) return NWC24_ERR_NOMEM; first = Mail_sprintf(buffer, (char*)ContentTypeA, mime, (char)('a' + index), msg->msgId, suffix); if (first <= 0) @@ -879,8 +897,8 @@ static NWC24Err WriteBase64Data(const u8* data, s32 size, u32* totalLength) { static NWC24Err WriteQPData(const u8* data, s32 size, u32* totalLength) { char* buffer = NWC24WorkP->stringWork; const u8* cursor = data; - s32 total = 0; s32 remaining = size; + s32 total = 0; u32 consumed, length; NWC24Err err = NWC24_OK; for (; remaining > 0; remaining -= consumed) { diff --git a/tools/decomp-assist/NWC24MsgCommit.attempts.md b/tools/decomp-assist/NWC24MsgCommit.attempts.md index f5383192..52c0099d 100644 --- a/tools/decomp-assist/NWC24MsgCommit.attempts.md +++ b/tools/decomp-assist/NWC24MsgCommit.attempts.md @@ -97,3 +97,74 @@ The source .sdata is 140 bytes versus the target's 152 bytes, including trailing section alignment. No extra object, packed string blob, alignment directive, symbol-size change, or placement trick was introduced to cover these gaps. Total matched data is 72/904 bytes. These data-layout differences remain open. + +## Continuation round: merged baseline + +Baseline: 11/18 instruction-exact by ctxdiff/gate, 13/18 objdiff-exact, +3364/8532 matched code bytes, 72/904 matched data bytes; pool identical. + +WriteToField now matches 100%, 90/90 instructions, zero differences. Reusing +one cursor for buffer initialization and recipient writing resolves the four +initial buffer register differences. WriteQPData now matches 100%, 60/60 +instructions, zero differences: initializing remaining before total resolves +the nine callee-saved register differences. + +### Remaining functions: new attempts + +- WriteMIMEAttachHeader: materializing the aggregate template count before the + MIME-name call gives 92.82609%, 91/92 instructions. Sequential MIME-count + additions give 99.61957%, 92/92, six differences. Reusing total for the + template sum gives 97.065216%, thirteen differences. Reusing typeLength or + mimeLength for the first formatted count leaves six differences. Aggregating + all template counts before the MIME call gives 92.88043%, 91/92. Reversing + the template operands gives 99.565216%. Assigning the complete size with + the template subtotal first retains 99.61957% and reduces the differences + to five; this is retained. Reverse declarations, accumulating into + typeLength, and accumulating into dispositionLength do not improve it. + All five surviving differences concern the template subtotal's register + allocation and addition order; the instruction count remains 92/92. +- NWC24CommitMsgInternal: explicit success initialization for absent board + fields leaves 94.72672%, 818/827. Caching masked registration/delay values + gives 95.272064%, 816/827. Assigning the open status directly to result + gives 94.351875%, 817/827. Loading optional command/tag/DWC/icon buffers after + their predicates gives 96.60217%, 816/827. Separate inline message-board + writers give 98.16203%, 822/827. Crediting attachment bytes only on successful + encoding gives 99.0387%, 825/827. An inline optional extra-header writer + restores 827/827 and gives 99.546555%, 64 differences. Reordering helper + count/buffer/status declarations gives 99.78839%, 24 differences. Casting + the final attachment-index comparison to signed while keeping the scan + unsigned gives 99.86095%, 23 differences. Declaring message-ID status before + its count gives 99.891174%, eighteen differences. Passing board masks into + the formatting helpers, reversing those arguments, signed masked values, + an unsigned message-ID count, and a scoped + plain-text status do not resolve the remaining allocation differences. + A separate multipart-buffer scope experiment failed compilation and was + discarded. Final cleanup removes the unused outer count and restores + consistent message-first arguments and signed counts without changing + the retained output. Remaining eighteen differences are register choices + in the registration/delay field writers and plain-text error handling. +- CheckMsgBoxSpace: grouping base64 line overhead leaves 85.57399%, 223/223, + 75 differences. An unsigned attachment index gives 78.57399%, 214/223. + Returning the mailbox status directly gives 83.33184%, 218/223. All three + were discarded. The remaining differences are allocation/scheduling in + the eight-way unrolled size estimate; the original signed scan and final + unsigned capacity comparison are retained. + +### Data: new attempts + +A named const subject line-ending array is still pooled by MWCC and leaves +.data at 678 bytes, .sdata at 140, and matched data at 72/904. Mutable month +string pointers produce the same layout and score. Making the month pointer +array const moves its 48 bytes to .rodata and shrinks .data to 630, without +improving matched data. All three experiments were discarded. String order +remains identical. The previously recorded scalar-size, duplicate-CRLF, +month relocation, symbol-grouping, and trailing-alignment differences remain +unresolved; no padding, forced placement, or artificial data was added. + +### Retained result + +15/18 instruction-exact and objdiff-exact functions, 3964/8532 matched code +bytes, 72/904 matched data bytes. Restoring the internal function's target +size also makes the two previously misnormalized cr1 functions compare at +zero differences. Every remaining function has at least three distinct +source-level attempts recorded above for this continuation. -- 2.49.1