Compare commits
11 Commits
matcher/NH
...
agent/w092
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2521ae15a | ||
|
|
a8c28a85d4 | ||
|
|
4602507d10 | ||
|
|
2cb5852df1 | ||
|
|
c0646f394a | ||
|
|
bc7ef44a94 | ||
|
|
a0ee1d05b1 | ||
|
|
341d5163a3 | ||
|
|
a19cd33c67 | ||
|
|
cea939b665 | ||
|
|
75ca887184 |
@@ -1083,7 +1083,7 @@ config.libs = [
|
||||
Object(NonMatching, "nwc24/NWC24MsgRead.c"),
|
||||
Object(NonMatching, "nwc24/NWC24MsgSubject.c"),
|
||||
Object(Matching, "nwc24/NWC24Schedule.c"),
|
||||
Object(NonMatching, "nwc24/NWC24DateParser.c"),
|
||||
Object(Matching, "nwc24/NWC24DateParser.c"),
|
||||
Object(Matching, "nwc24/NWC24FriendList.c"),
|
||||
Object(Matching, "nwc24/NWC24SecretFList.c"),
|
||||
Object(Matching, "nwc24/NWC24UserId.c"),
|
||||
|
||||
@@ -90,6 +90,7 @@ NWC24Err NWC24FDeleteVF(const char* path);
|
||||
NWC24Err NWC24MountVF(const char* drive, const char* fileName);
|
||||
NWC24Err NWC24UnmountVF(const char* drive);
|
||||
NWC24Err NWC24CheckSizeVF(const char* drive, u32* pSize);
|
||||
NWC24Err NWC24CreateVF(const char* path, u32 fileSize);
|
||||
|
||||
NWC24Err NWC24FStreamInit(NWC24FileStream* stream, NWC24File* file, u32 baseOffset, u32 areaSize, char* buffer, u32 size);
|
||||
u32 NWC24FStreamGetSize(NWC24FileStream* stream);
|
||||
|
||||
@@ -4,12 +4,125 @@
|
||||
#include <revolution/types.h>
|
||||
|
||||
#include <revolution/nwc24/NWC24Err.h>
|
||||
#include <revolution/nwc24/NWC24Types.h>
|
||||
#include <revolution/nwc24/NWC24MsgBoard.h>
|
||||
|
||||
#include <private/nwc24/NWC24File.h>
|
||||
#include <private/nwc24/NWC24Private.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define NWC24_MSG_RECIPIENT_MAX_ 8
|
||||
#define NWC24_MSG_ATTACHMENT_MAX_ 2
|
||||
|
||||
typedef union NWC24iAddr {
|
||||
u64 id;
|
||||
NWC24Data data;
|
||||
} NWC24iAddr;
|
||||
|
||||
typedef enum NWC24MsgBoxId {
|
||||
NWC24_MSGBOX_SEND = 0,
|
||||
NWC24_MSGBOX_RECV = 1,
|
||||
NWC24_MSGBOX_MAX
|
||||
} NWC24MsgBoxId;
|
||||
|
||||
typedef struct NWC24iMBCHeader {
|
||||
u32 magic; // at 0x0
|
||||
u32 version; // at 0x4
|
||||
u32 numMsgs; // at 0x8
|
||||
u32 capacity; // at 0xC
|
||||
u32 totalMsgSize; // at 0x10
|
||||
u32 fileSize; // at 0x14
|
||||
u32 nextMsgId; // at 0x18
|
||||
u32 freeChain; // at 0x1C
|
||||
u32 oldestMsgId; // at 0x20
|
||||
u32 freeSpace; // at 0x24
|
||||
u8 reserved[0x30]; // at 0x28
|
||||
char uidl[0x28]; // at 0x58
|
||||
} NWC24iMBCHeader;
|
||||
|
||||
typedef struct NWC24iMsgObj {
|
||||
u32 id; // at 0x0
|
||||
u32 flags; // at 0x4
|
||||
u32 length; // at 0x8
|
||||
u32 appId; // at 0xC
|
||||
u32 unk10; // at 0x10
|
||||
u32 tag; // at 0x14
|
||||
u32 command; // at 0x18
|
||||
u32 unk1C; // at 0x1C
|
||||
NWC24iAddr from; // at 0x20
|
||||
s32 createTime; // at 0x28
|
||||
u32 unk2C; // at 0x2C
|
||||
NWC24Data fromField; // at 0x30
|
||||
NWC24Data toField; // at 0x38
|
||||
NWC24Data subject; // at 0x40
|
||||
NWC24Data text; // at 0x48
|
||||
NWC24Data contentType; // at 0x50
|
||||
NWC24Data txEncoding; // at 0x58
|
||||
NWC24Charset charset; // at 0x60
|
||||
NWC24Encoding encoding; // at 0x64
|
||||
NWC24Data attached[NWC24_MSG_ATTACHMENT_MAX_]; // at 0x68
|
||||
u32 attachedSize[NWC24_MSG_ATTACHMENT_MAX_]; // at 0x78
|
||||
NWC24MIMEType attachedType[NWC24_MSG_ATTACHMENT_MAX_]; // at 0x80
|
||||
union {
|
||||
NWC24iAddr to[NWC24_MSG_RECIPIENT_MAX_];
|
||||
NWC24Data toAddrs[NWC24_MSG_RECIPIENT_MAX_];
|
||||
}; // at 0x88
|
||||
u8 numTo; // at 0xC8
|
||||
u8 numAttached; // at 0xC9
|
||||
u16 groupId; // at 0xCA
|
||||
u32 msgBoard; // at 0xCC
|
||||
NWC24Data user; // at 0xD0
|
||||
NWC24Data face; // at 0xD8
|
||||
NWC24Data alt; // at 0xE0
|
||||
NWC24Data dwcId; // at 0xE8
|
||||
u32 iconNew; // at 0xF0
|
||||
u8 unkF4[0x100 - 0xF4]; // at 0xF4
|
||||
} NWC24iMsgObj;
|
||||
|
||||
typedef struct NWC24iMBCEntry {
|
||||
u32 id; // at 0x0
|
||||
u32 flags; // at 0x4
|
||||
u32 length; // at 0x8
|
||||
u32 appId; // at 0xC
|
||||
u32 unk10; // at 0x10
|
||||
u32 tag; // at 0x14
|
||||
u32 command; // at 0x18
|
||||
u32 unk1C; // at 0x1C
|
||||
NWC24iAddr from; // at 0x20
|
||||
s32 createTime; // at 0x28
|
||||
u32 unk2C; // at 0x2C
|
||||
u8 numTo; // at 0x30
|
||||
u8 numAttached; // at 0x31
|
||||
u16 groupId; // at 0x32
|
||||
u32 fromField; // at 0x34
|
||||
u32 toField; // at 0x38
|
||||
u32 subject; // at 0x3C
|
||||
u32 contentType; // at 0x40
|
||||
u32 txEncoding; // at 0x44
|
||||
NWC24Data text; // at 0x48
|
||||
NWC24Data attached[NWC24_MSG_ATTACHMENT_MAX_]; // at 0x50
|
||||
u32 attachedSize[NWC24_MSG_ATTACHMENT_MAX_]; // at 0x60
|
||||
NWC24MIMEType attachedType[NWC24_MSG_ATTACHMENT_MAX_]; // at 0x68
|
||||
NWC24Data dwcId; // at 0x70
|
||||
u32 iconNew; // at 0x78
|
||||
u32 unk7C; // at 0x7C
|
||||
} NWC24iMBCEntry;
|
||||
|
||||
NWC24Err NWC24iOpenMBox();
|
||||
NWC24Err NWC24iInitMBox();
|
||||
|
||||
NWC24Err NWC24iMBoxOpenNewMsg(NWC24MsgBoxId id, NWC24File* file, u32* msgId);
|
||||
NWC24Err NWC24iMBoxOpenStoredMsg(NWC24MsgBoxId id, u32 msgId, NWC24File* file);
|
||||
NWC24Err NWC24iMBoxCloseMsg(NWC24File* file);
|
||||
NWC24Err NWC24iMBoxCancelMsg(NWC24File* file, NWC24MsgBoxId id, u32 msgId);
|
||||
NWC24Err NWC24iMBoxAddMsgObj(NWC24MsgBoxId id, const NWC24iMsgObj* msg);
|
||||
NWC24Err NWC24iMBoxFlushHeader(NWC24MsgBoxId id);
|
||||
NWC24Err NWC24iMBoxCheck(NWC24MsgBoxId id, u32 size);
|
||||
NWC24Err NWC24iMBoxSetLastUIDL(NWC24MsgBoxId id, const char* uidl);
|
||||
BOOL NWC24iIsMsgObjReadable(const NWC24iMBCEntry* entry);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ typedef struct NWC24MsgObjPrivate {
|
||||
u32 type; // 0x04
|
||||
u32 length; // 0x08
|
||||
u32 appId; // 0x0C
|
||||
u8 unk_0x10[0x4];
|
||||
u32 unk_0x10; // 0x10
|
||||
u32 tag; // 0x14
|
||||
u32 ledPattern; // 0x18
|
||||
NWC24UserId fromId; // 0x20
|
||||
|
||||
@@ -10,8 +10,11 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct NWC24Data {
|
||||
const void* ptr; // 0x00
|
||||
u32 size; // 0x04
|
||||
union {
|
||||
const void* ptr; // 0x00
|
||||
u32 offset;
|
||||
};
|
||||
u32 size; // 0x04
|
||||
} NWC24Data;
|
||||
|
||||
typedef struct NWC24Work NWC24Work;
|
||||
|
||||
@@ -15,6 +15,7 @@ extern "C" {
|
||||
int STD_strnlen(const char* str, u32 len);
|
||||
|
||||
int Util_xtoi(char ch);
|
||||
int Mail_isdigit(int ch);
|
||||
|
||||
void* Mail_memcpy(void* dest, const void* src, u32 len);
|
||||
void* Mail_memset(void* dest, int fill, u32 count);
|
||||
|
||||
@@ -24,6 +24,9 @@ typedef struct NWC24Date {
|
||||
|
||||
void NWC24Date_Init(NWC24Date* date);
|
||||
|
||||
NWC24Err NWC24iDateToMinutes(s32* pMinutes, const NWC24Date* pDate);
|
||||
NWC24Err NWC24iMinutesToDate(NWC24Date* pDate, s32 minutes);
|
||||
|
||||
void NWC24iConvIdToStr(NWC24UserId addr, char* out);
|
||||
|
||||
NWC24Err NWC24iCheckStringLength(const char* str, int minLen, int maxLen);
|
||||
|
||||
@@ -53,7 +53,7 @@ typedef enum NWC24EncodingRegion {
|
||||
/* NWC24 Message Object */
|
||||
|
||||
typedef struct NWC24MsgObj {
|
||||
u32 data[64]; // 0x00
|
||||
volatile u32 data[64]; // 0x00
|
||||
} NWC24MsgObj;
|
||||
|
||||
#define NWC24_FACE_DATA_SIZE 0x4A /* sizeof(RFLCharData) */
|
||||
|
||||
262
libs/RevoEX/src/nwc24/NWC24DateParser.c
Normal file
262
libs/RevoEX/src/nwc24/NWC24DateParser.c
Normal file
@@ -0,0 +1,262 @@
|
||||
#include <private/nwc24.h>
|
||||
#include <revolution/nwc24.h>
|
||||
|
||||
// The number of seconds between 01/01/1900 and 01/01/1970
|
||||
#define EPOCH_BASE 2208988800LL
|
||||
|
||||
#define NWC24i_YEAR_MIN 1900
|
||||
#define NWC24i_YEAR_DAY_MIN 1
|
||||
#define NWC24i_YEAR_DAY_MAX 365
|
||||
#define NWC24i_MONTH_MIN 1
|
||||
#define NWC24i_MONTH_MAX 12
|
||||
#define NWC24i_DAY_MIN 1
|
||||
#define NWC24i_WEEK_DAY_MAX 7
|
||||
#define NWC24i_HOUR_MAX 24
|
||||
#define NWC24i_MIN_MAX 60
|
||||
#define NWC24i_SEC_MAX 60
|
||||
|
||||
// Length of each month
|
||||
static const u8 DAYS_OF_MONTH[NWC24i_MONTH_MAX] = {31, 28, 31, 30, 31, 30,
|
||||
31, 31, 30, 31, 30, 31};
|
||||
// End of each month
|
||||
static const u16 DAYS_OF_YEAR[NWC24i_MONTH_MAX] = {
|
||||
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
|
||||
|
||||
// Forward declarations
|
||||
static BOOL IsLeapYear(u16 year);
|
||||
static s32 ConvertDateToDays(u16 year, u8 month, u8 day);
|
||||
static void ConvertDaysToDate(u16* pYear, u8* pMonth, u8* pDay, s32 days);
|
||||
static s32 ConvertTimeToMinutes(u8 hour, u8 min);
|
||||
static s32 ConvertDaysToMinutes(s32 days);
|
||||
static void ConvertMinutesToDays(s32* pDays, u8* pHour, u8* pMin, s32 minutes);
|
||||
static s32 GetDayOfWeek(s32 days);
|
||||
|
||||
NWC24Err NWC24iDateToMinutes(s32* pMinutes, const NWC24Date* pDate) {
|
||||
s32 days;
|
||||
s32 minutes;
|
||||
|
||||
days = ConvertDateToDays(pDate->year, pDate->month, pDate->day);
|
||||
if (days == -1) {
|
||||
return NWC24_ERR_FAILED;
|
||||
}
|
||||
|
||||
minutes = ConvertTimeToMinutes(pDate->hour, pDate->min);
|
||||
if (minutes == -1 || pDate->sec > NWC24i_SEC_MAX) {
|
||||
return NWC24_ERR_FAILED;
|
||||
}
|
||||
|
||||
*pMinutes = ConvertDaysToMinutes(days) + minutes;
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
NWC24Err NWC24iMinutesToDate(NWC24Date* pDate, s32 minutes) {
|
||||
s32 days;
|
||||
|
||||
ConvertMinutesToDays(&days, &pDate->hour, &pDate->min, minutes);
|
||||
ConvertDaysToDate(&pDate->year, &pDate->month, &pDate->day, days);
|
||||
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
NWC24Err NWC24iEpochSecondsToDate(NWC24Date* pDate, s64 timestamp) {
|
||||
s32 minutes;
|
||||
s32 days;
|
||||
|
||||
if (timestamp + EPOCH_BASE < 0) {
|
||||
timestamp = -EPOCH_BASE;
|
||||
}
|
||||
|
||||
pDate->sec = (timestamp + EPOCH_BASE) % NWC24i_MIN_MAX;
|
||||
minutes = (timestamp + EPOCH_BASE) / NWC24i_MIN_MAX;
|
||||
|
||||
ConvertMinutesToDays(&days, &pDate->hour, &pDate->min, minutes);
|
||||
ConvertDaysToDate(&pDate->year, &pDate->month, &pDate->day, days);
|
||||
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
NWC24Err NWC24iMinutesToOSCalendarTime(OSCalendarTime* pOSTime, s32 minutes) {
|
||||
s32 days;
|
||||
NWC24Date date;
|
||||
|
||||
ConvertMinutesToDays(&days, &date.hour, &date.min, minutes);
|
||||
ConvertDaysToDate(&date.year, &date.month, &date.day, days);
|
||||
|
||||
pOSTime->year = date.year;
|
||||
pOSTime->mon = date.month - 1;
|
||||
pOSTime->mday = date.day;
|
||||
pOSTime->hour = date.hour;
|
||||
pOSTime->min = date.min;
|
||||
pOSTime->sec = 0;
|
||||
pOSTime->msec = 0;
|
||||
pOSTime->usec = 0;
|
||||
pOSTime->yday = (DAYS_OF_YEAR[date.month - 1] + date.day) - NWC24i_YEAR_DAY_MIN;
|
||||
|
||||
// Leap year adds an extra day (February 29th)
|
||||
if (IsLeapYear(date.year) && date.month > 2) {
|
||||
pOSTime->yday++;
|
||||
}
|
||||
|
||||
pOSTime->wday = GetDayOfWeek(days);
|
||||
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
NWC24Err NWC24iDateToOSCalendarTime(OSCalendarTime* pOSTime,
|
||||
const NWC24Date* pDate) {
|
||||
|
||||
pOSTime->year = pDate->year;
|
||||
pOSTime->mon = pDate->month - 1;
|
||||
pOSTime->mday = pDate->day;
|
||||
pOSTime->hour = pDate->hour;
|
||||
pOSTime->min = pDate->min;
|
||||
pOSTime->sec = pDate->sec;
|
||||
pOSTime->msec = 0;
|
||||
pOSTime->usec = 0;
|
||||
pOSTime->yday =
|
||||
(DAYS_OF_YEAR[pOSTime->mon] + pDate->day) - NWC24i_YEAR_DAY_MIN;
|
||||
|
||||
// Leap year adds an extra day (February 29th)
|
||||
if (IsLeapYear(pDate->year) && pDate->month > 2) {
|
||||
pOSTime->yday++;
|
||||
}
|
||||
|
||||
pOSTime->wday =
|
||||
GetDayOfWeek(ConvertDateToDays(pDate->year, pDate->month, pDate->day));
|
||||
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
NWC24Err NWC24iIsValidDate(u16 year, u8 month, u8 day) {
|
||||
s32 days = ConvertDateToDays(year, month, day);
|
||||
NWC24Err result = NWC24_OK;
|
||||
|
||||
if (days == -1) {
|
||||
result = NWC24_ERR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static BOOL IsLeapYear(u16 year) {
|
||||
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
|
||||
}
|
||||
|
||||
static s32 ConvertDateToDays(u16 year, u8 month, u8 day) {
|
||||
s32 dayOfYear;
|
||||
s32 yearsSince1900;
|
||||
s32 days;
|
||||
s32 leapDays;
|
||||
s32 gregorianCorrection;
|
||||
|
||||
if (year < NWC24i_YEAR_MIN || month < NWC24i_MONTH_MIN ||
|
||||
month > NWC24i_MONTH_MAX) {
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Leap year adds an extra day (February 29th)
|
||||
if (month == 2 && IsLeapYear(year)) {
|
||||
if (day < NWC24i_DAY_MIN || day > 29) {
|
||||
return -1;
|
||||
}
|
||||
} else if (day < NWC24i_DAY_MIN ||
|
||||
DAYS_OF_MONTH[month - NWC24i_MONTH_MIN] < day) {
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
dayOfYear = day - 1;
|
||||
dayOfYear += DAYS_OF_YEAR[month - 1];
|
||||
|
||||
// Leap year adds an extra day (February 29th)
|
||||
if (month >= 3 && IsLeapYear(year)) {
|
||||
dayOfYear++;
|
||||
}
|
||||
|
||||
yearsSince1900 = year - NWC24i_YEAR_MIN;
|
||||
leapDays = (yearsSince1900 - 1) / 4 - (yearsSince1900 - 1) / 100;
|
||||
|
||||
// Relative to YEAR_MIN=1900
|
||||
gregorianCorrection = (yearsSince1900 + 299) / 400;
|
||||
|
||||
days = yearsSince1900 * NWC24i_YEAR_DAY_MAX + dayOfYear;
|
||||
days += leapDays + gregorianCorrection;
|
||||
|
||||
return days;
|
||||
}
|
||||
|
||||
static void ConvertDaysToDate(u16* pYear, u8* pMonth, u8* pDay, s32 days) {
|
||||
s32 backup;
|
||||
|
||||
*pYear = NWC24i_YEAR_MIN;
|
||||
*pMonth = NWC24i_MONTH_MIN;
|
||||
*pDay = NWC24i_DAY_MIN;
|
||||
|
||||
if (days < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (TRUE) {
|
||||
backup = days;
|
||||
|
||||
days -=
|
||||
IsLeapYear(*pYear) ? NWC24i_YEAR_DAY_MAX + 1 : NWC24i_YEAR_DAY_MAX;
|
||||
|
||||
if (days < 0) {
|
||||
days = backup;
|
||||
break;
|
||||
}
|
||||
|
||||
(*pYear)++;
|
||||
}
|
||||
|
||||
while (TRUE) {
|
||||
backup = days;
|
||||
|
||||
// Leap year adds an extra day (February 29th)
|
||||
if (*pMonth == 2 && IsLeapYear(*pYear)) {
|
||||
days -= 29;
|
||||
} else {
|
||||
days -= DAYS_OF_MONTH[*pMonth - NWC24i_DAY_MIN];
|
||||
}
|
||||
|
||||
if (days < 0) {
|
||||
days = backup;
|
||||
break;
|
||||
}
|
||||
|
||||
(*pMonth)++;
|
||||
}
|
||||
|
||||
*pDay += days;
|
||||
}
|
||||
|
||||
static s32 ConvertTimeToMinutes(u8 hour, u8 min) {
|
||||
// Hour/minute are zero indexed
|
||||
if (hour > NWC24i_HOUR_MAX - 1 || min > NWC24i_MIN_MAX - 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return hour * NWC24i_MIN_MAX + min;
|
||||
}
|
||||
|
||||
static s32 ConvertDaysToMinutes(s32 days) {
|
||||
return days * NWC24i_HOUR_MAX * NWC24i_MIN_MAX;
|
||||
}
|
||||
|
||||
static void ConvertMinutesToDays(s32* pDays, u8* pHour, u8* pMin, s32 minutes) {
|
||||
if (minutes < 0) {
|
||||
minutes = 0;
|
||||
}
|
||||
|
||||
*pDays = minutes / (NWC24i_MIN_MAX * NWC24i_HOUR_MAX);
|
||||
minutes %= (NWC24i_MIN_MAX * NWC24i_HOUR_MAX);
|
||||
|
||||
*pHour = minutes / NWC24i_MIN_MAX;
|
||||
*pMin = minutes % NWC24i_MIN_MAX;
|
||||
}
|
||||
|
||||
static s32 GetDayOfWeek(s32 days) {
|
||||
return (days + NWC24i_DAY_MIN) % NWC24i_WEEK_DAY_MAX;
|
||||
}
|
||||
1510
libs/RevoEX/src/nwc24/NWC24MBoxCtrl.c
Normal file
1510
libs/RevoEX/src/nwc24/NWC24MBoxCtrl.c
Normal file
File diff suppressed because it is too large
Load Diff
@@ -295,7 +295,6 @@ extern void _savegpr_27();
|
||||
extern void _restgpr_27();
|
||||
extern void NCDiGetEnabledConfigList();
|
||||
extern NWC24Err AnalyzeScdErrors(s32* errorCode, u32 usage);
|
||||
extern void NWC24iMBoxCheck();
|
||||
extern void SCCheckStatus();
|
||||
extern void SCGetWCFlags();
|
||||
extern void SCGetEULA();
|
||||
|
||||
1024
libs/RevoEX/src/nwc24/NWC24Mime.c
Normal file
1024
libs/RevoEX/src/nwc24/NWC24Mime.c
Normal file
File diff suppressed because it is too large
Load Diff
1593
libs/RevoEX/src/nwc24/NWC24MsgCommit.c
Normal file
1593
libs/RevoEX/src/nwc24/NWC24MsgCommit.c
Normal file
File diff suppressed because it is too large
Load Diff
878
libs/RevoEX/src/nwc24/NWC24MsgRead.c
Normal file
878
libs/RevoEX/src/nwc24/NWC24MsgRead.c
Normal file
@@ -0,0 +1,878 @@
|
||||
#include <private/nwc24.h>
|
||||
#include <revolution/nwc24.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define NWC24i_STRING_WORK_SIZE 1024
|
||||
|
||||
enum {
|
||||
MSG_OBJ_FOR_RECIPIENT = (1 << 0),
|
||||
MSG_OBJ_FOR_PUBLIC = (1 << 1),
|
||||
MSG_OBJ_FOR_APP = (1 << 2),
|
||||
MSG_OBJ_FOR_MENU = (1 << 3),
|
||||
MSG_OBJ_TO_SEND = (1 << 4),
|
||||
MSG_OBJ_TO_RECV = (1 << 5),
|
||||
MSG_OBJ_INITIALIZED = (1 << 8),
|
||||
MSG_OBJ_DELIVERING = (1 << 9),
|
||||
MSG_OBJ_FLAG_12 = (1 << 12),
|
||||
};
|
||||
|
||||
static NWC24Err ReadMsgTextInternal(const NWC24MsgObj* msg, char* text,
|
||||
u32 textLen, char* name, u32 nameLen,
|
||||
NWC24Encoding* encoding);
|
||||
static NWC24Err ReadBase64Data(NWC24File* file, const NWC24Data* data,
|
||||
u8* out, u32 outSize, u32* outLen);
|
||||
static NWC24Err ReadQPText(const NWC24MsgObj* msg, NWC24File* file, char* text,
|
||||
u32 textSize);
|
||||
|
||||
NWC24Err NWC24ReadMsgField(const NWC24MsgObj* msg, char* fieldName,
|
||||
u8* fieldBuf, u32 fieldBufLen) {
|
||||
const NWC24MsgObjPrivate* pMsg = (const NWC24MsgObjPrivate*)msg;
|
||||
NWC24Err result;
|
||||
NWC24Err closeResult;
|
||||
NWC24MsgBoxId id;
|
||||
u32 flags;
|
||||
char* pField;
|
||||
u32 fieldOffset;
|
||||
u32 fieldLength;
|
||||
NWC24FileStream stream;
|
||||
NWC24File file;
|
||||
|
||||
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool()) {
|
||||
return NWC24_ERR_LIB_NOT_OPENED;
|
||||
}
|
||||
|
||||
if (!(msg->data[1] & MSG_OBJ_DELIVERING)) {
|
||||
return NWC24_ERR_PROTECTED;
|
||||
}
|
||||
|
||||
flags = pMsg->type;
|
||||
if (flags & MSG_OBJ_TO_SEND) {
|
||||
id = NWC24_MSGBOX_SEND;
|
||||
} else if (flags & MSG_OBJ_TO_RECV) {
|
||||
id = NWC24_MSGBOX_RECV;
|
||||
} else {
|
||||
result = NWC24_ERR_INVALID_VALUE;
|
||||
goto select_done;
|
||||
}
|
||||
result = NWC24_OK;
|
||||
select_done:
|
||||
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = NWC24iMBoxOpenStoredMsg(id, pMsg->msgId, &file);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = NWC24FStreamInit(&stream, &file, 0, pMsg->length,
|
||||
NWC24WorkP->mainWork, NWC24i_STRING_WORK_SIZE);
|
||||
if (result == NWC24_OK) {
|
||||
result = NWC24iSearchHeaderF(&stream, fieldName, pMsg->unk_0x10,
|
||||
&fieldOffset, &fieldLength);
|
||||
|
||||
if (result == NWC24_OK) {
|
||||
if (fieldLength > fieldBufLen) {
|
||||
result = NWC24_ERR_OVERFLOW;
|
||||
} else {
|
||||
result = NWC24FStreamSeek(&stream, fieldOffset);
|
||||
if (result == NWC24_OK) {
|
||||
result = NWC24FStreamGetPtr(&stream, &pField, fieldLength);
|
||||
if (result == NWC24_OK) {
|
||||
Mail_strncpy((char*)fieldBuf, pField, fieldLength);
|
||||
fieldBuf[fieldLength - 2] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closeResult = NWC24iMBoxCloseMsg(&file);
|
||||
if (result == NWC24_OK) {
|
||||
result = closeResult;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgFaceData(const NWC24MsgObj* msg, u8* faceData) {
|
||||
const NWC24MsgObjPrivate* pMsg = (const NWC24MsgObjPrivate*)msg;
|
||||
char* pWork;
|
||||
NWC24Err result;
|
||||
u32 decodeSize;
|
||||
u32 length;
|
||||
|
||||
if ((pMsg->type & MSG_OBJ_FOR_PUBLIC) && !(pMsg->type & MSG_OBJ_FLAG_12)) {
|
||||
return NWC24_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
pWork = NWC24WorkP->stringWork;
|
||||
Mail_memset(pWork, 0, NWC24i_STRING_WORK_SIZE);
|
||||
|
||||
result = NWC24ReadMsgField(msg, "x-wiiface", (u8*)pWork,
|
||||
NWC24i_STRING_WORK_SIZE);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
length = STD_strnlen(pWork, NWC24i_STRING_WORK_SIZE);
|
||||
result = NWC24Base64Decode((u8*)pWork, length, faceData, NWC24_FACE_DATA_SIZE,
|
||||
&decodeSize);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (decodeSize != NWC24_FACE_DATA_SIZE) {
|
||||
return NWC24_ERR_FORMAT;
|
||||
}
|
||||
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgAltName(const NWC24MsgObj* msg, u16* altName,
|
||||
u32 altNameLen) {
|
||||
char* pWork = NWC24WorkP->stringWork;
|
||||
NWC24Err result;
|
||||
u32 decodeSize;
|
||||
u32 length;
|
||||
|
||||
Mail_memset(pWork, 0, NWC24i_STRING_WORK_SIZE);
|
||||
|
||||
result = NWC24ReadMsgField(msg, "x-wii-altname:", (u8*)pWork,
|
||||
NWC24i_STRING_WORK_SIZE);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
length = STD_strnlen(pWork, NWC24i_STRING_WORK_SIZE);
|
||||
result = NWC24Base64Decode((u8*)pWork, length, (u8*)altName, altNameLen * 2,
|
||||
&decodeSize);
|
||||
|
||||
return result != NWC24_OK ? result : NWC24_OK;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgMBNoReply(const NWC24MsgObj* msg, BOOL* mbNoReplyFlag) {
|
||||
char* pWork;
|
||||
NWC24Err result;
|
||||
|
||||
*mbNoReplyFlag = FALSE;
|
||||
|
||||
pWork = NWC24WorkP->stringWork;
|
||||
Mail_memset(pWork, 0, NWC24i_STRING_WORK_SIZE);
|
||||
|
||||
result = NWC24ReadMsgField(msg, "x-wii-mb-noreply:", (u8*)pWork,
|
||||
NWC24i_STRING_WORK_SIZE);
|
||||
if (result == NWC24_OK) {
|
||||
*mbNoReplyFlag = TRUE;
|
||||
} else if (result == NWC24_ERR_NOT_FOUND) {
|
||||
result = NWC24_OK;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgMBRegDate(const NWC24MsgObj* msg, u16* year, u8* month,
|
||||
u8* day) {
|
||||
NWC24Err result;
|
||||
char* pWork = NWC24WorkP->stringWork;
|
||||
s32 digit;
|
||||
u32 i;
|
||||
u32 digits;
|
||||
u32 value;
|
||||
char* p;
|
||||
|
||||
i = 0;
|
||||
digits = 0;
|
||||
value = 0;
|
||||
|
||||
Mail_memset(pWork, 0, NWC24i_STRING_WORK_SIZE);
|
||||
|
||||
result = NWC24ReadMsgField(msg, "x-wii-mb-regdate:", (u8*)pWork,
|
||||
NWC24i_STRING_WORK_SIZE - 1);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
p = pWork;
|
||||
while (*p == '\t' || *p == ' ') {
|
||||
p++;
|
||||
i++;
|
||||
}
|
||||
|
||||
while (i < NWC24i_STRING_WORK_SIZE) {
|
||||
digit = Util_xtoi(*p);
|
||||
if (digit < 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
value = (value << 4) | digit;
|
||||
p++;
|
||||
i++;
|
||||
digits++;
|
||||
|
||||
}
|
||||
|
||||
if (digits != 4) {
|
||||
result = NWC24_ERR_FORMAT;
|
||||
}
|
||||
|
||||
*year = ((value >> 9) & 0x7F) + 2000;
|
||||
*month = (value >> 5) & 0xF;
|
||||
*day = value & 0x1F;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgMBDelay(const NWC24MsgObj* msg, u8* mbDelay) {
|
||||
NWC24Err result;
|
||||
char* pWork = NWC24WorkP->stringWork;
|
||||
s32 digit;
|
||||
u32 i;
|
||||
u32 digits;
|
||||
u32 value;
|
||||
char* p;
|
||||
|
||||
i = 0;
|
||||
digits = 0;
|
||||
value = 0;
|
||||
|
||||
Mail_memset(pWork, 0, NWC24i_STRING_WORK_SIZE);
|
||||
|
||||
result = NWC24ReadMsgField(msg, "x-wii-mb-delay:", (u8*)pWork,
|
||||
NWC24i_STRING_WORK_SIZE - 1);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
p = pWork;
|
||||
while (*p == '\t' || *p == ' ') {
|
||||
p++;
|
||||
i++;
|
||||
}
|
||||
|
||||
while (i < NWC24i_STRING_WORK_SIZE) {
|
||||
digit = Util_xtoi(*p);
|
||||
if (digit < 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
value = (value << 4) | digit;
|
||||
p++;
|
||||
i++;
|
||||
digits++;
|
||||
|
||||
}
|
||||
|
||||
if (digits != 2) {
|
||||
result = NWC24_ERR_FORMAT;
|
||||
}
|
||||
|
||||
*mbDelay = value;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgMBUpdateSW(const NWC24MsgObj* msg, u32* mbUpdateSW) {
|
||||
const NWC24MsgObjPrivate* pMsg = (const NWC24MsgObjPrivate*)msg;
|
||||
NWC24Err result;
|
||||
u32 value;
|
||||
char* pWork;
|
||||
|
||||
value = 0;
|
||||
|
||||
if (!(pMsg->type & MSG_OBJ_FLAG_12)) {
|
||||
*mbUpdateSW = 0;
|
||||
return NWC24_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
pWork = NWC24WorkP->stringWork;
|
||||
Mail_memset(pWork, 0, NWC24i_STRING_WORK_SIZE);
|
||||
|
||||
result = NWC24ReadMsgField(msg, "x-wii-mb-updatesw:", (u8*)pWork,
|
||||
NWC24i_STRING_WORK_SIZE - 1);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
while (*pWork == '\t' || *pWork == ' ') {
|
||||
pWork++;
|
||||
}
|
||||
|
||||
if (Mail_isdigit(*pWork)) {
|
||||
value = *pWork - '0';
|
||||
}
|
||||
|
||||
*mbUpdateSW = value;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgMBOptOutFlag(const NWC24MsgObj* msg, BOOL* mbOptOutFlag,
|
||||
u32* appId) {
|
||||
NWC24Err result;
|
||||
char* pWork = NWC24WorkP->stringWork;
|
||||
s32 digit;
|
||||
u32 i;
|
||||
u32 digits;
|
||||
u32 value;
|
||||
char* p;
|
||||
|
||||
i = 0;
|
||||
digits = 0;
|
||||
value = 0;
|
||||
|
||||
Mail_memset(pWork, 0, NWC24i_STRING_WORK_SIZE);
|
||||
|
||||
*mbOptOutFlag = FALSE;
|
||||
*appId = 0;
|
||||
|
||||
result = NWC24ReadMsgField(msg, "x-wii-mb-optout:", (u8*)pWork,
|
||||
NWC24i_STRING_WORK_SIZE - 1);
|
||||
if (result == NWC24_ERR_NOT_FOUND) {
|
||||
return NWC24_OK;
|
||||
}
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
p = pWork;
|
||||
while (*p == '\t' || *p == ' ') {
|
||||
p++;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (*p == '1') {
|
||||
*mbOptOutFlag = TRUE;
|
||||
}
|
||||
|
||||
p += 2;
|
||||
if (p[-1] != '-') {
|
||||
return NWC24_OK;
|
||||
}
|
||||
i += 2;
|
||||
|
||||
while (i < NWC24i_STRING_WORK_SIZE) {
|
||||
digit = Util_xtoi(*p);
|
||||
if (digit < 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
value = (value << 4) | digit;
|
||||
p++;
|
||||
i++;
|
||||
digits++;
|
||||
|
||||
}
|
||||
|
||||
if (digits != 8) {
|
||||
result = NWC24_ERR_FORMAT;
|
||||
}
|
||||
|
||||
*appId = value;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgFromAddr(const NWC24MsgObj* msg, char* addr,
|
||||
u32 addrLen) {
|
||||
const NWC24MsgObjPrivate* pMsg = (const NWC24MsgObjPrivate*)msg;
|
||||
char* pWork;
|
||||
NWC24Err result;
|
||||
NWC24Err closeResult;
|
||||
NWC24MsgBoxId id;
|
||||
u32 flags;
|
||||
NWC24FileStream stream;
|
||||
NWC24File file;
|
||||
|
||||
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool()) {
|
||||
return NWC24_ERR_LIB_NOT_OPENED;
|
||||
}
|
||||
|
||||
if (!(msg->data[1] & MSG_OBJ_DELIVERING)) {
|
||||
return NWC24_ERR_PROTECTED;
|
||||
}
|
||||
|
||||
pWork = NWC24WorkP->stringWork;
|
||||
Mail_memset(pWork, 0, NWC24i_STRING_WORK_SIZE);
|
||||
|
||||
if (!(msg->data[1] & MSG_OBJ_FOR_PUBLIC)) {
|
||||
return NWC24_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
flags = pMsg->type;
|
||||
if (flags & MSG_OBJ_TO_SEND) {
|
||||
id = NWC24_MSGBOX_SEND;
|
||||
} else if (flags & MSG_OBJ_TO_RECV) {
|
||||
id = NWC24_MSGBOX_RECV;
|
||||
} else {
|
||||
result = NWC24_ERR_INVALID_VALUE;
|
||||
goto select_done;
|
||||
}
|
||||
result = NWC24_OK;
|
||||
|
||||
select_done:
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (pMsg->unk_0x30.size == 0) {
|
||||
return NWC24_ERR_NULL;
|
||||
}
|
||||
|
||||
result = NWC24iMBoxOpenStoredMsg(id, pMsg->msgId, &file);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = NWC24FStreamInit(&stream, &file, 0, pMsg->length, pWork,
|
||||
NWC24i_STRING_WORK_SIZE);
|
||||
if (result == NWC24_OK) {
|
||||
result = NWC24FStreamSeek(&stream, pMsg->unk_0x30.offset);
|
||||
if (result == NWC24_OK) {
|
||||
result = NWC24iExtractAddrSpec(&stream, pMsg->unk_0x30.size, 0,
|
||||
addr, addrLen);
|
||||
}
|
||||
}
|
||||
|
||||
closeResult = NWC24iMBoxCloseMsg(&file);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
return closeResult;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgSubject(const NWC24MsgObj* msg, char* subject,
|
||||
u32 subjectLen) {
|
||||
const NWC24MsgObjPrivate* pMsg = (const NWC24MsgObjPrivate*)msg;
|
||||
NWC24Err result;
|
||||
NWC24Err closeResult;
|
||||
NWC24MsgBoxId id;
|
||||
u32 flags;
|
||||
u32 length;
|
||||
NWC24File file;
|
||||
|
||||
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool()) {
|
||||
return NWC24_ERR_LIB_NOT_OPENED;
|
||||
}
|
||||
|
||||
if (!(msg->data[1] & MSG_OBJ_DELIVERING)) {
|
||||
return NWC24_ERR_PROTECTED;
|
||||
}
|
||||
|
||||
flags = pMsg->type;
|
||||
if (flags & MSG_OBJ_TO_SEND) {
|
||||
id = NWC24_MSGBOX_SEND;
|
||||
} else if (flags & MSG_OBJ_TO_RECV) {
|
||||
id = NWC24_MSGBOX_RECV;
|
||||
} else {
|
||||
result = NWC24_ERR_INVALID_VALUE;
|
||||
goto select_done;
|
||||
}
|
||||
result = NWC24_OK;
|
||||
|
||||
select_done:
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
length = pMsg->subject.size;
|
||||
if (length == 0) {
|
||||
return NWC24_ERR_NULL;
|
||||
}
|
||||
|
||||
result = NWC24iMBoxOpenStoredMsg(id, pMsg->msgId, &file);
|
||||
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (length > subjectLen - 1) {
|
||||
length = subjectLen - 1;
|
||||
}
|
||||
|
||||
NWC24FSeek(&file, pMsg->subject.offset, NWC24_SEEK_BEG);
|
||||
result = NWC24FRead(subject, length, &file);
|
||||
if (result == NWC24_OK) {
|
||||
subject[length] = '\0';
|
||||
if (pMsg->subject.size > subjectLen - 1) {
|
||||
result = NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
}
|
||||
|
||||
closeResult = NWC24iMBoxCloseMsg(&file);
|
||||
if (result == NWC24_OK) {
|
||||
result = closeResult;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgText(const NWC24MsgObj* msg, char* text, u32 textLen,
|
||||
NWC24Charset* charset, NWC24Encoding* encoding) {
|
||||
NWC24Err result;
|
||||
char* charsetStr = (char*)NWC24WorkP + 0x20;
|
||||
|
||||
result = ReadMsgTextInternal(msg, text, textLen, charsetStr, 0x40,
|
||||
encoding);
|
||||
if (result == NWC24_OK) {
|
||||
result = NWC24ParseCharsetStr(charset, charsetStr);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgTextEx(const NWC24MsgObj* msg, char* text, u32 textLen,
|
||||
const char* name, u32 nameLen,
|
||||
NWC24Charset* charset, NWC24Encoding* encoding) {
|
||||
NWC24Err result;
|
||||
NWC24Encoding enc;
|
||||
char* pName;
|
||||
char c;
|
||||
u32 i;
|
||||
|
||||
result = ReadMsgTextInternal(msg, text, textLen, (char*)name, nameLen,
|
||||
&enc);
|
||||
if (result == NWC24_OK) {
|
||||
pName = (char*)name;
|
||||
for (i = 0; i < nameLen; i++) {
|
||||
c = *pName;
|
||||
switch (c) {
|
||||
case ' ':
|
||||
case '\r':
|
||||
case '\t':
|
||||
case '\n':
|
||||
case ';':
|
||||
case '"':
|
||||
*pName = '\0';
|
||||
break;
|
||||
}
|
||||
|
||||
if (*pName == '\0') {
|
||||
break;
|
||||
}
|
||||
pName++;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static NWC24Err ReadMsgTextInternal(const NWC24MsgObj* msg, char* text,
|
||||
u32 textLen, char* name, u32 nameLen,
|
||||
NWC24Encoding* encoding) {
|
||||
const NWC24MsgObjPrivate* pMsg = (const NWC24MsgObjPrivate*)msg;
|
||||
NWC24Err retCode;
|
||||
NWC24Err result;
|
||||
NWC24Err closeResult;
|
||||
NWC24MsgBoxId id;
|
||||
u32 length;
|
||||
u32 decodeSize;
|
||||
u32 flags;
|
||||
char* pWork;
|
||||
NWC24File file;
|
||||
|
||||
retCode = NWC24_OK;
|
||||
|
||||
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool()) {
|
||||
return NWC24_ERR_LIB_NOT_OPENED;
|
||||
}
|
||||
|
||||
if (!(msg->data[1] & MSG_OBJ_DELIVERING)) {
|
||||
return NWC24_ERR_PROTECTED;
|
||||
}
|
||||
|
||||
pWork = NWC24WorkP->stringWork;
|
||||
if (pMsg->type & MSG_OBJ_TO_SEND) {
|
||||
id = NWC24_MSGBOX_SEND;
|
||||
} else if (pMsg->type & MSG_OBJ_TO_RECV) {
|
||||
id = NWC24_MSGBOX_RECV;
|
||||
} else {
|
||||
result = NWC24_ERR_INVALID_VALUE;
|
||||
goto select_done;
|
||||
}
|
||||
result = NWC24_OK;
|
||||
*encoding = NWC24_ENC_7BIT;
|
||||
|
||||
select_done:
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (pMsg->textSize != 0) {
|
||||
length = pMsg->textSize;
|
||||
} else {
|
||||
length = pMsg->text.size;
|
||||
}
|
||||
if (length == 0) {
|
||||
return NWC24_ERR_NULL;
|
||||
}
|
||||
|
||||
if (length > textLen - 1) {
|
||||
length = textLen - 1;
|
||||
retCode = NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
|
||||
result = NWC24iMBoxOpenStoredMsg(id, pMsg->msgId, &file);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Mail_memset(name, 0, nameLen);
|
||||
|
||||
if (pMsg->unk_0x50.size >= nameLen) {
|
||||
result = NWC24_ERR_FORMAT;
|
||||
} else {
|
||||
if (pMsg->unk_0x50.size != 0) {
|
||||
NWC24FSeek(&file, pMsg->unk_0x50.offset, NWC24_SEEK_BEG);
|
||||
result = NWC24FRead(name, pMsg->unk_0x50.size, &file);
|
||||
}
|
||||
|
||||
if (result == NWC24_OK && pMsg->unk_0x58.size != 0 &&
|
||||
pMsg->unk_0x58.size < 0x20) {
|
||||
Mail_memset(pWork, 0, 0x20);
|
||||
NWC24FSeek(&file, pMsg->unk_0x58.offset, NWC24_SEEK_BEG);
|
||||
result = NWC24FRead(pWork, pMsg->unk_0x58.size, &file);
|
||||
if (result == NWC24_OK) {
|
||||
result = NWC24ParseEncodingStr(encoding, pWork);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == NWC24_OK) {
|
||||
NWC24FSeek(&file, pMsg->text.offset, NWC24_SEEK_BEG);
|
||||
switch (*encoding) {
|
||||
case NWC24_ENC_7BIT:
|
||||
case NWC24_ENC_8BIT:
|
||||
result = NWC24FRead(text, length, &file);
|
||||
text[length] = '\0';
|
||||
break;
|
||||
|
||||
case NWC24_ENC_BASE64:
|
||||
decodeSize = 0;
|
||||
result = ReadBase64Data(&file, &pMsg->text, (u8*)text, length,
|
||||
&decodeSize);
|
||||
if (result == NWC24_OK && decodeSize != pMsg->textSize) {
|
||||
result = NWC24_ERR_FORMAT;
|
||||
}
|
||||
text[length] = '\0';
|
||||
break;
|
||||
|
||||
case NWC24_ENC_QUOTED_PRINTABLE:
|
||||
result = ReadQPText(msg, &file, text, length);
|
||||
break;
|
||||
|
||||
default:
|
||||
result = NWC24_ERR_NOT_SUPPORTED;
|
||||
text[0] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closeResult = NWC24iMBoxCloseMsg(&file);
|
||||
if (result != NWC24_OK) {
|
||||
closeResult = result;
|
||||
}
|
||||
if (closeResult != NWC24_OK) {
|
||||
retCode = closeResult;
|
||||
}
|
||||
|
||||
return retCode;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgAttached(const NWC24MsgObj* msg, u32 attachIndex,
|
||||
u8* attachData, u32 attachSize) {
|
||||
const NWC24MsgObjPrivate* pMsg = (const NWC24MsgObjPrivate*)msg;
|
||||
NWC24Err result;
|
||||
NWC24Err closeResult;
|
||||
NWC24MsgBoxId id;
|
||||
u32 flags;
|
||||
u32 decodeSize;
|
||||
NWC24File file;
|
||||
|
||||
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool()) {
|
||||
return NWC24_ERR_LIB_NOT_OPENED;
|
||||
}
|
||||
|
||||
if (!(msg->data[1] & MSG_OBJ_DELIVERING)) {
|
||||
return NWC24_ERR_PROTECTED;
|
||||
}
|
||||
|
||||
if (attachIndex >= pMsg->numAttached) {
|
||||
return NWC24_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
flags = pMsg->type;
|
||||
if (flags & MSG_OBJ_TO_SEND) {
|
||||
id = NWC24_MSGBOX_SEND;
|
||||
} else if (flags & MSG_OBJ_TO_RECV) {
|
||||
id = NWC24_MSGBOX_RECV;
|
||||
} else {
|
||||
result = NWC24_ERR_INVALID_VALUE;
|
||||
goto select_done;
|
||||
}
|
||||
result = NWC24_OK;
|
||||
|
||||
select_done:
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (pMsg->attached[attachIndex].size == 0 ||
|
||||
pMsg->attachedSize[attachIndex] == 0) {
|
||||
return NWC24_ERR_NULL;
|
||||
}
|
||||
|
||||
result = NWC24iMBoxOpenStoredMsg(id, pMsg->msgId, &file);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
decodeSize = 0;
|
||||
result = ReadBase64Data(&file, &pMsg->attached[attachIndex], attachData,
|
||||
attachSize, &decodeSize);
|
||||
if (result == NWC24_OK && decodeSize != pMsg->attachedSize[attachIndex]) {
|
||||
result = NWC24_ERR_FORMAT;
|
||||
}
|
||||
|
||||
closeResult = NWC24iMBoxCloseMsg(&file);
|
||||
if (result == NWC24_OK) {
|
||||
result = closeResult;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static NWC24Err ReadBase64Data(NWC24File* file, const NWC24Data* data,
|
||||
u8* out, u32 outSize, u32* outLen) {
|
||||
char* pWork;
|
||||
NWC24Err result;
|
||||
s32 remaining;
|
||||
u32 offset;
|
||||
u32 written;
|
||||
u32 chunk;
|
||||
u32 decodeLen;
|
||||
u32 decodeSize;
|
||||
BOOL done;
|
||||
int i;
|
||||
|
||||
pWork = NWC24WorkP->mainWork;
|
||||
|
||||
remaining = data->size;
|
||||
offset = data->offset;
|
||||
written = 0;
|
||||
result = NWC24_OK;
|
||||
done = FALSE;
|
||||
|
||||
while (remaining > 0 && !done) {
|
||||
chunk = remaining > NWC24i_STRING_WORK_SIZE
|
||||
? NWC24i_STRING_WORK_SIZE
|
||||
: remaining;
|
||||
|
||||
NWC24FSeek(file, offset, NWC24_SEEK_BEG);
|
||||
result = NWC24FRead(pWork, chunk, file);
|
||||
if (result != NWC24_OK) {
|
||||
break;
|
||||
}
|
||||
|
||||
decodeLen = 0;
|
||||
for (i = 0; i < chunk; i++) {
|
||||
if (pWork[i] == '\r') {
|
||||
decodeLen = i;
|
||||
}
|
||||
if (pWork[i] == '=') {
|
||||
decodeLen = i + 1;
|
||||
done = TRUE;
|
||||
result = NWC24_OK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (decodeLen == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
result = NWC24Base64Decode((u8*)pWork, decodeLen, out, outSize - written,
|
||||
&decodeSize);
|
||||
if (result != NWC24_OK) {
|
||||
break;
|
||||
}
|
||||
|
||||
offset += decodeLen;
|
||||
remaining -= decodeLen;
|
||||
written += decodeSize;
|
||||
out += decodeSize;
|
||||
}
|
||||
|
||||
if (result == NWC24_OK) {
|
||||
*outLen = written;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static NWC24Err ReadQPText(const NWC24MsgObj* msg, NWC24File* file, char* text,
|
||||
u32 textSize) {
|
||||
const NWC24MsgObjPrivate* pMsg = (const NWC24MsgObjPrivate*)msg;
|
||||
char* pWork;
|
||||
NWC24Err result;
|
||||
s32 remaining;
|
||||
u32 offset;
|
||||
u32 written;
|
||||
s32 chunk;
|
||||
u32 decLen;
|
||||
u32 encLen;
|
||||
|
||||
pWork = NWC24WorkP->mainWork;
|
||||
|
||||
remaining = pMsg->text.size;
|
||||
offset = pMsg->text.offset;
|
||||
written = 0;
|
||||
result = NWC24_OK;
|
||||
|
||||
while (remaining > 0) {
|
||||
chunk = remaining > NWC24i_STRING_WORK_SIZE
|
||||
? NWC24i_STRING_WORK_SIZE
|
||||
: remaining;
|
||||
|
||||
NWC24FSeek(file, offset, NWC24_SEEK_BEG);
|
||||
result = NWC24FRead(pWork, chunk, file);
|
||||
if (result != NWC24_OK) {
|
||||
break;
|
||||
}
|
||||
|
||||
encLen = chunk;
|
||||
if (chunk == NWC24i_STRING_WORK_SIZE) {
|
||||
if (pWork[chunk - 1] == '=') {
|
||||
encLen = chunk - 1;
|
||||
} else if (pWork[chunk - 2] == '=') {
|
||||
encLen = chunk - 2;
|
||||
}
|
||||
}
|
||||
|
||||
result = NWC24DecodeQuotedPrintable(text, textSize - written, &decLen,
|
||||
(u8*)pWork, encLen, &encLen);
|
||||
if (result != NWC24_OK && result != NWC24_ERR_OVERFLOW) {
|
||||
break;
|
||||
}
|
||||
|
||||
written += decLen;
|
||||
text += decLen;
|
||||
|
||||
if (result == NWC24_ERR_OVERFLOW) {
|
||||
break;
|
||||
}
|
||||
|
||||
offset += encLen;
|
||||
remaining -= encLen;
|
||||
}
|
||||
|
||||
*text = '\0';
|
||||
|
||||
if (result == NWC24_OK && written > pMsg->textSize) {
|
||||
result = NWC24_ERR_FORMAT;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
983
libs/RevoEX/src/nwc24/NWC24MsgSubject.c
Normal file
983
libs/RevoEX/src/nwc24/NWC24MsgSubject.c
Normal file
@@ -0,0 +1,983 @@
|
||||
#include <private/nwc24.h>
|
||||
#include <revolution/nwc24.h>
|
||||
|
||||
#include <revolution/enc.h>
|
||||
#include <string.h>
|
||||
|
||||
enum {
|
||||
MSG_OBJ_INITIALIZED = (1 << 8),
|
||||
MSG_OBJ_DELIVERING = (1 << 9),
|
||||
};
|
||||
|
||||
#define NWC24i_CHARSET_WORK_SIZE 0x40
|
||||
#define NWC24i_WORD_LINE_SIZE 0x3f
|
||||
#define NWC24i_WORD_LINE_SIZE_QP 0x47
|
||||
|
||||
static NWC24Err NWC24iGetDefaultCharset(char* charset, u32 size,
|
||||
NWC24EncodingRegion region);
|
||||
static NWC24Err NWC24iConvertToInternalEncoding(u16* dst, u32* dstSize,
|
||||
const u8* src, s32* srcSize,
|
||||
char* charset, u32 charsetSize,
|
||||
NWC24EncodingRegion region,
|
||||
u16 alt);
|
||||
static NWC24Err NWC24iConvertFromInternalEncoding(u8* dst, u32* dstSize,
|
||||
const u16* src, s32* srcSize,
|
||||
char* charset, u32 charsetSize,
|
||||
NWC24EncodingRegion region,
|
||||
u16 alt);
|
||||
static NWC24Err NWC24iDetectEncodingToSend(char* charset, u32 charsetSize,
|
||||
const u16* src1, u32 src1Len,
|
||||
const u16* src2, u32 src2Len,
|
||||
NWC24EncodingRegion region);
|
||||
static NWC24Err NWC24iDetectBreakPoint(u32* pos, NWC24Charset charset,
|
||||
const u8* str, u32 size, u32 limit);
|
||||
static NWC24Err NWC24iSetMsgSubjectPlain(NWC24MsgObj* msg, const u16* subject,
|
||||
u32 subjectLen,
|
||||
NWC24EncodingRegion encRegion, u16 unk,
|
||||
u8* work, u32 workSize, u32 charset,
|
||||
char* charsetName);
|
||||
static NWC24Err NWC24iSetMsgSubjectQP(NWC24MsgObj* msg, const u16* subject,
|
||||
u32 subjectLen,
|
||||
NWC24EncodingRegion encRegion, u16 unk,
|
||||
u8* work, u32 workSize, u32 charset,
|
||||
char* charsetName);
|
||||
static NWC24Err NWC24iSetMsgSubjectBase64(NWC24MsgObj* msg, const u16* subject,
|
||||
u32 subjectLen,
|
||||
NWC24EncodingRegion encRegion, u16 unk,
|
||||
u8* work, u32 workSize, u32 charset,
|
||||
char* charsetName);
|
||||
|
||||
static const char* sEncNamesJpn[3] = {
|
||||
"us-ascii", "iso-2022-jp", "utf-8",
|
||||
};
|
||||
|
||||
static const char* sEncNamesWest[8] = {
|
||||
"us-ascii", "iso-8859-1", "windows-1252", "iso-8859-2",
|
||||
"iso-8859-7", "iso-8859-10", "iso-8859-3", "utf-8",
|
||||
};
|
||||
|
||||
static const char* sEncNamesKor[3] = {
|
||||
"us-ascii", "euc-kr", "utf-8",
|
||||
};
|
||||
|
||||
static const char* sEncNamesChn[2] = {
|
||||
"us-ascii", "gb2312",
|
||||
};
|
||||
|
||||
NWC24Err NWC24ReadMsgSubjectPublic(const NWC24MsgObj* msg, u16* subject,
|
||||
u32* subjectLen,
|
||||
NWC24EncodingRegion encRegion, u16 unk,
|
||||
u8* work, u32 workSize) {
|
||||
NWC24Err result;
|
||||
NWC24Err readResult;
|
||||
BOOL overflow;
|
||||
u32 saved;
|
||||
u32 half;
|
||||
u32 quota;
|
||||
u8* buf;
|
||||
u32 size;
|
||||
u32 written;
|
||||
u32 i;
|
||||
u16* p;
|
||||
u16 c;
|
||||
|
||||
if (subjectLen == NULL) {
|
||||
return NWC24_ERR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
saved = *subjectLen;
|
||||
*subjectLen = 0;
|
||||
overflow = FALSE;
|
||||
|
||||
if (work == NULL) {
|
||||
return NWC24_ERR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
half = workSize / 2;
|
||||
buf = work + half;
|
||||
quota = workSize - half;
|
||||
|
||||
result = NWC24GetMsgSubjectSize(msg, &size);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (size > half) {
|
||||
size = half;
|
||||
overflow = TRUE;
|
||||
}
|
||||
|
||||
result = NWC24ReadMsgSubject(msg, (char*)work, size);
|
||||
if (result != NWC24_OK) {
|
||||
if (result != NWC24_ERR_OVERFLOW) {
|
||||
return result;
|
||||
}
|
||||
overflow = TRUE;
|
||||
}
|
||||
|
||||
result = NWC24DecodeMIMEHeaderFieldBody((u8*)NWC24WorkP, NWC24i_CHARSET_WORK_SIZE,
|
||||
buf, quota, &written, work, size);
|
||||
if (result != NWC24_OK) {
|
||||
if (result != NWC24_ERR_OVERFLOW) {
|
||||
return result;
|
||||
}
|
||||
overflow = TRUE;
|
||||
}
|
||||
|
||||
*subjectLen = saved;
|
||||
result = NWC24iConvertToInternalEncoding(subject, subjectLen, buf,
|
||||
(s32*)&written,
|
||||
(char*)NWC24WorkP,
|
||||
NWC24i_CHARSET_WORK_SIZE, encRegion,
|
||||
unk);
|
||||
if (result != NWC24_OK && result != NWC24_ERR_OVERFLOW) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (i = 0, p = subject; i < *subjectLen && (c = *p) != 0; i++, p++) {
|
||||
if (c == '\r' || c == '\n') {
|
||||
*p = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (overflow) {
|
||||
return NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
NWC24Err NWC24ReadMsgTextPublic(const NWC24MsgObj* msg, u16* text, u32* textLen,
|
||||
NWC24EncodingRegion encRegion, u16 unk,
|
||||
u8* work, u32 workSize) {
|
||||
NWC24Err result;
|
||||
u32 size;
|
||||
u32 saved;
|
||||
BOOL overflow;
|
||||
char* charsetBuf;
|
||||
|
||||
if (textLen == NULL) {
|
||||
return NWC24_ERR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
saved = *textLen;
|
||||
*textLen = 0;
|
||||
overflow = FALSE;
|
||||
|
||||
if (work == NULL) {
|
||||
return NWC24_ERR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
result = NWC24GetMsgTextSize(msg, &size);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (size > workSize) {
|
||||
size = workSize;
|
||||
overflow = TRUE;
|
||||
}
|
||||
|
||||
result = NWC24ReadMsgTextEx(msg, (char*)work, size,
|
||||
charsetBuf = (char*)NWC24WorkP + 0x20,
|
||||
NWC24i_CHARSET_WORK_SIZE, (NWC24Charset*)work,
|
||||
(NWC24Encoding*)workSize);
|
||||
if (result != NWC24_OK) {
|
||||
if (result != NWC24_ERR_OVERFLOW) {
|
||||
return result;
|
||||
}
|
||||
overflow = TRUE;
|
||||
}
|
||||
|
||||
*textLen = saved;
|
||||
result = NWC24iConvertToInternalEncoding(text, textLen, work,
|
||||
(s32*)&size, charsetBuf,
|
||||
NWC24i_CHARSET_WORK_SIZE, encRegion,
|
||||
unk);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (overflow) {
|
||||
result = NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NWC24Err NWC24SetMsgSubjectPublic(NWC24MsgObj* msg, const u16* subject,
|
||||
u32 subjectLen, NWC24EncodingRegion encRegion,
|
||||
u16 unk, u8* work, u32 workSize) {
|
||||
NWC24Err result;
|
||||
volatile u32 charset;
|
||||
NWC24Encoding encoding;
|
||||
char* charsetName;
|
||||
u32 type;
|
||||
|
||||
type = msg->data[1];
|
||||
if (!(type & MSG_OBJ_INITIALIZED) || (type & MSG_OBJ_DELIVERING)) {
|
||||
return NWC24_ERR_PROTECTED;
|
||||
}
|
||||
|
||||
if (subject == NULL || subject[0] == 0) {
|
||||
return NWC24_ERR_NULL;
|
||||
}
|
||||
|
||||
if (subject[subjectLen] != 0) {
|
||||
return NWC24_ERR_STRING_END;
|
||||
}
|
||||
|
||||
if (work == NULL) {
|
||||
return NWC24_ERR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
charsetName = (char*)NWC24WorkP;
|
||||
result = NWC24iDetectEncodingToSend(charsetName,
|
||||
NWC24i_CHARSET_WORK_SIZE, subject,
|
||||
subjectLen, NULL, 0, encRegion);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = NWC24ParseCharsetStr((NWC24Charset*)&charset, charsetName);
|
||||
if (result != NWC24_OK) {
|
||||
charset = 0;
|
||||
}
|
||||
|
||||
if (charset == 0) {
|
||||
return NWC24iSetMsgSubjectPlain(msg, subject, subjectLen, encRegion,
|
||||
unk, work, workSize, charset,
|
||||
charsetName);
|
||||
}
|
||||
|
||||
switch (encRegion) {
|
||||
case NWC24_ENCODING_REGION_JPN:
|
||||
encoding = 2;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_USA:
|
||||
case NWC24_ENCODING_REGION_EUR:
|
||||
encoding = 3;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_KOR:
|
||||
case NWC24_ENCODING_REGION_CHN:
|
||||
encoding = 2;
|
||||
break;
|
||||
default:
|
||||
encoding = 2;
|
||||
break;
|
||||
}
|
||||
|
||||
if (encoding == NWC24_ENC_QUOTED_PRINTABLE) {
|
||||
return NWC24iSetMsgSubjectQP(msg, subject, subjectLen, encRegion, unk,
|
||||
work, workSize, charset,
|
||||
charsetName);
|
||||
}
|
||||
|
||||
return NWC24iSetMsgSubjectBase64(msg, subject, subjectLen, encRegion, unk,
|
||||
work, workSize, charset,
|
||||
charsetName);
|
||||
}
|
||||
|
||||
NWC24Err NWC24SetMsgSubjectAndTextPublic(NWC24MsgObj* msg, const u16* subject,
|
||||
u32 subjectLen, const u16* text,
|
||||
u32 textLen,
|
||||
NWC24EncodingRegion encRegion, u16 unk,
|
||||
u8* work, u32 workSize) {
|
||||
char* charsetName;
|
||||
NWC24Err result;
|
||||
u32 quota;
|
||||
s32 convSize;
|
||||
volatile u32 charset;
|
||||
BOOL is7Bit;
|
||||
NWC24Encoding encoding;
|
||||
u32 subjQuota;
|
||||
u8* subjWork;
|
||||
u32 type;
|
||||
|
||||
type = msg->data[1];
|
||||
if (!(type & MSG_OBJ_INITIALIZED) || (type & MSG_OBJ_DELIVERING)) {
|
||||
return NWC24_ERR_PROTECTED;
|
||||
}
|
||||
|
||||
if (subject == NULL || subject[0] == 0) {
|
||||
return NWC24_ERR_NULL;
|
||||
}
|
||||
|
||||
if (text == NULL || textLen == 0) {
|
||||
NWC24Data_Init(&((NWC24MsgObjPrivate*)msg)->text);
|
||||
return NWC24SetMsgSubjectPublic(msg, subject, subjectLen, encRegion,
|
||||
unk, work, workSize);
|
||||
}
|
||||
|
||||
if (text[textLen] != 0 || subject[subjectLen] != 0) {
|
||||
return NWC24_ERR_STRING_END;
|
||||
}
|
||||
|
||||
if (work == NULL) {
|
||||
return NWC24_ERR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
charsetName = (char*)NWC24WorkP;
|
||||
result = NWC24iDetectEncodingToSend(charsetName, NWC24i_CHARSET_WORK_SIZE,
|
||||
subject, subjectLen, text, textLen,
|
||||
encRegion);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = NWC24ParseCharsetStr((NWC24Charset*)&charset, charsetName);
|
||||
if (result != NWC24_OK) {
|
||||
charset = 0;
|
||||
}
|
||||
|
||||
convSize = textLen;
|
||||
quota = (workSize * textLen) / (textLen + subjectLen * 4);
|
||||
subjQuota = workSize - quota;
|
||||
subjWork = work + quota;
|
||||
|
||||
result = NWC24iConvertFromInternalEncoding(work, "a, text, &convSize,
|
||||
charsetName,
|
||||
NWC24i_CHARSET_WORK_SIZE,
|
||||
encRegion, unk);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = ENCIs7BitEncoding(&is7Bit, charsetName);
|
||||
if (is7Bit && result == 0) {
|
||||
encoding = NWC24_ENC_7BIT;
|
||||
} else {
|
||||
switch (encRegion) {
|
||||
case NWC24_ENCODING_REGION_JPN:
|
||||
encoding = NWC24_ENC_BASE64;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_USA:
|
||||
case NWC24_ENCODING_REGION_EUR:
|
||||
encoding = NWC24_ENC_QUOTED_PRINTABLE;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_KOR:
|
||||
case NWC24_ENCODING_REGION_CHN:
|
||||
encoding = NWC24_ENC_BASE64;
|
||||
break;
|
||||
default:
|
||||
encoding = NWC24_ENC_BASE64;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result = NWC24SetMsgText(msg, (const char*)work, quota - 1, charset,
|
||||
encoding);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (charset == 0) {
|
||||
return NWC24iSetMsgSubjectPlain(msg, subject, subjectLen, encRegion,
|
||||
unk, subjWork, subjQuota, charset,
|
||||
charsetName);
|
||||
}
|
||||
|
||||
switch (encRegion) {
|
||||
case NWC24_ENCODING_REGION_JPN:
|
||||
encoding = NWC24_ENC_BASE64;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_USA:
|
||||
case NWC24_ENCODING_REGION_EUR:
|
||||
encoding = NWC24_ENC_QUOTED_PRINTABLE;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_KOR:
|
||||
case NWC24_ENCODING_REGION_CHN:
|
||||
encoding = NWC24_ENC_BASE64;
|
||||
break;
|
||||
default:
|
||||
encoding = NWC24_ENC_BASE64;
|
||||
break;
|
||||
}
|
||||
|
||||
if (encoding == NWC24_ENC_QUOTED_PRINTABLE) {
|
||||
return NWC24iSetMsgSubjectQP(msg, subject, subjectLen, encRegion, unk,
|
||||
subjWork, subjQuota, charset,
|
||||
charsetName);
|
||||
}
|
||||
|
||||
return NWC24iSetMsgSubjectBase64(msg, subject, subjectLen, encRegion, unk,
|
||||
subjWork, subjQuota, charset,
|
||||
charsetName);
|
||||
}
|
||||
|
||||
#pragma dont_inline on
|
||||
static NWC24Err NWC24iGetDefaultCharset(char* charset, u32 size,
|
||||
NWC24EncodingRegion region) {
|
||||
NWC24Err result = NWC24_OK;
|
||||
|
||||
switch (region) {
|
||||
case NWC24_ENCODING_REGION_JPN:
|
||||
strncpy(charset, "iso-2022-jp", size - 1);
|
||||
charset[size - 1] = 0;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_USA:
|
||||
strncpy(charset, "iso-8859-1", size - 1);
|
||||
charset[size - 1] = 0;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_EUR:
|
||||
strncpy(charset, "iso-8859-1", size - 1);
|
||||
charset[size - 1] = 0;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_KOR:
|
||||
strncpy(charset, "euc-kr", size - 1);
|
||||
charset[size - 1] = 0;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_CHN:
|
||||
strncpy(charset, "gb2312", size - 1);
|
||||
charset[size - 1] = 0;
|
||||
break;
|
||||
default:
|
||||
strncpy(charset, "iso-8859-1", size - 1);
|
||||
charset[size - 1] = 0;
|
||||
result = NWC24_ERR_INVALID_VALUE;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#pragma dont_inline reset
|
||||
|
||||
static NWC24Err NWC24iConvertToInternalEncoding(u16* dst, u32* dstSize,
|
||||
const u8* src, s32* srcSize,
|
||||
char* charset, u32 charsetSize,
|
||||
NWC24EncodingRegion region,
|
||||
u16 alt);
|
||||
static NWC24Err NWC24iConvertFromInternalEncoding(u8* dst, u32* dstSize,
|
||||
const u16* src, s32* srcSize,
|
||||
char* charset, u32 charsetSize,
|
||||
NWC24EncodingRegion region,
|
||||
u16 alt);
|
||||
static NWC24Err NWC24iDetectEncodingToSend(char* charset, u32 charsetSize,
|
||||
const u16* src1, u32 src1Len,
|
||||
const u16* src2, u32 src2Len,
|
||||
NWC24EncodingRegion region);
|
||||
static NWC24Err NWC24iDetectBreakPoint(u32* pos, NWC24Charset charset,
|
||||
const u8* str, u32 size, u32 limit);
|
||||
static NWC24Err NWC24iConvertToInternalEncoding(u16* dst, u32* dstSize,
|
||||
const u8* src, s32* srcSize,
|
||||
char* charset, u32 charsetSize,
|
||||
NWC24EncodingRegion region,
|
||||
u16 alt) {
|
||||
ENCContext ctx;
|
||||
NWC24Err result;
|
||||
s32 encErr;
|
||||
u32 byteSize;
|
||||
u32 cap;
|
||||
|
||||
cap = *dstSize * 2;
|
||||
*dstSize = 0;
|
||||
if (cap < 2) {
|
||||
return NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
|
||||
if (*charset == '\0') {
|
||||
result = NWC24iGetDefaultCharset(charset, charsetSize, region);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
encErr = ENCInitContext(&ctx);
|
||||
if (encErr != 0) {
|
||||
break;
|
||||
}
|
||||
encErr = ENCSetExternalEncoding(&ctx, charset);
|
||||
if (encErr != 0) {
|
||||
break;
|
||||
}
|
||||
encErr = ENCSetBreakType(&ctx, 3);
|
||||
if (encErr != 0) {
|
||||
break;
|
||||
}
|
||||
if (alt != 0) {
|
||||
encErr = ENCSetAlternativeCharacter(&ctx, alt, alt);
|
||||
if (encErr != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
byteSize = cap - 2;
|
||||
encErr = ENCConvertToInternalEncoding(&ctx, dst, (s32*)&byteSize,
|
||||
src, srcSize);
|
||||
dst[(byteSize & ~1u) / 2] = 0;
|
||||
*dstSize = byteSize / 2 + 1;
|
||||
} while (0);
|
||||
|
||||
switch (encErr) {
|
||||
case 0:
|
||||
result = NWC24_OK;
|
||||
break;
|
||||
case -3:
|
||||
result = NWC24_ERR_INVALID_VALUE;
|
||||
break;
|
||||
case -1:
|
||||
result = NWC24_ERR_OVERFLOW;
|
||||
break;
|
||||
case -2:
|
||||
case -4:
|
||||
result = NWC24_ERR_INVALID_CHAR;
|
||||
break;
|
||||
case -5:
|
||||
case -6:
|
||||
case -7:
|
||||
result = NWC24_ERR_NOT_SUPPORTED;
|
||||
break;
|
||||
default:
|
||||
result = NWC24_ERR_FATAL;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static NWC24Err NWC24iConvertFromInternalEncoding(u8* dst, u32* dstSize,
|
||||
const u16* src, s32* srcSize,
|
||||
char* charset, u32 charsetSize,
|
||||
NWC24EncodingRegion region,
|
||||
u16 alt) {
|
||||
ENCContext ctx;
|
||||
NWC24Err result;
|
||||
s32 encErr;
|
||||
u32 cap;
|
||||
u32 dstCap;
|
||||
u32 srcBytes;
|
||||
|
||||
cap = *dstSize;
|
||||
*dstSize = 0;
|
||||
if (cap < 1) {
|
||||
return NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
|
||||
if (*charset == '\0') {
|
||||
result = NWC24iGetDefaultCharset(charset, charsetSize, region);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
encErr = ENCInitContext(&ctx);
|
||||
if (encErr != 0) {
|
||||
break;
|
||||
}
|
||||
encErr = ENCSetExternalEncoding(&ctx, charset);
|
||||
if (encErr != 0) {
|
||||
break;
|
||||
}
|
||||
encErr = ENCSetBreakType(&ctx, 1);
|
||||
if (encErr != 0) {
|
||||
break;
|
||||
}
|
||||
if (alt != 0) {
|
||||
encErr = ENCSetAlternativeCharacter(&ctx, alt, alt);
|
||||
if (encErr != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
dstCap = cap - 1;
|
||||
srcBytes = *srcSize * 2;
|
||||
encErr = ENCConvertFromInternalEncoding(&ctx, dst, (s32*)&dstCap,
|
||||
src, (s32*)&srcBytes);
|
||||
dst[dstCap] = 0;
|
||||
*srcSize = srcBytes / 2;
|
||||
*dstSize = dstCap + 1;
|
||||
} while (0);
|
||||
|
||||
switch (encErr) {
|
||||
case 0:
|
||||
result = NWC24_OK;
|
||||
break;
|
||||
case -3:
|
||||
result = NWC24_ERR_INVALID_VALUE;
|
||||
break;
|
||||
case -1:
|
||||
result = NWC24_ERR_OVERFLOW;
|
||||
break;
|
||||
case -2:
|
||||
case -4:
|
||||
result = NWC24_ERR_INVALID_CHAR;
|
||||
break;
|
||||
case -5:
|
||||
case -6:
|
||||
case -7:
|
||||
result = NWC24_ERR_NOT_SUPPORTED;
|
||||
break;
|
||||
default:
|
||||
result = NWC24_ERR_FATAL;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static NWC24Err NWC24iDetectEncodingToSend(char* charset, u32 charsetSize,
|
||||
const u16* src1, u32 src1Len,
|
||||
const u16* src2, u32 src2Len,
|
||||
NWC24EncodingRegion region) {
|
||||
const char** names;
|
||||
int numNames;
|
||||
int found;
|
||||
int i;
|
||||
int prev;
|
||||
s32 err;
|
||||
u32 src1Bytes;
|
||||
u32 src2Bytes;
|
||||
|
||||
found = 0;
|
||||
i = 0;
|
||||
prev = -1;
|
||||
|
||||
switch (region) {
|
||||
case NWC24_ENCODING_REGION_JPN:
|
||||
names = (const char**)sEncNamesJpn;
|
||||
numNames = 3;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_USA:
|
||||
case NWC24_ENCODING_REGION_EUR:
|
||||
names = (const char**)sEncNamesWest;
|
||||
numNames = 8;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_KOR:
|
||||
names = (const char**)sEncNamesKor;
|
||||
numNames = 3;
|
||||
break;
|
||||
case NWC24_ENCODING_REGION_CHN:
|
||||
names = (const char**)sEncNamesChn;
|
||||
numNames = 2;
|
||||
break;
|
||||
default:
|
||||
names = (const char**)sEncNamesWest;
|
||||
numNames = 8;
|
||||
break;
|
||||
}
|
||||
|
||||
src1Bytes = src1Len * 2;
|
||||
src2Bytes = src2Len * 2;
|
||||
|
||||
while (found < numNames - 1) {
|
||||
err = ENCCheckEncoding(&found, names + i, numNames - 1 - i,
|
||||
(u16*)src1, src1Bytes);
|
||||
if (err != 0) {
|
||||
return NWC24_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
if (found == -1) {
|
||||
found = numNames - 1;
|
||||
break;
|
||||
}
|
||||
i += found;
|
||||
if (src2 == NULL || prev == i) {
|
||||
found = i;
|
||||
break;
|
||||
}
|
||||
err = ENCCheckEncoding(&found, names + i, numNames - 1 - i,
|
||||
(u16*)src2, src2Bytes);
|
||||
if (err != 0) {
|
||||
return NWC24_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
if (found == -1) {
|
||||
found = numNames - 1;
|
||||
break;
|
||||
}
|
||||
prev = i + found;
|
||||
if (prev == i) {
|
||||
found = prev;
|
||||
break;
|
||||
}
|
||||
i = prev;
|
||||
}
|
||||
|
||||
strncpy(charset, names[found], charsetSize - 1);
|
||||
charset[charsetSize - 1] = 0;
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
static NWC24Err NWC24iDetectBreakPoint(u32* pos, NWC24Charset charset,
|
||||
const u8* str, u32 size, u32 limit) {
|
||||
const u8* p;
|
||||
u32 flag;
|
||||
u32 i = 0;
|
||||
u32 last = 0;
|
||||
u32 j;
|
||||
|
||||
if (pos == NULL || str == NULL) {
|
||||
return NWC24_ERR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
if (charset == 0) {
|
||||
flag = 1;
|
||||
} else if (charset == 0x10008) {
|
||||
flag = 0;
|
||||
} else if ((charset & 0xffff0000u) == 0x80000 ||
|
||||
charset == 0xf1252) {
|
||||
flag = 1;
|
||||
} else {
|
||||
return NWC24_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if (size <= limit) {
|
||||
*pos = size;
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
for (p = str; i < limit && *str != '\0'; i++, p++) {
|
||||
if (*p == ' ') {
|
||||
last = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (last != 0) {
|
||||
*pos = last;
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
if (flag != 0) {
|
||||
*pos = limit;
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
for (j = 0, p = str; j < limit && *str != '\0'; j++, p++) {
|
||||
if ((*p & 0xc0u) != 0x80u) {
|
||||
last = j;
|
||||
}
|
||||
}
|
||||
*pos = last;
|
||||
return NWC24_OK;
|
||||
}
|
||||
|
||||
static NWC24Err NWC24iSetMsgSubjectPlain(NWC24MsgObj* msg, const u16* subject,
|
||||
u32 subjectLen,
|
||||
NWC24EncodingRegion encRegion, u16 unk,
|
||||
u8* work, u32 workSize, u32 charset,
|
||||
char* charsetName) {
|
||||
u32 halfSize;
|
||||
u32 srcLen;
|
||||
u32 srcPos;
|
||||
u32 cap;
|
||||
u8* buf2;
|
||||
u32 bufLen;
|
||||
u8* p;
|
||||
u32 i;
|
||||
NWC24Err result;
|
||||
u32 pos;
|
||||
|
||||
srcLen = subjectLen;
|
||||
halfSize = workSize / 2;
|
||||
pos = 0;
|
||||
cap = workSize - halfSize;
|
||||
buf2 = work + halfSize;
|
||||
|
||||
result = NWC24iConvertFromInternalEncoding(work, &halfSize, subject,
|
||||
(s32*)&srcLen, charsetName,
|
||||
NWC24i_CHARSET_WORK_SIZE,
|
||||
encRegion, unk);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
srcLen = halfSize - 1;
|
||||
for (p = work, i = 0; i < halfSize; p++, i++) {
|
||||
if (*p == '\n' || *p == '\r') {
|
||||
*p = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
NWC24iDetectBreakPoint(&pos, charset, work, srcLen,
|
||||
NWC24i_WORD_LINE_SIZE);
|
||||
if (pos == srcLen) {
|
||||
return NWC24SetMsgSubject(msg, (const char*)work, srcLen);
|
||||
}
|
||||
|
||||
halfSize = cap;
|
||||
if (cap <= pos) {
|
||||
return NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
|
||||
srcPos = pos;
|
||||
bufLen = NWC24iStrLCpy((char*)buf2, (const char*)work, pos + 1);
|
||||
while (srcPos < srcLen) {
|
||||
if (halfSize - bufLen < 3) {
|
||||
return NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
buf2[bufLen++] = '\r';
|
||||
buf2[bufLen++] = '\n';
|
||||
if (work[srcPos] != ' ') {
|
||||
buf2[bufLen++] = ' ';
|
||||
}
|
||||
NWC24iDetectBreakPoint(&pos, charset, work + srcPos,
|
||||
srcLen - srcPos, 0x47);
|
||||
if (halfSize - bufLen <= pos) {
|
||||
return NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
bufLen += NWC24iStrLCpy((char*)buf2 + bufLen,
|
||||
(const char*)work + srcPos, pos + 1);
|
||||
srcPos += pos;
|
||||
}
|
||||
buf2[bufLen] = 0;
|
||||
return NWC24SetMsgSubject(msg, (const char*)buf2, bufLen);
|
||||
}
|
||||
|
||||
static NWC24Err NWC24iSetMsgSubjectQP(NWC24MsgObj* msg, const u16* subject,
|
||||
u32 subjectLen,
|
||||
NWC24EncodingRegion encRegion, u16 unk,
|
||||
u8* work, u32 workSize, u32 charset,
|
||||
char* charsetName) {
|
||||
u32 halfSize;
|
||||
u32 srcLen;
|
||||
u32 pos;
|
||||
u32 cap;
|
||||
u8* buf2;
|
||||
u32 bufLen;
|
||||
u32 srcPos;
|
||||
u32 encLen;
|
||||
u8* p;
|
||||
u32 i;
|
||||
NWC24Err result;
|
||||
u32 limit;
|
||||
|
||||
srcLen = subjectLen;
|
||||
halfSize = workSize / 2;
|
||||
cap = workSize - halfSize;
|
||||
buf2 = work + halfSize;
|
||||
pos = 0;
|
||||
|
||||
result = NWC24iConvertFromInternalEncoding(work, &halfSize, subject,
|
||||
(s32*)&srcLen, charsetName,
|
||||
NWC24i_CHARSET_WORK_SIZE,
|
||||
encRegion, unk);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
srcLen = halfSize - 1;
|
||||
for (p = work, i = 0; i < halfSize; p++, i++) {
|
||||
if (*p == '\n' || *p == '\r') {
|
||||
*p = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
limit = (0x38 - strlen(charsetName)) / 3;
|
||||
NWC24iDetectBreakPoint(&pos, charset, work, srcLen, limit);
|
||||
result = NWC24EncodeWord(buf2, cap, &encLen, charsetName,
|
||||
NWC24i_CHARSET_WORK_SIZE, 'Q', work, pos);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
srcPos = pos;
|
||||
if (srcPos == srcLen) {
|
||||
return NWC24SetMsgSubject(msg, (const char*)buf2, encLen - 1);
|
||||
}
|
||||
|
||||
limit = (0x40 - strlen(charsetName)) / 3;
|
||||
halfSize = cap;
|
||||
bufLen = encLen - 1;
|
||||
|
||||
while (srcPos < srcLen) {
|
||||
if (halfSize - bufLen < 3) {
|
||||
return NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
buf2[bufLen++] = '\r';
|
||||
buf2[bufLen++] = '\n';
|
||||
buf2[bufLen++] = ' ';
|
||||
|
||||
NWC24iDetectBreakPoint(&pos, charset, work + srcPos,
|
||||
srcLen - srcPos, limit);
|
||||
result = NWC24EncodeWord(buf2 + bufLen, halfSize - bufLen, &encLen,
|
||||
charsetName, NWC24i_CHARSET_WORK_SIZE, 'Q',
|
||||
work + srcPos, pos);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
bufLen += encLen - 1;
|
||||
srcPos += pos;
|
||||
}
|
||||
|
||||
return NWC24SetMsgSubject(msg, (const char*)buf2, bufLen);
|
||||
}
|
||||
|
||||
static NWC24Err NWC24iSetMsgSubjectBase64(NWC24MsgObj* msg, const u16* subject,
|
||||
u32 subjectLen,
|
||||
NWC24EncodingRegion encRegion,
|
||||
u16 unk, u8* work, u32 workSize,
|
||||
u32 charset, char* charsetName) {
|
||||
u32 encLen;
|
||||
u32 srcLen;
|
||||
u32 convert;
|
||||
u32 halfSize;
|
||||
u32 cap;
|
||||
u8* buf2;
|
||||
u32 bufLen;
|
||||
u32 srcPos;
|
||||
u32 chunk;
|
||||
NWC24Err result;
|
||||
|
||||
halfSize = workSize / 2;
|
||||
buf2 = work + halfSize;
|
||||
cap = workSize - halfSize;
|
||||
|
||||
chunk = (0x36 - strlen(charsetName)) * 3 / 4;
|
||||
if (chunk > halfSize) {
|
||||
return NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
|
||||
srcLen = subjectLen;
|
||||
convert = chunk + 1;
|
||||
result = NWC24iConvertFromInternalEncoding(work, &convert, subject,
|
||||
(s32*)&srcLen, charsetName,
|
||||
NWC24i_CHARSET_WORK_SIZE,
|
||||
encRegion, unk);
|
||||
if (result != NWC24_OK && result != NWC24_ERR_OVERFLOW) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = NWC24EncodeWord(buf2, cap, &encLen, charsetName,
|
||||
NWC24i_CHARSET_WORK_SIZE, 'B', work, convert - 1);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
srcPos = srcLen;
|
||||
if (srcPos == subjectLen) {
|
||||
return NWC24SetMsgSubject(msg, (const char*)buf2, encLen - 1);
|
||||
}
|
||||
|
||||
chunk = (0x3e - strlen(charsetName)) * 3 / 4 + 1;
|
||||
bufLen = encLen - 1;
|
||||
|
||||
while (srcPos < subjectLen) {
|
||||
if (cap - bufLen < 3) {
|
||||
return NWC24_ERR_OVERFLOW;
|
||||
}
|
||||
srcLen = subjectLen - srcPos;
|
||||
convert = chunk;
|
||||
buf2[bufLen++] = '\r';
|
||||
buf2[bufLen++] = '\n';
|
||||
buf2[bufLen++] = ' ';
|
||||
|
||||
result = NWC24iConvertFromInternalEncoding(work, &convert,
|
||||
subject + srcPos,
|
||||
(s32*)&srcLen, charsetName,
|
||||
NWC24i_CHARSET_WORK_SIZE,
|
||||
encRegion, unk);
|
||||
if (result != NWC24_OK && result != NWC24_ERR_OVERFLOW) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = NWC24EncodeWord(buf2 + bufLen, cap - bufLen, &encLen,
|
||||
charsetName, NWC24i_CHARSET_WORK_SIZE, 'B',
|
||||
work, convert - 1);
|
||||
if (result != NWC24_OK) {
|
||||
return result;
|
||||
}
|
||||
bufLen += encLen - 1;
|
||||
srcPos += srcLen;
|
||||
}
|
||||
|
||||
return NWC24SetMsgSubject(msg, (const char*)buf2, bufLen);
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compact function diff with context: python ctxdiff.py <unit> <symbol> [--full]"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, "/home/cole/projects/tests")
|
||||
from odiff import dis, sym
|
||||
import os
|
||||
import itertools
|
||||
|
||||
|
||||
|
||||
62
tools/decomp-assist/odiff.py
Normal file
62
tools/decomp-assist/odiff.py
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
"""odiff: symbol lookup + normalized disassembly for object files.
|
||||
|
||||
Recreated for this environment (original lived on the author's machine).
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
from capstone import CS_ARCH_PPC, CS_MODE_32, CS_MODE_BIG_ENDIAN, Cs
|
||||
from elftools.elf.elffile import ELFFile
|
||||
|
||||
_cache = {}
|
||||
|
||||
|
||||
def _load(path):
|
||||
if path in _cache:
|
||||
return _cache[path]
|
||||
with open(path, "rb") as f:
|
||||
e = ELFFile(f)
|
||||
st = e.get_section_by_name(".symtab")
|
||||
names = [s.name for s in st.iter_symbols()]
|
||||
syms = {s.name: (s["st_value"], s["st_size"]) for s in st.iter_symbols()}
|
||||
data = {}
|
||||
for sec in e.iter_sections():
|
||||
data[sec.name] = (sec["sh_offset"], sec.data())
|
||||
relocs = {}
|
||||
for sec in e.iter_sections():
|
||||
if sec.name.startswith(".rela"):
|
||||
target = sec.name[5:]
|
||||
for r in sec.iter_relocations():
|
||||
relocs[(target, r["r_offset"])] = names[r["r_info_sym"]]
|
||||
_cache[path] = (data, syms, relocs)
|
||||
return _cache[path]
|
||||
|
||||
|
||||
def sym(path, name):
|
||||
_, syms, _ = _load(path)
|
||||
return syms[name]
|
||||
|
||||
|
||||
def dis(path, off, size, section=".text"):
|
||||
data, syms, relocs = _load(path)
|
||||
sec_data = data[section][1]
|
||||
md = Cs(CS_ARCH_PPC, CS_MODE_32 + CS_MODE_BIG_ENDIAN)
|
||||
out = []
|
||||
for i in md.disasm(sec_data[off:off + size], off):
|
||||
text = i.mnemonic + " " + i.op_str
|
||||
m = re.search(r"0x[0-9a-fA-F]+", i.op_str)
|
||||
if (section, i.address) in relocs:
|
||||
# External reference: show the symbol, drop the raw addend
|
||||
text = i.mnemonic + " " + re.sub(r"0x[0-9a-fA-F]+",
|
||||
"<" + relocs[(section, i.address)] + ">",
|
||||
i.op_str)
|
||||
elif m and i.mnemonic.startswith("b"):
|
||||
val = int(m.group(0), 16)
|
||||
if off <= val < off + size:
|
||||
# Intra-function branch: normalize to function-relative
|
||||
text = i.mnemonic + " " + (i.op_str[:m.start()] +
|
||||
"+" + hex(val - off) +
|
||||
i.op_str[m.end():])
|
||||
out.append((i.address - off, text))
|
||||
return out
|
||||
Reference in New Issue
Block a user