18431 Nov 28 10:00 stripchar3.cpp (source code)
25600 Nov 28 10:28 stripchar3.exe (ming compiled stripped and upx compressed)
13216 Nov 28 10:29 stripchar3 (g++ compiled stripped and upx compressed)
Note: regular compile with ming makes a 290K file, stripping it goes down to about 50K, and then using UPX takes it down to about 25K. Same source for programs. Not sure if source would compile with native windows compileter.
Coud make exe file available if anyone interested.
Both need to be run with path to Mail Directory.
linux for my setup ./stripchar3 /home/msetzerii/.wine/drive_c/PMAIL/MAIL/
multiuser would be
./stripchar3 /home/user/.wine/drive_c/PMAIL/MAIL/peguser/
running using wine to exe would be
wine stripchar3.exe "C:\PMAIL\MAIL\" or "C:\PMAIL\MAIL\user''' or C:/PMAIL/MAIL/ or combinations??
Under windows native it would just be stripchar3 C:\PMAIL\MAIL
It has built in to chhange to mail directory passed, and then check all CNM files, and modify any with UTF in subject.
Might need to refresh new mail folder. Would download new mail, then run. Does create a log file for records with more than accent characters. So, one could reviewer what was removed from Subject line.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#endif
#define MAX_HDR_BUF 65536
#define MAX_PATH_BUF 4096
static int hex_val(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
return -1;
}
static int b64_val(char c) {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1;
}
static size_t decode_qp(const char *src, size_t len, unsigned char *dst) {
size_t out = 0;
for (size_t i = 0; i < len; i++) {
if (src[i] == '_') {
dst[out++] = ' ';
} else if (src[i] == '=' && i + 2 < len) {
int h1 = hex_val(src[i + 1]);
int h2 = hex_val(src[i + 2]);
if (h1 >= 0 && h2 >= 0) {
dst[out++] = (unsigned char)((h1 << 4) | h2);
i += 2;
} else {
dst[out++] = (unsigned char)src[i];
}
} else {
dst[out++] = (unsigned char)src[i];
}
}
return out;
}
static size_t decode_b64(const char *src, size_t len, unsigned char *dst) {
size_t out = 0;
int val = 0, bits = -8;
for (size_t i = 0; i < len; i++) {
if (src[i] == '=') break;
int d = b64_val(src[i]);
if (d < 0) continue;
val = (val << 6) | d;
bits += 6;
if (bits >= 0) {
dst[out++] = (unsigned char)((val >> bits) & 0xFF);
bits -= 8;
}
}
return out;
}
static int starts_with_ci(const char *s, const char *prefix) {
while (*prefix) {
if (tolower((unsigned char)*s) != tolower((unsigned char)*prefix)) return 0;
s++;
prefix++;
}
return 1;
}
static int ends_with_cnm_ci(const char *name) {
size_t len = strlen(name);
if (len < 4) return 0;
const char *ext = name + len - 4;
return (ext[0] == '.' &&
tolower((unsigned char)ext[1]) == 'c' &&
tolower((unsigned char)ext[2]) == 'n' &&
tolower((unsigned char)ext[3]) == 'm');
}
static char map_latin1_c3(unsigned char b2) {
if (b2 >= 0x80 && b2 <= 0x85) return 'A';
if (b2 == 0x87) return 'C';
if (b2 >= 0x88 && b2 <= 0x8B) return 'E';
if (b2 >= 0x8C && b2 <= 0x8F) return 'I';
if (b2 == 0x91) return 'N';
if (b2 >= 0x92 && b2 <= 0x96) return 'O';
if (b2 >= 0x99 && b2 <= 0x9C) return 'U';
if (b2 == 0x9D) return 'Y';
if (b2 >= 0xA0 && b2 <= 0xA5) return 'a';
if (b2 == 0xA7) return 'c';
if (b2 >= 0xA8 && b2 <= 0xAB) return 'e';
if (b2 >= 0xAC && b2 <= 0xAF) return 'i';
if (b2 == 0xB1) return 'n';
if (b2 >= 0xB2 && b2 <= 0xB6) return 'o';
if (b2 >= 0xB9 && b2 <= 0xBC) return 'u';
if (b2 == 0xBD || b2 == 0xBF) return 'y';
return 0;
}
static void process_utf8_chunk(
const unsigned char *src, size_t slen,
char *out_clean, size_t *oi_clean, size_t clean_max,
char *out_utf8, size_t *oi_utf8, size_t utf8_max,
int *had_stripped
) {
size_t k = 0;
while (k < slen && *oi_clean + 1 < clean_max) {
unsigned char b1 = src[k];
if (b1 < 0x80) {
if (b1 >= 0x20 && b1 <= 0x7E) {
out_clean[(*oi_clean)++] = (char)b1;
if (*oi_utf8 + 1 < utf8_max) out_utf8[(*oi_utf8)++] = (char)b1;
}
k++;
} else if ((b1 & 0xE0) == 0xC0 && k + 1 < slen) {
if (*oi_utf8 + 2 < utf8_max) {
out_utf8[(*oi_utf8)++] = (char)src[k];
out_utf8[(*oi_utf8)++] = (char)src[k + 1];
}
if (b1 == 0xC3) {
char mapped = map_latin1_c3(src[k + 1]);
if (mapped) {
out_clean[(*oi_clean)++] = mapped;
} else {
*had_stripped = 1;
}
} else if (b1 == 0xC2 && src[k + 1] == 0xA0) {
out_clean[(*oi_clean)++] = ' ';
} else {
*had_stripped = 1;
}
k += 2;
} else if ((b1 & 0xF0) == 0xE0 && k + 2 < slen) {
if (*oi_utf8 + 3 < utf8_max) {
out_utf8[(*oi_utf8)++] = (char)src[k];
out_utf8[(*oi_utf8)++] = (char)src[k + 1];
out_utf8[(*oi_utf8)++] = (char)src[k + 2];
}
if (b1 == 0xE2 && src[k + 1] == 0x80) {
unsigned char b3 = src[k + 2];
if (b3 == 0x93 || b3 == 0x94) {
out_clean[(*oi_clean)++] = '-';
} else if (b3 == 0x98 || b3 == 0x99) {
out_clean[(*oi_clean)++] = '\'';
} else if (b3 == 0x9C || b3 == 0x9D) {
out_clean[(*oi_clean)++] = '"';
} else if (b3 == 0xA6 && *oi_clean + 3 < clean_max) {
out_clean[(*oi_clean)++] = '.';
out_clean[(*oi_clean)++] = '.';
out_clean[(*oi_clean)++] = '.';
} else {
*had_stripped = 1;
}
} else {
*had_stripped = 1;
}
k += 3;
} else if ((b1 & 0xF8) == 0xF0 && k + 3 < slen) {
if (*oi_utf8 + 4 < utf8_max) {
out_utf8[(*oi_utf8)++] = (char)src[k];
out_utf8[(*oi_utf8)++] = (char)src[k + 1];
out_utf8[(*oi_utf8)++] = (char)src[k + 2];
out_utf8[(*oi_utf8)++] = (char)src[k + 3];
}
*had_stripped = 1;
k += 4;
} else {
k++;
}
}
}
static int clean_subject_value(
const char *in,
char *out_clean, size_t clean_max,
char *out_utf8, size_t utf8_max
) {
size_t oi_clean = 0;
size_t oi_utf8 = 0;
const char *p = in;
int prev_was_encoded = 0;
int had_stripped = 0;
while (*p && oi_clean + 1 < clean_max) {
if (p[0] == '=' && p[1] == '?') {
const char *q1 = strchr(p + 2, '?');
if (q1 && (q1[1] == 'Q' || q1[1] == 'q' || q1[1] == 'B' || q1[1] == 'b') && q1[2] == '?') {
char enc = (char)toupper((unsigned char)q1[1]);
const char *payload = q1 + 3;
const char *end = strstr(payload, "?=");
if (end) {
if (prev_was_encoded) {
while (oi_clean > 0 && (out_clean[oi_clean - 1] == ' ' || out_clean[oi_clean - 1] == '\t')) {
oi_clean--;
}
while (oi_utf8 > 0 && (out_utf8[oi_utf8 - 1] == ' ' || out_utf8[oi_utf8 - 1] == '\t')) {
oi_utf8--;
}
}
size_t plen = (size_t)(end - payload);
unsigned char *tmp = (unsigned char *)malloc(plen + 4);
if (tmp) {
size_t dlen = (enc == 'Q')
? decode_qp(payload, plen, tmp)
: decode_b64(payload, plen, tmp);
process_utf8_chunk(
tmp, dlen,
out_clean, &oi_clean, clean_max,
out_utf8, &oi_utf8, utf8_max,
&had_stripped
);
free(tmp);
}
p = end + 2;
prev_was_encoded = 1;
continue;
}
}
}
unsigned char ch = (unsigned char)*p;
if (ch != ' ' && ch != '\t') {
prev_was_encoded = 0;
}
if (ch < 0x80) {
if (ch >= 0x20 && ch <= 0x7E) {
out_clean[oi_clean++] = (char)ch;
if (oi_utf8 + 1 < utf8_max) out_utf8[oi_utf8++] = (char)ch;
}
p++;
} else {
size_t avail = strlen(p);
size_t seq_len = 1;
if ((ch & 0xE0) == 0xC0 && avail >= 2) seq_len = 2;
else if ((ch & 0xF0) == 0xE0 && avail >= 3) seq_len = 3;
else if ((ch & 0xF8) == 0xF0 && avail >= 4) seq_len = 4;
process_utf8_chunk(
(const unsigned char *)p, seq_len,
out_clean, &oi_clean, clean_max,
out_utf8, &oi_utf8, utf8_max,
&had_stripped
);
p += seq_len;
}
}
while (oi_clean > 0 && (out_clean[oi_clean - 1] == ' ' || out_clean[oi_clean - 1] == '\t')) {
oi_clean--;
}
out_clean[oi_clean] = '\0';
while (oi_utf8 > 0 && (out_utf8[oi_utf8 - 1] == ' ' || out_utf8[oi_utf8 - 1] == '\t')) {
oi_utf8--;
}
out_utf8[oi_utf8] = '\0';
return had_stripped;
}
static void log_stripped_subject(const char *msg_path, const char *custom_log, const char *utf8_subj, const char *clean_subj) {
char log_path[MAX_PATH_BUF];
if (custom_log && custom_log[0] != '\0') {
snprintf(log_path, sizeof(log_path), "%s", custom_log);
} else {
snprintf(log_path, sizeof(log_path), "%s", msg_path);
char *slash1 = strrchr(log_path, '/');
char *slash2 = strrchr(log_path, '\\');
char *last_slash = (slash1 > slash2) ? slash1 : slash2;
if (last_slash) {
*(last_slash + 1) = '\0';
strncat(log_path, "stripped_utf8.log", sizeof(log_path) - strlen(log_path) - 1);
} else {
snprintf(log_path, sizeof(log_path), "stripped_utf8.log");
}
}
FILE *flog = fopen(log_path, "ab");
if (!flog) return;
time_t now = time(NULL);
struct tm *tm_info = localtime(&now);
char ts[32] = "0000-00-00 00:00:00";
if (tm_info) {
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", tm_info);
}
const char *fname = msg_path;
const char *s1 = strrchr(msg_path, '/');
const char *s2 = strrchr(msg_path, '\\');
if (s1 || s2) {
fname = ((s1 > s2) ? s1 : s2) + 1;
}
fprintf(flog, "[%s] %s\n UTF-8 : %s\n Clean : %s\n\n", ts, fname, utf8_subj, clean_subj);
fclose(flog);
}
static int file_needs_cleaning(const char *path) {
FILE *f = fopen(path, "rb");
if (!f) return 0;
char line[8192];
int in_subject = 0;
int needs_work = 0;
while (fgets(line, sizeof(line), f)) {
size_t len = strlen(line);
while (len > 0 && (line[len - 1] == '\r' || line[len - 1] == '\n')) {
line[--len] = '\0';
}
if (len == 0) break;
if (in_subject) {
if (line[0] != ' ' && line[0] != '\t') {
break;
}
} else if (starts_with_ci(line, "Subject:")) {
in_subject = 1;
}
if (in_subject) {
if (strstr(line, "=?") != NULL) {
needs_work = 1;
break;
}
for (size_t i = 0; i < len; i++) {
if ((unsigned char)line[i] >= 0x80) {
needs_work = 1;
break;
}
}
if (needs_work) break;
}
}
fclose(f);
return needs_work;
}
static int process_cnm_file(const char *path, const char *custom_log) {
if (!file_needs_cleaning(path)) {
return 0;
}
FILE *fin = fopen(path, "rb");
if (!fin) return -1;
char tmp_path[MAX_PATH_BUF];
snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
FILE *fout = fopen(tmp_path, "wb");
if (!fout) {
fclose(fin);
return -1;
}
char line[8192];
char subj_raw[MAX_HDR_BUF];
char subj_clean[MAX_HDR_BUF];
char subj_utf8[MAX_HDR_BUF];
int in_subject = 0;
int headers_done = 0;
int detected_crlf = 1;
while (fgets(line, sizeof(line), fin)) {
if (!headers_done) {
size_t len = strlen(line);
int has_crlf = (len >= 2 && line[len - 2] == '\r' && line[len - 1] == '\n');
if (len >= 1) detected_crlf = has_crlf;
while (len > 0 && (line[len - 1] == '\r' || line[len - 1] == '\n')) {
line[--len] = '\0';
}
if (in_subject) {
if (len > 0 && (line[0] == ' ' || line[0] == '\t')) {
const char *cont = line;
while (*cont == ' ' || *cont == '\t') cont++;
size_t cur = strlen(subj_raw);
if (cur + strlen(cont) + 2 < sizeof(subj_raw)) {
if (cur > 0) strcat(subj_raw, " ");
strcat(subj_raw, cont);
}
continue;
} else {
int had_stripped = clean_subject_value(
subj_raw,
subj_clean, sizeof(subj_clean),
subj_utf8, sizeof(subj_utf8)
);
if (had_stripped) {
log_stripped_subject(path, custom_log, subj_utf8, subj_clean);
}
fprintf(fout, "Subject: %s%s", subj_clean, detected_crlf ? "\r\n" : "\n");
in_subject = 0;
}
}
if (len == 0) {
headers_done = 1;
fprintf(fout, "%s", detected_crlf ? "\r\n" : "\n");
continue;
}
if (starts_with_ci(line, "Subject:")) {
const char *val = line + 8;
while (*val == ' ' || *val == '\t') val++;
snprintf(subj_raw, sizeof(subj_raw), "%s", val);
in_subject = 1;
continue;
}
fprintf(fout, "%s%s", line, has_crlf ? "\r\n" : "\n");
} else {
fwrite(line, 1, strlen(line), fout);
}
}
if (in_subject) {
int had_stripped = clean_subject_value(
subj_raw,
subj_clean, sizeof(subj_clean),
subj_utf8, sizeof(subj_utf8)
);
if (had_stripped) {
log_stripped_subject(path, custom_log, subj_utf8, subj_clean);
}
fprintf(fout, "Subject: %s%s", subj_clean, detected_crlf ? "\r\n" : "\n");
}
fclose(fin);
fclose(fout);
remove(path);
if (rename(tmp_path, path) != 0) {
return -1;
}
return 1;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <mail_dir_or_file.cnm> [logfile]\n", argv[0]);
return 1;
}
char target[MAX_PATH_BUF];
snprintf(target, sizeof(target), "%s", argv[1]);
size_t tlen = strlen(target);
while (tlen > 1 && (target[tlen - 1] == '/' || target[tlen - 1] == '\\')) {
target[--tlen] = '\0';
}
const char *custom_log = (argc >= 3) ? argv[2] : NULL;
#ifdef _WIN32
DWORD attr = GetFileAttributesA(target);
if (attr == INVALID_FILE_ATTRIBUTES) {
fprintf(stderr, "Error: Path not found: %s\n", target);
return 1;
}
if (attr & FILE_ATTRIBUTE_DIRECTORY) {
char search_pattern[MAX_PATH_BUF];
snprintf(search_pattern, sizeof(search_pattern), "%s\\*", target);
WIN32_FIND_DATAA ffd;
HANDLE hFind = FindFirstFileA(search_pattern, &ffd);
if (hFind == INVALID_HANDLE_VALUE) return 1;
int cleaned_count = 0;
do {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
if (ends_with_cnm_ci(ffd.cFileName)) {
char full_path[MAX_PATH_BUF];
snprintf(full_path, sizeof(full_path), "%s\\%s", target, ffd.cFileName);
if (process_cnm_file(full_path, custom_log) == 1) {
cleaned_count++;
}
}
} while (FindNextFileA(hFind, &ffd) != 0);
FindClose(hFind);
printf("Processed directory: %d file(s) updated.\n", cleaned_count);
} else {
process_cnm_file(target, custom_log);
}
#else
struct stat st;
if (stat(target, &st) != 0) {
fprintf(stderr, "Error: Path not found: %s\n", target);
return 1;
}
if (S_ISDIR(st.st_mode)) {
DIR *dir = opendir(target);
if (!dir) {
fprintf(stderr, "Error: Cannot open directory: %s\n", target);
return 1;
}
struct dirent *ent;
int cleaned_count = 0;
while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
if (ends_with_cnm_ci(ent->d_name)) {
char full_path[MAX_PATH_BUF];
snprintf(full_path, sizeof(full_path), "%s/%s", target, ent->d_name);
struct stat fst;
if (stat(full_path, &fst) == 0 && S_ISREG(fst.st_mode)) {
if (process_cnm_file(full_path, custom_log) == 1) {
cleaned_count++;
}
}
}
}
closedir(dir);
printf("Processed directory: %d file(s) updated.\n", cleaned_count);
} else if (S_ISREG(st.st_mode)) {
process_cnm_file(target, custom_log);
}
#endif
return 0;
}
Did try to add as externsion ot toolbar button, but nothing we (google ai) worked.
Interesting. Didn't really look at code much, had done similar in past with other things.
18431 Nov 28 10:00 stripchar3.cpp (source code)
25600 Nov 28 10:28 stripchar3.exe (ming compiled stripped and upx compressed)
13216 Nov 28 10:29 stripchar3 (g++ compiled stripped and upx compressed)
Note: regular compile with ming makes a 290K file, stripping it goes down to about 50K, and then using UPX takes it down to about 25K. Same source for programs. Not sure if source would compile with native windows compileter.
Coud make exe file available if anyone interested.
Both need to be run with path to Mail Directory.
linux for my setup ./stripchar3 /home/msetzerii/.wine/drive_c/PMAIL/MAIL/
multiuser would be
./stripchar3 /home/user/.wine/drive_c/PMAIL/MAIL/peguser/
running using wine to exe would be
wine stripchar3.exe "C:\\PMAIL\\MAIL\\" or "C:\\PMAIL\\MAIL\\user''' or C:/PMAIL/MAIL/ or combinations??
Under windows native it would just be stripchar3 C:\PMAIL\MAIL
It has built in to chhange to mail directory passed, and then check all CNM files, and modify any with UTF in subject.
Might need to refresh new mail folder. Would download new mail, then run. Does create a log file for records with more than accent characters. So, one could reviewer what was removed from Subject line.
````
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#endif
#define MAX_HDR_BUF 65536
#define MAX_PATH_BUF 4096
static int hex_val(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
return -1;
}
static int b64_val(char c) {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1;
}
static size_t decode_qp(const char *src, size_t len, unsigned char *dst) {
size_t out = 0;
for (size_t i = 0; i < len; i++) {
if (src[i] == '_') {
dst[out++] = ' ';
} else if (src[i] == '=' && i + 2 < len) {
int h1 = hex_val(src[i + 1]);
int h2 = hex_val(src[i + 2]);
if (h1 >= 0 && h2 >= 0) {
dst[out++] = (unsigned char)((h1 << 4) | h2);
i += 2;
} else {
dst[out++] = (unsigned char)src[i];
}
} else {
dst[out++] = (unsigned char)src[i];
}
}
return out;
}
static size_t decode_b64(const char *src, size_t len, unsigned char *dst) {
size_t out = 0;
int val = 0, bits = -8;
for (size_t i = 0; i < len; i++) {
if (src[i] == '=') break;
int d = b64_val(src[i]);
if (d < 0) continue;
val = (val << 6) | d;
bits += 6;
if (bits >= 0) {
dst[out++] = (unsigned char)((val >> bits) & 0xFF);
bits -= 8;
}
}
return out;
}
static int starts_with_ci(const char *s, const char *prefix) {
while (*prefix) {
if (tolower((unsigned char)*s) != tolower((unsigned char)*prefix)) return 0;
s++;
prefix++;
}
return 1;
}
static int ends_with_cnm_ci(const char *name) {
size_t len = strlen(name);
if (len < 4) return 0;
const char *ext = name + len - 4;
return (ext[0] == '.' &&
tolower((unsigned char)ext[1]) == 'c' &&
tolower((unsigned char)ext[2]) == 'n' &&
tolower((unsigned char)ext[3]) == 'm');
}
/* Map UTF-8 0xC3 xx Latin-1 Supplement accented letters to base ASCII */
static char map_latin1_c3(unsigned char b2) {
if (b2 >= 0x80 && b2 <= 0x85) return 'A';
if (b2 == 0x87) return 'C';
if (b2 >= 0x88 && b2 <= 0x8B) return 'E';
if (b2 >= 0x8C && b2 <= 0x8F) return 'I';
if (b2 == 0x91) return 'N';
if (b2 >= 0x92 && b2 <= 0x96) return 'O';
if (b2 >= 0x99 && b2 <= 0x9C) return 'U';
if (b2 == 0x9D) return 'Y';
if (b2 >= 0xA0 && b2 <= 0xA5) return 'a';
if (b2 == 0xA7) return 'c';
if (b2 >= 0xA8 && b2 <= 0xAB) return 'e';
if (b2 >= 0xAC && b2 <= 0xAF) return 'i';
if (b2 == 0xB1) return 'n';
if (b2 >= 0xB2 && b2 <= 0xB6) return 'o';
if (b2 >= 0xB9 && b2 <= 0xBC) return 'u';
if (b2 == 0xBD || b2 == 0xBF) return 'y';
return 0;
}
/* Append decoded UTF-8 bytes into clean ASCII out AND raw decoded UTF-8 out.
Sets *had_stripped = 1 if any character was dropped instead of transliterated. */
static void process_utf8_chunk(
const unsigned char *src, size_t slen,
char *out_clean, size_t *oi_clean, size_t clean_max,
char *out_utf8, size_t *oi_utf8, size_t utf8_max,
int *had_stripped
) {
size_t k = 0;
while (k < slen && *oi_clean + 1 < clean_max) {
unsigned char b1 = src[k];
if (b1 < 0x80) {
if (b1 >= 0x20 && b1 <= 0x7E) {
out_clean[(*oi_clean)++] = (char)b1;
if (*oi_utf8 + 1 < utf8_max) out_utf8[(*oi_utf8)++] = (char)b1;
}
k++;
} else if ((b1 & 0xE0) == 0xC0 && k + 1 < slen) {
/* Copy original 2-byte UTF-8 to decoded log buffer */
if (*oi_utf8 + 2 < utf8_max) {
out_utf8[(*oi_utf8)++] = (char)src[k];
out_utf8[(*oi_utf8)++] = (char)src[k + 1];
}
if (b1 == 0xC3) {
char mapped = map_latin1_c3(src[k + 1]);
if (mapped) {
out_clean[(*oi_clean)++] = mapped;
} else {
*had_stripped = 1;
}
} else if (b1 == 0xC2 && src[k + 1] == 0xA0) {
/* Non-breaking space -> regular space */
out_clean[(*oi_clean)++] = ' ';
} else {
*had_stripped = 1;
}
k += 2;
} else if ((b1 & 0xF0) == 0xE0 && k + 2 < slen) {
/* Copy original 3-byte UTF-8 to decoded log buffer */
if (*oi_utf8 + 3 < utf8_max) {
out_utf8[(*oi_utf8)++] = (char)src[k];
out_utf8[(*oi_utf8)++] = (char)src[k + 1];
out_utf8[(*oi_utf8)++] = (char)src[k + 2];
}
/* Common 3-byte smart punctuation (0xE2 0x80 xx) */
if (b1 == 0xE2 && src[k + 1] == 0x80) {
unsigned char b3 = src[k + 2];
if (b3 == 0x93 || b3 == 0x94) {
out_clean[(*oi_clean)++] = '-';
} else if (b3 == 0x98 || b3 == 0x99) {
out_clean[(*oi_clean)++] = '\'';
} else if (b3 == 0x9C || b3 == 0x9D) {
out_clean[(*oi_clean)++] = '"';
} else if (b3 == 0xA6 && *oi_clean + 3 < clean_max) {
out_clean[(*oi_clean)++] = '.';
out_clean[(*oi_clean)++] = '.';
out_clean[(*oi_clean)++] = '.';
} else {
*had_stripped = 1;
}
} else {
*had_stripped = 1;
}
k += 3;
} else if ((b1 & 0xF8) == 0xF0 && k + 3 < slen) {
/* Copy original 4-byte UTF-8 (emojis) to decoded log buffer */
if (*oi_utf8 + 4 < utf8_max) {
out_utf8[(*oi_utf8)++] = (char)src[k];
out_utf8[(*oi_utf8)++] = (char)src[k + 1];
out_utf8[(*oi_utf8)++] = (char)src[k + 2];
out_utf8[(*oi_utf8)++] = (char)src[k + 3];
}
*had_stripped = 1;
k += 4;
} else {
/* Stray/invalid byte -> skip */
k++;
}
}
}
static int clean_subject_value(
const char *in,
char *out_clean, size_t clean_max,
char *out_utf8, size_t utf8_max
) {
size_t oi_clean = 0;
size_t oi_utf8 = 0;
const char *p = in;
int prev_was_encoded = 0;
int had_stripped = 0;
while (*p && oi_clean + 1 < clean_max) {
if (p[0] == '=' && p[1] == '?') {
const char *q1 = strchr(p + 2, '?');
if (q1 && (q1[1] == 'Q' || q1[1] == 'q' || q1[1] == 'B' || q1[1] == 'b') && q1[2] == '?') {
char enc = (char)toupper((unsigned char)q1[1]);
const char *payload = q1 + 3;
const char *end = strstr(payload, "?=");
if (end) {
/* Strip inter-word whitespace between adjacent encoded words */
if (prev_was_encoded) {
while (oi_clean > 0 && (out_clean[oi_clean - 1] == ' ' || out_clean[oi_clean - 1] == '\t')) {
oi_clean--;
}
while (oi_utf8 > 0 && (out_utf8[oi_utf8 - 1] == ' ' || out_utf8[oi_utf8 - 1] == '\t')) {
oi_utf8--;
}
}
size_t plen = (size_t)(end - payload);
unsigned char *tmp = (unsigned char *)malloc(plen + 4);
if (tmp) {
size_t dlen = (enc == 'Q')
? decode_qp(payload, plen, tmp)
: decode_b64(payload, plen, tmp);
process_utf8_chunk(
tmp, dlen,
out_clean, &oi_clean, clean_max,
out_utf8, &oi_utf8, utf8_max,
&had_stripped
);
free(tmp);
}
p = end + 2;
prev_was_encoded = 1;
continue;
}
}
}
unsigned char ch = (unsigned char)*p;
if (ch != ' ' && ch != '\t') {
prev_was_encoded = 0;
}
if (ch < 0x80) {
if (ch >= 0x20 && ch <= 0x7E) {
out_clean[oi_clean++] = (char)ch;
if (oi_utf8 + 1 < utf8_max) out_utf8[oi_utf8++] = (char)ch;
}
p++;
} else {
/* Handle unencoded 8-bit UTF-8 sequences directly in the Subject header */
size_t avail = strlen(p);
size_t seq_len = 1;
if ((ch & 0xE0) == 0xC0 && avail >= 2) seq_len = 2;
else if ((ch & 0xF0) == 0xE0 && avail >= 3) seq_len = 3;
else if ((ch & 0xF8) == 0xF0 && avail >= 4) seq_len = 4;
process_utf8_chunk(
(const unsigned char *)p, seq_len,
out_clean, &oi_clean, clean_max,
out_utf8, &oi_utf8, utf8_max,
&had_stripped
);
p += seq_len;
}
}
while (oi_clean > 0 && (out_clean[oi_clean - 1] == ' ' || out_clean[oi_clean - 1] == '\t')) {
oi_clean--;
}
out_clean[oi_clean] = '\0';
while (oi_utf8 > 0 && (out_utf8[oi_utf8 - 1] == ' ' || out_utf8[oi_utf8 - 1] == '\t')) {
oi_utf8--;
}
out_utf8[oi_utf8] = '\0';
return had_stripped;
}
static void log_stripped_subject(const char *msg_path, const char *custom_log, const char *utf8_subj, const char *clean_subj) {
char log_path[MAX_PATH_BUF];
if (custom_log && custom_log[0] != '\0') {
snprintf(log_path, sizeof(log_path), "%s", custom_log);
} else {
/* Place stripped_utf8.log in the same directory as the .CNM file */
snprintf(log_path, sizeof(log_path), "%s", msg_path);
char *slash1 = strrchr(log_path, '/');
char *slash2 = strrchr(log_path, '\\');
char *last_slash = (slash1 > slash2) ? slash1 : slash2;
if (last_slash) {
*(last_slash + 1) = '\0';
strncat(log_path, "stripped_utf8.log", sizeof(log_path) - strlen(log_path) - 1);
} else {
snprintf(log_path, sizeof(log_path), "stripped_utf8.log");
}
}
FILE *flog = fopen(log_path, "ab");
if (!flog) return;
time_t now = time(NULL);
struct tm *tm_info = localtime(&now);
char ts[32] = "0000-00-00 00:00:00";
if (tm_info) {
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", tm_info);
}
/* Extract just the filename for cleaner log entries */
const char *fname = msg_path;
const char *s1 = strrchr(msg_path, '/');
const char *s2 = strrchr(msg_path, '\\');
if (s1 || s2) {
fname = ((s1 > s2) ? s1 : s2) + 1;
}
fprintf(flog, "[%s] %s\n UTF-8 : %s\n Clean : %s\n\n", ts, fname, utf8_subj, clean_subj);
fclose(flog);
}
/* Quick scan of headers to skip files that do not need updating */
static int file_needs_cleaning(const char *path) {
FILE *f = fopen(path, "rb");
if (!f) return 0;
char line[8192];
int in_subject = 0;
int needs_work = 0;
while (fgets(line, sizeof(line), f)) {
size_t len = strlen(line);
while (len > 0 && (line[len - 1] == '\r' || line[len - 1] == '\n')) {
line[--len] = '\0';
}
if (len == 0) break;
if (in_subject) {
if (line[0] != ' ' && line[0] != '\t') {
break;
}
} else if (starts_with_ci(line, "Subject:")) {
in_subject = 1;
}
if (in_subject) {
if (strstr(line, "=?") != NULL) {
needs_work = 1;
break;
}
for (size_t i = 0; i < len; i++) {
if ((unsigned char)line[i] >= 0x80) {
needs_work = 1;
break;
}
}
if (needs_work) break;
}
}
fclose(f);
return needs_work;
}
static int process_cnm_file(const char *path, const char *custom_log) {
if (!file_needs_cleaning(path)) {
return 0;
}
FILE *fin = fopen(path, "rb");
if (!fin) return -1;
char tmp_path[MAX_PATH_BUF];
snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
FILE *fout = fopen(tmp_path, "wb");
if (!fout) {
fclose(fin);
return -1;
}
char line[8192];
char subj_raw[MAX_HDR_BUF];
char subj_clean[MAX_HDR_BUF];
char subj_utf8[MAX_HDR_BUF];
int in_subject = 0;
int headers_done = 0;
int detected_crlf = 1;
while (fgets(line, sizeof(line), fin)) {
if (!headers_done) {
size_t len = strlen(line);
int has_crlf = (len >= 2 && line[len - 2] == '\r' && line[len - 1] == '\n');
if (len >= 1) detected_crlf = has_crlf;
while (len > 0 && (line[len - 1] == '\r' || line[len - 1] == '\n')) {
line[--len] = '\0';
}
if (in_subject) {
if (len > 0 && (line[0] == ' ' || line[0] == '\t')) {
const char *cont = line;
while (*cont == ' ' || *cont == '\t') cont++;
size_t cur = strlen(subj_raw);
if (cur + strlen(cont) + 2 < sizeof(subj_raw)) {
if (cur > 0) strcat(subj_raw, " ");
strcat(subj_raw, cont);
}
continue;
} else {
int had_stripped = clean_subject_value(
subj_raw,
subj_clean, sizeof(subj_clean),
subj_utf8, sizeof(subj_utf8)
);
if (had_stripped) {
log_stripped_subject(path, custom_log, subj_utf8, subj_clean);
}
fprintf(fout, "Subject: %s%s", subj_clean, detected_crlf ? "\r\n" : "\n");
in_subject = 0;
}
}
if (len == 0) {
headers_done = 1;
fprintf(fout, "%s", detected_crlf ? "\r\n" : "\n");
continue;
}
if (starts_with_ci(line, "Subject:")) {
const char *val = line + 8;
while (*val == ' ' || *val == '\t') val++;
snprintf(subj_raw, sizeof(subj_raw), "%s", val);
in_subject = 1;
continue;
}
fprintf(fout, "%s%s", line, has_crlf ? "\r\n" : "\n");
} else {
fwrite(line, 1, strlen(line), fout);
}
}
if (in_subject) {
int had_stripped = clean_subject_value(
subj_raw,
subj_clean, sizeof(subj_clean),
subj_utf8, sizeof(subj_utf8)
);
if (had_stripped) {
log_stripped_subject(path, custom_log, subj_utf8, subj_clean);
}
fprintf(fout, "Subject: %s%s", subj_clean, detected_crlf ? "\r\n" : "\n");
}
fclose(fin);
fclose(fout);
remove(path);
if (rename(tmp_path, path) != 0) {
return -1;
}
return 1;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <mail_dir_or_file.cnm> [logfile]\n", argv[0]);
return 1;
}
char target[MAX_PATH_BUF];
snprintf(target, sizeof(target), "%s", argv[1]);
size_t tlen = strlen(target);
while (tlen > 1 && (target[tlen - 1] == '/' || target[tlen - 1] == '\\')) {
target[--tlen] = '\0';
}
const char *custom_log = (argc >= 3) ? argv[2] : NULL;
#ifdef _WIN32
DWORD attr = GetFileAttributesA(target);
if (attr == INVALID_FILE_ATTRIBUTES) {
fprintf(stderr, "Error: Path not found: %s\n", target);
return 1;
}
if (attr & FILE_ATTRIBUTE_DIRECTORY) {
char search_pattern[MAX_PATH_BUF];
snprintf(search_pattern, sizeof(search_pattern), "%s\\*", target);
WIN32_FIND_DATAA ffd;
HANDLE hFind = FindFirstFileA(search_pattern, &ffd);
if (hFind == INVALID_HANDLE_VALUE) return 1;
int cleaned_count = 0;
do {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
if (ends_with_cnm_ci(ffd.cFileName)) {
char full_path[MAX_PATH_BUF];
snprintf(full_path, sizeof(full_path), "%s\\%s", target, ffd.cFileName);
if (process_cnm_file(full_path, custom_log) == 1) {
cleaned_count++;
}
}
} while (FindNextFileA(hFind, &ffd) != 0);
FindClose(hFind);
printf("Processed directory: %d file(s) updated.\n", cleaned_count);
} else {
process_cnm_file(target, custom_log);
}
#else
struct stat st;
if (stat(target, &st) != 0) {
fprintf(stderr, "Error: Path not found: %s\n", target);
return 1;
}
if (S_ISDIR(st.st_mode)) {
DIR *dir = opendir(target);
if (!dir) {
fprintf(stderr, "Error: Cannot open directory: %s\n", target);
return 1;
}
struct dirent *ent;
int cleaned_count = 0;
while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
if (ends_with_cnm_ci(ent->d_name)) {
char full_path[MAX_PATH_BUF];
snprintf(full_path, sizeof(full_path), "%s/%s", target, ent->d_name);
struct stat fst;
if (stat(full_path, &fst) == 0 && S_ISREG(fst.st_mode)) {
if (process_cnm_file(full_path, custom_log) == 1) {
cleaned_count++;
}
}
}
}
closedir(dir);
printf("Processed directory: %d file(s) updated.\n", cleaned_count);
} else if (S_ISREG(st.st_mode)) {
process_cnm_file(target, custom_log);
}
#endif
return 0;
}
````
Did try to add as externsion ot toolbar button, but nothing we (google ai) worked.
Interesting. Didn't really look at code much, had done similar in past with other things.