From f9cb9f91cf31f368dd5e5095008858dd5da2ac4d Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 21:06:43 +0000 Subject: [PATCH] Decompile NHTTP_thread to 23/26 functions (gpt-6.1-sol low) (worker 64796af8) --- libs/RevoEX/src/nhttp/NHTTP_thread.c | 1218 ++++++++++++++++++ tools/decomp-assist/NHTTP_thread.attempts.md | 93 ++ 2 files changed, 1311 insertions(+) create mode 100644 libs/RevoEX/src/nhttp/NHTTP_thread.c create mode 100644 tools/decomp-assist/NHTTP_thread.attempts.md diff --git a/libs/RevoEX/src/nhttp/NHTTP_thread.c b/libs/RevoEX/src/nhttp/NHTTP_thread.c new file mode 100644 index 00000000..0750263d --- /dev/null +++ b/libs/RevoEX/src/nhttp/NHTTP_thread.c @@ -0,0 +1,1218 @@ +#include +#include + +#pragma auto_inline off + +static const char STR_POST_DISPOS[] = "Content-Disposition: form-data; name=\""; +static const char STR_POST_TYPE_BIN[] = "Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n"; +static const char STR_POST_TYPE_URLENCODE[] = "Content-Type: application/x-www-form-urlencoded\r\n"; +static const char STR_POST_TYPE_MULTIPART[] = "Content-Type: multipart/form-data; boundary="; + +typedef struct NHTTPThreadInfo +{ + OSMessageQueue messageQueue; + OSMessage messages[3]; + OSThread thread; + BOOL isCreateCommThreadMessageQueue; + u8 reserved[0x14]; + char commBuf[0x100]; +} NHTTPThreadInfo; + +struct NHTTPBgnEndInfo +{ + NCDIpConfig ipConfig; + NHTTPAlloc alloc; + NHTTPFree free; + BOOL started; + s32 socket; + s32 sslError; + NHTTPErr error; + BOOL stopping; + void* threadStack; + u32 reserved; +}; + +typedef struct NHTTPThreadContext +{ + s32 requestId; + char hostname[0x100]; + char discard[0x200]; + char statusLine[0x10]; + u32 address; + u32 previousAddress; + s32 port; + s32 previousPort; + s32 sendLength; + s32 recvLength; + s32 contentLength; + NHTTPErr error; + BOOL retry; + BOOL keepAlive; + BOOL chunked; +} NHTTPThreadContext; + +void* NHTTPi_GetSystemInfoP(void); +NHTTPBgnEndInfo* NHTTPi_GetBgnEndInfoP(void* system); +NHTTPThreadInfo* NHTTPi_GetThreadInfoP(void* system); +NHTTPReqInfo* NHTTPi_GetReqInfoP(void* system); +NHTTPListInfo* NHTTPi_GetListInfoP(void* system); +void* NHTTPi_GetMutexInfoP(void* system); +void NHTTPi_lockReqList(void* mutex); +void NHTTPi_unlockReqList(void* mutex); +void NHTTPi_idleCommThread(NHTTPThreadInfo* thread); +void* NHTTPi_alloc(u32 size, s32 alignment); +void NHTTPi_free(void* buffer); +void* NHTTPi_memcpy(void* destination, const void* source, u32 size); +void* NHTTPi_memclr(void* destination, u32 size); +s32 NHTTPi_strlen(const char* text); +s32 NHTTPi_strcmp(const char* first, const char* second); +s32 NHTTPi_strnicmp(const char* first, const char* second, s32 length); +s32 NHTTPi_intToStr(char* destination, s32 number); +s32 NHTTPi_strToInt(const char* text, s32 length); +s32 NHTTPi_strToHex(const char* text, s32 length); +s32 NHTTPi_encodeUrlChar(char* destination, char character); +s32 NHTTPi_getUrlEncodedSize(const char* text); +s32 NHTTPi_getUrlEncodedSize2(const char* text, u32 length); +NHTTPConnectionInfo* NHTTPi_Request2Connection(void* mutex, NHTTPRequestInfo* request); +NHTTPConnectionInfo* NHTTPi_Response2Connection(void* mutex, NHTTPResponseInfo* response); +s32 NHTTPi_PostSendCallback(void* mutex, NHTTPConnectionInfo* connection, const char* label, s32 offset); +s32 NHTTPi_BufferFullCallback(void* mutex, NHTTPConnectionInfo* connection); +s32 NHTTPi_CompleteCallback(void* mutex, NHTTPConnectionInfo* connection); +s32 NHTTPi_ReceivedCallback(void* mutex, NHTTPConnectionInfo* connection); +void NHTTPi_NotifyCompletion(NHTTPConnectionInfo* connection); +void NHTTPi_SetVirtualContentLength(NHTTPConnectionInfo* connection, u32 length); +BOOL NHTTPi_isRecvBufFull(NHTTPResponseInfo* response, s32 offset); +s32 NHTTPi_SocSend(NHTTPRequestInfo* request, s32 socket, const void* buffer, s32 length, s32 flags); +s32 NHTTPi_SocRecv(void* mutex, NHTTPRequestInfo* request, s32 socket, void* buffer, s32 length, s32 flags); +s32 NHTTPi_SocClose(void* mutex, NHTTPRequestInfo* request, s32 socket); +s32 NHTTPi_SocOpen(NHTTPRequestInfo* request); +s32 NHTTPi_SocConnect(NHTTPBgnEndInfo* info, void* mutex, NHTTPRequestInfo* request, s32 socket, u32 address, u32 port); +s32 NHTTPi_SocSSLConnect(NHTTPBgnEndInfo* info, void* mutex, NHTTPRequestInfo* request, s32 socket); +u32 NHTTPi_resolveHostname(NHTTPRequestInfo* request, const char* host); +s32 NHTTPi_GetSSLError(NHTTPBgnEndInfo* info); +void NHTTPi_SetError(NHTTPBgnEndInfo* info, NHTTPErr error); +NHTTPReqQueue* NHTTPi_getReqFromReqQueue(NHTTPListInfo* list); +NHTTPHeader* NHTTPi_getHdrFromList(NHTTPHeader** list); +void NHTTPi_destroyRequestObject(void* mutex, NHTTPRequestInfo* request); +s32 NHTTPi_loadFromHdrRecvBuf(NHTTPResponseInfo* response, char* destination, s32 offset, s32 length); +s32 NHTTPi_findNextLineHdrRecvBuf(const NHTTPResponseInfo* response, s32 offset, s32 end, s32* length, s32* returnCodeSize); +s32 NHTTPi_getHeaderValue(const NHTTPResponseInfo* response, const char* label, s32* offset); +s32 NHTTPi_compareTokenN_HdrRecvBuf(const NHTTPResponseInfo* response, s32 start, s32 end, const char* token, char delimiter); +s32 NHTTPi_RecvBuf(void* mutex, NHTTPRequestInfo* request, s32 socket, s32 offset, s32 flags); +s32 NHTTPi_RecvBufN(void* mutex, NHTTPRequestInfo* request, s32 socket, s32 offset, s32 length, s32 flags); + +void NHTTPi_InitThreadInfo(NHTTPThreadInfo* info) +{ + info->isCreateCommThreadMessageQueue = FALSE; +} + +void NHTTPi_IsCreateCommThreadMessageQueueOn(NHTTPThreadInfo* info) +{ + info->isCreateCommThreadMessageQueue = TRUE; +} + +BOOL NHTTPi_IsCreateCommThreadMessageQueue(NHTTPThreadInfo* info) +{ + return info->isCreateCommThreadMessageQueue; +} + +BOOL NHTTP_GetProgress(void* system, u32* complete, u32* total) +{ + void* mutex = NHTTPi_GetMutexInfoP(system); + NHTTPReqQueue* queue = NHTTPi_GetReqInfoP(system)->reqQueue; + BOOL active; + *complete = 0; + *total = 0; + NHTTPi_lockReqList(mutex); + if (queue != NULL) + { + if (queue->request->response->contentLength != 0) + { + *complete = queue->request->response->bodyLen; + *total = queue->request->response->contentLength == (u32)-1 ? 0 : queue->request->response->contentLength; + } + active = TRUE; + } + else + { + active = NHTTPi_GetListInfoP(system)->reqQueue != NULL; + } + NHTTPi_unlockReqList(mutex); + return active; +} + +static BOOL NHTTPi_CheckHeaderEnd(const char* recent, s32 length) +{ + u8 before = recent[(length - 2) & 3]; + if ((char)before == '\r' && recent[(length - 1) & 3] == '\r') return TRUE; + if ((char)before == '\n' && recent[(length - 1) & 3] == '\n') return TRUE; + if (recent[(length - 4) & 3] == '\r' && recent[(length - 3) & 3] == '\n' + && (char)before == '\r' && recent[(length - 1) & 3] == '\n') return TRUE; + return FALSE; +} + +static s32 NHTTPi_SaveBuf(NHTTPRequestInfo* request, char* buffer, s32 socket, + s32* used, const char* data, s32 length) +{ + s32 amount; + s32 remaining = length; + while (remaining > 0) + { + if (request->cancel) return -1; + amount = remaining; + if (amount > 0x100 - *used) amount = 0x100 - *used; + NHTTPi_memcpy(buffer + *used, data, amount); + data += amount; + remaining -= amount; + *used += amount; + if (*used == 0x100) + { + s32 sent = NHTTPi_SocSend(request, socket, buffer, 0x100, 0); + if (sent <= 0) return sent; + *used -= sent; + } + } + return length; +} + +static BOOL NHTTPi_GetPostContentlength(void* mutex, NHTTPRequestInfo* request, + const char* label, s32* length, s32 encoding) +{ + s32 offset = 0; + NHTTPConnectionInfo* connection = NHTTPi_Request2Connection(mutex, request); + if (connection == NULL) return FALSE; + connection->_unk24 = 0; + for (;;) + { + s32 size; + const char* data; + if (request->cancel) return FALSE; + connection->_unk28 = 0; + if (NHTTPi_PostSendCallback(mutex, connection, label, offset) < 0) return FALSE; + size = connection->_unk28; + data = (const char*)connection->_unk24; + if (size == 0) break; + if (data == NULL) return FALSE; + offset += size; + switch (encoding) + { + case 0: + case 1: *length += size; break; + case 2: *length += NHTTPi_getUrlEncodedSize2(data, size); break; + } + } + return TRUE; +} + +static s32 NHTTPi_SendPostData(void* mutex, NHTTPRequestInfo* request, char* buffer, + const char* label, s32 socket, s32* used, s32 encoding) +{ + s32 offset = 0; + u32 size; + u32 index; + NHTTPConnectionInfo* connection; + const char* data; + s32 result; + connection = NHTTPi_Request2Connection(mutex, request); + if (connection == NULL) return 3; + connection->_unk24 = 0; + for (;;) + { + if (request->cancel) return 3; + connection->_unk28 = 0; + if (NHTTPi_PostSendCallback(mutex, connection, label, offset) < 0) return 3; + size = connection->_unk28; + data = (const char*)connection->_unk24; + if (size == 0) break; + if (data == NULL) return 3; + offset += size; + switch (encoding) + { + case 0: + case 1: + result = NHTTPi_SaveBuf(request, buffer, socket, used, data, size); + if (result < 0) return 1; + if (result == 0) return 2; + break; + case 2: + { + + for (index = 0; index < size; ++index) + { + char encoded[3]; + s32 count; + NHTTPi_memclr(encoded, 3); + count = NHTTPi_encodeUrlChar(encoded, data[index]); + result = NHTTPi_SaveBuf(request, buffer, socket, used, encoded, count); + if (result < 0) return 1; + if (result == 0) return 2; + } + break; + } + } + } + return 0; +} + +static BOOL NHTTPi_BufFull(void* mutex, NHTTPResponseInfo* response) +{ + BOOL result = FALSE; + BOOL full = NHTTPi_isRecvBufFull(response, response->bodyLen); + if (response->recvBufLen == 0 || response->recvBuf_p == NULL || full) + { + NHTTPConnectionInfo* connection = NHTTPi_Response2Connection(mutex, response); + if (connection != NULL) + { + NHTTPi_BufferFullCallback(mutex, connection); + if (response->recvBuf_p != NULL && response->recvBufLen != 0 + && !NHTTPi_isRecvBufFull(response, response->bodyLen)) result = TRUE; + } + } + else if (!full) result = TRUE; + return result; +} + +static s32 NHTTPi_SendData(NHTTPThreadContext* context, const char* data, s32 length) +{ + void* system = NHTTPi_GetSystemInfoP(); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + NHTTPThreadInfo* thread = NHTTPi_GetThreadInfoP(system); + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(system)->reqQueue->request; + char* buffer = thread->commBuf; + s32 result; + if (length == 0) return 0; + result = NHTTPi_SaveBuf(request, buffer, + info->socket, &context->sendLength, data, length); + if (result < 0) return 1; + if (result == 0) return 2; + return 0; +} + +static inline s32 NHTTPi_SendProxyAuthorization(NHTTPThreadContext* context) +{ + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(NHTTPi_GetSystemInfoP())->reqQueue->request; + s32 result; + if (request->proxyAuthorizationLength == 0) return 0; + result = NHTTPi_SendData(context, "Proxy-Authorization: Basic ", 27); + if (result != 0) return result; + result = NHTTPi_SendData(context, request->proxyAuthorization, request->proxyAuthorizationLength); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + return 0; +} + +static s32 NHTTPi_SendProxyConnectMethod(NHTTPThreadContext* context) +{ + void* system = NHTTPi_GetSystemInfoP(); + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(system)->reqQueue->request; + NHTTPThreadInfo* thread = NHTTPi_GetThreadInfoP(system); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + char* buffer = thread->commBuf; + char port[12]; + s32 length = NHTTPi_intToStr(port, request->port); + s32 result; + result = NHTTPi_SendData(context, "CONNECT ", 8); + if (result != 0) return result; + result = NHTTPi_SendData(context, request->url + 8, request->hostEnd - 8); + if (result != 0) return result; + result = NHTTPi_SendData(context, ":", 1); + if (result != 0) return result; + result = NHTTPi_SendData(context, port, length); + if (result != 0) return result; + result = NHTTPi_SendData(context, " HTTP/1.1\r\n", 11); + if (result != 0) return result; + result = NHTTPi_SendData(context, "Host: ", 6); + if (result != 0) return result; + result = NHTTPi_SendData(context, request->url + 8, request->hostEnd - 8); + if (result != 0) return result; + result = NHTTPi_SendData(context, ":", 1); + if (result != 0) return result; + result = NHTTPi_SendData(context, port, length); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + result = NHTTPi_SendData(context, "Content-Length: 0\r\nPragma: no-cache\r\n", 37); + if (result != 0) return result; + result = NHTTPi_SendProxyAuthorization(context); + if (result != 0) return result; + NHTTPi_SendData(context, "\r\n", 2); + if (context->sendLength > 0) + { + s32 sent = NHTTPi_SocSend(request, info->socket, buffer, context->sendLength, 0); + if (sent < 0) return 1; + if (sent == 0) return 2; + } + context->sendLength = 0; + NHTTPi_memclr(buffer, 0x100); + return 0; +} + +static BOOL NHTTPi_RecvProxyConnectHeader(NHTTPThreadContext* context) +{ + void* system = NHTTPi_GetSystemInfoP(); + NHTTPReqInfo* requests = NHTTPi_GetReqInfoP(system); + NHTTPThreadInfo* thread = NHTTPi_GetThreadInfoP(system); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + void* mutex = NHTTPi_GetMutexInfoP(system); + char* buffer = thread->commBuf; + NHTTPRequestInfo* request = requests->reqQueue->request; + NHTTPResponseInfo* response = request->response; + BOOL accepted = FALSE; + s32 used = 0; + char header[0x200]; + for (;;) + { + s32 received = NHTTPi_SocRecv(mutex, request, info->socket, header + used, 0x200 - used, 0); + char* cursor; + s32 index; + BOOL ended; + used += received; + response->httpStatus = NHTTPi_strToInt(header + 9, 3); + if (NHTTPi_strnicmp(header, "HTTP/", 5) == 0 && header[8] == ' ' + && response->httpStatus == 200) accepted = TRUE; + ended = FALSE; + cursor = header; + index = 0; + while (index < used) + { + if (index > 1 && cursor[-1] == '\r' && *cursor == '\r') ended = TRUE; + else if (index > 1 && cursor[-1] == '\n' && *cursor == '\n') ended = TRUE; + else if (index > 3 && cursor[-3] == '\r' && cursor[-2] == '\n' + && cursor[-1] == '\r' && *cursor == '\n') ended = TRUE; + ++cursor; + ++index; + } + if (ended) return accepted ? TRUE : FALSE; + if (received < 0) return FALSE; + if (used >= 0x200) + { + received = NHTTPi_SocRecv(mutex, request, info->socket, buffer, 1, 0); + if (received < 0) return FALSE; + if (received != 0) return FALSE; + } + } +} + +static s32 NHTTPi_SendHeaderList(NHTTPThreadContext* context) +{ + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(NHTTPi_GetSystemInfoP())->reqQueue->request; + NHTTPHeader* header = NHTTPi_getHdrFromList(&request->headers); + while (header != NULL) + { + s32 result = NHTTPi_SendData(context, header->name, NHTTPi_strlen(header->name)); + if (result != 0) return result; + result = NHTTPi_SendData(context, ": ", 2); + if (result != 0) return result; + result = NHTTPi_SendData(context, header->value, NHTTPi_strlen(header->value)); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + NHTTPi_free(header); + header = NHTTPi_getHdrFromList(&request->headers); + } + return 0; +} + +static s32 NHTTPi_SendProcPostDataRaw(NHTTPThreadContext* context) +{ + void* system = NHTTPi_GetSystemInfoP(); + void* mutex = NHTTPi_GetMutexInfoP(system); + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(system)->reqQueue->request; + NHTTPThreadInfo* thread = NHTTPi_GetThreadInfoP(system); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + char* buffer = thread->commBuf; + s32 length = 0; + char number[12]; + s32 count; + s32 result; + if (request->postBuffer == NULL) + { + if (!NHTTPi_GetPostContentlength(mutex, request, NULL, &length, 0)) return 3; + } + else length = request->postBufferSize; + count = NHTTPi_intToStr(number, length); + result = NHTTPi_SendData(context, "Content-Length: ", 16); + if (result != 0) return result; + result = NHTTPi_SendData(context, number, count); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + if (request->postBuffer == NULL) + { + result = NHTTPi_SendPostData(mutex, request, buffer, NULL, info->socket, &context->sendLength, 0); + if (result != 0) return result; + } + else + { + result = NHTTPi_SendData(context, request->postBuffer, request->postBufferSize); + if (result != 0) return result; + } + return 0; +} + +static s32 NHTTPi_SendProcPostDataBinary(NHTTPThreadContext* context) +{ + void* system = NHTTPi_GetSystemInfoP(); + void* mutex = NHTTPi_GetMutexInfoP(system); + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(system)->reqQueue->request; + NHTTPThreadInfo* thread = NHTTPi_GetThreadInfoP(system); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + NHTTPHeader* header; + char* buffer = thread->commBuf; + s32 length = 0; + char number[12]; + s32 count; + s32 result; + for (header = request->postData; header != NULL; header = header->prev) + { + length += 22; + length += NHTTPi_strlen(header->name) + 41; + if (header->_unk14 != 0) length += 75; + length += 2; + if (header->value == NULL) + { + if (!NHTTPi_GetPostContentlength(mutex, request, header->name, &length, 1)) return 3; + } + else length += header->length; + length += 2; + if (header == request->postData->next) break; + } + length += 24; + count = NHTTPi_intToStr(number, length); + result = NHTTPi_SendData(context, STR_POST_TYPE_MULTIPART, 44); + if (result != 0) return result; + result = NHTTPi_SendData(context, request->multipartBoundary + 2, 18); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + result = NHTTPi_SendData(context, "Content-Length: ", 16); + if (result != 0) return result; + result = NHTTPi_SendData(context, number, count); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + for (header = request->postData; header != NULL; header = header->prev) + { + result = NHTTPi_SendData(context, request->multipartBoundary, 20); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + result = NHTTPi_SendData(context, STR_POST_DISPOS, 38); + if (result != 0) return result; + result = NHTTPi_SendData(context, header->name, NHTTPi_strlen(header->name)); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\"\r\n", 3); + if (result != 0) return result; + if (header->_unk14 != 0) + { + result = NHTTPi_SendData(context, STR_POST_TYPE_BIN, 75); + if (result != 0) return result; + } + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + if (header->value == NULL) + { + result = NHTTPi_SendPostData(mutex, request, buffer, header->name, info->socket, &context->sendLength, 1); + if (result != 0) return result; + } + else + { + result = NHTTPi_SendData(context, header->value, header->length); + if (result != 0) return result; + } + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + if (header == request->postData->next) break; + } + result = NHTTPi_SendData(context, request->multipartBoundary, 20); + if (result != 0) return result; + result = NHTTPi_SendData(context, "--\r\n", 4); + if (result != 0) return result; + return 0; +} + +static s32 NHTTPi_SendProcPostDataAscii(NHTTPThreadContext* context) +{ + void* system = NHTTPi_GetSystemInfoP(); + void* mutex = NHTTPi_GetMutexInfoP(system); + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(system)->reqQueue->request; + NHTTPThreadInfo* thread = NHTTPi_GetThreadInfoP(system); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + NHTTPHeader* header; + char* buffer = thread->commBuf; + s32 length = 0; + char number[12]; + s32 count; + s32 result; + for (header = request->postData; header != NULL; header = header->prev) + { + length = length + NHTTPi_getUrlEncodedSize(header->name); + ++length; + if (header->value == NULL) + { + if (!NHTTPi_GetPostContentlength(mutex, request, header->name, &length, 2)) return 3; + } + else length += NHTTPi_getUrlEncodedSize(header->value); + if (header == request->postData->next) break; + ++length; + } + count = NHTTPi_intToStr(number, length); + result = NHTTPi_SendData(context, STR_POST_TYPE_URLENCODE, 49); + if (result != 0) return result; + result = NHTTPi_SendData(context, "Content-Length: ", 16); + if (result != 0) return result; + result = NHTTPi_SendData(context, number, count); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + for (header = request->postData; header != NULL; header = header->prev) + { + s32 index; + char character; + for (index = 0; (character = header->name[index]) != 0; ++index) + { + count = NHTTPi_encodeUrlChar(number, character); + result = NHTTPi_SendData(context, number, count); + if (result != 0) return result; + } + result = NHTTPi_SendData(context, "=", 1); + if (result != 0) return result; + if (header->value == NULL) + { + result = NHTTPi_SendPostData(mutex, request, buffer, header->name, info->socket, &context->sendLength, 2); + if (result != 0) return result; + } + else + { + for (index = 0; (character = header->value[index]) != 0; ++index) + { + count = NHTTPi_encodeUrlChar(number, character); + result = NHTTPi_SendData(context, number, count); + if (result != 0) return result; + } + } + if (header == request->postData->next) break; + result = NHTTPi_SendData(context, "&", 1); + if (result != 0) return result; + } + return 0; +} + +static void NHTTPi_ThreadReqEnd(NHTTPThreadContext* context) +{ + void* system = NHTTPi_GetSystemInfoP(); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + NHTTPReqInfo* requests = NHTTPi_GetReqInfoP(system); + void* mutex = NHTTPi_GetMutexInfoP(system); + NHTTPRequestInfo* request = requests->reqQueue->request; + NHTTPResponseInfo* response = request->response; + NHTTPConnectionInfo* connection = NHTTPi_Request2Connection(mutex, request); + if (request->cancel) + { + context->error = 8; + context->keepAlive = FALSE; + } + if (!context->keepAlive && info->socket >= 0) + { + if (NHTTPi_SocClose(mutex, request, info->socket) < 0) context->error = 10; + info->socket = -1; + } + if (context->error == 0) response->isSuccess = TRUE; + else + { + response->isSuccess = FALSE; + NHTTPi_SetError(info, context->error); + if (response->recvBuf_p == context->discard) + { + response->recvBuf_p = NULL; + response->recvBufLen = 0; + } + } + if (connection != NULL) connection->state = context->error; + NHTTPi_lockReqList(mutex); + NHTTPi_free(requests->reqQueue); + requests->reqQueue = NULL; + NHTTPi_unlockReqList(mutex); + NHTTPi_destroyRequestObject(mutex, request); + if (connection != NULL && response->isSuccess) connection->started = 5; + NHTTPi_CompleteCallback(mutex, connection); + if (connection != NULL) NHTTPi_NotifyCompletion(connection); +} + +static BOOL NHTTPi_ThreadExecReqQueue(NHTTPThreadContext* context) +{ + void* system = NHTTPi_GetSystemInfoP(); + void* mutex = NHTTPi_GetMutexInfoP(system); + NHTTPReqQueue* queue; + NHTTPi_lockReqList(mutex); + queue = NHTTPi_getReqFromReqQueue(NHTTPi_GetListInfoP(system)); + if (queue != NULL) + { + NHTTPReqInfo* requests = NHTTPi_GetReqInfoP(system); + context->requestId = queue->requestId; + requests->reqQueue = queue; + } + else context->requestId = -1; + NHTTPi_unlockReqList(mutex); + if (context->requestId < 0) + { + NHTTPi_idleCommThread(NHTTPi_GetThreadInfoP(system)); + return FALSE; + } + return TRUE; +} + +static BOOL NHTTPi_ThreadHostAddrProc(NHTTPThreadContext* context) +{ + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(NHTTPi_GetSystemInfoP())->reqQueue->request; + const char* host = request->host; + if (request->proxyEnabled) host = request->proxyServer; + if (NHTTPi_strlen(host) == 0 || NHTTPi_strcmp(host, context->hostname) != 0) + { + context->address = NHTTPi_resolveHostname(request, host); + if (context->address == 0) + { + if (request->proxyEnabled) { context->error = 12; return FALSE; } + context->error = 4; + return FALSE; + } + } + else context->address = context->previousAddress; + NHTTPi_memclr(context->hostname, 0x100); + NHTTPi_memcpy(context->hostname, host, NHTTPi_strlen(host)); + context->port = request->port; + if (request->proxyEnabled) context->port = request->proxyPort; + if (context->address != context->previousAddress || context->port != context->previousPort || request->secure == TRUE) + context->keepAlive = FALSE; + context->previousAddress = context->address; + context->previousPort = context->port; + return TRUE; +} + +static BOOL NHTTPi_ThreadConnectProc(NHTTPThreadContext* context) +{ + void* system = NHTTPi_GetSystemInfoP(); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + NHTTPReqInfo* requests = NHTTPi_GetReqInfoP(system); + NHTTPRequestInfo* request = requests->reqQueue->request; + void* mutex = NHTTPi_GetMutexInfoP(system); + if (!context->keepAlive) + { + if (info->socket >= 0 && NHTTPi_SocClose(mutex, request, info->socket) < 0) + { + info->socket = -1; + context->error = 10; + return FALSE; + } + info->socket = NHTTPi_SocOpen(request); + if (info->socket < 0) + { + context->error = 3; + return FALSE; + } + NHTTPi_lockReqList(mutex); + requests->reqQueue->_unk10 = info->socket; + NHTTPi_unlockReqList(mutex); + if (request->cancel) return FALSE; + if (NHTTPi_SocConnect(info, mutex, request, info->socket, context->address, context->port) < 0) + { + if (request->proxyEnabled) { context->error = 13; return FALSE; } + else + { + if (NHTTPi_GetSSLError(info) != 0) { context->error = 14; return FALSE; } + context->error = 5; return FALSE; + } + } + } + else + { + NHTTPi_lockReqList(mutex); + requests->reqQueue->_unk10 = info->socket; + NHTTPi_unlockReqList(mutex); + } + return TRUE; +} + +static s32 NHTTPi_ThreadProxyProc(NHTTPThreadContext* context) +{ + void* system = NHTTPi_GetSystemInfoP(); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + NHTTPReqInfo* requests = NHTTPi_GetReqInfoP(system); + void* mutex = NHTTPi_GetMutexInfoP(system); + NHTTPRequestInfo* request = requests->reqQueue->request; + context->error = 10; + context->sendLength = 0; + if (request->secure && request->proxyEnabled) + { + s32 result = NHTTPi_SendProxyConnectMethod(context); + s32 sslResult; + if (result != 0) return result; + if (!NHTTPi_RecvProxyConnectHeader(context)) return 1; + sslResult = NHTTPi_SocSSLConnect(info, mutex, request, info->socket); + if (sslResult != 0) + { + if (sslResult == -1004) + { + if (NHTTPi_GetSSLError(info) != 0) context->error = 16; + return 1; + } + if (sslResult == -1005) + { + if (NHTTPi_GetSSLError(info) != 0) context->error = 17; + return 1; + } + if (NHTTPi_GetSSLError(info) != 0) context->error = 14; + return 1; + } + } + return 0; +} + +static inline s32 NHTTPi_SendAuthorization(NHTTPThreadContext* context) +{ + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(NHTTPi_GetSystemInfoP())->reqQueue->request; + s32 result; + if (request->authorizationLength == 0) return 0; + result = NHTTPi_SendData(context, "Authorization: Basic ", 21); + if (result != 0) return result; + result = NHTTPi_SendData(context, request->authorization, request->authorizationLength); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + return 0; +} + +static s32 NHTTPi_ThreadSendProc(NHTTPThreadContext* context) +{ + s32 start; + void* system = NHTTPi_GetSystemInfoP(); + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(system)->reqQueue->request; + void* mutex = NHTTPi_GetMutexInfoP(system); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + NHTTPConnectionInfo* connection = NHTTPi_Request2Connection(mutex, request); + char* buffer = NHTTPi_GetThreadInfoP(system)->commBuf; + s32 urlLength = NHTTPi_strlen(request->url); + s32 result = 0; + context->error = 10; + if (connection != NULL) connection->started = 2; + context->sendLength = 0; + switch (request->method) + { + case 0: result = NHTTPi_SendData(context, "GET ", 4); break; + case 1: result = NHTTPi_SendData(context, "POST ", 5); break; + case 2: result = NHTTPi_SendData(context, "HEAD ", 5); break; + } + if (result != 0) return result; + if (request->proxyEnabled && !request->secure) + { + result = NHTTPi_SendData(context, request->url, urlLength); + if (result != 0) return result; + } + else if (urlLength > request->pathStart) + { + result = NHTTPi_SendData(context, request->url + request->pathStart, urlLength - request->pathStart); + if (result != 0) return result; + } + else + { + result = NHTTPi_SendData(context, "/", 1); + if (result != 0) return result; + } + result = NHTTPi_SendData(context, " HTTP/1.1\r\n", 11); + if (result != 0) return result; + start = (request->secure != 0) + 7; + result = NHTTPi_SendData(context, "Host: ", 6); + if (result != 0) return result; + result = NHTTPi_SendData(context, request->url + start, request->hostEnd - start); + if (result != 0) return result; + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + if (request->proxyEnabled && !request->secure) + { + result = NHTTPi_SendProxyAuthorization(context); + if (result != 0) return result; + } + result = NHTTPi_SendAuthorization(context); + if (result != 0) return result; + result = NHTTPi_SendHeaderList(context); + if (result != 0) return result; + if (request->method == 1) + { + if (request->isRawData) result = NHTTPi_SendProcPostDataRaw(context); + else + { + BOOL binary; + if (request->encodingType == 0) + { + NHTTPHeader* header; + binary = FALSE; + for (header = request->postData; header != NULL; header = header->prev) + { + if (header->_unk14 != 0) { binary = TRUE; break; } + if (header == request->postData->next) break; + } + } + else binary = request->encodingType == 2; + if (!binary) result = NHTTPi_SendProcPostDataAscii(context); + else result = NHTTPi_SendProcPostDataBinary(context); + } + if (result != 0) + { + if (result == 3) context->error = 3; + return result; + } + } + else + { + result = NHTTPi_SendData(context, "\r\n", 2); + if (result != 0) return result; + } + result = 0; + if (context->sendLength > 0) + { + s32 sent = NHTTPi_SocSend(request, info->socket, buffer, context->sendLength, 0); + context->sendLength = 0; + NHTTPi_memclr(buffer, 0x100); + if (sent < 0) result = 1; + if (sent == 0) result = 2; + } + context->sendLength = 0; + NHTTPi_memclr(buffer, 0x100); + return result; +} + +static BOOL NHTTPi_ThreadRecvHeaderProc(NHTTPThreadContext* context) +{ + u32 index; + void* system = NHTTPi_GetSystemInfoP(); + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(system)->reqQueue->request; + NHTTPResponseInfo* response = request->response; + void* mutex = NHTTPi_GetMutexInfoP(system); + NHTTPConnectionInfo* connection = NHTTPi_Request2Connection(mutex, request); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + char recent[4] = {0}; + NHTTPi_HDRBUFLIST* block; + if (connection != NULL) connection->started = 3; + response->headerLen = 0; + NHTTPi_memclr(context->statusLine, 14); + block = response->hdrBufBlock_p; + context->recvLength = 0; + for (;;) + { + s32 received; + if (request->cancel) return FALSE; + if (context->recvLength < 0x400) + { + received = NHTTPi_SocRecv(mutex, request, info->socket, response->hdrBufFirst + context->recvLength, 1, 0); + recent[context->recvLength & 3] = response->hdrBufFirst[context->recvLength]; + } + else + { + char* destination; + index = context->recvLength & 0x1ff; + if (index == 0) + { + if (block != NULL) + { + NHTTPi_HDRBUFLIST* next = NHTTPi_alloc(sizeof(NHTTPi_HDRBUFLIST), 4); + block->next_p = next; + block = next; + } + else + { + block = NHTTPi_alloc(sizeof(NHTTPi_HDRBUFLIST), 4); + response->hdrBufBlock_p = block; + } + if (block == NULL) { context->error = 1; return FALSE; } + block->next_p = NULL; + } + destination = (char*)block->block + index; + received = NHTTPi_SocRecv(mutex, request, info->socket, destination, 1, 0); + recent[context->recvLength & 3] = *destination; + } + if (received <= 0) { context->error = 10; return FALSE; } + context->recvLength += received; + if (NHTTPi_CheckHeaderEnd(recent, context->recvLength)) break; + } + response->headerLen = context->recvLength; + if (response->headerLen == 0) { context->error = 7; return FALSE; } + return TRUE; +} + +static BOOL NHTTPi_ThreadParseHeaderProc(NHTTPThreadContext* context) +{ + void* system = NHTTPi_GetSystemInfoP(); + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(system)->reqQueue->request; + NHTTPResponseInfo* response = request->response; + NHTTPThreadInfo* thread = NHTTPi_GetThreadInfoP(system); + s32 lineLength; + s32 offset; + char* buffer = thread->commBuf; + s32 length; + if (!NHTTPi_loadFromHdrRecvBuf(response, context->statusLine, 0, 14)) { context->error = 7; return FALSE; } + if (NHTTPi_strnicmp(context->statusLine, "HTTP/", 5) != 0) { context->error = 7; return FALSE; } + if (context->statusLine[8] != ' ') { context->error = 7; return FALSE; } + response->httpStatus = NHTTPi_strToInt(context->statusLine + 9, 3); + if (response->httpStatus < 0) { context->error = 7; return FALSE; } + if (NHTTPi_findNextLineHdrRecvBuf(response, 12, response->headerLen, &lineLength, 0) < 0) { context->error = 7; return FALSE; } + context->contentLength = NHTTPi_getHeaderValue(response, "Content-Length", &offset); + if (context->contentLength == 0) { context->error = 0; return FALSE; } + if (context->contentLength > 0x100) { context->error = 7; return FALSE; } + if (context->contentLength > 0) + { + if (!NHTTPi_loadFromHdrRecvBuf(response, buffer, offset, context->contentLength)) { context->error = 7; return FALSE; } + context->contentLength = NHTTPi_strToInt(buffer, context->contentLength); + if (context->contentLength < 0) { context->error = 7; return FALSE; } + response->contentLength = context->contentLength; + } + else response->contentLength = -1; + if (request->secure) context->keepAlive = FALSE; + else + { + length = NHTTPi_getHeaderValue(response, "Connection", &offset); + if (length == 0) + { + context->error = 7; + context->keepAlive = FALSE; + return FALSE; + } + if (length > 0x100) context->keepAlive = FALSE; + else if (length > 0) + { + if (NHTTPi_compareTokenN_HdrRecvBuf(response, offset, offset + length, "Keep-Alive", 0) == 0) + context->keepAlive = TRUE; + else context->keepAlive = FALSE; + } + else context->keepAlive = FALSE; + } + context->chunked = NHTTPi_getHeaderValue(response, "Transfer-Encoding", &offset); + if (context->chunked == 0) { context->error = 7; return FALSE; } + if (context->chunked > 0x100) context->chunked = FALSE; + else + { + BOOL chunked; + if (context->chunked > 0) + chunked = NHTTPi_compareTokenN_HdrRecvBuf(response, offset, offset + context->chunked, "chunked", ';') == 0; + else chunked = FALSE; + context->chunked = chunked; + } + context->error = 0; + response->isHeaderParse = TRUE; + return TRUE; +} + +static inline s32 NHTTPi_RecvChunkLine(void* mutex, NHTTPRequestInfo* request, s32 socket) +{ + s32 total = 0; + u32 index = 0; + char recent[2]; + recent[0] = 0; + recent[1] = 0; + while (recent[index & 1] != '\r' || recent[(index - 1) & 1] != '\n') + { + s32 received = NHTTPi_SocRecv(mutex, request, socket, &recent[index & 1], 1, 0); + if (received <= 0) return received; + total += received; + ++index; + } + return total; +} + +static BOOL NHTTPi_ThreadRecvBodyProc(NHTTPThreadContext* context) +{ + s32 remaining; + void* system = NHTTPi_GetSystemInfoP(); + NHTTPRequestInfo* request = NHTTPi_GetReqInfoP(system)->reqQueue->request; + NHTTPResponseInfo* response = request->response; + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + void* mutex = NHTTPi_GetMutexInfoP(system); + NHTTPConnectionInfo* connection = NHTTPi_Request2Connection(mutex, request); + char* buffer = NHTTPi_GetThreadInfoP(system)->commBuf; + s32 status; + if (request->method == 2 || (status = response->httpStatus) == 204 || status == 304 || (status >= 100 && status < 200)) return TRUE; + NHTTPi_SetVirtualContentLength(connection, 0); + if (connection != NULL) connection->started = 4; + if (context->contentLength >= 0) + { + NHTTPi_SetVirtualContentLength(connection, context->contentLength); + while (context->contentLength > 0) + { + s32 received; + if (context->error != 6 && !NHTTPi_BufFull(mutex, response)) + { + context->error = 6; + response->recvBuf_p = context->discard; + response->recvBufLen = 0x200; + } + if (context->error == 6) + received = NHTTPi_RecvBufN(mutex, request, info->socket, 0, context->contentLength, 0); + else received = NHTTPi_RecvBufN(mutex, request, info->socket, response->bodyLen, context->contentLength, 0); + if (received < 0) return FALSE; + if (received == 0) break; + if (context->error != 6) + { + response->bodyLen += received; + response->totalBodyLen += received; + } + context->contentLength -= received; + } + if (context->error != 6) + { + if (context->contentLength != 0) + context->error = NHTTPi_isRecvBufFull(response, response->bodyLen) ? 6 : 10; + else context->error = 0; + } + } + else + { + context->error = 10; + if (context->chunked) + { + remaining = -1; + for (;;) + { + char recent[2]; + s32 received; + recent[0] = 0; + recent[1] = 0; + context->recvLength = 0; + while (context->recvLength < 0x100) + { + u32 length; + char character; + received = NHTTPi_SocRecv(mutex, request, info->socket, buffer + context->recvLength, 1, 0); + if (received < 0) return FALSE; + length = context->recvLength; + character = buffer[length]; + recent[length & 1] = character; + if (character == ';' || (character == '\n' && recent[(length - 1) & 1] == '\r')) + { + if (character == '\n') --length; + else + { + if (NHTTPi_RecvChunkLine(mutex, request, info->socket) <= 0) return FALSE; + } + if (length == 0) return FALSE; + length = NHTTPi_strToHex(buffer, length); + remaining = length; + if ((s32)length < 0) return FALSE; + break; + } + ++context->recvLength; + } + if (context->recvLength == 0x100) { context->error = 7; return FALSE; } + if (remaining > 0) + { + NHTTPi_SetVirtualContentLength(connection, remaining); + while (remaining > 0) + { + if (context->error != 6 && !NHTTPi_BufFull(mutex, response)) + { + context->error = 6; + response->recvBuf_p = context->discard; + response->recvBufLen = 0x200; + } + if (context->error == 6) + received = NHTTPi_RecvBufN(mutex, request, info->socket, 0, remaining, 0); + else received = NHTTPi_RecvBufN(mutex, request, info->socket, response->bodyLen, remaining, 0); + if (received <= 0) return FALSE; + remaining -= received; + response->bodyLen += received; + response->totalBodyLen += received; + if (remaining == 0 && NHTTPi_SocRecv(mutex, request, info->socket, buffer, 2, 0) <= 0) return FALSE; + } + } + else + { + NHTTPi_RecvChunkLine(mutex, request, info->socket); + context->error = 0; + break; + } + } + } + else + { + for (;;) + { + s32 received; + if (!NHTTPi_BufFull(mutex, response)) + { + context->error = 6; + response->recvBuf_p = context->discard; + response->recvBufLen = 0x200; + } + if (context->error == 6) received = NHTTPi_RecvBuf(mutex, request, info->socket, 0, 0); + else received = NHTTPi_RecvBuf(mutex, request, info->socket, response->bodyLen, 0); + if (received < 0) return FALSE; + if (received == 0) + { + if (context->error != 6) context->error = 0; + break; + } + response->bodyLen += received; + response->totalBodyLen += received; + } + } + } + connection = NHTTPi_Response2Connection(mutex, response); + if (context->error == 0 && connection != NULL) NHTTPi_ReceivedCallback(mutex, connection); + return TRUE; +} + +void NHTTPi_CommThreadProcMain(void* argument) +{ + void* system = NHTTPi_GetSystemInfoP(); + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(system); + NHTTPReqInfo* requests = NHTTPi_GetReqInfoP(system); + NHTTPThreadContext context; + context.requestId = -1; + NHTTPi_memclr(context.hostname, 0x100); + NHTTPi_memclr(context.discard, 0x200); + context.address = -1; + context.previousAddress = -1; + context.sendLength = 0; + context.keepAlive = FALSE; + context.chunked = FALSE; + context.retry = FALSE; + context.contentLength = 0; + context.error = 0; + while (!info->stopping) + { + s32 result; + if (!context.retry) + { + if (!NHTTPi_ThreadExecReqQueue(&context)) continue; + if (requests->reqQueue->request->cancel) { NHTTPi_ThreadReqEnd(&context); continue; } + if (!NHTTPi_ThreadHostAddrProc(&context)) { NHTTPi_ThreadReqEnd(&context); continue; } + } + if (context.retry == TRUE) context.retry = FALSE; + if (!NHTTPi_ThreadConnectProc(&context)) { NHTTPi_ThreadReqEnd(&context); continue; } + result = NHTTPi_ThreadProxyProc(&context); + switch (result) + { + case 0: break; + case 2: context.retry = TRUE; continue; + case 1: NHTTPi_ThreadReqEnd(&context); continue; + } + result = NHTTPi_ThreadSendProc(&context); + switch (result) + { + case 0: break; + case 2: context.retry = TRUE; continue; + case 1: + case 3: NHTTPi_ThreadReqEnd(&context); continue; + } + if (requests->reqQueue->request->cancel) { NHTTPi_ThreadReqEnd(&context); continue; } + if (!NHTTPi_ThreadRecvHeaderProc(&context)) { NHTTPi_ThreadReqEnd(&context); continue; } + if (!NHTTPi_ThreadParseHeaderProc(&context)) { NHTTPi_ThreadReqEnd(&context); continue; } + NHTTPi_ThreadRecvBodyProc(&context); + NHTTPi_ThreadReqEnd(&context); + } +} diff --git a/tools/decomp-assist/NHTTP_thread.attempts.md b/tools/decomp-assist/NHTTP_thread.attempts.md new file mode 100644 index 00000000..1e2eb5df --- /dev/null +++ b/tools/decomp-assist/NHTTP_thread.attempts.md @@ -0,0 +1,93 @@ +# NHTTP_thread leaf attempts + +Target: Wii Menu 4.3U, libs/RevoEX/src/nhttp/NHTTP_thread.c. +Baseline: no source, 0/26 instruction-exact functions, 0/11292 matched code bytes, +0/504 matched data bytes. Implemented all 26 functions in object order using the +private nhttp structures. Other translation units and shared headers are unchanged. + +Current: 23/26 instruction-exact functions, 8504/11292 matched code bytes, +88/504 matched data bytes. The eight-string .data pool is identical. +The unit remains NonMatching after a failed linking experiment. + +## Remaining functions + +### NHTTPi_RecvProxyConnectHeader: 99.92063% + +126/126 instructions. Only the order of the two zero initializations differs: +source initializes r5 then r4; target initializes r4 then r5. + +- Initial pointer/countdown scan: 90.2381%. +- Materialized communication buffer and a for loop: 96.74603%. +- Index-bounded scan: 98.690475%; pointer-end bound: 95.833336%. +- Declared request/response before the accepted flag: 99.44444%. +- Cursor declaration before index: 99.7619%; while-loop pointer increments: + 99.92063%, retained. +- Initialized index before the end flag: still 99.92063%. +- Moved flag before cursor declarations: 99.24603%. +- Other scan declaration orders: 99.64286%, 99.32539%, 99.166664%. +- Inline header scan helper: 97.53968%, rejected and restored. + +### NHTTPi_ThreadParseHeaderProc: 98.58639% + +191/191 instructions. Differences remain in string-base and request registers, +and zero initialization/scheduling around the Keep-Alive comparison. There are +no missing branches or instructions in the retained version. + +- Shared error label: 66.84293%; individual error returns: 85.125656%. +- Positive Content-Length branch first: 89.84293%. +- Positive header-value branches first: 94.84293%. +- Secure connection branch first, with all false assignments preserved: + 98.03665%. +- Store computed chunked flag after its length branches: 98.58639%, retained. +- Default keepalive value before comparison, and declaration hoisting: + 98.58639%, no improvement. +- Buffer declaration first: 98.50785%, rejected. +- Request declaration first: 98.58639%, no improvement. + +### NHTTPi_ThreadRecvBodyProc: 99.1579% + +379/380 instructions. Register assignment differs for system info, the chunk +count and the two-byte recent-character buffer. The ignored trailer-line return +lets the compiler reuse a zero-valued loop counter for scratch initialization; +the target emits a separate zero load. No undefined values are used. + +- Initial implementation: 75.11053%. +- Half-open informational status range: 75.12631%. +- Incomplete fixed-length body branch first: 76.71842%. +- Hoisted chunk count: 76.86316%; scratch array scope variations: unchanged + or 76.71842%. +- Corrected Ghidra's apparent 64-bit chunk count to the assembly's 32-bit + count, and restored the consumed-byte return from the inline chunk-line + reader: 81.48421%. +- Positive chunk branch before zero-chunk trailers: 96.905266%. +- Handle end-of-stream success inside the receive loop: 99.1579%, retained. +- Unsigned recent-character local and function-scope recent array: + 99.1579%, no improvement. Hoisted byte count: 98.07895%, rejected. + +## Linking experiment + +Temporarily changed only NHTTP_thread.c to Matching and built main.dol. +The final experiment produced SHA1 18abd4071ea17c4ef4708fa149dc479009c12b8d, +so configure.py was restored to NonMatching. + +unitaudit.py reported 25 text symbols at their target addresses, with only the +following CommThreadProcMain shifted by -4. The next unit, +d_nhttp_private.c at 0x8149b390, and subsequent text also shifted by -4. +nm confirms the source .text is 11288 bytes, against 11292 target bytes. +The missing instruction is in ThreadRecvBodyProc. + +The real named multipart string arrays were declared as ordinary const char +arrays. Source STR_POST_DISPOS starts at 0x0, STR_POST_TYPE_BIN at 0x28, +STR_POST_TYPE_URLENCODE at 0x74, and STR_POST_TYPE_MULTIPART at 0xa8. The target +object names these symbols at 0x0, 0x27, 0x74, and 0xa6. unitaudit reports +rodata deltas of 0, +1, and +2. Source section sizes are .data 194, .rodata 213, +.sdata 88; target sizes are .data 200, .rodata 216, .sdata 88. The pool's string +contents and offsets agree; section trailing alignment and named constant +boundaries still need investigation. No padding or address-pinning was added. + +A byte comparison against orig/43U/00000008.app found 1305353 differing bytes, +first at file offset 0x5bb. Both files are 3867904 bytes. The text shift changes +relocated references outside this unit, so that count is not a count of local +instruction mismatches. + +Final acceptance uses the full non-quick gate after restoring NonMatching. -- 2.49.1