From 3b286167ba6152d54b970f81f50fcaabf01f67a8 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 15:42:41 -0500 Subject: [PATCH 1/4] Decompile NWC24Download to 7/30 functions (worker be3767b3) (#279) --- configure.py | 2 +- libs/RevoEX/src/nwc24/NWC24Download.c | 2068 +++++++++++++++++++++++++ 2 files changed, 2069 insertions(+), 1 deletion(-) create mode 100644 libs/RevoEX/src/nwc24/NWC24Download.c diff --git a/configure.py b/configure.py index a95b2794..716cda71 100644 --- a/configure.py +++ b/configure.py @@ -1089,7 +1089,7 @@ config.libs = [ Object(Matching, "nwc24/NWC24UserId.c"), Object(Matching, "nwc24/NWC24Time.c"), Object(Matching, "nwc24/NWC24Ipc.c"), - Object(NonMatching, "nwc24/NWC24Download.c"), + Object(Equivalent, "nwc24/NWC24Download.c"), Object(Matching, "nwc24/NWC24CHJump.c"), Object(NonMatching, "nwc24/NWC24System.c"), ] diff --git a/libs/RevoEX/src/nwc24/NWC24Download.c b/libs/RevoEX/src/nwc24/NWC24Download.c new file mode 100644 index 00000000..7f023401 --- /dev/null +++ b/libs/RevoEX/src/nwc24/NWC24Download.c @@ -0,0 +1,2068 @@ +#include + +#include +#include + +#include +#include + +typedef struct { + u16 id; + u8 type; + u8 priority; + u32 flags; + u32 appId; + u32 nwc24IdHigh; + u32 nwc24IdLow; + u16 groupId; + u8 unknown_0x16[2]; + u16 subTaskCount; + u16 unknown_0x1a; + u16 interval; + u16 intervalWait; + u32 unknown_0x20; + u8 retryCount; + u8 retryEnabled; + u8 unknown_0x26[2]; + u32 retryMask; + u8 unknown_0x2c[0x88]; + char url[0xec]; + char fileName[0x5c]; + u8 optOutFlags; + u8 unknown_0x1fd[3]; +} DlTaskData; + +typedef struct { + u32 magic; + u32 version; + u32 unknown_0x08; + u16 unknown_0x0c; + u16 unknown_0x0e; + u16 legacyTaskCount; + u16 taskCount; + u16 maxTaskCount; + u8 unknown_0x16[0x6a]; + struct { + u32 flags; + u32 nextTime; + u32 lastAccess; + u8 priority; + u8 unknown_0x0d[3]; + } entries[NWC24_DL_TASK_MAX]; +} DlTaskListHeader; + +typedef struct { + u32 sortMode; + s32 comparisonValue; + s32 selectedValue; + u32 comparisonId; + u32 initialized; + u32 valid; +} DlTaskIterationState; + +static char* DLFilePath = "/shared2/wc24/nwc24dl.bin"; +static BOOL NWC24iLaxParameterCheck; + +NWC24Err NWC24iLoadDlHeader(); +NWC24Err NWC24iCheckDlHeaderConsistency(DlTaskListHeader* header, BOOL repair); +NWC24Err NWC24iCreateDlTaskList(); +NWC24Err NWC24iOpenDlTaskList(); +NWC24Err StoreDlTask(NWC24DlTask* dlTask); +NWC24Err AddTaskInternal(NWC24DlTask* dlTask, u16 taskCount, u16 maxTaskCount); +NWC24Err DeleteDlTask(NWC24DlTask* dlTask); +static s32 IterationPredicatorLastAccess(NWC24DlId taskId); +static s32 IterationPredicatorNextTime(NWC24DlId taskId); +static s32 IterationPredicatorPriority(NWC24DlId taskId); + +NWC24Err NWC24InitDlTask(NWC24DlTask* dlTask, NWC24DLType dlType) { + char homePath[64] = { 0 }; + u32 nwc24IdHigh; + u32 nwc24IdLow; + DlTaskData* task; + DlTaskListHeader* header; + NWC24Err result; + BOOL allowed; + BOOL useContentFile; + + NANDGetHomeDir(homePath); + homePath[0x0f] = '\0'; + nwc24IdHigh = strtoul(homePath + 7, NULL, 16); + homePath[0x18] = '\0'; + nwc24IdLow = strtoul(homePath + 0x10, NULL, 16); + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + if (dlTask == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (dlType >= 4) { + return NWC24_ERR_INVALID_VALUE; + } + + memset(dlTask, 0, sizeof(*dlTask)); + task = (DlTaskData*)dlTask; + task->type = dlType; + task->priority = 0x7f; + task->appId = NWC24GetAppId(); + task->groupId = NWC24GetGroupId(); + task->nwc24IdHigh = nwc24IdHigh; + task->nwc24IdLow = nwc24IdLow; + task->id = 0xffff; + task->subTaskCount = 1; + task->interval = 0x0b40; + task->intervalWait = 0x05a0; + + useContentFile = FALSE; + if (dlType == NWC24_DLTYPE_OCTETSTREAM_V1 || dlType == NWC24_DLTYPE_OCTETSTREAM_V2) { + useContentFile = TRUE; + } + if (useContentFile) { + strcpy(task->fileName, "content.bin"); + } + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (dlTask == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + + result = NWC24_OK; + allowed = NWC24IsMsgLibOpenedByTool(); + if (!allowed) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + allowed = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!allowed) { + allowed = FALSE; + if (task->flags & 0x40) { + if (task->groupId == NWC24GetGroupId()) { + allowed = TRUE; + } + } + if (!allowed) { + result = NWC24_ERR_PROTECTED; + } + } + } + + if (result == NWC24_OK && task->id != 0xffff && task->id >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } + + return result >> 31; +} + +NWC24Err NWC24SetDlId(NWC24DlTask* dlTask, NWC24DlId dlId) { + NWC24Err result; + u16 taskId; + DlTaskData* task; + DlTaskListHeader* header; + NWC24Work* work = NWC24WorkP; + + if (work != NULL) { + header = (DlTaskListHeader*)work->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + task = (DlTaskData*)dlTask; + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24_OK; + } + } + + if (result != NWC24_OK) { + return result; + } + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (dlId >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + + task->id = dlId; + return NWC24_OK; +} + +NWC24Err NWC24SetDlPriority(NWC24DlTask* dlTask, u8 dlPriority) { + DlTaskListHeader* header; + NWC24Err result; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + result = NWC24_OK; + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = ((DlTaskData*)dlTask)->appId; + u32 appId = NWC24GetAppId(); + BOOL sameAppId = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!sameAppId) { + BOOL allowed = FALSE; + if (((DlTaskData*)dlTask)->flags & 0x40) { + u16 groupId = ((DlTaskData*)dlTask)->groupId; + if (groupId == NWC24GetGroupId()) { + allowed = TRUE; + } + } + if (!allowed) { + result = NWC24_ERR_PROTECTED; + } + } + } + if (result == NWC24_OK) { + u16 taskId = ((DlTaskData*)dlTask)->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } + } + } + + if (result != NWC24_OK) { + return result; + } + + ((DlTaskData*)dlTask)->priority = dlPriority; + return NWC24_OK; +} + +NWC24Err NWC24SetDlInterval(NWC24DlTask* dlTask, u16 dlInterval) { + DlTaskListHeader* header; + DlTaskData* task; + u16 taskId; + s64 nextTime; + s32 intervalSeconds; + NWC24Work* work; + NWC24Err result; + OSTime universalTime; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + task = (DlTaskData*)dlTask; + result = NWC24_OK; + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + if ((taskAppId & 0xffffff00) != (appId & 0xffffff00) && + (!(task->flags & 0x40) || task->groupId != NWC24GetGroupId())) { + result = NWC24_ERR_PROTECTED; + } + } + if (result == NWC24_OK) { + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } + } + } + + if (result != NWC24_OK) { + return result; + } + + task = (DlTaskData*)dlTask; + if (!NWC24iLaxParameterCheck) { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + taskId = task->id; + if (taskId >= header->taskCount && !(task->flags & 0x40000000) && + (dlInterval < 0x00b4 || dlInterval > 0x2760)) { + return NWC24_ERR_INVALID_VALUE; + } + } + + if (task->interval != dlInterval) { + task->interval = dlInterval; + if (task->id != 0xffff) { + universalTime = 0; + if (NWC24iGetUniversalTime(&universalTime) >= NWC24_OK) { + intervalSeconds = task->interval * 60; + work = NWC24WorkP; + nextTime = (s64)universalTime + intervalSeconds; + if (work != NULL) { + header = (DlTaskListHeader*)work->dlHead; + } else { + header = NULL; + } + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24_OK; + } + } + if (result == NWC24_OK) { + taskId = task->id; + if (taskId != 0xffff) { + nextTime /= 60; + if (work != NULL) { + header = (DlTaskListHeader*)work->dlHead; + } else { + header = NULL; + } + header->entries[taskId].nextTime = nextTime; + } + } + } + } + } + return NWC24_OK; +} + +NWC24Err NWC24SetDlUrl(NWC24DlTask* dlTask, const char* dlUrl) { + DlTaskListHeader* header; + DlTaskData* task; + u16 taskId; + NWC24Err result; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + + task = (DlTaskData*)dlTask; + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + BOOL allowed = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!allowed) { + allowed = FALSE; + if (task->flags & 0x40) { + if (task->groupId == NWC24GetGroupId()) { + allowed = TRUE; + } + } + if (!allowed) { + return NWC24_ERR_PROTECTED; + } + } + } + + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + + result = NWC24iCheckStringLength(dlUrl, 7, 0x100); + if (result < NWC24_OK) { + return result; + } + if (strncmp(dlUrl, "http://", 7) != 0 && strncmp(dlUrl, "https://", 8) != 0) { + result = NWC24_ERR_FORMAT; + } else { + result = NWC24_OK; + } + if (result < NWC24_OK) { + return result; + } + + task = (DlTaskData*)dlTask; + if (!NWC24iLaxParameterCheck && (task->flags & 0x4) && strncmp(dlUrl, "http://", 7) == 0) { + return NWC24_ERR_INVALID_VALUE; + } + + NWC24iStrLCpy(task->url, dlUrl, sizeof(task->url)); + return NWC24_OK; +} + +NWC24Err NWC24SetDlFlags(NWC24DlTask* dlTask, u32 dlFlags) { + DlTaskListHeader* header; + DlTaskData* task; + u16 taskId; + NWC24Err result; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + + task = (DlTaskData*)dlTask; + result = NWC24_OK; + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + if ((taskAppId & 0xffffff00) != (appId & 0xffffff00)) { + BOOL allowed = FALSE; + if (task->flags & 0x40) { + u16 groupId = task->groupId; + if (groupId == NWC24GetGroupId()) { + allowed = TRUE; + } + } + if (!allowed) { + result = NWC24_ERR_PROTECTED; + } + } + } + + if (result == NWC24_OK) { + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } + } + if (result != NWC24_OK) { + return result; + } + + task = (DlTaskData*)dlTask; + if (!NWC24iLaxParameterCheck && (dlFlags & 0x4) && strncmp(task->url, "http://", 7) == 0) { + return NWC24_ERR_INVALID_VALUE; + } + switch (task->type) { + case NWC24_DLTYPE_MULTIPART_V1: + case NWC24_DLTYPE_OCTETSTREAM_V1: { + if (dlFlags & 0x80000038) { + return NWC24_ERR_INVALID_OPERATION; + } + break; + } + default: + break; + } + task->flags = dlFlags; + return NWC24_OK; +} + +NWC24Err NWC24GetDlAppId(const NWC24DlTask* dlTask, u32* dlAppId) { + NWC24Err result; + const DlTaskData* task; + DlTaskListHeader* header; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + task = (const DlTaskData*)dlTask; + if (task->id != 0xffff && task->id >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24_OK; + } + } + if (result != NWC24_OK) { + return result; + } + if (dlAppId == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + *dlAppId = ((const DlTaskData*)dlTask)->appId; + return NWC24_OK; +} + +NWC24Err NWC24DumpDlTask(NWC24DlTask* dlTask) { + DlTaskListHeader* header; + DlTaskData* task; + u16 taskId; + NWC24Err result; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + task = (DlTaskData*)dlTask; + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24_OK; + } + } + if (result != NWC24_OK) { + return result; + } + return NWC24_OK; +} + +NWC24Err NWC24IterateDlTask(NWC24DlId* dlIterateId, BOOL begin) { + DlTaskListHeader* header; + NWC24Work* work; + u16 taskId; + u16 maxTaskCount; + NWC24Err result; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + if (dlIterateId == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (begin) { + *dlIterateId = 0; + } else { + *dlIterateId += 1; + } + + work = NWC24WorkP; + if (work != NULL) { + header = (DlTaskListHeader*)work->dlHead; + } else { + header = NULL; + } + taskId = *dlIterateId; + maxTaskCount = header->maxTaskCount; + if (taskId >= maxTaskCount) { + return NWC24_ERR_DONE; + } + + do { + if (work != NULL) { + header = (DlTaskListHeader*)work->dlHead; + } else { + header = NULL; + } + if (taskId >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + if (taskId == 0xffff) { + return NWC24_ERR_INVALID_VALUE; + } + if (work != NULL) { + header = (DlTaskListHeader*)work->dlHead; + } else { + header = NULL; + } + result = NWC24_ERR_NOT_FOUND; + if (header->entries[taskId].flags == 0) { + result = NWC24_OK; + } + if (result >= NWC24_OK) { + *dlIterateId = taskId; + return result; + } + taskId++; + } while (taskId < header->maxTaskCount); + return NWC24_ERR_DONE; +} + +NWC24Err NWC24IterateDlTaskEx(NWC24DlIterateWork* dlIterateWork, NWC24DlId* dlIterateId) { + DlTaskIterationState* state = (DlTaskIterationState*)dlIterateWork; + s32 (*getValue)(NWC24DlId); + NWC24DlId taskId; + s32 value; + BOOL descending; + BOOL passesSelected; + BOOL bestCandidate; + NWC24Err result; + BOOL found = FALSE; + + if (state->valid == 0) { + return NWC24_ERR_INVALID_VALUE; + } + + descending = (state->sortMode >> 31) != 0; + switch (state->sortMode & 0xffff) { + case 0: + getValue = IterationPredicatorLastAccess; + break; + case 1: + getValue = IterationPredicatorNextTime; + break; + case 2: + getValue = IterationPredicatorPriority; + break; + default: + return NWC24_ERR_INVALID_VALUE; + } + + if (state->initialized == 0) { + result = NWC24IterateDlTask(&taskId, TRUE); + while (result >= NWC24_OK) { + value = getValue(taskId); + if (value == state->comparisonValue && taskId > state->comparisonId) { + *dlIterateId = taskId; + state->comparisonValue = value; + state->selectedValue = value; + state->comparisonId = taskId; + return NWC24_OK; + } + result = NWC24IterateDlTask(&taskId, FALSE); + } + } else { + state->initialized = 0; + } + + state->comparisonValue = descending ? 0x80000001 : 0x7fffffff; + result = NWC24IterateDlTask(&taskId, TRUE); + while (result >= NWC24_OK) { + value = getValue(taskId); + if (descending) { + passesSelected = value <= state->selectedValue; + } else { + passesSelected = value >= state->selectedValue; + } + if (!passesSelected) { + if (descending) { + bestCandidate = value > state->comparisonValue; + } else { + bestCandidate = value < state->comparisonValue; + } + if (!bestCandidate) { + *dlIterateId = taskId; + state->comparisonValue = value; + state->comparisonId = taskId; + found = TRUE; + } + } + result = NWC24IterateDlTask(&taskId, FALSE); + } + + if (found) { + state->selectedValue = state->comparisonValue; + return NWC24_OK; + } + state->valid = 0; + return NWC24_ERR_DONE; +} + +NWC24Err NWC24UpdateDlTask(NWC24DlTask* dlTask) { + DlTaskData* task = (DlTaskData*)dlTask; + DlTaskListHeader* header; + NWC24Err result; + OSTime universalTime; + NWC24DlId taskId; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + BOOL allowed = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!allowed && (task->flags & 0x40)) { + allowed = task->groupId == NWC24GetGroupId(); + } + if (!allowed) { + return NWC24_ERR_PROTECTED; + } + } + + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + + if (taskId != 0xffff) { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + if (taskId >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + + result = NWC24iGetUniversalTime(&universalTime); + if (result >= NWC24_OK) { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + result = NWC24_OK; + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + taskId = task->id; + if (taskId == 0xffff) { + result = NWC24_ERR_FAILED; + } else if (taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } + } + if (result >= NWC24_OK) { + header->entries[taskId].lastAccess = universalTime / 60; + } + } + if (result < NWC24_OK) { + return result; + } + } + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + result = NWC24_OK; + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + BOOL allowed = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!allowed && (task->flags & 0x40)) { + allowed = task->groupId == NWC24GetGroupId(); + } + if (!allowed) { + result = NWC24_ERR_PROTECTED; + } + } + taskId = task->id; + if (result == NWC24_OK && taskId != 0xffff && taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } + } + + if (result == NWC24_OK && task->id != 0xffff) { + task->unknown_0x20 = 0; + task->unknown_0x1a = 0; + } + + if (task->retryEnabled == 1) { + do { + u8 retryCount; + u32 retryMask; + + task->retryCount = (task->retryCount + 1) & 0x1f; + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (dlTask == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + BOOL allowed = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!allowed && (task->flags & 0x40)) { + allowed = task->groupId == NWC24GetGroupId(); + } + if (!allowed) { + return NWC24_ERR_PROTECTED; + } + } + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + if (task->retryEnabled == 0) { + return NWC24_ERR_INVALID_OPERATION; + } + retryCount = task->retryCount; + retryMask = task->retryMask; + if (retryMask == 0) { + return NWC24_ERR_FATAL; + } + if (retryCount > 0x1f) { + return NWC24_ERR_INVALID_VALUE; + } + result = NWC24_OK; + if ((retryMask & (1 << retryCount)) == 0) { + result = NWC24_ERR_DISABLED; + } + } while (result == NWC24_ERR_DISABLED); + if (result < NWC24_OK) { + return result; + } + } + + return StoreDlTask(dlTask); +} + +NWC24Err NWC24DeleteDlTask(NWC24DlTask* dlTask) { + DlTaskData* task; + DlTaskListHeader* header; + NWC24Err result; + + if (NWC24GetAppId() == 0x48414541) { + result = NWC24_OK; + } else { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + + task = (DlTaskData*)dlTask; + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + BOOL allowed = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!allowed && (task->flags & 0x40)) { + allowed = task->groupId == NWC24GetGroupId(); + } + if (!allowed) { + return NWC24_ERR_PROTECTED; + } + } + + if (task->id != 0xffff && task->id >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + result = NWC24_OK; + } + + if (result != NWC24_OK) { + return result; + } + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (dlTask == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + + task = (DlTaskData*)dlTask; + if (task->id != 0xffff && task->id >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + + result = DeleteDlTask(dlTask); + if (result >= NWC24_OK) { + task->id = 0xffff; + } + return result; +} + +NWC24Err NWC24AddDlTask(NWC24DlTask* dlTask) { + DlTaskListHeader* header; + NWC24File file; + NWC24Err result; + OSTime universalTime; + s64 nextTime; + s32 intervalSeconds; + u16 taskCount; + u16 maxTaskCount; + u16 taskId; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (header->maxTaskCount == 0 && header->legacyTaskCount != 0) { + header->maxTaskCount = header->legacyTaskCount; + if (header->legacyTaskCount > 0x20) { + header->legacyTaskCount = 0x20; + } + result = NWC24FOpen(&file, DLFilePath, 4); + if (result >= NWC24_OK) { + result = NWC24FSeek(&file, 0, NWC24_SEEK_BEG); + if (result >= NWC24_OK) { + result = NWC24FWrite(header, 0x800, &file); + } + NWC24FClose(&file); + } + } + + if (header->maxTaskCount < 1 || header->taskCount < 1 || + header->maxTaskCount < header->taskCount) { + return NWC24_ERR_BROKEN; + } + + taskCount = header->taskCount; + maxTaskCount = header->maxTaskCount; + result = AddTaskInternal(dlTask, taskCount, maxTaskCount); + if (result < NWC24_OK) { + return result; + } + + universalTime = 0; + result = NWC24iGetUniversalTime(&universalTime); + if (result < NWC24_OK) { + return result; + } + + intervalSeconds = ((DlTaskData*)dlTask)->interval * 60; + nextTime = universalTime + intervalSeconds; + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + result = NWC24_OK; + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + taskId = ((DlTaskData*)dlTask)->id; + if (taskId == 0xffff) { + result = NWC24_ERR_FAILED; + } else if (taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } + } + + if (result >= NWC24_OK) { + nextTime /= 60; + header->entries[taskId].nextTime = nextTime; + } + return result; +} + +NWC24Err NWC24GetDlTask(NWC24DlTask* dlTask, NWC24DlId dlId) { + DlTaskListHeader* header; + NWC24File file; + NWC24Err result; + NWC24Err operationResult; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (dlId >= header->maxTaskCount || dlId == 0xffff) { + result = NWC24_ERR_INVALID_VALUE; + } else { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header->entries[dlId].flags == 0) { + result = NWC24_ERR_NOT_FOUND; + } else { + result = NWC24_OK; + } + } + if (result < NWC24_OK) { + return result; + } + + result = NWC24FOpen(&file, DLFilePath, 0x0a); + if (result < NWC24_OK) { + return result; + } + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header->maxTaskCount > NWC24_DL_TASK_MAX || dlId >= header->maxTaskCount) { + operationResult = NWC24_ERR_INVALID_VALUE; + } else { + operationResult = NWC24FSeek(&file, 0x800 + dlId * 0x200, NWC24_SEEK_BEG); + } + if (operationResult < NWC24_OK) { + result = operationResult; + } else { + operationResult = NWC24FRead(dlTask, 0x200, &file); + result = NWC24_OK; + if (operationResult < NWC24_OK) { + result = operationResult; + } + } + operationResult = NWC24FClose(&file); + if (result != NWC24_OK) { + return result; + } + return operationResult; +} + +NWC24Err NWC24PurgeOldestDlTask() { + DlTaskIterationState state; + NWC24File file; + NWC24DlTask task; + DlTaskListHeader* header; + NWC24DlId taskId; + NWC24DlTask* taskPointer; + NWC24DlId selectedId; + NWC24Err result; + NWC24Err closeResult; + + memset(&state, 0, sizeof(state)); + state.sortMode = 0; + state.comparisonValue = 0x7fffffff; + state.selectedValue = (s32)0x80000001; + state.comparisonId = 0xffffffff; + state.initialized = 1; + state.valid = 1; + taskId = 0; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + result = NWC24_OK; + do { + result = NWC24IterateDlTaskEx((NWC24DlIterateWork*)&state, &taskId); + if (result != NWC24_OK) { + break; + } + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + } while (taskId < header->taskCount); + + if (result == NWC24_ERR_DONE) { + return NWC24_ERR_FAILED; + } + if (result < NWC24_OK) { + return result; + } + + taskPointer = &task; + selectedId = taskId; + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + if (selectedId >= header->maxTaskCount || selectedId == 0xffff) { + return NWC24_ERR_INVALID_VALUE; + } + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header->entries[selectedId].flags == 0) { + return NWC24_ERR_NOT_FOUND; + } + + result = NWC24FOpen(&file, DLFilePath, 0x0a); + if (result < NWC24_OK) { + return result; + } + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header->maxTaskCount > NWC24_DL_TASK_MAX || selectedId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24FSeek(&file, 0x800 + selectedId * 0x200, NWC24_SEEK_BEG); + } + if (result >= NWC24_OK) { + result = NWC24FRead(taskPointer, sizeof(task), &file); + } + closeResult = NWC24FClose(&file); + if (result < NWC24_OK) { + return result; + } + result = closeResult; + if (result < NWC24_OK) { + return result; + } + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else if (((DlTaskData*)taskPointer)->id == 0xffff || + ((DlTaskData*)taskPointer)->id >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24_OK; + } + if (result == NWC24_OK) { + result = DeleteDlTask(taskPointer); + } + if (result >= NWC24_OK) { + ((DlTaskData*)taskPointer)->id = 0xffff; + } + return result; +} + +NWC24Err NWC24ManageDlTaskListForMenu() { + DlTaskListHeader* header; + NWC24File file; + NWC24DlTask task; + NWC24Err result; + NWC24Err closeResult; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + result = NWC24_OK; + if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } + if (result < NWC24_OK) { + return result; + } + if (header->maxTaskCount >= NWC24_DL_TASK_MAX) { + return result; + } + + result = NWC24ExtendDlTaskList(NWC24_DL_TASK_MAX); + if (result < NWC24_OK) { + return result; + } + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + result = NWC24_OK; + if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else if (header->maxTaskCount <= 2) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header->entries[2].flags == 0) { + result = NWC24_ERR_NOT_FOUND; + } + if (result < NWC24_OK) { + return result; + } + + result = NWC24FOpen(&file, DLFilePath, 0x0a); + if (result < NWC24_OK) { + return result; + } + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else if (header->maxTaskCount > NWC24_DL_TASK_MAX || header->maxTaskCount <= 2) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24FSeek(&file, 0xc00, NWC24_SEEK_BEG); + } + if (result >= NWC24_OK) { + result = NWC24FRead(&task, sizeof(task), &file); + } + closeResult = NWC24FClose(&file); + if (result >= NWC24_OK) { + result = closeResult; + } + if (result == NWC24_ERR_NOT_FOUND) { + return NWC24_OK; + } + if (result < NWC24_OK) { + return result; + } + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else if (((DlTaskData*)&task)->id == 0xffff || + ((DlTaskData*)&task)->id >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24_OK; + } + if (result == NWC24_OK) { + result = DeleteDlTask(&task); + } + if (result >= NWC24_OK) { + ((DlTaskData*)&task)->id = 0xffff; + } + return result; +} + +NWC24Err NWC24GetDlOptOutFlags(NWC24DlTask* dlTask, u8* dlOptOutFlags) { + DlTaskListHeader* header; + DlTaskData* task; + u16 taskId; + NWC24Err result; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + task = (DlTaskData*)dlTask; + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24_OK; + } + } + + if (result != NWC24_OK) { + return result; + } + if (dlOptOutFlags == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + *dlOptOutFlags = ((DlTaskData*)dlTask)->optOutFlags; + return NWC24_OK; +} + +NWC24Err NWC24iOpenDlTaskList() { + NWC24Err result; + DlTaskListHeader* header; + + result = NWC24iLoadDlHeader(); + if (result >= NWC24_OK) { + NWC24iSynchronizeRtcCounter(FALSE); + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + result = NWC24iCheckDlHeaderConsistency(header, FALSE); + if (result >= NWC24_OK) { + result = NWC24_OK; + } + } + return result; +} + +NWC24Err NWC24iCloseDlTaskList() { + return NWC24_OK; +} + +NWC24Err NWC24ExtendDlTaskList(u32 num) { + DlTaskListHeader* header; + NWC24File file; + DlTaskData* task; + NWC24Err result; + NWC24Err closeResult; + u16 taskId; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + + taskId = header->maxTaskCount; + if (num > NWC24_DL_TASK_MAX || taskId > num) { + return NWC24_ERR_INVALID_VALUE; + } + if (taskId == num) { + return NWC24_OK; + } + + header->maxTaskCount = num; + result = NWC24FOpen(&file, DLFilePath, 4); + if (result < NWC24_OK) { + return result; + } + + for (; taskId < num; taskId++) { + if (NWC24WorkP != NULL) { + task = (DlTaskData*)NWC24WorkP->dlTask; + } else { + task = NULL; + } + memset(task, 0, sizeof(*task)); + task->type = 0xff; + task->id = taskId; + memset(&((DlTaskListHeader*)NWC24WorkP->dlHead)->entries[taskId], 0, + sizeof(header->entries[taskId])); + + if (((DlTaskListHeader*)NWC24WorkP->dlHead)->maxTaskCount > NWC24_DL_TASK_MAX || + taskId >= ((DlTaskListHeader*)NWC24WorkP->dlHead)->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + break; + } + result = NWC24FSeek(&file, 0x800 + taskId * 0x200, NWC24_SEEK_BEG); + if (result >= NWC24_OK) { + result = NWC24FWrite(task, sizeof(*task), &file); + if (result >= NWC24_OK) { + result = NWC24_OK; + } + } + if (result < NWC24_OK) { + break; + } + } + + if (result >= NWC24_OK) { + result = NWC24FSeek(&file, 0, NWC24_SEEK_BEG); + if (result >= NWC24_OK) { + result = NWC24FWrite((DlTaskListHeader*)NWC24WorkP->dlHead, 0x800, &file); + if (result >= NWC24_OK) { + result = NWC24_OK; + } + } + } + closeResult = NWC24FClose(&file); + if (result != NWC24_OK) { + closeResult = result; + } + result = NWC24iLoadDlHeader(); + if (closeResult != NWC24_OK) { + result = closeResult; + } + return result; +} + +NWC24Err NWC24iCheckDlHeaderConsistency(DlTaskListHeader* header, BOOL repair) { + NWC24DlId taskId; + NWC24DlTask task; + NWC24File file; + DlTaskListHeader* currentHeader; + NWC24Err result; + NWC24Err closeResult; + + for (taskId = 0; taskId < header->maxTaskCount; taskId++) { + if (NWC24WorkP != NULL) { + currentHeader = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + currentHeader = NULL; + } + result = NWC24_OK; + if (currentHeader == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else if (taskId >= currentHeader->maxTaskCount || taskId == 0xffff) { + result = NWC24_ERR_INVALID_VALUE; + } else if (currentHeader->entries[taskId].flags == 0) { + result = NWC24_ERR_NOT_FOUND; + } + if (result < NWC24_OK) { + continue; + } + + if (repair) { + if (NWC24WorkP != NULL) { + currentHeader = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + currentHeader = NULL; + } + result = NWC24_OK; + if (currentHeader == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else if (taskId >= currentHeader->maxTaskCount || taskId == 0xffff) { + result = NWC24_ERR_INVALID_VALUE; + } else if (currentHeader->entries[taskId].flags == 0) { + result = NWC24_ERR_NOT_FOUND; + } + if (result < NWC24_OK) { + continue; + } + + result = NWC24FOpen(&file, DLFilePath, 0x0a); + if (result < NWC24_OK) { + return result; + } + if (NWC24WorkP != NULL) { + currentHeader = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + currentHeader = NULL; + } + if (currentHeader == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else if (currentHeader->maxTaskCount > NWC24_DL_TASK_MAX || + taskId >= currentHeader->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24FSeek(&file, 0x800 + taskId * 0x200, NWC24_SEEK_BEG); + } + if (result >= NWC24_OK) { + result = NWC24FRead(&task, sizeof(task), &file); + } + closeResult = NWC24FClose(&file); + if (result >= NWC24_OK) { + result = closeResult; + } + if (result == NWC24_ERR_NOT_FOUND) { + continue; + } + if (result < NWC24_OK) { + return result; + } + if (NWC24WorkP != NULL) { + currentHeader = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + currentHeader = NULL; + } + if (currentHeader != NULL && taskId >= currentHeader->taskCount && + ((DlTaskData*)&task)->subTaskCount == 0) { + if (((DlTaskData*)&task)->id == 0xffff || + ((DlTaskData*)&task)->id >= currentHeader->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = DeleteDlTask(&task); + } + if (result < NWC24_OK) { + return result; + } + ((DlTaskData*)&task)->id = 0xffff; + } + } + } + + return NWC24_OK; +} + +NWC24Err NWC24iCreateDlTaskList() { + NWC24File file; + DlTaskListHeader* header; + DlTaskData* task; + NWC24Err result; + u16 taskId; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + memset(header, 0, 0x800); + header->magic = 0x5763446c; + header->version = 1; + header->unknown_0x0c = 0; + header->unknown_0x0e = 0; + header->maxTaskCount = NWC24_DL_TASK_MAX; + header->legacyTaskCount = 0x20; + header->taskCount = 8; + + result = NWC24FOpen(&file, DLFilePath, 1); + if (result < NWC24_OK) { + return result; + } + + result = NWC24FSeek(&file, 0, NWC24_SEEK_BEG); + if (result >= NWC24_OK) { + result = NWC24FWrite(header, 0x800, &file); + if (result >= NWC24_OK) { + result = NWC24_OK; + } + } + + if (result >= NWC24_OK) { + for (taskId = 0; taskId < NWC24_DL_TASK_MAX; taskId++) { + if (NWC24WorkP != NULL) { + task = (DlTaskData*)NWC24WorkP->dlTask; + } else { + task = NULL; + } + + memset(task, 0, sizeof(*task)); + task->type = 0xff; + task->id = taskId; + memset(&header->entries[taskId], 0, sizeof(header->entries[taskId])); + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header->maxTaskCount > NWC24_DL_TASK_MAX || taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + break; + } + + result = NWC24FSeek(&file, 0x800 + taskId * 0x200, NWC24_SEEK_BEG); + if (result < NWC24_OK) { + break; + } + + memcpy((DlTaskData*)NWC24WorkP->dlTask, task, sizeof(*task)); + result = NWC24FWrite((DlTaskData*)NWC24WorkP->dlTask, 0x200, &file); + if (result < NWC24_OK) { + break; + } + } + } + + NWC24FClose(&file); + return result; +} + +NWC24Err NWC24iInitDlTaskList(BOOL force) { + NWC24Err result; + DlTaskListHeader* header; + + result = NWC24iOpenDlTaskList(); + if (result == NWC24_OK) { + if (!force) { + return result; + } + } else if (result == NWC24_ERR_VER_MISMATCH) { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header != NULL && header->version > 1) { + return result; + } + } + return NWC24iCreateDlTaskList(); +} + +NWC24Err NWC24iLoadDlHeader() { + NWC24File file; + NWC24File updateFile; + DlTaskListHeader* header; + NWC24Err result; + NWC24Err closeResult; + u32 fileLength = 0; + + result = NWC24_OK; + closeResult = NWC24FOpen(&file, DLFilePath, NWC24_OPEN_READ); + if (closeResult < NWC24_OK) { + return closeResult; + } + + closeResult = NWC24FSeek(&file, 0, NWC24_SEEK_BEG); + if (closeResult < NWC24_OK) { + return closeResult; + } + + closeResult = NWC24FRead(NWC24WorkP->dlHead, 0x800, &file); + result = closeResult < NWC24_OK ? closeResult : NWC24_OK; + if (result < NWC24_OK) { + return result; + } + + result = NWC24FGetLength(&file, &fileLength); + if (result >= NWC24_OK) { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header->maxTaskCount == 0 && header->legacyTaskCount != 0) { + header->maxTaskCount = header->legacyTaskCount; + if (header->legacyTaskCount > 0x20) { + header->legacyTaskCount = 0x20; + } + if (NWC24FOpen(&updateFile, DLFilePath, NWC24_OPEN_RW) >= NWC24_OK) { + if (NWC24FSeek(&updateFile, 0, NWC24_SEEK_BEG) >= NWC24_OK) { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + NWC24FWrite(header, 0x800, &updateFile); + } + NWC24FClose(&updateFile); + } + } + + if (header->maxTaskCount < 1 || header->taskCount < 1 || + header->maxTaskCount < header->taskCount) { + result = NWC24_ERR_BROKEN; + } else { + result = NWC24_OK; + } + } + + closeResult = NWC24FClose(&file); + if (result != NWC24_OK) { + return result; + } + return closeResult; +} + +NWC24Err StoreDlTask(NWC24DlTask* dlTask) { + NWC24File file; + DlTaskData* task; + DlTaskListHeader* header; + NWC24Err result; + NWC24DlId taskId; + + task = (DlTaskData*)dlTask; + result = NWC24FOpen(&file, DLFilePath, 4); + if (result < NWC24_OK) { + return result; + } + + taskId = task->id; + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header->maxTaskCount > NWC24_DL_TASK_MAX || taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24FSeek(&file, 0x800 + taskId * 0x200, NWC24_SEEK_BEG); + } + + if (result >= NWC24_OK) { + memcpy(NWC24WorkP->dlTask, task, sizeof(*task)); + result = NWC24FWrite(NWC24WorkP->dlTask, sizeof(*task), &file); + } + + if (result >= NWC24_OK) { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + header->entries[taskId].flags = task->flags; + header->entries[taskId].priority = task->priority; + result = NWC24FSeek(&file, 0, NWC24_SEEK_BEG); + if (result >= NWC24_OK) { + result = NWC24FWrite(header, 0x800, &file); + } + } + + NWC24FClose(&file); + return result; +} + +NWC24Err AddTaskInternal(NWC24DlTask* dlTask, u16 taskCount, u16 maxTaskCount) { + DlTaskData* task; + DlTaskListHeader* header; + NWC24Err result; + u16 taskId; + char* url; + OSTime universalTime; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + if (dlTask == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + + task = (DlTaskData*)dlTask; + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + BOOL allowed = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!allowed && (task->flags & 0x40)) { + allowed = task->groupId == NWC24GetGroupId(); + } + if (!allowed) { + return NWC24_ERR_PROTECTED; + } + } + + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + + url = task->url; + result = NWC24iCheckStringLength(url, 7, 0x100); + if (result >= NWC24_OK && strncmp(url, "http://", 7) != 0 && + strncmp(url, "https://", 8) != 0) { + result = NWC24_ERR_FORMAT; + } + if (result < NWC24_OK) { + return result; + } + +retryExistingTask: + taskId = task->id; + if (taskId != 0xffff) { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + + result = NWC24_OK; + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + BOOL allowed = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!allowed && (task->flags & 0x40)) { + allowed = task->groupId == NWC24GetGroupId(); + } + if (!allowed) { + result = NWC24_ERR_PROTECTED; + } + } + taskId = task->id; + if (result == NWC24_OK && taskId != 0xffff && taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } + } + if (result < NWC24_OK) { + return result; + } + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (taskId >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + + result = NWC24iGetUniversalTime(&universalTime); + if (result >= NWC24_OK) { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + result = NWC24_OK; + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + taskId = task->id; + if (taskId == 0xffff) { + result = NWC24_ERR_FAILED; + } else if (taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } + } + if (result >= NWC24_OK) { + header->entries[taskId].nextTime = universalTime / 60; + } + } + if (result < NWC24_OK) { + return result; + } + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + result = NWC24_OK; + if (dlTask == NULL) { + result = NWC24_ERR_INVALID_VALUE; + } else if (header == NULL) { + result = NWC24_ERR_LIB_NOT_OPENED; + } else { + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + BOOL allowed = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!allowed && (task->flags & 0x40)) { + allowed = task->groupId == NWC24GetGroupId(); + } + if (!allowed) { + result = NWC24_ERR_PROTECTED; + } + } + taskId = task->id; + if (result == NWC24_OK && taskId != 0xffff && taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } + } + + if (result == NWC24_OK && task->id != 0xffff) { + task->unknown_0x20 = 0; + task->unknown_0x1a = 0; + } + + if (task->retryEnabled == 1) { + do { + u8 retryCount; + u32 retryMask; + + task->retryCount = (task->retryCount + 1) & 0x1f; + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (dlTask == NULL) { + return NWC24_ERR_INVALID_VALUE; + } + if (header == NULL) { + return NWC24_ERR_LIB_NOT_OPENED; + } + if (!NWC24IsMsgLibOpenedByTool()) { + u32 taskAppId = task->appId; + u32 appId = NWC24GetAppId(); + BOOL allowed = (taskAppId & 0xffffff00) == (appId & 0xffffff00); + if (!allowed && (task->flags & 0x40)) { + allowed = task->groupId == NWC24GetGroupId(); + } + if (!allowed) { + return NWC24_ERR_PROTECTED; + } + } + taskId = task->id; + if (taskId != 0xffff && taskId >= header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + if (task->retryEnabled == 0) { + return NWC24_ERR_INVALID_OPERATION; + } + retryCount = task->retryCount; + retryMask = task->retryMask; + if (retryMask == 0) { + return NWC24_ERR_FATAL; + } + if (retryCount > 0x1f) { + return NWC24_ERR_INVALID_VALUE; + } + result = NWC24_OK; + if ((retryMask & (1 << retryCount)) == 0) { + result = NWC24_ERR_DISABLED; + } + } while (result == NWC24_ERR_DISABLED); + if (result < NWC24_OK) { + return result; + } + } + return StoreDlTask(dlTask); + } + + for (;;) { + u16 currentTaskId; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (dlTask == NULL || header == NULL || taskCount > maxTaskCount || + taskCount >= header->maxTaskCount || maxTaskCount > header->maxTaskCount) { + return NWC24_ERR_INVALID_VALUE; + } + + currentTaskId = taskCount; + while (currentTaskId < maxTaskCount) { + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + if (header->entries[currentTaskId].flags == 0) { + task->id = currentTaskId; + goto retryExistingTask; + } + currentTaskId++; + } + + result = NWC24PurgeOldestDlTask(); + if (result < NWC24_OK) { + return result; + } + goto retryExistingTask; + } +} + +NWC24Err DeleteDlTask(NWC24DlTask* dlTask) { + NWC24File file; + DlTaskData* task; + DlTaskListHeader* header; + NWC24Err result; + NWC24Err closeResult; + NWC24DlId taskId; + + result = NWC24FOpen(&file, DLFilePath, 4); + if (result < NWC24_OK) { + return result; + } + + taskId = ((DlTaskData*)dlTask)->id; + task = (DlTaskData*)NWC24WorkP->dlTask; + memset(task, 0, sizeof(*task)); + task->id = taskId; + task->type = 0xff; + memset(&((DlTaskListHeader*)NWC24WorkP->dlHead)->entries[taskId], 0, + sizeof(((DlTaskListHeader*)NWC24WorkP->dlHead)->entries[taskId])); + + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + if (header->maxTaskCount > NWC24_DL_TASK_MAX || taskId >= header->maxTaskCount) { + result = NWC24_ERR_INVALID_VALUE; + } else { + result = NWC24FSeek(&file, 0x800 + taskId * 0x200, NWC24_SEEK_BEG); + } + if (result >= NWC24_OK) { + memcpy(NWC24WorkP->dlTask, task, sizeof(*task)); + result = NWC24FWrite(NWC24WorkP->dlTask, sizeof(*task), &file); + } + if (result >= NWC24_OK) { + memset(&((DlTaskListHeader*)NWC24WorkP->dlHead)->entries[taskId], 0, + sizeof(((DlTaskListHeader*)NWC24WorkP->dlHead)->entries[taskId])); + result = NWC24FSeek(&file, 0, NWC24_SEEK_BEG); + if (result >= NWC24_OK) { + result = NWC24FWrite((DlTaskListHeader*)NWC24WorkP->dlHead, 0x800, &file); + } + } + + closeResult = NWC24FClose(&file); + if (result != NWC24_OK) { + return result; + } + return closeResult; +} + +static s32 IterationPredicatorLastAccess(NWC24DlId taskId) { + DlTaskListHeader* header; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + return header->entries[taskId].lastAccess; +} + +static s32 IterationPredicatorNextTime(NWC24DlId taskId) { + DlTaskListHeader* header; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + return header->entries[taskId].nextTime; +} + +static s32 IterationPredicatorPriority(NWC24DlId taskId) { + DlTaskListHeader* header; + + if (NWC24WorkP != NULL) { + header = (DlTaskListHeader*)NWC24WorkP->dlHead; + } else { + header = NULL; + } + return header->entries[taskId].priority; +} -- 2.49.1 From 5cdca2e8aa5858f8280d3407e664e1f655382fe9 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 15:51:33 -0500 Subject: [PATCH 2/4] iplGCSaveData: replace hand-placed lbl_ strings with named data (worker e4de343a) (#280) --- src/scene/memoryCard/iplGCSaveData.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/scene/memoryCard/iplGCSaveData.cpp b/src/scene/memoryCard/iplGCSaveData.cpp index 2feaf6bd..0989e3c2 100644 --- a/src/scene/memoryCard/iplGCSaveData.cpp +++ b/src/scene/memoryCard/iplGCSaveData.cpp @@ -17,7 +17,7 @@ namespace ipl { {"it_ObjCubeEdit_b_SaveDataFoucusOut.brlan", "G_Data"}, {"it_ObjCubeEdit_b_SaveDataFlash.brlan", "G_DataFlash"}, }; - extern "C" const char* lbl_81696CA8 = "B_Data_00"; + static const char* scDataPaneName = "B_Data_00"; GCSaveData::GCSaveData(EGG::Heap* heap, nand::LayoutFile* layoutFile, const char* directory, const char* fileName, @@ -32,9 +32,9 @@ namespace ipl { mpPaneManager->setupScene(mpLayout); mpPaneManager->setAllComponentTriggerTarget(false); - mpPaneManager->setTriggerTarget(mpLayout->FindPaneByName(lbl_81696CA8), true); + mpPaneManager->setTriggerTarget(mpLayout->FindPaneByName(scDataPaneName), true); - add_anmbutton(lbl_81696CA8, get_animation(2), get_animation(3), NULL); + add_anmbutton(scDataPaneName, get_animation(2), get_animation(3), NULL); setTranslate(translate); } @@ -42,7 +42,7 @@ namespace ipl { void GCSaveData::calc() { mpLayout->calc(); mpPaneManager->calc(); - get_anmbutton(lbl_81696CA8)->calc(); + get_anmbutton(scDataPaneName)->calc(); } void GCSaveData::draw() { @@ -90,9 +90,9 @@ namespace ipl { ->getManager() ->getComment(mSlot, mIndex, 0), 0); - get_anmbutton(lbl_81696CA8)->setBalloon(mpBalloon); + get_anmbutton(scDataPaneName)->setBalloon(mpBalloon); } else { - get_anmbutton(lbl_81696CA8)->setBalloon(NULL); + get_anmbutton(scDataPaneName)->setBalloon(NULL); } } snd::getSystem()->startSE("WIPL_SE_BT_TARGETTING"); @@ -132,7 +132,7 @@ namespace ipl { } void GCSaveData::init() { - clear_button(lbl_81696CA8); + clear_button(scDataPaneName); if (mpBalloon != NULL) { mpBalloon->terminate(); } -- 2.49.1 From a620eec6a460e5205766720dfb66ee255b91bf86 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 16:07:10 -0500 Subject: [PATCH 3/4] Decompile NHTTP_thread to 23/26 functions (gpt-6.1-sol low) (worker 64796af8) (#281) --- 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 From ca753deae1b783749c2898f410b915ab4c2f0ec3 Mon Sep 17 00:00:00 2001 From: devin Date: Tue, 29 Sep 2026 21:13:24 +0000 Subject: [PATCH 4/4] setting: link iplAOSSThread (objdiff 100% code+data) Co-Authored-By: wch --- configure.py | 2 +- src/scene/setting/iplAOSSThread.cpp | 290 ++++++++++++++++++++++++++++ 2 files changed, 291 insertions(+), 1 deletion(-) create mode 100644 src/scene/setting/iplAOSSThread.cpp diff --git a/configure.py b/configure.py index a95b2794..538863ec 100644 --- a/configure.py +++ b/configure.py @@ -875,7 +875,7 @@ config.libs = [ Object(Matching, "scene/setting/iplAPScanThread.cpp"), Object(NonMatching, "scene/setting/iplUSBAP.cpp"), Object(NonMatching, "scene/setting/iplUSBAPThread.cpp"), - Object(NonMatching, "scene/setting/iplAOSSThread.cpp"), + Object(Matching, "scene/setting/iplAOSSThread.cpp"), Object(NonMatching, "scene/setting/AOSSLink.c"), Object(NonMatching, "scene/setting/iplRakuRakuThread.cpp"), Object(NonMatching, "scene/setting/AOSS.c"), diff --git a/src/scene/setting/iplAOSSThread.cpp b/src/scene/setting/iplAOSSThread.cpp new file mode 100644 index 00000000..82c89b11 --- /dev/null +++ b/src/scene/setting/iplAOSSThread.cpp @@ -0,0 +1,290 @@ +#include + +#include + +#include + +#include +#include +#include +#include + +#include "utility/iplThread.h" + +extern "C" { +int AOSSi_Init(void* config); +int AOSSi_InitLocal(SOAlloc alloc, SOFree free_); +int AOSSi_EndLocal(void); +int AOSSi_Cancel(void); +int AOSS_SetCallback(void (*callback)(void)); +} + +namespace ipl { + namespace scene { + class USBAPThread { + public: + static void callback(); + }; + + MEMAllocator m_allocator; + + typedef struct AOSSResult { + u8 wep40Key[4][6]; // 0x000 + char wep40Ssid[0x20]; // 0x018 + u8 pad_0x38; // 0x038 + u8 wep104Key[4][0xE]; // 0x039 + char wep104Ssid[0x20]; // 0x071 + u8 pad_0x91; // 0x091 + char tkipKey[0x40]; // 0x092 + char tkipSsid[0x20]; // 0x0D2 + u8 pad_0xF2; // 0x0F2 + char aesKey[0x40]; // 0x0F3 + char aesSsid[0x20]; // 0x133 + u8 pad_0x153[2]; // 0x153 + } AOSSResult; // 0x155 + + typedef struct AOSSConfig { + u16 version; // 0x000 + u8 unk_0x02; // 0x002 + u16 unk_0x04; // 0x004 + u8 name[3]; // 0x006 + u8 pad_0x09[0xFD]; // 0x009 + u16 unk_0x106; // 0x106 + u16 unk_0x108; // 0x108 + u16 unk_0x10A; // 0x10A + u16 unk_0x10C; // 0x10C + u16 unk_0x10E; // 0x10E + u8 mac[6]; // 0x110 + u8 result; // 0x116 + AOSSResult security; // 0x117 + } AOSSConfig; // 0x26C + + static u32 sAOSSStartTimeHi; + static u32 sAOSSStartTimeLo; + static s32 sAOSSState; + static s32 sAOSSUnk; + + class AOSSThread : public utility::ut_thread { + public: + AOSSThread(EGG::Heap* heap); + + virtual ~AOSSThread(); // 0x08 + virtual void* Run(); // 0x0C + virtual void unk_0x2C() = 0; // 0x2C + + void destroy(int flag); + int start(); + int cancel(); + int finish(NCDAossConfig* config, int* result); + void printInfo(); + + static void* AOSSThread_813FCC8C(u32 id, s32 size); + static void AOSSThread_813FCCE4(u32 id, void* ptr, s32 align); + + private: + s32 mState; // 0x32C + s32 mThreadPriority; // 0x330 + u8* mpStackBuffer; // 0x334 + u8* mpHeapBuffer; // 0x338 + MEMHeapHandle mpExpHeap; // 0x33C + AOSSConfig mAoss; // 0x340 + }; + + AOSSThread::AOSSThread(EGG::Heap* heap) : utility::ut_thread() { + mpExpHeap = NULL; + sAOSSState = 0; + mpHeapBuffer = (u8*)heap->alloc(0x40000, 0x20); + mpStackBuffer = (u8*)heap->alloc(0x1000, 0x20); + } + + void AOSSThread::destroy(int flag) { + if (sAOSSState != 0) { + if (IsThreadTerminated()) { + WaitForThreadExit(); + SOFinish(); + sAOSSState = 0; + } + } + if (mpExpHeap != NULL) { + MEMDestroyExpHeap(mpExpHeap); + mpExpHeap = NULL; + } + } + + AOSSThread::~AOSSThread() { + destroy(1); + } + + int AOSSThread::start() { + SOLibraryConfig config; + + if (sAOSSState != 0) { + return 0; + } + + config.alloc = AOSSThread_813FCC8C; + config.free = AOSSThread_813FCCE4; + + BOOL level = OSDisableInterrupts(); + + OSTime t = OSGetTime(); + sAOSSStartTimeLo = (u32)t; + sAOSSStartTimeHi = (u32)(t >> 32); + + mpExpHeap = MEMCreateExpHeapEx(mpHeapBuffer, 0x40000, 2); + MEMInitAllocatorForExpHeap(&m_allocator, mpExpHeap, 0x20); + + mThreadPriority = OSGetThreadPriority(OSGetCurrentThread()) - 2; + sAOSSState = 1; + SOInit(&config); + memset(mpStackBuffer, 0, 4); + Create(mpStackBuffer, 0x1000, mThreadPriority, TRUE); + + OSRestoreInterrupts(level); + return 1; + } + + void* AOSSThread::AOSSThread_813FCC8C(u32 id, s32 size) { + void* buffer; + BOOL level = OSDisableInterrupts(); + buffer = MEMAllocFromAllocator(&m_allocator, size); + OSRestoreInterrupts(level); + return buffer; + } + + void AOSSThread::AOSSThread_813FCCE4(u32 id, void* ptr, s32 align) { + BOOL level = OSDisableInterrupts(); + MEMFreeToAllocator(&m_allocator, ptr); + OSRestoreInterrupts(level); + } + + void* AOSSThread::Run() { + mState = 0xF; + AOSS_SetCallback(USBAPThread::callback); + + if (AOSSi_InitLocal(AOSSThread_813FCC8C, AOSSThread_813FCCE4) == -1) { + return this; + } + + memset(&mAoss, 0, sizeof(mAoss)); + mAoss.version = 0xF; + mAoss.unk_0x106 = 0x32; + mAoss.unk_0x10A = 0x32; + mAoss.unk_0x108 = 0x64; + mAoss.unk_0x10C = 0x64; + mAoss.unk_0x10E = 0x4E20; + mAoss.unk_0x02 = 0x50; + memcpy(mAoss.name, "Wii", 3); + mAoss.unk_0x04 = 4; + NETGetWirelessMacAddress(mAoss.mac); + + *(u32*)(mpStackBuffer + 0x3FC0) = 0x97654321; + + mState = AOSSi_Init(&mAoss); + AOSSi_EndLocal(); + OSCheckActiveThreads(); + return this; + } + + int AOSSThread::cancel() { + if (sAOSSState == 0) { + return 0; + } + BOOL level = OSDisableInterrupts(); + AOSSi_Cancel(); + OSRestoreInterrupts(level); + return 1; + } + + int AOSSThread::finish(NCDAossConfig* config, int* result) { + if (sAOSSState == 0) { + *result = -0x63; + return 1; + } + if (IsThreadTerminated()) { + destroy(0); + } + else { + if (((u32)OSGetTime() - (u32)sAOSSStartTimeLo) + / (OS_TIMER_CLOCK / 1000) + >= 90000) + { + BOOL level = OSDisableInterrupts(); + AOSSi_Cancel(); + OSRestoreInterrupts(level); + } + return 0; + } + + if (mState == 0) { + BOOL level = OSDisableInterrupts(); + AOSSResult* sec = &mAoss.security; + + if ((mAoss.version & 0x0001) == 1) { + memcpy(config->wep40.ssid, sec->wep40Ssid, 0x20); + memcpy(config->wep40.key[0], sec->wep40Key[0], 5); + memcpy(config->wep40.key[1], sec->wep40Key[1], 5); + memcpy(config->wep40.key[2], sec->wep40Key[2], 5); + memcpy(config->wep40.key[3], sec->wep40Key[3], 5); + config->wep40.keyId = 1; + config->wep40.ssidLength = strlen(sec->wep40Ssid); + } + else { + memset(config->wep40.ssid, 0, 0x20); + config->wep40.ssidLength = 0; + } + + if ((mAoss.version & 0x0002) == 2) { + memcpy(config->wep104.ssid, sec->wep104Ssid, 0x20); + memcpy(config->wep104.key[0], sec->wep104Key[0], 0xD); + memcpy(config->wep104.key[1], sec->wep104Key[1], 0xD); + memcpy(config->wep104.key[2], sec->wep104Key[2], 0xD); + memcpy(config->wep104.key[3], sec->wep104Key[3], 0xD); + config->wep104.keyId = 1; + config->wep104.ssidLength = strlen(sec->wep104Ssid); + } + else { + memset(config->wep104.ssid, 0, 0x20); + config->wep104.ssidLength = 0; + } + + if ((mAoss.version & 0x0004) == 4) { + memcpy(config->tkip.ssid, sec->tkipSsid, 0x20); + memcpy(config->tkip.key, sec->tkipKey, 0x40); + config->tkip.ssidLength = strlen(sec->tkipSsid); + config->tkip.keyLen = strlen(sec->tkipKey); + } + else { + memset(config->tkip.ssid, 0, 0x20); + config->tkip.ssidLength = 0; + } + + if ((mAoss.version & 0x0008) == 8) { + memcpy(config->aes.ssid, sec->aesSsid, 0x20); + memcpy(config->aes.key, sec->aesKey, 0x40); + config->aes.ssidLength = strlen(sec->aesSsid); + config->aes.keyLen = strlen(sec->aesKey); + } + else { + memset(config->aes.ssid, 0, 0x20); + config->aes.ssidLength = 0; + } + + printInfo(); + OSRestoreInterrupts(level); + } + else if (mState == -2) { + *result = -0x62; + } + + *result = mAoss.result; + return 1; + } + +#pragma dont_inline on + void AOSSThread::printInfo() { + } +#pragma dont_inline reset + + } // namespace scene +} // namespace ipl -- 2.49.1