Compare commits

...

1 Commits

Author SHA1 Message Date
coah
e623bf0161 NWC24Mime 8/16, NWC24MsgSubject 5/12 (worker fd8d384c) 2026-09-30 03:27:20 +00:00
6 changed files with 1825 additions and 2 deletions

View File

@@ -53,9 +53,9 @@ Progress
<!-- progress:start -->
| Decompiled | Matched | Linked | Data |
|:---:|:---:|:---:|:---:|
| 85.47% | 75.72% | 56.76% | 86.72% |
| 85.82% | 75.85% | 56.76% | 86.74% |
units 748/1027 complete, functions 11027/12563 matched.
units 748/1027 complete, functions 11040/12563 matched.
decompiled = code with a C/C++ implementation (objdiff fuzzy), matched = byte-exact code, linked = code actually linked into the DOL, data = byte-exact data
<!-- progress:end -->

View File

@@ -104,8 +104,12 @@ NWC24Err NWC24ReadMsgMBOptOutFlag(const NWC24MsgObj* msg, BOOL* mbOptOutFlag, u3
NWC24Err NWC24ReadMsgFromAddr(const NWC24MsgObj* msg, char* addr, u32 addrLen);
NWC24Err NWC24ReadMsgSubject(const NWC24MsgObj* msg, char* subject, u32 subjectLen);
NWC24Err NWC24ReadMsgText(const NWC24MsgObj* msg, char* text, u32 textLen, NWC24Charset* charset, NWC24Encoding* encoding);
#ifdef NWC24_MSG_SUBJECT
NWC24Err NWC24ReadMsgTextEx(const NWC24MsgObj* msg, char* text, u32 textLen, char* name, u32 nameLen);
#else
NWC24Err NWC24ReadMsgTextEx(const NWC24MsgObj* msg, char* text, u32 textLen, const char* name, u32 nameLen, NWC24Charset* charset,
NWC24Encoding* encoding);
#endif
NWC24Err NWC24ReadMsgAttached(const NWC24MsgObj* msg, u32 attachIndex, u8* attachData, u32 attachSize);
NWC24Err NWC24ReadMsgSubjectPublic(const NWC24MsgObj* msg, u16* subject, u32* subjectLen, NWC24EncodingRegion encRegion, u16 unk, u8* work,

View File

@@ -0,0 +1,787 @@
#include <private/nwc24.h>
#include <revolution/nwc24.h>
static const char* MIMEEncStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static NWC24Err Base64Encode(u8* decoded, int decodedSize, u32* stringSize, u8* encoded, int encodedSize, u32* encodedSizeOut) {
NWC24Err result = NWC24_OK;
s32 textIdx;
s32 dataIdx = 0;
BOOL hasThird;
BOOL hasSecond;
u32 combined;
for (textIdx = 0; dataIdx < decodedSize; dataIdx += 3) {
if (textIdx + 3 >= encodedSize) {
result = NWC24_ERR_OVERFLOW;
break;
}
hasThird = FALSE;
hasSecond = FALSE;
combined = (u32)decoded[dataIdx] << 8;
if (dataIdx + 1 < decodedSize) {
hasSecond = TRUE;
combined |= (u8)decoded[dataIdx + 1];
}
combined <<= 8;
if (dataIdx + 2 < decodedSize) {
hasThird = TRUE;
combined |= (u8)decoded[dataIdx + 2];
}
encoded[textIdx + 0] = MIMEEncStr[combined >> 18 & (64 - 1)];
encoded[textIdx + 1] = MIMEEncStr[combined >> 12 & (64 - 1)];
encoded[textIdx + 2] = MIMEEncStr[hasSecond ? (combined >> 6 & (64 - 1)) : 64];
encoded[textIdx + 3] = MIMEEncStr[hasThird ? (combined & (64 - 1)) : 64];
textIdx += 4;
}
*encodedSizeOut = textIdx;
if (stringSize != NULL) {
textIdx = decodedSize;
if (decodedSize < dataIdx) {
dataIdx = decodedSize;
}
*stringSize = dataIdx;
}
return result;
}
NWC24Err NWC24Base64Encode(u8* decoded, int decodedSize, u8* encoded, int encodedSize, u32* stringSize) {
return Base64Encode(decoded, decodedSize, NULL, encoded, encodedSize, stringSize);
}
NWC24Err NWC24Base64Decode(u8* encoded, int encodedSize, u8* decoded, int decodedSize, u32* stringSize) {
int bitCount = 0;
u32 bitBuffer = 0;
u32 decodedOffset = 0;
s8* table = (s8*)NWC24WorkP->base64Work;
BOOL produceOutput;
int i;
for (i = 0; i < encodedSize; i++) {
if ((s32)table[encoded[i]] >= 0) {
if ((s32)table[encoded[i]] < 0x40) {
bitCount += 6;
bitBuffer <<= 6;
produceOutput = bitCount >= 8;
bitBuffer |= (u32)table[encoded[i]];
if (produceOutput) {
if (decodedOffset >= (u32)decodedSize) {
*stringSize = decodedSize;
return NWC24_ERR_OVERFLOW;
}
bitCount -= 8;
*decoded++ = (u8)((s32)bitBuffer >> bitCount);
decodedOffset++;
}
}
}
}
*stringSize = decodedOffset;
return NWC24_OK;
}
void NWC24InitBase64Table(u8* table) {
s8* signedTable = (s8*)table;
int i;
for (i = 0; i < 256; i++) {
signedTable[i] = -1;
}
for (i = 'A'; i <= 'Z'; i++) {
table[i] = i - 'A';
}
for (i = 0; i < 26; i++) {
table['a' + i] = i + 26;
}
for (i = 0; i < 10; i++) {
table['0' + i] = i + 52;
}
table['+'] = 62;
table['/'] = 63;
}
static NWC24Err QEncode(char* encoded, u32 encodedCapacity, u32* encodedSize, u8* decoded, u32 decodedSize, u32* decodedSizeOut, s32 maxLineLength) {
char* output;
u32 textWritten;
u32 dataIdx;
NWC24Err result;
s32 lineLength;
u8 octets[3];
BOOL lineBreak;
s32 emitCount;
s32 bytesRead;
s16 currentByte;
s32 i;
result = NWC24_OK;
if (encoded == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
if (decoded == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
if (decodedSize <= 0) {
return NWC24_ERR_OVERFLOW;
}
if (decodedSizeOut != NULL) {
*decodedSizeOut = 0;
}
if (encodedSize != NULL) {
*encodedSize = 0;
}
output = encoded;
lineLength = 0;
textWritten = 0;
dataIdx = 0;
while (dataIdx < decodedSize) {
currentByte = *decoded;
lineBreak = FALSE;
if ((char)currentByte >= '!' && (char)currentByte <= '~' && (char)currentByte != '=') {
bytesRead = 1;
emitCount = 1;
octets[0] = *decoded;
} else if (dataIdx + 1 < decodedSize && (char)currentByte == '\r' && (char)decoded[1] == '\n') {
bytesRead = 2;
emitCount = 1;
octets[0] = *decoded;
octets[1] = decoded[1];
} else {
octets[0] = '=';
if ((*decoded >> 4) >= 10) {
octets[1] = (*decoded >> 4) % 10 + 'A';
} else {
octets[1] = (*decoded >> 4) % 10 + '0';
}
if ((*decoded & 0x0F) >= 10) {
octets[2] = (*decoded & 0x0F) % 10 + 'A';
} else {
octets[2] = (*decoded & 0x0F) % 10 + '0';
}
bytesRead = 1;
emitCount = 3;
}
if (maxLineLength == 0 && lineLength + emitCount >= 75) {
lineBreak = TRUE;
emitCount += 3;
}
if (textWritten + emitCount >= encodedCapacity) {
result = NWC24_ERR_OVERFLOW;
break;
}
textWritten += emitCount;
if (lineBreak) {
output[0] = '=';
output[1] = '\r';
output[2] = '\n';
output += 3;
emitCount -= 3;
lineLength = 0;
}
lineLength += emitCount;
for (i = 0; i < emitCount; i++) {
*(output++) = octets[i];
}
dataIdx += bytesRead;
decoded += bytesRead;
}
if (decodedSizeOut != NULL) {
*decodedSizeOut = dataIdx;
}
if (encodedSize != NULL) {
*encodedSize = textWritten;
}
return result;
}
static NWC24Err QDecode(char* decoded, u32 decodedCapacity, u32* decodedSize, char* encoded, u32 encodedSize, u32* encodedSizeOut, int flags) {
u32 decodedOffset = 0;
u32 encodedOffset;
char* input;
char* output;
u32 value = 0;
NWC24Err result = NWC24_OK;
if (decoded == NULL || encoded == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
if (decodedCapacity == 0) {
return NWC24_ERR_OVERFLOW;
}
if (decodedSize != NULL) {
*decodedSize = 0;
}
if (encodedSizeOut != NULL) {
*encodedSizeOut = 0;
}
input = encoded;
output = decoded;
for (encodedOffset = 0; encodedOffset < encodedSize; encodedOffset++) {
if (decodedCapacity <= decodedOffset) {
result = NWC24_ERR_OVERFLOW;
break;
}
if (flags != 0 && *input == '_') {
*output++ = ' ';
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;
break;
}
if (*next != ' ' && *next != '\t') {
break;
}
previous = next;
scan++;
}
}
if (*current == '\r' && current[1] == '\n') {
current += 2;
encodedOffset = inputAfterEqual + 1;
input = current;
} else {
int high = Util_xtoi(*current);
int low;
if (high >= 0) {
value = (high & 0x0F) << 4;
}
low = Util_xtoi(current[1]);
if (low >= 0) {
value |= low & 0x0F;
}
current += 2;
if (high >= 0 && low >= 0) {
*output++ = (char)value;
decodedOffset++;
}
encodedOffset = inputAfterEqual + 1;
input = current;
}
} else {
*output++ = *input++;
decodedOffset++;
}
}
if (decodedSize != NULL) {
*decodedSize = decodedOffset;
}
if (encodedSizeOut != NULL) {
*encodedSizeOut = encodedOffset;
}
return result;
}
NWC24Err NWC24EncodeQuotedPrintable(char* encoded, u32 encodedCapacity, u32* encodedSize, u8* decoded, u32 decodedSize, u32* decodedSizeOut) {
return QEncode(encoded, encodedCapacity, encodedSize, decoded, decodedSize, decodedSizeOut, 0);
}
NWC24Err NWC24DecodeQuotedPrintable(char* decoded, u32 decodedCapacity, u32* decodedSize, u8* encoded, u32 encodedSize, u32* encodedSizeOut) {
return QDecode(decoded, decodedCapacity, decodedSize, (char*)encoded, encodedSize, encodedSizeOut, 0);
}
static NWC24Err ConcatEncodedText(char* encoded, u32 encodedCapacity, u32* encodedSize, char encoding, u8* decoded, u32 decodedSize,
u32* decodedSizeOut);
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;
BOOL encodeOverflow = FALSE;
if (decodedSizeOut != NULL) {
*decodedSizeOut = 0;
}
if (encodedCapacity == 0) {
if (decodedSizeOut != NULL) {
*decodedSizeOut = 0;
}
return NWC24_ERR_INVALID_VALUE;
}
*encoded = '\0';
if (encodedSize != NULL) {
*encodedSize = 1;
}
if (Mail_strlen(encoded) + 2 < encodedCapacity) {
Mail_strncat(encoded, "=?", 2);
} else {
result = NWC24_ERR_OVERFLOW;
}
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 (result == NWC24_OK) {
if (Mail_strlen(encoded) + 2 < encodedCapacity) {
Mail_strncat(encoded, "?", 1);
} else {
result = NWC24_ERR_OVERFLOW;
}
}
if (result == NWC24_OK) {
if (encoding == 'B' || encoding == 'b' || encoding == 'Q' || encoding == 'q') {
char value[1];
u32 length = Mail_strlen(encoded);
value[0] = encoding;
if (length + 1 < encodedCapacity) {
Mail_strncat(encoded, value, 1);
} else {
result = NWC24_ERR_OVERFLOW;
}
} else {
result = NWC24_ERR_INVALID_VALUE;
}
}
if (result == NWC24_OK) {
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;
}
result = NWC24_ERR_OVERFLOW;
}
} else {
*encoded = '\0';
if (decodedSizeOut != NULL) {
*decodedSizeOut = 1;
}
}
} else {
*encoded = '\0';
}
return result;
}
NWC24Err NWC24EncodeWord(u8* encoded, u32 encodedCapacity, u32* encodedSize, char* charset, u32 charsetSize, char encoding, u8* decoded,
u32 decodedSize) {
NWC24Err result;
if (encodedSize == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
if (encoded == NULL || encodedCapacity == 0) {
return NWC24_ERR_INVALID_VALUE;
}
if (charset == NULL || charsetSize == 0) {
return NWC24_ERR_INVALID_VALUE;
}
if (encoding == '\0') {
return NWC24_ERR_INVALID_VALUE;
}
if (decoded == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
*encoded = 0;
*encodedSize = 1;
result = EncodeWord((char*)encoded, encodedCapacity, encodedSize, charset, charsetSize, encoding, decoded, decodedSize, NULL);
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
encoded[*encodedSize - 1] = 0;
}
return result;
}
static NWC24Err ExtractCharset(char* charset, u32 charsetCapacity, u32* charsetSize, char* encoded, u32 encodedSize);
static NWC24Err ExtractEncodedText(char* decoded, u32 decodedCapacity, u32* decodedSize, char encoding, char* encoded, u32 encodedSize,
u32* encodedSizeOut);
static BOOL CopyWithoutLinearWhiteSpaces(char* output, int* outputSize, char* input, int inputSize) {
BOOL afterNewline = FALSE;
u32 outputOffset = 0;
s32 capacity;
if (output == NULL || outputSize == NULL || *outputSize == 0) {
return FALSE;
}
capacity = *outputSize;
if (input == NULL) {
*output = '\0';
*outputSize = 1;
return TRUE;
}
while (outputOffset < capacity - 1U && inputSize != 0 && *input != '\0') {
char value = *input;
switch (value) {
case '\r':
case '\n':
if (!afterNewline) {
*output++ = ' ';
outputOffset++;
}
afterNewline = TRUE;
break;
case '\t':
case ' ':
if (!afterNewline) {
*output++ = value;
outputOffset++;
}
break;
default:
*output = value;
afterNewline = FALSE;
output++;
outputOffset++;
break;
}
input++;
inputSize--;
}
*output = '\0';
*outputSize = outputOffset + 1;
return inputSize == 0 || *input == '\0';
}
static NWC24Err DecodeWord(char* charsetData, u32 charsetCapacity, char* decoded, u32 decodedCapacity, u32* decodedSize, char* encoded,
u32 encodedSize, u32* encodedSizeOut) {
int encodedWordPosition = 0;
int encodedLength;
u32 decodedLength;
u32 consumedSize;
NWC24Err result = NWC24_OK;
if (encodedSizeOut != NULL) {
*encodedSizeOut = 0;
}
if (decodedCapacity == 0) {
if (encodedSizeOut != NULL)
*encodedSizeOut = 0;
return NWC24_ERR_INVALID_VALUE;
}
*decoded = '\0';
if (decodedSize != NULL) {
*decodedSize = 1;
}
if (encodedSizeOut != NULL) {
*encodedSizeOut = 0;
}
{
u32 markerLength = Mail_strlen("=?");
u32 offset;
for (offset = 0; offset <= encodedSize; offset++) {
encodedWordPosition = (int)(encoded + offset);
if (Mail_strncmp((char*)encodedWordPosition, "=?", markerLength) == 0) {
break;
}
}
if (offset > encodedSize) {
encodedWordPosition = 0;
}
}
if (encodedWordPosition == (int)encoded) {
result = ExtractCharset(charsetData, charsetCapacity, &consumedSize, (char*)encoded, encodedSize);
if (result != NWC24_OK) {
return result;
}
encodedLength = consumedSize;
{
u32 markerLength = Mail_strlen("?");
u32 offset;
char* delimiter = NULL;
char* current = (char*)encodedWordPosition + encodedLength;
for (offset = 0; offset <= encodedSize - encodedLength; offset++) {
if (Mail_strncmp((char*)current, "?", markerLength) == 0) {
delimiter = current;
break;
}
current++;
}
if (delimiter == 0) {
return NWC24_ERR_INVALID_VALUE;
}
{
char encoding = delimiter[1];
consumedSize = 2;
encodedLength += consumedSize;
result = ExtractEncodedText(decoded, decodedCapacity, &decodedLength, encoding, encoded + encodedLength, encodedSize - encodedLength,
&consumedSize);
if (result != NWC24_OK) {
return result;
}
encodedLength += consumedSize;
}
}
} else {
u32 localSize = decodedCapacity;
if (encodedWordPosition == 0) {
encodedLength = STD_strnlen(encoded, encodedSize);
} else {
encodedLength = encodedWordPosition - (int)encoded;
}
if (!CopyWithoutLinearWhiteSpaces(decoded, (int*)&localSize, encoded, encodedLength)) {
if (decodedSize != NULL) {
*decodedSize = localSize;
}
if (encodedSizeOut != NULL) {
*encodedSizeOut = encodedLength;
}
return NWC24_ERR_OVERFLOW;
}
decodedLength = localSize;
if (encodedWordPosition != 0) {
BOOL whitespaceOnly = TRUE;
u32 offset;
for (offset = 0; offset < localSize && decoded[offset] != '\0'; offset++) {
char value = decoded[offset];
if (value != ' ' && value != '\t' && value != '\r' && value != '\n') {
whitespaceOnly = FALSE;
}
}
if (whitespaceOnly) {
*decoded = '\0';
decodedLength = 1;
}
}
}
if (decodedSize != NULL) {
*decodedSize = decodedLength;
}
if (encodedSizeOut != NULL) {
*encodedSizeOut = encodedLength;
}
return result;
}
NWC24Err NWC24DecodeMIMEHeaderFieldBody(u8* charsetData, u32 charsetDataSize, u8* decoded, u32 decodedCapacity, u32* decodedSize, u8* encoded,
u32 encodedSize) {
int result = 0;
u8* output;
u32 inputSize = encodedSize;
if (decodedSize == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
if (charsetData == NULL || charsetDataSize == 0) {
return NWC24_ERR_INVALID_VALUE;
}
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;
*charsetData = 0;
*decodedSize = 1;
output = decoded;
while ((s32)inputSize > 0 && (s32)decodedCapacity > 0 && *encoded != '\0') {
u32 copiedSize = 0;
u32 consumedSize;
result =
DecodeWord((char*)charsetData, charsetDataSize, (char*)output, decodedCapacity, &copiedSize, (char*)encoded, inputSize, &consumedSize);
if (copiedSize == 0) {
break;
}
copiedSize--;
output += copiedSize;
decodedCapacity -= copiedSize;
*decodedSize += copiedSize;
encoded += consumedSize;
inputSize -= consumedSize;
if (result != NWC24_OK) {
break;
}
}
if (result == NWC24_OK && inputSize > 0 && *encoded != '\0') {
result = NWC24_ERR_OVERFLOW;
}
decoded[*decodedSize - 1] = '\0';
return result;
}
static NWC24Err ConcatEncodedText(char* encoded, u32 encodedCapacity, u32* encodedSize, char encoding, u8* decoded, u32 decodedSize,
u32* decodedSizeOut) {
u32 prefixSize = Mail_strlen(encoded);
int available = encodedCapacity - prefixSize - 2;
NWC24Err result;
if (available <= 0) {
return NWC24_ERR_OVERFLOW;
}
if (encoding == 'B' || encoding == 'b') {
result = Base64Encode(decoded, decodedSize, decodedSizeOut, (u8*)encoded + prefixSize, (u32)(available - 1), encodedSize);
} else if (encoding == 'Q' || encoding == 'q') {
result = QEncode(encoded + prefixSize, (u32)(available - 1), encodedSize, decoded, decodedSize, decodedSizeOut, 1);
} else {
result = NWC24_ERR_NOT_SUPPORTED;
}
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
encoded[prefixSize + *encodedSize] = '\0';
*encodedSize += 1;
}
return result;
}
static NWC24Err ExtractCharset(char* charset, u32 charsetCapacity, u32* charsetSize, char* encoded, u32 encodedSize) {
u32 markerLength;
u32 offset;
int start = 0;
int end = 0;
u32 charsetLength;
if (charset == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
markerLength = Mail_strlen("=?");
for (offset = 0; offset <= encodedSize; offset++) {
int current = (int)(encoded + offset);
if (Mail_strncmp((char*)current, "=?", markerLength) == 0) {
start = current;
break;
}
}
if (start == 0) {
return NWC24_ERR_NOT_SUPPORTED;
}
markerLength = Mail_strlen("?");
start += 2;
for (offset = 0; offset <= encodedSize - (u32)(start - (int)encoded); offset++) {
int current = start + offset;
if (Mail_strncmp((char*)current, "?", markerLength) == 0) {
end = current;
break;
}
}
if (end == 0) {
return NWC24_ERR_NOT_SUPPORTED;
}
charsetLength = (u32)(end - start);
if (charsetLength >= charsetCapacity) {
return NWC24_ERR_OVERFLOW;
}
Mail_strncpy(charset, (char*)start, charsetLength);
charset[charsetLength] = '\0';
*charsetSize = charsetLength + 2;
return NWC24_OK;
}
static NWC24Err ExtractEncodedText(char* decoded, u32 decodedCapacity, u32* decodedSize, char encoding, char* encoded, u32 encodedSize,
u32* encodedSizeOut) {
u32 markerLength;
u32 offset;
int start = 0;
int end = 0;
int length;
NWC24Err result;
if (encoded == NULL || decodedSize == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
*decodedSize = 0;
if (encodedSizeOut == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
*encodedSizeOut = 0;
markerLength = Mail_strlen("?");
for (offset = 0; offset <= encodedSize; offset++) {
int current = (int)(encoded + offset);
if (Mail_strncmp((char*)current, "?", markerLength) == 0) {
start = current;
break;
}
}
if (start == 0) {
return NWC24_ERR_NOT_SUPPORTED;
}
markerLength = Mail_strlen("?=");
start++;
for (offset = 0; offset <= encodedSize - (u32)(start - (int)encoded); offset++) {
int current = start + offset;
if (Mail_strncmp((char*)current, "?=", markerLength) == 0) {
end = current;
break;
}
}
if (end == 0) {
return NWC24_ERR_NOT_SUPPORTED;
}
length = end - start;
if (encoding == 'B' || encoding == 'b') {
result = NWC24Base64Decode((u8*)start, length, (u8*)decoded, decodedCapacity - 1, decodedSize);
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
decoded[*decodedSize] = '\0';
*decodedSize += 1;
}
} else if (encoding == 'Q' || encoding == 'q') {
result = QDecode(decoded, decodedCapacity - 1, decodedSize, (char*)start, length, NULL, 1);
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
decoded[*decodedSize] = '\0';
*decodedSize += 1;
}
} else {
return NWC24_ERR_NOT_SUPPORTED;
}
*encodedSizeOut = (u32)(end - (int)encoded) + 2;
return result;
}

View File

@@ -0,0 +1,855 @@
#define NWC24_MSG_SUBJECT
#include <private/enc.h>
#include <private/nwc24.h>
#include <revolution/enc.h>
#include <revolution/nwc24.h>
#include <string.h>
static char CharsetUsAscii[] = "us-ascii";
static char CharsetIso2022Jp[] = "iso-2022-jp";
static char CharsetUtf8[] = "utf-8";
static char CharsetEucKr[] = "euc-kr";
static char CharsetGb2312[] = "gb2312";
static const char* JapaneseCharsets[] = {CharsetUsAscii, CharsetIso2022Jp, CharsetUtf8};
static const char* ChineseCharsets[] = {CharsetUsAscii, CharsetGb2312};
static char CharsetIso88591[] = "iso-8859-1";
static char CharsetWindows1252[] = "windows-1252";
static char CharsetIso88592[] = "iso-8859-2";
static char CharsetIso88597[] = "iso-8859-7";
static char CharsetIso885910[] = "iso-8859-10";
char CharsetIso88593[] = "iso-8859-3";
static const char* WesternCharsets[] = {CharsetUsAscii, CharsetIso88591, CharsetWindows1252, CharsetIso88592,
CharsetIso88597, CharsetIso885910, CharsetIso88593, CharsetUtf8};
static const char* KoreanCharsets[] = {CharsetUsAscii, CharsetEucKr, CharsetUtf8};
static char DefaultCharsetJp[] = "iso-2022-jp";
static char DefaultCharsetKr[] = "euc-kr";
NWC24Err NWC24iGetDefaultCharset(char* charset, u32 charsetSize, NWC24EncodingRegion region);
NWC24Err NWC24iConvertToInternalEncoding(u16* dst, u32* dstSize, const u8* src, u32* srcSize, char* charset, u32 charsetSize,
NWC24EncodingRegion region, u16 alternative);
NWC24Err NWC24iConvertFromInternalEncoding(u8* dst, u32* dstSize, const u16* src, u32* srcSize, char* charset, u32 charsetSize,
NWC24EncodingRegion region, u16 alternative);
NWC24Err NWC24iDetectEncodingToSend(char* charset, u32 charsetSize, const u16* subject, u32 subjectSize, const u16* text, u32 textSize,
NWC24EncodingRegion region);
NWC24Err NWC24iDetectBreakPoint(u32* breakPoint, NWC24Charset charset, const u8* text, u32 textSize, u32 maxSize);
NWC24Err NWC24iSetMsgSubjectPlain(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize, NWC24Charset charset, char* charsetName);
NWC24Err NWC24iSetMsgSubjectQP(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize, NWC24Charset charset, char* charsetName);
NWC24Err NWC24iSetMsgSubjectBase64(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize, NWC24Charset charset, char* charsetName);
NWC24Err NWC24ReadMsgSubjectPublic(const NWC24MsgObj* msg, u16* subject, u32* subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize) {
BOOL truncated;
u8* decodedData;
u32 decodedCapacity;
u32 originalSize;
u32 subjectDataSize;
u32 workHalf;
u32 decodedSize;
char* charsetBuffer;
NWC24Err result;
u32 i;
if (subjectSize == NULL) {
result = NWC24_ERR_INVALID_VALUE;
} else {
originalSize = *subjectSize;
*subjectSize = 0;
truncated = FALSE;
if (work == NULL) {
result = NWC24_ERR_INVALID_VALUE;
} else {
workHalf = workSize >> 1;
decodedData = work + workHalf;
decodedCapacity = workSize - workHalf;
result = NWC24GetMsgSubjectSize(msg, &subjectDataSize);
switch (result) {
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;
}
}
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;
}
}
*subjectSize = originalSize;
result =
NWC24iConvertToInternalEncoding(subject, subjectSize, decodedData, &decodedSize, charsetBuffer, 0x40, region, alternative);
if ((result == NWC24_OK) || (result == NWC24_ERR_OVERFLOW)) {
for (i = 0; (i < *subjectSize) && (subject[i] != 0); i++) {
if ((subject[i] == 0x0D) || (subject[i] == 0x0A)) {
subject[i] = 0x20;
}
}
if ((result == NWC24_OK) && truncated) {
result = NWC24_ERR_OVERFLOW;
}
}
break;
default:
goto done;
}
}
}
done:
return result;
}
NWC24Err NWC24ReadMsgTextPublic(const NWC24MsgObj* msg, u16* text, u32* textSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize) {
char* charsetBuffer;
BOOL truncated;
u32 originalSize;
u32 textDataSize;
NWC24Err result;
if (textSize == NULL) {
result = NWC24_ERR_INVALID_VALUE;
} else {
originalSize = *textSize;
*textSize = 0;
truncated = FALSE;
if (work == NULL) {
result = NWC24_ERR_INVALID_VALUE;
} else {
result = NWC24GetMsgTextSize(msg, &textDataSize);
switch (result) {
case NWC24_OK:
if (textDataSize > workSize) {
textDataSize = workSize;
truncated = TRUE;
}
charsetBuffer = NWC24WorkP->stringWork + 0x20;
result = NWC24ReadMsgTextEx(msg, (char*)work, textDataSize, charsetBuffer, 0x40);
if (result != NWC24_OK) {
if (result != NWC24_ERR_OVERFLOW) {
goto done;
}
truncated = TRUE;
}
*textSize = originalSize;
result = NWC24iConvertToInternalEncoding(text, textSize, work, &textDataSize, charsetBuffer, 0x40, region, alternative);
if ((result == NWC24_OK) && truncated) {
result = NWC24_ERR_OVERFLOW;
}
break;
default:
goto done;
}
}
}
done:
return result;
}
NWC24Err NWC24SetMsgSubjectPublic(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize) {
NWC24MsgObjPrivate* privateMsg = (NWC24MsgObjPrivate*)msg;
NWC24Work* nwcWork;
NWC24Charset charset;
NWC24Err result;
if (((privateMsg->type & 0x100) == 0) || ((privateMsg->type & 0x200) != 0)) {
result = NWC24_ERR_PROTECTED;
goto done;
}
if ((subject == NULL) || (*subject == 0)) {
result = NWC24_ERR_NULL;
goto done;
}
if (subject[subjectSize] != 0) {
result = NWC24_ERR_STRING_END;
goto done;
}
if (work == NULL) {
result = NWC24_ERR_INVALID_VALUE;
goto done;
}
nwcWork = NWC24WorkP;
result = NWC24iDetectEncodingToSend(nwcWork->stringWork, 0x40, subject, subjectSize, NULL, 0, region);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
if (NWC24ParseCharsetStr(&charset, nwcWork->stringWork) != NWC24_OK) {
charset = 0;
}
if (charset == 0) {
result = NWC24iSetMsgSubjectPlain(msg, subject, subjectSize, region, alternative, work, workSize, charset, nwcWork->stringWork);
goto done;
}
switch (region) {
case 0:
result = 2;
break;
case 1:
case 2:
result = 3;
break;
case 3:
case 4:
result = 2;
break;
default:
result = 2;
break;
}
if (result == 3) {
result = NWC24iSetMsgSubjectQP(msg, subject, subjectSize, region, alternative, work, workSize, charset, nwcWork->stringWork);
} else {
result = NWC24iSetMsgSubjectBase64(msg, subject, subjectSize, region, alternative, work, workSize, charset, nwcWork->stringWork);
}
done:
return result;
}
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;
u32 textSourceSize;
u32 textWorkSize;
BOOL is7Bit;
NWC24Charset charset;
u32 subjectWorkSize;
u8* subjectWork;
NWC24Err result;
if (((privateMsg->type & 0x100) == 0) || ((privateMsg->type & 0x200) != 0)) {
result = NWC24_ERR_PROTECTED;
goto done;
}
if ((subject == NULL) || (*subject == 0)) {
result = NWC24_ERR_NULL;
goto done;
}
if ((text == NULL) || (textSize == 0)) {
NWC24Data_Init(&privateMsg->text);
result = NWC24SetMsgSubjectPublic(msg, subject, subjectSize, region, alternative, work, workSize);
goto done;
}
if ((text[textSize] != 0) || (subject[subjectSize] != 0)) {
result = NWC24_ERR_STRING_END;
goto done;
}
if (work == NULL) {
result = NWC24_ERR_INVALID_VALUE;
goto done;
}
nwcWork = NWC24WorkP;
result = NWC24iDetectEncodingToSend(nwcWork->stringWork, 0x40, subject, subjectSize, text, textSize, region);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
if (NWC24ParseCharsetStr(&charset, nwcWork->stringWork) != NWC24_OK) {
charset = 0;
}
textWorkSize = (workSize * textSize) / (textSize + subjectSize * 4);
subjectWorkSize = workSize - textWorkSize;
subjectWork = work + textWorkSize;
textSourceSize = textSize;
result = NWC24iConvertFromInternalEncoding(work, &textWorkSize, text, &textSourceSize, nwcWork->stringWork, 0x40, region, alternative);
if (result != NWC24_OK) {
goto done;
}
result = ENCIs7BitEncoding(&is7Bit, nwcWork->stringWork);
if (!is7Bit || (result != ENC_OK)) {
switch (region) {
case 0:
result = 2;
break;
case 1:
case 2:
result = 3;
break;
case 3:
case 4:
result = 2;
break;
default:
result = 2;
break;
}
} else {
result = 0;
}
result = NWC24SetMsgText(msg, (char*)work, textWorkSize - 1, charset, result);
if (result != NWC24_OK) {
goto done;
}
if (charset == 0) {
result = NWC24iSetMsgSubjectPlain(msg, subject, subjectSize, region, alternative, subjectWork, subjectWorkSize, charset, nwcWork->stringWork);
goto done;
}
switch (region) {
case 0:
result = 2;
break;
case 1:
case 2:
result = 3;
break;
case 3:
result = 2;
break;
case 4:
result = 2;
break;
default:
result = 2;
break;
}
if (result == 3) {
result = NWC24iSetMsgSubjectQP(msg, subject, subjectSize, region, alternative, subjectWork, subjectWorkSize, charset, nwcWork->stringWork);
} else {
result =
NWC24iSetMsgSubjectBase64(msg, subject, subjectSize, region, alternative, subjectWork, subjectWorkSize, charset, nwcWork->stringWork);
}
done:
return result;
}
NWC24Err NWC24iGetDefaultCharset(char* charset, u32 charsetSize, NWC24EncodingRegion region) {
NWC24Err result = NWC24_OK;
switch (region) {
case 0:
strncpy(charset, DefaultCharsetJp, charsetSize - 1);
charset[charsetSize - 1] = 0;
goto done;
case 1:
strncpy(charset, "iso-8859-1", charsetSize - 1);
charset[charsetSize - 1] = 0;
goto done;
case 2:
strncpy(charset, "iso-8859-1", charsetSize - 1);
charset[charsetSize - 1] = 0;
goto done;
case 3:
strncpy(charset, DefaultCharsetKr, charsetSize - 1);
charset[charsetSize - 1] = 0;
goto done;
case 4:
strncpy(charset, "gb2312", charsetSize - 1);
charset[charsetSize - 1] = 0;
goto done;
default:
strncpy(charset, "iso-8859-1", charsetSize - 1);
charset[charsetSize - 1] = 0;
result = NWC24_ERR_INVALID_VALUE;
}
done:
return result;
}
NWC24Err NWC24iConvertToInternalEncoding(u16* dst, u32* dstSize, const u8* src, u32* srcSize, char* charset, u32 charsetSize,
NWC24EncodingRegion region, u16 alternative) {
ENCContext context;
u32 dstBytes;
u32 convertedBytes;
NWC24Err result;
dstBytes = *dstSize * 2;
*dstSize = 0;
if (dstBytes < 2) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
if (charset[0] == '\0') {
result = NWC24iGetDefaultCharset(charset, charsetSize, region);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
}
result = ENCInitContext(&context);
if ((result == ENC_OK) && ((result = ENCSetExternalEncoding(&context, charset)) == ENC_OK) &&
((result = ENCSetBreakType(&context, ENC_BR_LF)) == ENC_OK) &&
((alternative == 0) || ((result = ENCSetAlternativeCharacter(&context, alternative, alternative)) == ENC_OK))) {
convertedBytes = dstBytes - 2;
result = ENCConvertToInternalEncoding(&context, dst, (s32*)&convertedBytes, src, (s32*)srcSize);
dst[convertedBytes >> 1] = 0;
*dstSize = (convertedBytes >> 1) + 1;
}
if (result == ENC_ERR_NO_MAP_RULE) {
goto invalidCharacter;
}
if (result < ENC_ERR_NO_MAP_RULE) {
if (result == ENC_ERR_INVALID_FORMAT) {
goto invalidCharacter;
}
if (result >= ENC_ERR_INVALID_FORMAT) {
goto invalidValue;
}
if (result >= ENC_ERR_NOT_LOADED) {
goto notSupported;
}
} else {
if (result == ENC_OK) {
goto success;
}
if (result >= ENC_OK) {
goto fatal;
}
goto overflow;
}
goto fatal;
success:
result = ENC_OK;
goto done;
invalidValue:
result = NWC24_ERR_INVALID_VALUE;
goto done;
overflow:
result = NWC24_ERR_OVERFLOW;
goto done;
invalidCharacter:
result = NWC24_ERR_INVALID_CHAR;
goto done;
notSupported:
result = NWC24_ERR_NOT_SUPPORTED;
goto done;
fatal:
result = NWC24_ERR_FATAL;
done:
return result;
}
NWC24Err NWC24iConvertFromInternalEncoding(u8* dst, u32* dstSize, const u16* src, u32* srcSize, char* charset, u32 charsetSize,
NWC24EncodingRegion region, u16 alternative) {
ENCContext context;
u32 outputCapacity;
u32 outputBytes;
u32 inputBytes;
NWC24Err result;
outputCapacity = *dstSize;
*dstSize = 0;
if (outputCapacity < 1) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
if (charset[0] == '\0') {
result = NWC24iGetDefaultCharset(charset, charsetSize, region);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
}
result = ENCInitContext(&context);
if ((result == ENC_OK) && ((result = ENCSetExternalEncoding(&context, charset)) == ENC_OK) &&
((result = ENCSetBreakType(&context, ENC_BR_CRLF)) == ENC_OK) &&
((alternative == 0) || ((result = ENCSetAlternativeCharacter(&context, alternative, alternative)) == ENC_OK))) {
outputBytes = outputCapacity - 1;
inputBytes = *srcSize << 1;
result = ENCConvertFromInternalEncoding(&context, dst, (s32*)&outputBytes, src, (s32*)&inputBytes);
dst[outputBytes] = 0;
*srcSize = inputBytes >> 1;
*dstSize = outputBytes + 1;
}
if (result == ENC_ERR_NO_MAP_RULE) {
goto invalidCharacter;
}
if (result < ENC_ERR_NO_MAP_RULE) {
if (result == ENC_ERR_INVALID_FORMAT) {
goto invalidCharacter;
}
if (result >= ENC_ERR_INVALID_FORMAT) {
goto invalidValue;
}
if (result >= ENC_ERR_NOT_LOADED) {
goto notSupported;
}
goto fatal;
}
if (result == ENC_OK) {
goto success;
}
if (result >= ENC_OK) {
goto fatal;
}
goto overflow;
success:
result = ENC_OK;
goto done;
invalidValue:
result = NWC24_ERR_INVALID_VALUE;
goto done;
overflow:
result = NWC24_ERR_OVERFLOW;
goto done;
invalidCharacter:
result = NWC24_ERR_INVALID_CHAR;
goto done;
notSupported:
result = NWC24_ERR_NOT_SUPPORTED;
goto done;
fatal:
result = NWC24_ERR_FATAL;
done:
return result;
}
NWC24Err NWC24iDetectEncodingToSend(char* charset, u32 charsetSize, const u16* subject, u32 subjectSize, const u16* text, u32 textSize,
NWC24EncodingRegion region) {
const char** names;
int numNames;
int index;
int lastIndex;
int endIndex;
int startIndex;
int subjectIndex;
int result;
index = 0;
lastIndex = -1;
if (region == 3) {
names = KoreanCharsets;
numNames = 3;
} else if (region < 3) {
if (region == 0) {
names = JapaneseCharsets;
numNames = 3;
} else if (region >= 0) {
names = WesternCharsets;
numNames = 8;
} else {
names = WesternCharsets;
numNames = 8;
}
} else if (region < 5) {
names = ChineseCharsets;
numNames = 2;
} else {
names = WesternCharsets;
numNames = 8;
}
endIndex = numNames - 1;
startIndex = 0;
do {
subjectIndex = index;
if (endIndex <= index) {
break;
}
result = ENCCheckEncoding(&index, names + startIndex, endIndex - startIndex, subject, subjectSize << 1);
if (result != ENC_OK) {
return NWC24_ERR_NOT_SUPPORTED;
}
if (index == ENC_CHECK_ENCODING_NOT_FOUND) {
subjectIndex = numNames - 1;
break;
}
subjectIndex = startIndex + index;
if ((text == NULL) || (lastIndex == subjectIndex)) {
break;
}
lastIndex = subjectIndex;
result = ENCCheckEncoding(&index, names + subjectIndex, endIndex - subjectIndex, text, textSize << 1);
if (result != ENC_OK) {
return NWC24_ERR_NOT_SUPPORTED;
}
if (index == ENC_CHECK_ENCODING_NOT_FOUND) {
subjectIndex = numNames - 1;
break;
}
lastIndex = subjectIndex + index;
startIndex = lastIndex;
} while (lastIndex != subjectIndex);
index = subjectIndex;
strncpy(charset, names[index], charsetSize - 1);
charset[charsetSize - 1] = '\0';
return NWC24_OK;
}
NWC24Err NWC24iDetectBreakPoint(u32* breakPoint, NWC24Charset charset, const u8* text, u32 textSize, u32 maxSize) {
const u8* ptr;
BOOL byteEncoding;
u32 i;
u32 lastBreak;
i = 0;
lastBreak = 0;
if ((breakPoint == NULL) || (text == NULL)) {
return NWC24_ERR_INVALID_VALUE;
}
if (charset == 0) {
byteEncoding = TRUE;
} else if (charset == NWC24_UTF_8) {
byteEncoding = FALSE;
} else if (((charset & 0xFFFF0000) == 0x00080000) || (charset == NWC24_WINDOWS_1252)) {
byteEncoding = TRUE;
} else {
return NWC24_ERR_NOT_SUPPORTED;
}
if (textSize <= maxSize) {
*breakPoint = textSize;
return NWC24_OK;
}
ptr = text;
while ((i < maxSize) && (*text != 0)) {
if (*ptr == 0x20) {
lastBreak = i;
}
i++;
ptr++;
}
if (lastBreak != 0) {
*breakPoint = lastBreak;
return NWC24_OK;
}
if (byteEncoding) {
*breakPoint = maxSize;
return NWC24_OK;
}
{
u32 utf8Index = 0;
ptr = text;
while (utf8Index < maxSize && *text != 0) {
if (((u32)*ptr & 0xC0) != 0x80U)
lastBreak = utf8Index;
utf8Index++;
ptr++;
}
}
*breakPoint = lastBreak;
return NWC24_OK;
}
NWC24Err NWC24iSetMsgSubjectPlain(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 subjectLength;
u32 lineLength;
u32 sourceOffset;
s32 copied;
u32 i;
NWC24Err result;
workHalf = workSize >> 1;
secondSize = workSize - workHalf;
second = work + workHalf;
subjectLength = subjectSize;
lineLength = 0;
result = NWC24iConvertFromInternalEncoding(work, &workHalf, subject, &subjectLength, charsetName, 0x40, region, alternative);
if (result == NWC24_OK) {
subjectLength = workHalf - 1;
for (i = 0; i < workHalf; i++) {
if ((work[i] == '\n') || (work[i] == '\r')) {
work[i] = ' ';
}
}
NWC24iDetectBreakPoint(&lineLength, charset, work, subjectLength, 0x3F);
if (lineLength == subjectLength) {
result = NWC24SetMsgSubject(msg, (char*)work, subjectLength);
} else if (lineLength < secondSize) {
copied = NWC24iStrLCpy((char*)second, (char*)work, lineLength + 1);
workHalf = secondSize;
for (sourceOffset = lineLength; sourceOffset < subjectLength; sourceOffset += lineLength) {
if (workHalf - copied < 3) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
second[copied] = '\r';
second[copied + 1] = '\n';
copied += 2;
if (work[sourceOffset] != ' ') {
second[copied] = ' ';
copied++;
}
NWC24iDetectBreakPoint(&lineLength, charset, work + sourceOffset, subjectLength - sourceOffset, 0x47);
if (workHalf - copied <= lineLength) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
copied += NWC24iStrLCpy((char*)second + copied, (char*)work + sourceOffset, lineLength + 1);
}
second[copied] = 0;
result = NWC24SetMsgSubject(msg, (char*)second, copied);
} else {
result = NWC24_ERR_OVERFLOW;
}
}
done:
return result;
}
NWC24Err NWC24iSetMsgSubjectQP(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 subjectLength;
u32 lineLength;
u32 outputLength;
u32 sourceOffset;
u32 nextOutput;
u32 total;
u32 i;
u32 charsetLength;
NWC24Err result;
workHalf = workSize >> 1;
secondSize = workSize - workHalf;
second = work + workHalf;
subjectLength = subjectSize;
lineLength = 0;
result = NWC24iConvertFromInternalEncoding(work, &workHalf, subject, &subjectLength, charsetName, 0x40, region, alternative);
if (result == NWC24_OK) {
subjectLength = workHalf - 1;
for (i = 0; i < workHalf; i++) {
if ((work[i] == '\n') || (work[i] == '\r')) {
work[i] = ' ';
}
}
charsetLength = strlen(charsetName);
NWC24iDetectBreakPoint(&lineLength, charset, work, subjectLength, (0x38 - charsetLength) / 3);
result = NWC24EncodeWord(second, secondSize, &outputLength, charsetName, 0x40, 'Q', work, lineLength);
sourceOffset = lineLength;
if (result == NWC24_OK) {
if (lineLength == subjectLength) {
result = NWC24SetMsgSubject(msg, (char*)second, outputLength - 1);
} else {
charsetLength = strlen(charsetName);
total = outputLength;
workHalf = secondSize;
for (; sourceOffset < subjectLength; sourceOffset += lineLength) {
nextOutput = total - 1;
if (workHalf - nextOutput < 3) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
second[nextOutput] = '\r';
second[total] = '\n';
second[total + 1] = ' ';
total += 2;
NWC24iDetectBreakPoint(&lineLength, charset, work + sourceOffset, subjectLength - sourceOffset, (0x40 - charsetLength) / 3);
result =
NWC24EncodeWord(second + total, workHalf - total, &outputLength, charsetName, 0x40, 'Q', work + sourceOffset, lineLength);
if (result != NWC24_OK) {
goto done;
}
total += outputLength;
}
result = NWC24SetMsgSubject(msg, (char*)second, total - 1);
}
}
}
done:
return result;
}
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 subjectLength;
u32 lineLength;
u32 outputLength;
u32 sourceOffset;
u32 total;
u32 charsetLength;
NWC24Err result;
workHalf = workSize >> 1;
secondSize = workSize - workHalf;
second = work + 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);
}
}
}
done:
return result;
}

View File

@@ -0,0 +1,77 @@
# NWC24Mime matching attempts
Baseline: no source, 0/16 functions, 0/6124 code bytes, 0/88 data bytes.
All functions are implemented in object order, with ordinary literals.
The encoding alphabet, pointer and small marker strings match all 88 data bytes.
The initial implementation was adapted from readable C references available
in /tmp, then checked against the target object and object-exported Ghidra C.
Oversized Ghidra stack arrays were replaced with actual octet/scalar storage.
## Source-level experiments
Base64Encode | canonical three-byte loop from SDK | (96.233765, -37) | src 0x134 base 0x134 insns 77/77
Base64Encode | mask initial sextet | (80.19481, -9999) | src 0x144 base 0x134 insns 81/77
Base64Encode | pointer-relative second byte | (80.38961, -9999) | src 0x144 base 0x134 insns 81/77
NWC24InitBase64Table | unsigned scan index | (97.41304, -51) | src 0x2e0 base 0x2e0 insns 184/184
NWC24InitBase64Table | one character scan with explicit bounds | (100.0, 0) | src 0x2e0 base 0x2e0 insns 184/184
QEncode | signed copy count | (73.83251, -9999) | src 0x324 base 0x32c insns 201/203
QEncode | credit output before optional line break | (72.28571, -9999) | src 0x2fc base 0x32c insns 191/203
QEncode | initialize encoded byte before classification | (69.3202, -9999) | src 0x2f4 base 0x32c insns 189/203
QDecode | signed cursors | (74.9315, -9999) | src 0x234 base 0x248 insns 141/146
QDecode | predicate return order | (74.863014, -9999) | src 0x234 base 0x248 insns 141/146
QDecode | remaining input while loop | (74.9315, -9999) | src 0x234 base 0x248 insns 141/146
QEncode | SDK signed copy scan and octet operations | (100.0, 0) | src 0x32c base 0x32c insns 203/203
EncodeWord | unsigned intermediate string length | (69.91919, -9999) | src 0x2c0 base 0x318 insns 176/198
EncodeWord | capacity condition reversed | (69.86869, -9999) | src 0x2c0 base 0x318 insns 176/198
EncodeWord | defer overflow flag initialization | (69.91919, -9999) | src 0x2c0 base 0x318 insns 176/198
CopyWithoutLinearWhiteSpaces | switch classification | (8.835616, -55) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | signed output offset | (14.315068, -9999) | src 0x114 base 0x124 insns 69/73
CopyWithoutLinearWhiteSpaces | cache input value across capacity check | (10.068493, -9999) | src 0x110 base 0x124 insns 68/73
DecodeWord | repeat consumed initialization before capacity return | (77.37222, -161) | src 0x2d0 base 0x2d0 insns 180/180
DecodeWord | independent whitespace scan pointer | (74.02778, -9999) | src 0x2c0 base 0x2d0 insns 176/180
DecodeWord | unconditional decoded size on copy overflow | (74.094444, -9999) | src 0x2b8 base 0x2d0 insns 174/180
NWC24DecodeMIMEHeaderFieldBody | check decode status before consumed count | (74.13043, -9999) | src 0x158 base 0x170 insns 86/92
NWC24DecodeMIMEHeaderFieldBody | signed remaining input | (81.195656, -9999) | src 0x158 base 0x170 insns 86/92
NWC24DecodeMIMEHeaderFieldBody | direct input size parameter scan | (81.195656, -9999) | src 0x158 base 0x170 insns 86/92
ConcatEncodedText | signed prefix length | (93.22222, -43) | src 0xfc base 0xfc insns 63/63
ConcatEncodedText | single remaining capacity expression | (93.492065, -41) | src 0xfc base 0xfc insns 63/63
ConcatEncodedText | positive capacity branch | (90.84127, -9999) | src 0x100 base 0xfc insns 64/63
ExtractCharset | signed scan indices | (74.04762, -9999) | src 0x148 base 0x150 insns 82/84
ExtractCharset | signed extracted length | (75.34524, -9999) | src 0x144 base 0x150 insns 81/84
ExtractCharset | reuse marker length for span | (75.34524, -9999) | src 0x144 base 0x150 insns 81/84
ExtractEncodedText | signed scan index | (85.4, -9999) | src 0x208 base 0x21c insns 130/135
ExtractEncodedText | unsigned extracted length | (87.437035, -9999) | src 0x204 base 0x21c insns 129/135
ExtractEncodedText | merge invalid pointer predicates | (87.36296, -9999) | src 0x204 base 0x21c insns 129/135
CopyWithoutLinearWhiteSpaces | switch with loop continuation predicate | (98.287674, -20) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | unsigned cached capacity | (98.42466, -18) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | capacity before output counter | (98.287674, -20) | src 0x124 base 0x124 insns 73/73
Base64Encode | combine shifted initial octet | (100.0, 0) | src 0x134 base 0x134 insns 77/77
CopyWithoutLinearWhiteSpaces | declare character before flags | (98.42466, -18) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | separate default pointer advancement | (98.561646, -17) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | cache capacity minus terminator | (96.9863, -10) | src 0x124 base 0x124 insns 73/73
NWC24DecodeMIMEHeaderFieldBody | bounded signed input and output cursors | (85.92391, -9999) | src 0x16c base 0x170 insns 91/92
NWC24DecodeMIMEHeaderFieldBody | avoid initial consumed-count clear | (82.065216, -9999) | src 0x154 base 0x170 insns 85/92
NWC24DecodeMIMEHeaderFieldBody | fold status into loop condition | (83.36957, -9999) | src 0x16c base 0x170 insns 91/92
QDecode | moving input/output cursors | (81.99315, -9999) | src 0x230 base 0x248 insns 140/146
QDecode | late moving cursor initialization | (81.03425, -9999) | src 0x230 base 0x248 insns 140/146
QDecode | decode high nibble before low conversion | (86.20548, -9999) | src 0x230 base 0x248 insns 140/146
QDecode | independent pointer validation | (81.10274, -9999) | src 0x238 base 0x248 insns 142/146
NWC24DecodeMIMEHeaderFieldBody | independent encoded-pointer validation | (88.20652, -9999) | src 0x174 base 0x170 insns 93/92
NWC24DecodeMIMEHeaderFieldBody | consumed count written by decoder | (86.95652, -9999) | src 0x168 base 0x170 insns 90/92
NWC24DecodeMIMEHeaderFieldBody | defer remaining input initialization | (88.097824, -9999) | src 0x16c base 0x170 insns 91/92
## Retained result
8/16 instruction-exact and objdiff-exact functions; 2240/6124 code bytes;
88/88 data bytes. Base64 encoding uses the canonical three-byte scan and
one initial shifted-octet expression. Alphabet initialization scans actual
character bounds. Quoted-printable encoding uses a signed byte-copy scan.
All remaining functions have at least three logged distinct experiments.
Remaining differences: QDecode cursor/error control flow; EncodeWord append
status propagation; CopyWithoutLinearWhiteSpaces register allocation;
DecodeWord search/status allocation; MIME header validation and cursor
allocation; ConcatEncodedText allocation; charset/text search loop scheduling.
Combination of moving QDecode cursor initialization and sequential nibble
conversion gives 86.54794%. Combining bounded MIME-header scanning, independent
input validation and decoder-owned consumed count gives 86.03261%.
No other unit or shared header was changed.

View File

@@ -0,0 +1,100 @@
# NWC24MsgSubject matching attempts
Baseline: no source, 0/12 functions, 0/5352 code bytes, 0/232 data bytes.
All twelve functions are implemented in object order. The initial readable C
reference from /tmp was checked against target assembly and Ghidra output.
Charset strings and pointer tables retain source order; .data and .sdata
match 100%, with an identical pool.
## Source-level experiments
NWC24ReadMsgSubjectPublic | switch write overflow status | (92.11539, -10) | src 0x1a0 base 0x1a0 insns 104/104
NWC24ReadMsgSubjectPublic | signed subject whitespace index | (96.15385, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24ReadMsgSubjectPublic | late original-size read | (96.15385, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24ReadMsgTextPublic | switch read overflow status | (94.14286, -5) | src 0x118 base 0x118 insns 70/70
NWC24ReadMsgTextPublic | truncation flag after size temporaries | (96.71429, -9999) | src 0x110 base 0x118 insns 68/70
NWC24ReadMsgTextPublic | explicit error status declaration first | (97.14286, -9999) | src 0x110 base 0x118 insns 68/70
NWC24SetMsgSubjectPublic | regional encoding switch | (94.59649, -9999) | src 0x1b0 base 0x1c8 insns 108/114
NWC24SetMsgSubjectPublic | charset initialized before detection | (87.89474, -9999) | src 0x1bc base 0x1c8 insns 111/114
NWC24SetMsgSubjectPublic | private message view used directly | (95.74561, -9999) | src 0x1b8 base 0x1c8 insns 110/114
NWC24SetMsgSubjectAndTextPublic | regional subject encoding switch | (0, -9999) | [1/1] MWCC build/43U/src/libs/RevoEX/src/nwc24/NWC24MsgSubject.o
NWC24SetMsgSubjectAndTextPublic | unsigned intermediate text size | (0, -9999) | [1/1] MWCC build/43U/src/libs/RevoEX/src/nwc24/NWC24MsgSubject.o
NWC24SetMsgSubjectAndTextPublic | initialize charset before charset parser | (86.23158, -9999) | src 0x2d0 base 0x2f8 insns 180/190
NWC24ReadMsgSubjectPublic | switch only exceptional statuses | (94.03846, -9999) | src 0x198 base 0x1a0 insns 102/104
NWC24ReadMsgTextPublic | switch only exceptional statuses | (95.57143, -9999) | src 0x114 base 0x118 insns 69/70
NWC24SetMsgSubjectAndTextPublic | regional subject switch at final choice | (91.405266, -9999) | src 0x2e8 base 0x2f8 insns 186/190
NWC24SetMsgSubjectAndTextPublic | defer subject workspace pointer calculation | (86.6579, -9999) | src 0x2c4 base 0x2f8 insns 177/190
NWC24SetMsgSubjectAndTextPublic | reorder text source/work count declarations | (90.052635, -9999) | src 0x2cc base 0x2f8 insns 179/190
NWC24iDetectEncodingToSend | continue when text charset advances subject charset | (72.376144, -9999) | src 0x18c base 0x1b4 insns 99/109
NWC24iDetectEncodingToSend | signed charset region switch | (58.19266, -9999) | src 0x154 base 0x1b4 insns 85/109
NWC24iDetectEncodingToSend | result index before loop bound check | (65.39449, -9999) | src 0x164 base 0x1b4 insns 89/109
NWC24iDetectBreakPoint | signed scan counter | (77.75676, -53) | src 0x128 base 0x128 insns 74/74
NWC24iDetectBreakPoint | direct size fit return | (77.554054, -53) | src 0x128 base 0x128 insns 74/74
NWC24iDetectBreakPoint | cache leading byte for scan | (73.432434, -9999) | src 0x120 base 0x128 insns 72/74
NWC24iSetMsgSubjectPlain | signed folding scan index | (88.424, -9999) | src 0x1e8 base 0x1f4 insns 122/125
NWC24iSetMsgSubjectPlain | moving newline normalization cursor | (87.904, -9999) | src 0x1e8 base 0x1f4 insns 122/125
NWC24iSetMsgSubjectPlain | defer folded buffer pointer calculation | (81.744, -9999) | src 0x1e8 base 0x1f4 insns 122/125
NWC24iSetMsgSubjectQP | signed folding scan index | (76.75, -9999) | src 0x258 base 0x240 insns 150/144
NWC24iSetMsgSubjectQP | moving newline normalization cursor | (76.298615, -9999) | src 0x258 base 0x240 insns 150/144
NWC24iSetMsgSubjectQP | defer folded buffer pointer calculation | (71.02778, -9999) | src 0x258 base 0x240 insns 150/144
NWC24iSetMsgSubjectBase64 | signed folding scan index | (70.0, -9999) | src 0x230 base 0x220 insns 140/136
NWC24iSetMsgSubjectBase64 | moving newline normalization cursor | (70.0, -9999) | src 0x230 base 0x220 insns 140/136
NWC24iSetMsgSubjectBase64 | defer folded buffer pointer calculation | (70.0, -9999) | src 0x230 base 0x220 insns 140/136
NWC24ReadMsgSubjectPublic | success/default status switch | (96.15385, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24ReadMsgTextPublic | success/default status switch | (97.14286, -9999) | src 0x110 base 0x118 insns 68/70
NWC24SetMsgSubjectPublic | individual regional switch cases | (97.149124, -9999) | src 0x1d4 base 0x1c8 insns 117/114
NWC24iSetMsgSubjectPlain | begin folding after first line and reuse capacity slot | (90.496, -96) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | conversion status switch | (89.304, -9999) | src 0x1ec base 0x1f4 insns 123/125
NWC24iSetMsgSubjectPlain | folded capacity comparison direction | (88.384, -9999) | src 0x1e8 base 0x1f4 insns 122/125
NWC24iSetMsgSubjectQP | unsigned charset-size arithmetic | (78.13194, -9999) | src 0x250 base 0x240 insns 148/144
NWC24iSetMsgSubjectQP | separate running and per-word output counts | (86.270836, -9999) | src 0x230 base 0x240 insns 140/144
NWC24iSetMsgSubjectQP | conversion status switch | (77.513885, -9999) | src 0x25c base 0x240 insns 151/144
NWC24iSetMsgSubjectBase64 | signed line size and unsigned charset length | (71.43382, -9999) | src 0x230 base 0x220 insns 140/136
NWC24iSetMsgSubjectBase64 | separate total and encoded word sizes | (79.70588, -9999) | src 0x208 base 0x220 insns 130/136
NWC24iSetMsgSubjectBase64 | folded output capacity cached in input capacity | (70.0, -9999) | src 0x230 base 0x220 insns 140/136
NWC24iDetectBreakPoint | supported charset branch and early breakpoint returns | (98.64865, -8) | src 0x128 base 0x128 insns 74/74
NWC24ReadMsgSubjectPublic | break from overflowing switch case | (96.15385, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24ReadMsgTextPublic | break from overflowing switch case | (97.14286, -9999) | src 0x110 base 0x118 insns 68/70
NWC24iDetectBreakPoint | pointer declared before byte-encoding flag | (100.0, 0) | src 0x128 base 0x128 insns 74/74
NWC24SetMsgSubjectPublic | merge Korean and Chinese base64 cases | (100.0, 0) | src 0x1c8 base 0x1c8 insns 114/114
NWC24SetMsgSubjectAndTextPublic | merge second base64 regional cases | (93.947365, -9999) | src 0x2dc base 0x2f8 insns 183/190
NWC24SetMsgSubjectAndTextPublic | regional text encoding switch | (94.789474, -125) | src 0x2f8 base 0x2f8 insns 190/190
NWC24SetMsgSubjectAndTextPublic | test external conversion status before seven-bit flag | (90.378944, -9999) | src 0x2e8 base 0x2f8 insns 186/190
NWC24SetMsgSubjectAndTextPublic | work pointer before message view | (94.789474, -125) | src 0x2f8 base 0x2f8 insns 190/190
NWC24SetMsgSubjectAndTextPublic | detection status switch | (95.36842, -9999) | src 0x2fc base 0x2f8 insns 191/190
NWC24SetMsgSubjectAndTextPublic | work pointer declared after size variables | (94.789474, -125) | src 0x2f8 base 0x2f8 insns 190/190
## Retained corrections
Grouped Korean/Chinese base64 cases match NWC24SetMsgSubjectPublic.
Breakpoint detection uses the supported-charset branch, separate UTF-8 scan,
early returns, and unsigned masked-byte comparison; it matches exactly.
Both internal-encoding conversion functions and the default charset selector
also match exactly. Plain subject folding starts after the initial line.
Charset detection repeats while the text charset advances the subject charset.
QP/base64 folding uses distinct running and per-word lengths; the short
single-word branch uses its initialized word length. Unsigned charset-length
arithmetic reproduces the target division. Removing unused base64 temporaries
and fixing those single-word branches gives QP 88.4375% and base64 81.94853%.
The detection-status switch gives SubjectAndText 95.36842%.
NWC24_MSG_SUBJECT is defined only by this source. Its header gate supplies the
five-argument, writable-name NWC24ReadMsgTextEx declaration seen in the target
callee, which overwrites r8 on entry. Every other translation unit keeps the
existing declaration. The gate reports zero regressions.
Several temporary variants failed compilation and were discarded. Unchanged
source variants in the raw log are not counted as distinct attempts. Every
remaining function has at least three actual source edits recorded.
## Remaining functions
NWC24ReadMsgSubjectPublic 96.15385%: 104 target instructions versus 100; overflow-handling branch structure
NWC24ReadMsgTextPublic 97.14286%: 70 target instructions versus 68; overflow-handling branch structure
NWC24SetMsgSubjectAndTextPublic 95.36842%: regional dispatch, detection-status flow and register allocation
NWC24iDetectEncodingToSend 72.376144%: charset scan state and search-loop scheduling/allocation
NWC24iSetMsgSubjectPlain 90.496%: folded-line control flow and allocation
NWC24iSetMsgSubjectQP 88.4375%: word-size accounting, folding control flow and allocation
NWC24iSetMsgSubjectBase64 81.94853%: conversion/encoding status flow and folding allocation
Retained: 5/12 exact functions; 1840/5352 code bytes; 232/232 data bytes.