Compare commits

...

6 Commits

8 changed files with 455 additions and 542 deletions

View File

@@ -4,14 +4,67 @@
#include <revolution/types.h>
#include <revolution/nwc24/NWC24Err.h>
#include <revolution/nwc24/NWC24Dl.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct NWC24DlTaskInfo {
u32 used; // 0x00
u32 nextTime; // 0x04
u32 lastAccess; // 0x08
u8 priority; // 0x0c
u8 padding[3];
} NWC24DlTaskInfo;
typedef struct NWC24DlHeader {
u32 magic; // 0x00
u32 version; // 0x04
u32 unk_0x08; // 0x08
u16 unk_0x0C; // 0x0c
u16 unk_0x0E; // 0x0e
u16 numTasksInFile; // 0x10
u16 minTasks; // 0x12
u16 numTasks; // 0x14
u8 padding[0x6A]; // 0x16
NWC24DlTaskInfo infos[NWC24_DL_TASK_MAX]; // 0x80
} NWC24DlHeader;
typedef struct NWC24DlTaskEx {
u16 id; // 0x000
u8 dlType; // 0x002
u8 priority; // 0x003
u32 flags; // 0x004
u32 appId; // 0x008
u32 userIdLow; // 0x00c
u32 userIdHigh; // 0x010
u16 groupId; // 0x014
u16 unk_0x16; // 0x016
u16 numSubTasks; // 0x018
u16 processedSubTasks; // 0x01a
u16 interval; // 0x01c
u16 subInterval; // 0x01e
u32 unk_0x20; // 0x020
u8 subTaskIndex; // 0x024
u8 stType; // 0x025
u8 padding0[2]; // 0x026
u32 stFlags; // 0x028
u8 padding1[0x88]; // 0x02c
char url[236]; // 0x0b4
char fileName[0x5C]; // 0x1a0
u8 optOutFlags; // 0x1fc
u8 padding2[3]; // 0x1fd
} NWC24DlTaskEx;
NWC24Err NWC24iOpenDlTaskList();
NWC24Err NWC24iCloseDlTaskList();
NWC24Err NWC24iLoadDlHeader();
NWC24Err NWC24iInitDlTaskList(NWC24Err mode);
NWC24Err NWC24iCreateDlTaskList();
NWC24Err NWC24iCheckDlHeaderConsistency(NWC24DlHeader* dlHead, BOOL fixBroken);
#ifdef __cplusplus
}
#endif

View File

@@ -176,7 +176,8 @@ s32 ConvertDateToDays(u16 year, u8 month, u8 day) {
}
validDate:
daysOfYear = day - 1 + DAYS_OF_YEAR[month - 1];
daysOfYear = day - 1;
daysOfYear += DAYS_OF_YEAR[month - 1];
if (month >= 3) {
isLeapYear = 0;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
@@ -217,20 +218,15 @@ void ConvertDaysToDate(u16* year, u8* month, u8* day, s32 days) {
for (;;) {
s32 previousDays = days;
if (*month == 2) {
BOOL leapYear = IsLeapYear(*year);
if (leapYear) {
days -= 29;
} else {
days -= DAYS_OF_MONTH[*month - 1];
}
if (*month == 2 && IsLeapYear(*year)) {
days -= 29;
} else {
days -= DAYS_OF_MONTH[*month - 1];
}
if (days < 0) {
*day += previousDays;
return;
break;
}
*month = *month + 1;
*month += 1;
}
}

View File

@@ -66,7 +66,7 @@ static char* DLFilePath = "/shared2/wc24/nwc24dl.bin";
static BOOL NWC24iLaxParameterCheck;
NWC24Err NWC24iLoadDlHeader();
NWC24Err NWC24iCheckDlHeaderConsistency(DlTaskListHeader* header, BOOL repair);
NWC24Err NWC24iCheckDlHeaderConsistency(NWC24DlHeader* header, BOOL repair);
NWC24Err NWC24iCreateDlTaskList();
NWC24Err NWC24iOpenDlTaskList();
NWC24Err StoreDlTask(NWC24DlTask* dlTask) NO_INLINE;
@@ -408,21 +408,24 @@ NWC24Err NWC24SetDlFlags(NWC24DlTask* dlTask, u32 dlFlags) {
}
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;
result = NWC24_ERR_INVALID_VALUE;
} else {
switch (task->type) {
case NWC24_DLTYPE_MULTIPART_V1:
case NWC24_DLTYPE_OCTETSTREAM_V1: {
if (dlFlags & 0x80000038) {
result = NWC24_ERR_INVALID_OPERATION;
return result;
}
break;
}
break;
default:
break;
}
default:
break;
task->flags = dlFlags;
result = NWC24_OK;
}
task->flags = dlFlags;
return NWC24_OK;
return result;
}
NWC24Err NWC24GetDlAppId(const NWC24DlTask* dlTask, u32* dlAppId) {
@@ -507,33 +510,23 @@ NWC24Err NWC24IterateDlTask(NWC24DlId* dlIterateId, BOOL begin) {
return NWC24_ERR_DONE;
}
do {
if (work != NULL) {
header = (DlTaskListHeader*)work->dlHead;
while (taskId < GetCachedDlHeader()->maxTaskCount) {
header = GetCachedDlHeader();
if (taskId >= header->maxTaskCount || taskId == 0xffff) {
result = NWC24_ERR_INVALID_VALUE;
} 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].appId == 0) {
header = GetCachedDlHeader();
result = NWC24_OK;
if (header->entries[taskId].appId == 0) {
result = NWC24_ERR_NOT_FOUND;
}
}
if (result >= NWC24_OK) {
*dlIterateId = taskId;
return result;
return NWC24_OK;
}
taskId++;
} while (taskId < header->maxTaskCount);
}
return NWC24_ERR_DONE;
}
@@ -584,22 +577,12 @@ NWC24Err NWC24IterateDlTaskEx(NWC24DlIterateWork* dlIterateWork, NWC24DlId* dlIt
state->initialized = 0;
}
state->comparisonValue = descending ? 0x80000001 : 0x7fffffff;
state->comparisonValue = (state->sortMode & 0x80000000) ? 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) {
if (descending ? value < state->selectedValue : value > state->selectedValue) {
if (descending ? value > state->comparisonValue : value < state->comparisonValue) {
*dlIterateId = taskId;
state->comparisonValue = value;
state->comparisonId = taskId;
@@ -621,6 +604,7 @@ NWC24Err NWC24UpdateDlTask(NWC24DlTask* dlTask) {
DlTaskData* task = (DlTaskData*)dlTask;
NWC24DlId taskId;
NWC24Err result;
NWC24Err closeResult;
OSTime universalTime;
DlTaskListHeader* header;
@@ -629,128 +613,60 @@ NWC24Err NWC24UpdateDlTask(NWC24DlTask* dlTask) {
return result;
}
task = (DlTaskData*)dlTask;
header = GetCachedDlHeader();
taskId = task->id;
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;
}
if (task->id != 0xffff && task->id >= header->maxTaskCount) {
return NWC24_ERR_INVALID_VALUE;
}
result = NWC24iGetUniversalTime(&universalTime);
if (result >= NWC24_OK) {
if (NWC24WorkP != NULL) {
header = (DlTaskListHeader*)NWC24WorkP->dlHead;
result = NWC24iGetUniversalTime(&universalTime);
if (result >= NWC24_OK) {
closeResult = ValidateDlTask(dlTask, FALSE);
if (closeResult == NWC24_OK) {
if (taskId == 0xffff) {
closeResult = NWC24_ERR_FAILED;
} 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;
GetCachedDlHeader()->entries[taskId].lastAccess = universalTime / 60;
}
}
if (result < NWC24_OK) {
return result;
}
result = closeResult;
}
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;
}
}
result = ValidateDlTask(dlTask, TRUE);
if (result == NWC24_OK && task->id != 0xffff) {
if (result == NWC24_OK) {
task->unknown_0x20 = 0;
task->unknown_0x1a = 0;
}
if (task->retryEnabled == 1) {
do {
u8 retryCount;
u8 retryCount = task->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;
result = ValidateDlTask(dlTask, FALSE);
if (result == NWC24_OK) {
if (task->retryEnabled == 0) {
result = NWC24_ERR_INVALID_OPERATION;
} else {
retryMask = task->retryMask;
if (retryMask == 0) {
result = NWC24_ERR_FATAL;
} else if (retryCount > 0x1f) {
result = NWC24_ERR_INVALID_VALUE;
} else {
result = NWC24_OK;
if ((retryMask & (1 << retryCount)) == 0) {
result = NWC24_ERR_DISABLED;
}
}
}
}
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;
if (result == NWC24_ERR_DISABLED) {
task->retryCount = (retryCount + 1) & 0x1f;
}
} while (result == NWC24_ERR_DISABLED);
if (result < NWC24_OK) {
@@ -762,62 +678,28 @@ NWC24Err NWC24UpdateDlTask(NWC24DlTask* dlTask) {
}
NWC24Err NWC24DeleteDlTask(NWC24DlTask* dlTask) {
DlTaskData* task;
DlTaskListHeader* header;
DlTaskData* task = (DlTaskData*)dlTask;
NWC24Err result;
if (NWC24GetAppId() == 0x48414541) {
result = NWC24_OK;
} else {
header = GetCachedDlHeader();
if (dlTask == NULL) {
return NWC24_ERR_INVALID_VALUE;
result = ValidateDlTask(dlTask, TRUE);
if (result != NWC24_OK) {
return result;
}
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;
}
result = ValidateDlTask(dlTask, FALSE);
if (result != NWC24_OK) {
return result;
}
header = GetCachedDlHeader();
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;
if (result < NWC24_OK) {
return result;
}
task->id = 0xffff;
return result;
}
@@ -847,18 +729,22 @@ NWC24Err NWC24AddDlTask(NWC24DlTask* dlTask) {
if (result >= NWC24_OK) {
result = NWC24FSeek(&file, 0, NWC24_SEEK_BEG);
if (result >= NWC24_OK) {
result = NWC24FWrite(header, 0x800, &file);
result = NWC24FWrite(GetCachedDlHeader(), 0x800, &file);
}
NWC24FClose(&file);
}
}
result = NWC24_OK;
if (header->maxTaskCount < 1 || header->taskCount < 1 || header->maxTaskCount < header->taskCount) {
return NWC24_ERR_BROKEN;
result = NWC24_ERR_BROKEN;
}
if (result < NWC24_OK) {
return result;
}
taskCount = header->taskCount;
maxTaskCount = header->maxTaskCount;
taskCount = GetCachedDlHeader()->taskCount;
maxTaskCount = GetCachedDlHeader()->maxTaskCount;
result = AddTaskInternal(dlTask, taskCount, maxTaskCount);
if (result < NWC24_OK) {
return result;
@@ -872,30 +758,16 @@ NWC24Err NWC24AddDlTask(NWC24DlTask* dlTask) {
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 {
result = ValidateDlTask(dlTask, FALSE);
if (result == NWC24_OK) {
taskId = ((DlTaskData*)dlTask)->id;
if (taskId == 0xffff) {
result = NWC24_ERR_FAILED;
} else if (taskId >= header->maxTaskCount) {
result = NWC24_ERR_INVALID_VALUE;
} else {
GetCachedDlHeader()->entries[taskId].nextTime = nextTime / 60;
}
}
if (result >= NWC24_OK) {
nextTime /= 60;
header->entries[taskId].nextTime = nextTime;
}
return result;
}
@@ -979,29 +851,30 @@ NWC24Err NWC24PurgeOldestDlTask() {
NWC24Err closeResult;
memset(&state, 0, sizeof(state));
state.sortMode = 0;
state.comparisonValue = 0x7fffffff;
state.selectedValue = (s32)0x80000001;
state.sortMode = 0;
state.comparisonId = 0xffffffff;
state.initialized = 1;
state.valid = 1;
taskId = 0;
header = GetCachedDlHeader();
result = NWC24_OK;
do {
while (result >= NWC24_OK) {
result = NWC24IterateDlTaskEx((NWC24DlIterateWork*)&state, &taskId);
if (result != NWC24_OK) {
break;
if (result == NWC24_OK) {
header = GetCachedDlHeader();
if (taskId >= header->taskCount) {
break;
}
}
header = GetCachedDlHeader();
} while (taskId < header->taskCount);
if (result == NWC24_ERR_DONE) {
return NWC24_ERR_FAILED;
}
if (result < NWC24_OK) {
if (result == NWC24_ERR_DONE) {
result = NWC24_ERR_FAILED;
}
return result;
}
@@ -1011,6 +884,7 @@ NWC24Err NWC24PurgeOldestDlTask() {
if (header == NULL) {
return NWC24_ERR_LIB_NOT_OPENED;
}
header = GetCachedDlHeader();
if (selectedId >= header->maxTaskCount || selectedId == 0xffff) {
return NWC24_ERR_INVALID_VALUE;
}
@@ -1020,112 +894,100 @@ NWC24Err NWC24PurgeOldestDlTask() {
}
result = NWC24FOpen(&file, DLFilePath, 0x0a);
if (result >= NWC24_OK) {
header = GetCachedDlHeader();
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) {
result = closeResult;
}
}
if (result < NWC24_OK) {
return result;
}
header = GetCachedDlHeader();
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);
closeResult = ValidateDlTask(taskPointer, FALSE);
if (closeResult == NWC24_OK) {
closeResult = DeleteDlTask(taskPointer);
if (closeResult >= NWC24_OK) {
((DlTaskData*)taskPointer)->id = 0xffff;
}
}
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;
}
header = GetCachedDlHeader();
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;
return closeResult;
}
NWC24Err NWC24ManageDlTaskListForMenu() {
DlTaskListHeader* header;
NWC24File file;
NWC24DlTask task;
u16 maxTaskCount;
NWC24Err result;
NWC24Err closeResult;
if (NWC24WorkP != NULL) {
header = (DlTaskListHeader*)NWC24WorkP->dlHead;
} else {
header = NULL;
}
result = NWC24_OK;
header = GetCachedDlHeader();
if (header == NULL) {
result = NWC24_ERR_LIB_NOT_OPENED;
} else {
maxTaskCount = header->maxTaskCount;
result = NWC24_OK;
}
if (result < NWC24_OK) {
return result;
}
if (header->maxTaskCount >= NWC24_DL_TASK_MAX) {
return result;
if (maxTaskCount >= NWC24_DL_TASK_MAX) {
return NWC24_OK;
}
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;
header = GetCachedDlHeader();
if (header == NULL) {
result = NWC24_ERR_LIB_NOT_OPENED;
} else if (header->maxTaskCount <= 2) {
result = NWC24_ERR_INVALID_VALUE;
} else if (header->entries[2].appId == 0) {
result = NWC24_ERR_NOT_FOUND;
} else {
header = GetCachedDlHeader();
if (header->maxTaskCount <= 2) {
result = NWC24_ERR_INVALID_VALUE;
} else {
header = GetCachedDlHeader();
result = NWC24_OK;
if (header->entries[2].appId == 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;
header = GetCachedDlHeader();
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 = NWC24_OK;
if (result < NWC24_OK) {
closeResult = result;
}
result = NWC24FClose(&file);
if (closeResult != NWC24_OK) {
result = closeResult;
}
}
if (result == NWC24_ERR_NOT_FOUND) {
return NWC24_OK;
@@ -1134,23 +996,12 @@ NWC24Err NWC24ManageDlTaskListForMenu() {
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;
}
result = ValidateDlTask(&task, FALSE);
if (result == NWC24_OK) {
result = DeleteDlTask(&task);
}
if (result >= NWC24_OK) {
((DlTaskData*)&task)->id = 0xffff;
if (result >= NWC24_OK) {
((DlTaskData*)&task)->id = 0xffff;
}
}
return result;
}
@@ -1203,7 +1054,7 @@ NWC24Err NWC24iOpenDlTaskList() {
} else {
header = NULL;
}
result = NWC24iCheckDlHeaderConsistency(header, FALSE);
result = NWC24iCheckDlHeaderConsistency((NWC24DlHeader*)header, FALSE);
if (result >= NWC24_OK) {
result = NWC24_OK;
}
@@ -1264,9 +1115,11 @@ closeList:
return result;
}
NWC24Err NWC24iCheckDlHeaderConsistency(DlTaskListHeader* header, BOOL repair) {
NWC24Err NWC24iCheckDlHeaderConsistency(NWC24DlHeader* dlHead, BOOL repair) {
DlTaskListHeader* header = (DlTaskListHeader*)dlHead;
NWC24DlId taskId;
NWC24DlTask task;
DlTaskData* taskData = (DlTaskData*)&task;
NWC24File file;
DlTaskListHeader* currentHeader;
NWC24Err result;
@@ -1274,68 +1127,69 @@ NWC24Err NWC24iCheckDlHeaderConsistency(DlTaskListHeader* header, BOOL repair) {
for (taskId = 0; taskId < header->maxTaskCount; taskId++) {
currentHeader = GetCachedDlHeader();
result = NWC24_OK;
if (taskId >= currentHeader->maxTaskCount || taskId == 0xffff) {
result = NWC24_ERR_INVALID_VALUE;
} else {
currentHeader = GetCachedDlHeader();
result = NWC24_OK;
if (currentHeader->entries[taskId].appId == 0) {
result = NWC24_ERR_NOT_FOUND;
}
}
if (result != NWC24_OK) {
continue;
}
if (!repair) {
continue;
}
currentHeader = GetCachedDlHeader();
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].appId == 0) {
result = NWC24_ERR_NOT_FOUND;
}
if (result < NWC24_OK) {
continue;
}
if (repair) {
} else {
currentHeader = GetCachedDlHeader();
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].appId == 0) {
if (currentHeader->entries[taskId].appId == 0) {
result = NWC24_ERR_NOT_FOUND;
}
if (result < NWC24_OK) {
continue;
}
}
if (result >= NWC24_OK) {
result = NWC24FOpen(&file, DLFilePath, 0x0a);
if (result < NWC24_OK) {
return result;
}
currentHeader = GetCachedDlHeader();
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;
}
currentHeader = GetCachedDlHeader();
if (currentHeader != NULL && taskId >= currentHeader->taskCount && ((DlTaskData*)&task)->subTaskCount == 0) {
if (((DlTaskData*)&task)->id == 0xffff || ((DlTaskData*)&task)->id >= currentHeader->maxTaskCount) {
currentHeader = GetCachedDlHeader();
if (currentHeader->maxTaskCount > NWC24_DL_TASK_MAX || taskId >= currentHeader->maxTaskCount) {
result = NWC24_ERR_INVALID_VALUE;
} else {
result = DeleteDlTask(&task);
result = NWC24FSeek(&file, 0x800 + taskId * 0x200, NWC24_SEEK_BEG);
}
if (result < NWC24_OK) {
return result;
if (result >= NWC24_OK) {
result = NWC24FRead(taskData, sizeof(task), &file);
}
closeResult = result;
result = NWC24FClose(&file);
if (closeResult != NWC24_OK) {
result = closeResult;
}
}
}
if (result < NWC24_OK) {
if (ValidateDlTask((NWC24DlTask*)taskData, FALSE) == NWC24_OK) {
result = DeleteDlTask((NWC24DlTask*)taskData);
if (result >= NWC24_OK) {
taskData->id = 0xffff;
}
}
continue;
}
currentHeader = GetCachedDlHeader();
if (taskId >= currentHeader->taskCount && taskData->subTaskCount == 0) {
if (ValidateDlTask((NWC24DlTask*)taskData, FALSE) == NWC24_OK) {
result = DeleteDlTask((NWC24DlTask*)taskData);
if (result >= NWC24_OK) {
taskData->id = 0xffff;
}
((DlTaskData*)&task)->id = 0xffff;
}
}
}
@@ -1388,7 +1242,7 @@ NWC24Err NWC24iCreateDlTaskList() {
}
#pragma dont_inline on
NWC24Err NWC24iInitDlTaskList(BOOL force) {
NWC24Err NWC24iInitDlTaskList(NWC24Err force) {
NWC24Err result;
DlTaskListHeader* header;
@@ -1510,7 +1364,11 @@ NWC24Err AddTaskInternal(NWC24DlTask* dlTask, u16 taskCount, u16 maxTaskCount) {
return result;
}
task = (DlTaskData*)dlTask;
header = GetCachedDlHeader();
taskId = task->id;
if (taskId >= header->maxTaskCount && taskId != 0xffff) {
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) {

View File

@@ -555,8 +555,8 @@ NWC24Err NWC24iMBoxFlushHeader(NWC24MBoxType type) {
return result != NWC24_OK ? result : closeResult;
}
static inline void GetOldestMsgId(const MBCHeader* header, u32* id) {
*id = header->oldestId;
static inline void GetOldestMsgId(const MBCHeader* header, u32* msgId) {
*msgId = header->oldestId;
}
NWC24Err NWC24iMBoxCheck(NWC24MBoxType type, u32 size) {

View File

@@ -231,12 +231,13 @@ static NWC24Err QEncode(char* encoded, u32 encodedCapacity, u32* encodedSize, u8
}
static NWC24Err QDecode(char* decoded, u32 decodedCapacity, u32* decodedSize, char* encoded, u32 encodedSize, u32* encodedSizeOut, int flags) {
u32 decodedOffset = 0;
u32 encodedOffset;
u32 decodedOffset = 0;
char* input;
char* output;
u32 value = 0;
u8 value = 0;
NWC24Err result = NWC24_OK;
int valid;
if (decoded == NULL)
return NWC24_ERR_INVALID_VALUE;
@@ -255,7 +256,7 @@ static NWC24Err QDecode(char* decoded, u32 decodedCapacity, u32* decodedSize, ch
input = encoded;
output = decoded;
for (encodedOffset = 0; encodedOffset < encodedSize; encodedOffset++) {
if (decodedCapacity <= decodedOffset) {
if (decodedOffset >= decodedCapacity) {
result = NWC24_ERR_OVERFLOW;
break;
}
@@ -264,46 +265,48 @@ static NWC24Err QDecode(char* decoded, u32 decodedCapacity, u32* decodedSize, ch
input++;
decodedOffset++;
} else if (*input == '=' && encodedOffset + 2 < encodedSize) {
u32 inputAfterEqual = encodedOffset + 1;
char* current = ++input;
if (*current == ' ' || *current == '\t') {
char* previous = current;
u32 scan = 1;
while (inputAfterEqual + scan + 1 < encodedSize) {
char* next = previous + 1;
if (*next == '\r' && previous[2] == '\n') {
inputAfterEqual += scan;
current = next;
input++;
encodedOffset++;
valid = 1;
if (*input == ' ' || *input == '\t') {
char* scan = input + 1;
u32 scanLen = 1;
while (encodedOffset + scanLen + 1 < encodedSize) {
if (*scan == '\r' && scan[1] == '\n') {
input = scan;
encodedOffset += scanLen;
break;
}
if (*next != ' ' && *next != '\t') {
if (*scan != ' ' && *scan != '\t') {
break;
}
previous = next;
scan++;
scanLen++;
}
}
if (*current == '\r' && current[1] == '\n') {
current += 2;
encodedOffset = inputAfterEqual + 1;
input = current;
if (*input == '\r' && input[1] == '\n') {
input += 2;
encodedOffset++;
} else {
int high = Util_xtoi(*current);
int high = Util_xtoi(*input);
int low;
if (high >= 0) {
value = (high & 0x0F) << 4;
} else {
valid = 0;
}
low = Util_xtoi(current[1]);
low = Util_xtoi(input[1]);
if (low >= 0) {
value |= low & 0x0F;
value = (value | (low & 0x0F)) & 0xFF;
} else {
valid = 0;
}
current += 2;
if (high >= 0 && low >= 0) {
input += 2;
if (valid) {
*output++ = (char)value;
decodedOffset++;
}
encodedOffset = inputAfterEqual + 1;
input = current;
encodedOffset++;
}
} else {
*output++ = *input++;
@@ -330,6 +333,14 @@ NWC24Err NWC24DecodeQuotedPrintable(char* decoded, u32 decodedCapacity, u32* dec
static NWC24Err ConcatEncodedText(char* encoded, u32 encodedCapacity, u32* encodedSize, char encoding, u8* decoded, u32 decodedSize,
u32* decodedSizeOut);
static inline NWC24Err AppendBounded(char* encoded, u32 encodedCapacity, u32 needed, const char* str, u32 strLen) {
if (Mail_strlen(encoded) + needed >= encodedCapacity) {
return NWC24_ERR_OVERFLOW;
}
Mail_strncat(encoded, str, strLen);
return NWC24_OK;
}
static NWC24Err EncodeWord(char* encoded, u32 encodedCapacity, u32* encodedSize, char* charset, u32 charsetSize, char encoding, u8* decoded,
u32 decodedSize, u32* decodedSizeOut) {
NWC24Err result = NWC24_OK;
@@ -348,67 +359,47 @@ static NWC24Err EncodeWord(char* encoded, u32 encodedCapacity, u32* encodedSize,
if (encodedSize != NULL) {
*encodedSize = 1;
}
if (Mail_strlen(encoded) + 2 < encodedCapacity) {
Mail_strncat(encoded, "=?", 2);
} else {
result = NWC24_ERR_OVERFLOW;
if (AppendBounded(encoded, encodedCapacity, 2, "=?", 2) != NWC24_OK) {
*encoded = '\0';
goto done;
}
if (result == NWC24_OK) {
if (charset == NULL) {
result = NWC24_ERR_INVALID_VALUE;
} else if (charsetSize + Mail_strlen(encoded) < encodedCapacity) {
u32 length = Mail_strlen(charset);
Mail_strncat(encoded, charset, length);
} else {
result = NWC24_ERR_OVERFLOW;
}
if ((charset == NULL ? NWC24_ERR_INVALID_VALUE : AppendBounded(encoded, encodedCapacity, charsetSize, charset, Mail_strlen(charset))) !=
NWC24_OK) {
*encoded = '\0';
goto done;
}
if (result == NWC24_OK) {
if (Mail_strlen(encoded) + 2 < encodedCapacity) {
Mail_strncat(encoded, "?", 1);
} else {
result = NWC24_ERR_OVERFLOW;
}
if (AppendBounded(encoded, encodedCapacity, 2, "?", 1) != NWC24_OK) {
*encoded = '\0';
goto done;
}
if (result == NWC24_OK) {
{
char value[1];
NWC24Err appendResult;
value[0] = encoding;
if (encoding == 'B' || encoding == 'b' || encoding == 'Q' || encoding == 'q') {
u32 length = Mail_strlen(encoded);
if (length + 1 < encodedCapacity) {
Mail_strncat(encoded, value, 1);
} else {
result = NWC24_ERR_OVERFLOW;
}
appendResult = AppendBounded(encoded, encodedCapacity, 1, value, 1);
} else {
result = NWC24_ERR_INVALID_VALUE;
appendResult = NWC24_ERR_INVALID_VALUE;
}
if (appendResult != NWC24_OK) {
*encoded = '\0';
goto done;
}
}
if (result == NWC24_OK) {
if (AppendBounded(encoded, encodedCapacity, 2, "?", 1) != NWC24_OK) {
*encoded = '\0';
goto done;
}
result = ConcatEncodedText(encoded, encodedCapacity, encodedSize, encoding, decoded, decodedSize, decodedSizeOut);
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
encodeOverflow = result == NWC24_ERR_OVERFLOW;
if (Mail_strlen(encoded) + 2 < encodedCapacity) {
Mail_strncat(encoded, "?", 1);
} else {
result = NWC24_ERR_OVERFLOW;
}
}
if (result == NWC24_OK) {
result = ConcatEncodedText(encoded, encodedCapacity, encodedSize, encoding, decoded, decodedSize, decodedSizeOut);
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
encodeOverflow = result == NWC24_ERR_OVERFLOW;
if (Mail_strlen(encoded) + 2 < encodedCapacity) {
Mail_strncat(encoded, "?=", 2);
result = NWC24_OK;
if (encodedSize != NULL) {
*encodedSize = Mail_strlen(encoded) + 1;
}
if (encodeOverflow) {
result = NWC24_ERR_OVERFLOW;
}
} else {
*encoded = '\0';
if (decodedSizeOut != NULL) {
*decodedSizeOut = 1;
}
Mail_strncat(encoded, "?=", 2);
result = NWC24_OK;
if (encodedSize != NULL) {
*encodedSize = Mail_strlen(encoded) + 1;
}
if (encodeOverflow) {
result = NWC24_ERR_OVERFLOW;
}
} else {
@@ -416,10 +407,16 @@ static NWC24Err EncodeWord(char* encoded, u32 encodedCapacity, u32* encodedSize,
if (decodedSizeOut != NULL) {
*decodedSizeOut = 1;
}
result = NWC24_ERR_OVERFLOW;
}
} else {
*encoded = '\0';
if (decodedSizeOut != NULL) {
*decodedSizeOut = 1;
}
}
done:
return result;
}
@@ -452,7 +449,7 @@ NWC24Err NWC24EncodeWord(u8* encoded, u32 encodedCapacity, u32* encodedSize, cha
return result;
}
static NWC24Err ExtractCharset(char* charset, u32 charsetCapacity, u32* charsetSize, char* encoded, u32 encodedSize);
static NWC24Err ExtractCharset(char* charset, u32 charsetCapacity, u32* charsetSize, char* encoded, u32 encodedSize) NO_INLINE;
static NWC24Err ExtractEncodedText(char* decoded, u32 decodedCapacity, u32* decodedSize, char encoding, char* encoded, u32 encodedSize,
u32* encodedSizeOut);
@@ -506,18 +503,19 @@ static BOOL CopyWithoutLinearWhiteSpaces(char* output, int* outputSize, char* in
static inline char* FindMarker(char* input, u32 size, const char* marker) {
u32 markerLength = Mail_strlen(marker);
u32 offset;
char* scan = input;
for (offset = 0; offset <= size; offset++) {
char* current = input + offset;
if (Mail_strncmp(current, marker, markerLength) == 0)
return current;
if (Mail_strncmp(scan, marker, markerLength) == 0)
return scan;
scan++;
}
return NULL;
}
static NWC24Err DecodeWord(char* charsetData, u32 charsetCapacity, char* decoded, u32 decodedCapacity, u32* decodedSize, char* encoded,
u32 encodedSize, u32* encodedSizeOut) {
int encodedWordPosition = 0;
int encodedLength;
char* encodedWordPosition = NULL;
u32 encodedLength;
u32 consumedSize;
u32 decodedLength;
NWC24Err result = NWC24_OK;
@@ -541,16 +539,16 @@ static NWC24Err DecodeWord(char* charsetData, u32 charsetCapacity, char* decoded
u32 markerLength = Mail_strlen("=?");
u32 offset;
for (offset = 0; offset <= encodedSize; offset++) {
encodedWordPosition = (int)(encoded + offset);
if (Mail_strncmp((char*)encodedWordPosition, "=?", markerLength) == 0) {
encodedWordPosition = (char*)encoded + offset;
if (Mail_strncmp(encodedWordPosition, "=?", markerLength) == 0) {
break;
}
}
if (offset > encodedSize) {
encodedWordPosition = 0;
encodedWordPosition = NULL;
}
}
if (encodedWordPosition == (int)encoded) {
if (encodedWordPosition == (char*)encoded) {
result = ExtractCharset(charsetData, charsetCapacity, &consumedSize, (char*)encoded, encodedSize);
if (result != NWC24_OK) {
return result;
@@ -585,10 +583,10 @@ static NWC24Err DecodeWord(char* charsetData, u32 charsetCapacity, char* decoded
}
} else {
u32 localSize = decodedCapacity;
if (encodedWordPosition == 0) {
if (encodedWordPosition == NULL) {
encodedLength = STD_strnlen(encoded, encodedSize);
} else {
encodedLength = encodedWordPosition - (int)encoded;
encodedLength = encodedWordPosition - (char*)encoded;
}
if (!CopyWithoutLinearWhiteSpaces(decoded, (int*)&localSize, encoded, encodedLength)) {
*decodedSize = localSize;
@@ -598,7 +596,7 @@ static NWC24Err DecodeWord(char* charsetData, u32 charsetCapacity, char* decoded
return NWC24_ERR_OVERFLOW;
}
decodedLength = localSize;
if (encodedWordPosition != 0) {
if (encodedWordPosition != NULL) {
BOOL whitespaceOnly = TRUE;
u32 offset;
for (offset = 0; offset < localSize && decoded[offset] != '\0'; offset++) {
@@ -637,8 +635,6 @@ NWC24Err NWC24DecodeMIMEHeaderFieldBody(u8* charsetData, u32 charsetDataSize, u8
if (decoded == NULL || decodedCapacity == 0) {
return NWC24_ERR_INVALID_VALUE;
}
if (encoded == NULL)
return NWC24_ERR_INVALID_VALUE;
if (encoded == NULL)
return NWC24_ERR_INVALID_VALUE;
*decoded = 0;
@@ -707,17 +703,17 @@ static NWC24Err ExtractCharset(char* charset, u32 charsetCapacity, u32* charsetS
if (start == NULL) {
return NWC24_ERR_NOT_SUPPORTED;
}
start += 2;
end = FindMarker(start, encodedSize - (u32)(start - encoded), "?");
end = FindMarker(start + 2, encodedSize - (u32)(start + 2 - encoded), "?");
if (end == NULL) {
return NWC24_ERR_NOT_SUPPORTED;
}
charsetLength = (u32)(end - start);
if (charsetLength >= charsetCapacity) {
charsetLength = (u32)(end - (start + 2));
if (charsetLength < charsetCapacity) {
Mail_strncpy(charset, start + 2, charsetLength);
charset[charsetLength] = '\0';
} else {
return NWC24_ERR_OVERFLOW;
}
Mail_strncpy(charset, start, charsetLength);
charset[charsetLength] = '\0';
*charsetSize = charsetLength + 2;
return NWC24_OK;
}
@@ -729,6 +725,9 @@ static NWC24Err ExtractEncodedText(char* decoded, u32 decodedCapacity, u32* deco
int length;
NWC24Err result;
if (decoded == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
if (encoded == NULL || decodedSize == NULL) {
return NWC24_ERR_INVALID_VALUE;
}

View File

@@ -424,6 +424,10 @@ static NWC24Err CheckMsgObject(const NWC24MsgObjPrivate* msg) {
return NWC24_OK;
}
static inline u32 EstimateBase64Size(u32 size) {
return (size * 4 + 2) / 3 + size / 57 * 2 + 4;
}
static NWC24Err CheckMsgBoxSpace(const NWC24MsgObjPrivate* msg, NWC24MBoxType type) {
u32 total = 0;
u32 textSize = 0;
@@ -431,7 +435,7 @@ static NWC24Err CheckMsgBoxSpace(const NWC24MsgObjPrivate* msg, NWC24MBoxType ty
NWC24Err err;
for (index = 0; index < msg->numAttached; ++index) {
u32 size = msg->attachedSize[index];
total += (size * 4 + 2) / 3 + size / 57 * 2 + 4;
total += EstimateBase64Size(size);
}
switch (msg->encoding) {
case NWC24_ENC_7BIT:
@@ -439,7 +443,7 @@ static NWC24Err CheckMsgBoxSpace(const NWC24MsgObjPrivate* msg, NWC24MBoxType ty
textSize = msg->text.size;
break;
case NWC24_ENC_BASE64:
textSize = (msg->text.size * 4 + 2) / 3 + msg->text.size / 57 * 2 + 4;
textSize = EstimateBase64Size(msg->text.size);
break;
case NWC24_ENC_QUOTED_PRINTABLE:
textSize = msg->text.size * 4 / 3;

View File

@@ -30,7 +30,7 @@ NWC24Err NWC24ReadMsgField(const NWC24MsgObj* msg, char* fieldName, u8* output,
NWC24Err closeResult;
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool())
return NWC24_ERR_LIB_NOT_OPENED;
if (!(privateMsg->type & 0x200))
if (!(msg->data[1] & 0x200))
return NWC24_ERR_PROTECTED;
result = SelectMBox(privateMsg, &type);
switch (result) {
@@ -276,7 +276,7 @@ NWC24Err NWC24ReadMsgFromAddr(const NWC24MsgObj* msg, char* address, u32 capacit
NWC24Err result, closeResult;
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool())
return NWC24_ERR_LIB_NOT_OPENED;
if (!(privateMsg->type & 0x200))
if (!(msg->data[1] & 0x200))
return NWC24_ERR_PROTECTED;
buffer = NWC24WorkP->stringWork;
Mail_memset(buffer, 0, 1024);
@@ -319,7 +319,7 @@ NWC24Err NWC24ReadMsgSubject(const NWC24MsgObj* msg, char* subject, u32 capacity
NWC24Err result, closeResult;
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool())
return NWC24_ERR_LIB_NOT_OPENED;
if (!(privateMsg->type & 0x200))
if (!(msg->data[1] & 0x200))
return NWC24_ERR_PROTECTED;
result = SelectMBox(privateMsg, &type);
switch (result) {
@@ -396,7 +396,7 @@ static NWC24Err ReadMsgTextInternal(const NWC24MsgObj* msg, char* text, u32 capa
char* buffer;
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool())
return NWC24_ERR_LIB_NOT_OPENED;
if (!(privateMsg->type & 0x200))
if (!(msg->data[1] & 0x200))
return NWC24_ERR_PROTECTED;
buffer = NWC24WorkP->stringWork;
*encoding = 0;
@@ -483,7 +483,7 @@ NWC24Err NWC24ReadMsgAttached(const NWC24MsgObj* msg, u32 index, u8* output, u32
NWC24Err result, closeResult;
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool())
return NWC24_ERR_LIB_NOT_OPENED;
if (!(privateMsg->type & 0x200))
if (!(msg->data[1] & 0x200))
return NWC24_ERR_PROTECTED;
if (index >= privateMsg->numAttached)
return NWC24_ERR_NOT_FOUND;

View File

@@ -67,28 +67,28 @@ NWC24Err NWC24ReadMsgSubjectPublic(const NWC24MsgObj* msg, u16* subject, u32* su
decodedCapacity = workSize - workHalf;
result = NWC24GetMsgSubjectSize(msg, &subjectDataSize);
switch (result) {
default:
goto done;
case NWC24_OK:
if (subjectDataSize > workHalf) {
subjectDataSize = workHalf;
truncated = TRUE;
}
result = NWC24ReadMsgSubject(msg, (char*)work, subjectDataSize);
if (result != NWC24_OK) {
if (result == NWC24_ERR_OVERFLOW) {
truncated = TRUE;
} else {
goto done;
}
if (result != NWC24_OK && result != NWC24_ERR_OVERFLOW) {
goto done;
}
if (result == NWC24_ERR_OVERFLOW) {
truncated = TRUE;
}
charsetBuffer = NWC24WorkP->stringWork;
result =
NWC24DecodeMIMEHeaderFieldBody((u8*)charsetBuffer, 0x40, decodedData, decodedCapacity, &decodedSize, work, subjectDataSize);
if (result != NWC24_OK) {
if (result == NWC24_ERR_OVERFLOW) {
truncated = TRUE;
} else {
goto done;
}
if (result != NWC24_OK && result != NWC24_ERR_OVERFLOW) {
goto done;
}
if (result == NWC24_ERR_OVERFLOW) {
truncated = TRUE;
}
*subjectSize = originalSize;
result =
@@ -104,8 +104,6 @@ NWC24Err NWC24ReadMsgSubjectPublic(const NWC24MsgObj* msg, u16* subject, u32* su
}
}
break;
default:
goto done;
}
}
}
@@ -133,6 +131,8 @@ NWC24Err NWC24ReadMsgTextPublic(const NWC24MsgObj* msg, u16* text, u32* textSize
} else {
result = NWC24GetMsgTextSize(msg, &textDataSize);
switch (result) {
default:
goto done;
case NWC24_OK:
if (textDataSize > workSize) {
textDataSize = workSize;
@@ -140,11 +140,14 @@ NWC24Err NWC24ReadMsgTextPublic(const NWC24MsgObj* msg, u16* text, u32* textSize
}
charsetBuffer = NWC24WorkP->stringWork + 0x20;
result = NWC24ReadMsgTextEx(msg, (char*)work, textDataSize, charsetBuffer, 0x40);
if (result != NWC24_OK) {
if (result != NWC24_ERR_OVERFLOW) {
switch (result) {
case NWC24_OK:
break;
case NWC24_ERR_OVERFLOW:
truncated = TRUE;
break;
default:
goto done;
}
truncated = TRUE;
}
*textSize = originalSize;
@@ -153,8 +156,6 @@ NWC24Err NWC24ReadMsgTextPublic(const NWC24MsgObj* msg, u16* text, u32* textSize
result = NWC24_ERR_OVERFLOW;
}
break;
default:
goto done;
}
}
}
@@ -230,8 +231,8 @@ done:
NWC24Err NWC24SetMsgSubjectAndTextPublic(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, const u16* text, u32 textSize,
NWC24EncodingRegion region, u16 alternative, u8* work, u32 workSize) {
NWC24MsgObjPrivate* privateMsg = (NWC24MsgObjPrivate*)msg;
NWC24Work* nwcWork;
NWC24MsgObjPrivate* privateMsg = (NWC24MsgObjPrivate*)msg;
u32 textSourceSize;
u32 textWorkSize;
BOOL is7Bit;
@@ -817,61 +818,63 @@ done:
NWC24Err NWC24iSetMsgSubjectBase64(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize, NWC24Charset charset, char* charsetName) {
u32 workHalf;
u32 secondSize;
u8* second;
u32 secondSize;
u32 convertedLength;
u32 subjectLength;
u32 lineLength;
u32 outputLength;
u32 lineLength;
u32 sourceOffset;
u32 total;
u32 charsetLength;
NWC24Err result;
workHalf = workSize >> 1;
secondSize = workSize - workHalf;
second = work + workHalf;
secondSize = workSize - workHalf;
charsetLength = strlen(charsetName);
lineLength = ((0x36 - charsetLength) * 3) >> 2;
if (workHalf < lineLength) {
result = NWC24_ERR_OVERFLOW;
} else {
outputLength = lineLength + 1;
subjectLength = subjectSize;
result = NWC24iConvertFromInternalEncoding(work, &outputLength, subject, &subjectLength, charsetName, 0x40, region, alternative);
if (((result == NWC24_OK) || (result == NWC24_ERR_OVERFLOW)) &&
((result = NWC24EncodeWord(second, secondSize, &outputLength, charsetName, 0x40, 'B', work, outputLength - 1)) == NWC24_OK)) {
sourceOffset = subjectLength;
if (sourceOffset == subjectSize) {
result = NWC24SetMsgSubject(msg, (char*)second, outputLength - 1);
} else {
charsetLength = strlen(charsetName);
lineLength = (((0x3E - charsetLength) * 3) >> 2) + 1;
total = outputLength;
for (; sourceOffset < subjectSize; sourceOffset += subjectLength) {
subjectLength = subjectSize - sourceOffset;
workHalf = lineLength;
if (secondSize - (total - 1) < 3) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
second[total - 1] = '\r';
second[total] = '\n';
second[total + 1] = ' ';
total += 2;
result = NWC24iConvertFromInternalEncoding(work, &workHalf, subject + sourceOffset, &subjectLength, charsetName, 0x40, region,
alternative);
if (((result != NWC24_OK) && (result != NWC24_ERR_OVERFLOW)) ||
((result = NWC24EncodeWord(second + total, secondSize - total, &outputLength, charsetName, 0x40, 'B', work, workHalf - 1)) !=
NWC24_OK)) {
goto done;
}
total += outputLength;
}
result = NWC24SetMsgSubject(msg, (char*)second, total - 1);
}
}
if (lineLength > workHalf) {
return NWC24_ERR_OVERFLOW;
}
convertedLength = lineLength + 1;
subjectLength = subjectSize;
result = NWC24iConvertFromInternalEncoding(work, &convertedLength, subject, &subjectLength, charsetName, 0x40, region, alternative);
if (!(result == NWC24_OK || result == NWC24_ERR_OVERFLOW)) {
return result;
}
result = NWC24EncodeWord(second, secondSize, &outputLength, charsetName, 0x40, 'B', work, convertedLength - 1);
if (result != NWC24_OK) {
return result;
}
sourceOffset = subjectLength;
if (sourceOffset == subjectSize) {
result = NWC24SetMsgSubject(msg, (char*)second, outputLength - 1);
} else {
charsetLength = strlen(charsetName);
lineLength = (((0x3E - charsetLength) * 3) >> 2) + 1;
total = outputLength - 1;
for (; sourceOffset < subjectSize; sourceOffset += subjectLength) {
subjectLength = subjectSize - sourceOffset;
convertedLength = lineLength;
if (secondSize - total < 3) {
return NWC24_ERR_OVERFLOW;
}
second[total++] = '\r';
second[total++] = '\n';
second[total++] = ' ';
result = NWC24iConvertFromInternalEncoding(work, &convertedLength, subject + sourceOffset, &subjectLength, charsetName, 0x40,
region, alternative);
if (!(result == NWC24_OK || result == NWC24_ERR_OVERFLOW)) {
return result;
}
result = NWC24EncodeWord(second + total, secondSize - total, &outputLength, charsetName, 0x40, 'B', work, convertedLength - 1);
if (result != NWC24_OK) {
return result;
}
total += outputLength - 1;
}
result = NWC24SetMsgSubject(msg, (char*)second, total);
}
done:
return result;
}