Community Discussions and Support
Long UTF-8 clobbered in message properties

When putting any utf-8 strings into the two text fields which are longer than 28 (from), resp. 34 (subject) octets, they are being truncated to make them fit into their fixed length fields in the .PMI index.
Truncation happens without regard to codepoints.
If the cut happens to mangle a multibyte character, first the complete string is shown in the proper column, but after closing and re-opening the folder, the content is gibberish.


Try changing a field to "Γιώργος Α. Παπανδρέου" (Giorgos A. Papandreou).


Truncation of utf-8 strings is not so easy, it requires a look-ahead for octets which start a multi-byte sequence. But a suitable algorithm should be easy to find for every programming language.


I don't know if maybe this faulty cut off happens in other places besides the message property form as well.


(BTW, I would really like to know, if the fixed length strings in the .PMI must be zero-terminated C-Strings.)


Steffen


Edit:
I just now realized again that the messages with long utf-8 sender names are showing a sequence of question marks '??????????????' in the title bar.
So it seems to happen as well, when the message is opened and the sender's name is taken from the original mail header.


When putting any utf-8 strings into the two text fields which are longer than 28 (from), resp. 34 (subject) octets, they are being truncated to make them fit into their fixed length fields in the .PMI index. Truncation happens without regard to codepoints. If the cut happens to mangle a multibyte character, first the complete string is shown in the proper column, but after closing and re-opening the folder, the content is gibberish. Try changing a field to "Γιώργος Α. Παπανδρέου" (Giorgos A. Papandreou). Truncation of utf-8 strings is not so easy, it requires a look-ahead for octets which start a multi-byte sequence. But a suitable algorithm should be easy to find for every programming language. I don't know if maybe this faulty cut off happens in other places besides the message property form as well. (BTW, I would really like to know, if the fixed length strings in the .PMI must be zero-terminated C-Strings.) Steffen Edit: I just now realized again that the messages with long utf-8 sender names are showing a sequence of question marks '??????????????' in the title bar. So it seems to happen as well, when the message is opened and the sender's name is taken from the original mail header.
edited 4 days ago at 5:19 pm

In September 2025 there was a post that I think might be related. It specifically asked about subject content appearing blank in a message list. Han van den Bogaerde posted this response:


David Harris wrote about this problem:


===
This is a problem with the format of the index file used in the old v2 folder
format. If a UTF-8 string is a particular length, it may get truncated in the
middle of a multibyte character, which renders the entire string unusable.
When you load and display the message in the reader or preview screen, it
actually loads and parses the header from the message without using the
index precis form, so it gets it right.
===


There were no additional posts in the topic.


In September 2025 there was a post that I think might be related. It specifically asked about subject content appearing blank in a message list. Han van den Bogaerde posted this response: _ David Harris wrote about this problem: _ _=== This is a problem with the format of the index file used in the old v2 folder format. If a UTF-8 string is a particular length, it may get truncated in the middle of a multibyte character, which renders the entire string unusable. When you load and display the message in the reader or preview screen, it actually loads and parses the header from the message without using the index precis form, so it gets it right. === _ There were no additional posts in the topic.

Well, in pmail v. 4.91 international, the problem still seems to be present in both places, in the message reader as well as in the folder content lists.
To cut utf-8 strings to a desired byte length, Ikegami proposed this solution in a Perl forum:



Now, to split between codepoints. The UTF-8 encoding of every code point will start with a byte matching 0b0xxxxxxx or 0b11xxxxxx, and you will never find those bytes in the middle of a code point. That means you want to truncate before
[\x00-\x7F\xC0-\xFF]



https://stackoverflow.com/questions/10953069/perl-trim-utf8-bytes-to-length-and-sanitize-the-data


Ignoring graphemes, it boils down to just



my $bytes = encode_utf8($str);
$bytes =~ s/^.{0, $max_bytes}(?![^\x00-\x7F\xC0-\xFF])\K.*//s;



After the string was encoded into their utf-8 representing octets, everything after 0 to max_bytes which is not immediately followed by something that is not a start byte, gets cut off.
(Because the length range {0, $max_bytes} is acting greedy, the result will be the longest possible byte sequence, that preserves multibyte characters.)


Regards, Steffen


Well, in pmail v. 4.91 international, the problem still seems to be present in both places, in the message reader as well as in the folder content lists. To cut utf-8 strings to a desired byte length, Ikegami proposed this solution in a Perl forum: > Now, to split between codepoints. The UTF-8 encoding of every code point will start with a byte matching 0b0xxxxxxx or 0b11xxxxxx, and you will never find those bytes in the middle of a code point. That means you want to truncate before [\x00-\x7F\xC0-\xFF] https://stackoverflow.com/questions/10953069/perl-trim-utf8-bytes-to-length-and-sanitize-the-data Ignoring graphemes, it boils down to just > my $bytes = encode_utf8($str); $bytes =~ s/^.{0, $max_bytes}(?![^\x00-\x7F\xC0-\xFF])\K.*//s; After the string was encoded into their utf-8 representing octets, everything after 0 to max_bytes which is not immediately followed by something that is not a start byte, gets cut off. (Because the length range {0, $max_bytes} is acting greedy, the result will be the longest possible byte sequence, that preserves multibyte characters.) Regards, Steffen

Just as some info. utf-8 codes are 4 6 8 lenghts.


4 byte utf8 - finding these codes could 4 bytes
c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df
6 byte utf8 - similar with 6 byte
e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef
8 byte utf8 - similar with 8 byte.
f0 f1 f2 f3 f4


if find c1 - df delete 4 bytes
if find e0 - ef delete 6 bytes
if find f0 - ff delete 8 bytes


But that is just a quick thought..


Just as some info. utf-8 codes are 4 6 8 lenghts. 4 byte utf8 - finding these codes could 4 bytes c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df 6 byte utf8 - similar with 6 byte e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef 8 byte utf8 - similar with 8 byte. f0 f1 f2 f3 f4 if find c1 - df delete 4 bytes if find e0 - ef delete 6 bytes if find f0 - ff delete 8 bytes But that is just a quick thought..

mikes@guam.net

I just now realized again that the messages with long utf-8 sender names are showing a sequence of question marks '??????????????' in the title bar.


I just received a message from disney+ for which the question marks appeared in the title bar, and the subject line was blank in the folder window list. The Subject: contains UTF-8 but is not a long one:


Subject: Best watched with leftovers =?UTF-8?B?8J+Nve+4jw==?=


I wanted to play with it so I decided to copy the entire raw view content, paste it into Notepad, and save it as a .cnm. The saved .cnm does not exhibit the same question mark and blank subject as the original does yet both raw views appear to be identical when comparing them visually.


Does anyone have a thought about why this might be?


6b08413b50510


[quote="pid:59415, uid:89708"]I just now realized again that the messages with long utf-8 sender names are showing a sequence of question marks '??????????????' in the title bar.[/quote] I just received a message from disney+ for which the question marks appeared in the title bar, and the subject line was blank in the folder window list. The Subject: contains UTF-8 but is not a long one: Subject: Best watched with leftovers =?UTF-8?B?8J+Nve+4jw==?= I wanted to play with it so I decided to copy the entire raw view content, paste it into Notepad, and save it as a .cnm. The saved .cnm does not exhibit the same question mark and blank subject as the original does yet both raw views appear to be identical when comparing them visually. Does anyone have a thought about why this might be? ![6b08413b50510](serve/attachment&path=6b08413b50510)

This is what I get:


6b085655c8ed7


This is what I get: ![6b085655c8ed7](serve/attachment&path=6b085655c8ed7)
			Michael
--
IERenderer's Homepage
PGP Key ID (RSA 2048): 0xC45D831B
S/MIME Fingerprint: 94C6B471 0C623088 A5B27701 742B8666 3B7E657C

Here's another one:


6b085d256e60a


The difference between my screenshots is caused by the following Windows option:
Control Panel → Region → Administrative → Change system locale → Beta: Use Unicode UTF-8 for worldwide language support


The latter is with this option disabled and the previous one with it enabled. It's available on both Win10 & Win11.


(I only found out about this option since someone complained about IERenderer failing to render several HTML messages with the Windows option enabled - which I can't duplicate with my current development version of Pegasus Mail).


Here's another one: ![6b085d256e60a](serve/attachment&path=6b085d256e60a) The difference between my screenshots is caused by the following Windows option: Control Panel → Region → Administrative → Change system locale → **Beta: Use Unicode UTF-8 for worldwide language support** The latter is with this option disabled and the previous one with it enabled. It's available on both Win10 & Win11. (I only found out about this option since someone complained about IERenderer failing to render several HTML messages with the Windows option enabled - which I can't duplicate with my current development version of Pegasus Mail).
			Michael
--
IERenderer's Homepage
PGP Key ID (RSA 2048): 0xC45D831B
S/MIME Fingerprint: 94C6B471 0C623088 A5B27701 742B8666 3B7E657C
edited 3 days ago at 4:32 pm
Just to state how I understand the issue (which could be totally wrong)

Mr. Harris added some support for utf-8 in message body part but the subject line currently has no utf-8 support? That is why utf-8 in Subject/other header lines causes issues.

I have a little project findnoascii that scans text file for utf characters and displays info. Its database has 1111936 utf-8codeslook.csv characters.

Example:
findnoascii uogemp.csv
Using File /usr/local/share/findnoascii/utf-8codeslook.csv
Created - uogemp.csv.out with line/position details of utf8 characters
Created - uogemp.csv.out-sum with total counts of utf8 characters
$ cat uogemp.csv.out*
140 13 c3b1 [ñ]
187 3 c3b1 [ñ]
266 14 e28099 [’]
328 5 c3b1 [ñ]
378 6 c3b1 [ñ]
397 10 e28099 [’]
557 14 e28099 [’]
557 17 c3a5 [å]
1 c3a5 [å] LATIN SMALL LETTER A WITH RING ABOVE (U+00E5)
4 c3b1 [ñ] LATIN SMALL LETTER N WITH TILDE (U+00F1)
3 e28099 [’] RIGHT SINGLE QUOTATION MARK (U+2019)


So option might be to clear out any utf characters completely?

Other option might be to clear only utf character that gets chopped.

Know if character is missing a byte(s) it throws things off.

Think was mentioned something is limited to 28 character, so a multi-byte utf character could get chopped. I don't know enough on subject line and PMI index process at this point. I'd probable just remove any utf characters completely. But not clear how such a filter could be inserted?

Did a test on a PMM file and got this?

11 c2a0 [] NO-BREAK SPACE (U+00A0)
2 e28099 [] RIGHT SINGLE QUOTATION MARK (U+2019)
6 f09f988a [] SMILING FACE WITH SMILING EYES (U+1F60A)

PMI File for same PMM (filtered actual characters would not let post)
1 c27a [] Not Found??
1 ca06 [] Not Found??
1 d63d [] Not Found??
1 dc07 [] Not Found??
3 dcb7 [] SYRIAC RBASA BELOW (U+0737)
1 e85701 [] Not Found??
1 ed0401 [] Not Found??
1 ed4a01 [] Not Found??
1 f24801c0 [] Not Found??


PMI more likely just binary code that has character

Just throwing things on wall, and seeing if something might stick??

```` Just to state how I understand the issue (which could be totally wrong) Mr. Harris added some support for utf-8 in message body part but the subject line currently has no utf-8 support? That is why utf-8 in Subject/other header lines causes issues. I have a little project findnoascii that scans text file for utf characters and displays info. Its database has 1111936 utf-8codeslook.csv characters. Example: findnoascii uogemp.csv Using File /usr/local/share/findnoascii/utf-8codeslook.csv Created - uogemp.csv.out with line/position details of utf8 characters Created - uogemp.csv.out-sum with total counts of utf8 characters $ cat uogemp.csv.out* 140 13 c3b1 [ñ] 187 3 c3b1 [ñ] 266 14 e28099 [’] 328 5 c3b1 [ñ] 378 6 c3b1 [ñ] 397 10 e28099 [’] 557 14 e28099 [’] 557 17 c3a5 [å] 1 c3a5 [å] LATIN SMALL LETTER A WITH RING ABOVE (U+00E5) 4 c3b1 [ñ] LATIN SMALL LETTER N WITH TILDE (U+00F1) 3 e28099 [’] RIGHT SINGLE QUOTATION MARK (U+2019) So option might be to clear out any utf characters completely? Other option might be to clear only utf character that gets chopped. Know if character is missing a byte(s) it throws things off. Think was mentioned something is limited to 28 character, so a multi-byte utf character could get chopped. I don't know enough on subject line and PMI index process at this point. I'd probable just remove any utf characters completely. But not clear how such a filter could be inserted? Did a test on a PMM file and got this? 11 c2a0 [] NO-BREAK SPACE (U+00A0) 2 e28099 [] RIGHT SINGLE QUOTATION MARK (U+2019) 6 f09f988a [] SMILING FACE WITH SMILING EYES (U+1F60A) PMI File for same PMM (filtered actual characters would not let post) 1 c27a [] Not Found?? 1 ca06 [] Not Found?? 1 d63d [] Not Found?? 1 dc07 [] Not Found?? 3 dcb7 [] SYRIAC RBASA BELOW (U+0737) 1 e85701 [] Not Found?? 1 ed0401 [] Not Found?? 1 ed4a01 [] Not Found?? 1 f24801c0 [] Not Found?? PMI more likely just binary code that has character Just throwing things on wall, and seeing if something might stick?? ````

mikes@guam.net

I try to answer, but are being hampered by:


<br />
<b>Fatal error</b>: Uncaught PDOException: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xF0\x9F\x8D\xBD\xEF\xB8...' for column codoforum.codo_posts.imessage at row 1 in /var/www/codoforum/sys/CODOF/Forum/Post.php:347
Stack trace:


0 /var/www/codoforum/sys/CODOF/Forum/Post.php(347): PDOStatement->execute(Array)


1 /var/www/codoforum/sys/Controller/Ajax/forum/topic.php(165): CODOF\Forum\Post->ins_post(10, 12530, 'Best watched wi...', '


2 /var/www/codoforum/routes.php(103): Controller\Ajax\forum\topic->reply()


3 [internal function]: {closure}()


4 /var/www/codoforum/sys/CODOF/Router/Router.php(91): call_user_func_array(Object(Closure), Array)


5 /var/www/codoforum/sys/CODOF/Router/Router.php(56): CODOF\Router\Router::_dispatch(Object(FastRoute\Dispatcher\GroupCountBased))


6 /var/www/codoforum/routes.php(892): CODOF\Router\Router::dispatch()


7 /var/www/codoforum/index.php(22): require('/var/www/codofo...')


8 {main}


thrown in <b>/var/www/codoforum/sys/CODOF/Forum/Post.php</b> on line <b>347</b><br />


I try to answer, but are being hampered by: &lt;br /&gt; &lt;b&gt;Fatal error&lt;/b&gt;: Uncaught PDOException: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: &#039;\xF0\x9F\x8D\xBD\xEF\xB8...&#039; for column `codoforum`.`codo_posts`.`imessage` at row 1 in /var/www/codoforum/sys/CODOF/Forum/Post.php:347 Stack trace: #0 /var/www/codoforum/sys/CODOF/Forum/Post.php(347): PDOStatement-&amp;gt;execute(Array) #1 /var/www/codoforum/sys/Controller/Ajax/forum/topic.php(165): CODOF\Forum\Post-&amp;gt;ins_post(10, 12530, &#039;Best watched wi...&#039;, &#039;STARTCODOTAGp&amp;gt;B...&#039;, false) #2 /var/www/codoforum/routes.php(103): Controller\Ajax\forum\topic-&amp;gt;reply() #3 [internal function]: {closure}() #4 /var/www/codoforum/sys/CODOF/Router/Router.php(91): call_user_func_array(Object(Closure), Array) #5 /var/www/codoforum/sys/CODOF/Router/Router.php(56): CODOF\Router\Router::_dispatch(Object(FastRoute\Dispatcher\GroupCountBased)) #6 /var/www/codoforum/routes.php(892): CODOF\Router\Router::dispatch() #7 /var/www/codoforum/index.php(22): require(&#039;/var/www/codofo...&#039;) #8 {main} thrown in &lt;b&gt;/var/www/codoforum/sys/CODOF/Forum/Post.php&lt;/b&gt; on line &lt;b&gt;347&lt;/b&gt;&lt;br /&gt;

I tried to cut my message down in every way, but no, I still get the error above.


This is the encoded character
https://unicodeplus.com/U+1F37D


Steffen


I tried to cut my message down in every way, but no, I still get the error above. This is the encoded character https://unicodeplus.com/U+1F37D Steffen

Sending my reply with this utf character is not possible. So this php system is broken as well.


@BrianFluet
Well, the subject is a long one, taking the ANSII part into account.

It should be displayed as
Best watched with leftovers [stupid emoji of a plate with knife and fork next to it]


The utf-8 encoded part consists of one character only:
https://unicodeplus.com/U+1F37D
If and how this specific character gets displayed on your system is up to some other conditions.
Why it appears to break the subject in the message viewer, I have no idea yet.


Steffen


Sending my reply with this utf character is not possible. So this php system is broken as well. @BrianFluet Well, the subject _is_ a long one, taking the ANSII part into account. It should be displayed as ```` Best watched with leftovers [stupid emoji of a plate with knife and fork next to it]```` The utf-8 encoded part consists of one character only: https://unicodeplus.com/U+1F37D If and how this specific character gets displayed on your system is up to some other conditions. Why it appears to break the subject in the message viewer, I have no idea yet. Steffen

Pulled data from some of my PMM files.


cat PM*.PMM |grep -a "Subject:" | grep "?UTF"
>Subject: =3D?UTF-8?Q?Spacekl=3DC3=3DA4nge?=3D and =3D?UTF-8?Q?_Pausenf=3D=
le=3D" font-size:12pt"><i>&gt;Subject: =3D?UTF-8?Q?Spacekl=3DC3=3DA4nge?=3D=
Subject: =3D?UTF-8?Q?Spacekl=3DC3=3DA4nge?=3D and =3D?UTF-8?Q?_Pausenf=3D=
Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?=
Subject: =3D?UTF-8?B?W1VLSEZDb250ZXN0aW5nXSAxOG0gc3BpZGVyIHBvbGXigKbigKZpd=
Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?=
Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?=
Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?=
Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?=
Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?=
Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?=
Subject: Re: IERenderer The parameter is incorrect (000070057) @OD900DB9=?UTF-8?Q?=C2=A0?=
Subject: Re: IERenderer The parameter is incorrect (000070057) @OD900DB9=?UTF-8?Q?=C2=A0?=
Subject: IERenderer The parameter is incorrect (000070057) @OD900DB9=?UTF-8?Q?=C2=A0?=
Subject: IERenderer The parameter is incorrect (000070057) @OD900DB9=?UTF-8?Q?=C2=A0?=
Subject: =?UTF-8?Q?Vi_fick_nyheter_om_IVAR_p=C3=A5_IKEA_Link=C3=B6ping?=
Pulled data from some of my PMM files. ```` cat PM*.PMM |grep -a &quot;Subject:&quot; | grep &quot;?UTF&quot; &gt;Subject: =3D?UTF-8?Q?Spacekl=3DC3=3DA4nge?=3D and =3D?UTF-8?Q?_Pausenf=3D= le=3D&quot; font-size:12pt&quot;&gt;&lt;i&gt;&amp;gt;Subject: =3D?UTF-8?Q?Spacekl=3DC3=3DA4nge?=3D= Subject: =3D?UTF-8?Q?Spacekl=3DC3=3DA4nge?=3D and =3D?UTF-8?Q?_Pausenf=3D= Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?= Subject: =3D?UTF-8?B?W1VLSEZDb250ZXN0aW5nXSAxOG0gc3BpZGVyIHBvbGXigKbigKZpd= Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?= Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?= Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?= Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?= Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?= Subject: Re: utf-8 encoded subject line displays blank =?UTF-8?Q?=E2=9B=84?= Subject: Re: IERenderer The parameter is incorrect (000070057) @OD900DB9=?UTF-8?Q?=C2=A0?= Subject: Re: IERenderer The parameter is incorrect (000070057) @OD900DB9=?UTF-8?Q?=C2=A0?= Subject: IERenderer The parameter is incorrect (000070057) @OD900DB9=?UTF-8?Q?=C2=A0?= Subject: IERenderer The parameter is incorrect (000070057) @OD900DB9=?UTF-8?Q?=C2=A0?= Subject: =?UTF-8?Q?Vi_fick_nyheter_om_IVAR_p=C3=A5_IKEA_Link=C3=B6ping?= ````

mikes@guam.net

Chatting with google ai, and it came up with a program. to maybe filter out subject line.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_HDR_BUF 65536

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 void clean_subject_value(const char *in, char *out, size_t out_max) {
size_t oi = 0;
const char *p = in;
int prev_was_encoded = 0;

while (*p && oi + 1 < out_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 > 0 && (out[oi - 1] == ' ' || out[oi - 1] == '\t')) {
oi--;
}
}

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);

for (size_t k = 0; k < dlen && oi + 1 < out_max; k++) {
if (tmp[k] >= 0x20 && tmp[k] <= 0x7E) {
out[oi++] = (char)tmp[k];
}
}
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 >= 0x20 && ch <= 0x7E) {
out[oi++] = (char)ch;
}
p++;
}

/* Trim trailing spaces left over after stripping trailing emojis/NBSP */
while (oi > 0 && (out[oi - 1] == ' ' || out[oi - 1] == '\t')) {
oi--;
}
out[oi] = '\0';
}

int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <message.cnm>\n", argv[0]);
return 1;
}

const char *path = argv[1];
FILE *fin = fopen(path, "rb");
if (!fin) return 1;

char tmp_path[1024];
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];
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;

/* Strip trailing \r and \n for inspection */
while (len > 0 && (line[len - 1] == '\r' || line[len - 1] == '\n')) {
line[--len] = '\0';
}

/* Check if we are currently accumulating a folded Subject header */
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 {
clean_subject_value(subj_raw, subj_clean, sizeof(subj_clean));
fprintf(fout, "Subject: %s%s", subj_clean, detected_crlf ? "\r\n" : "\n");
in_subject = 0;
}
}

/* Blank line marks end of headers */
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 {
/* Copy body untouched */
fwrite(line, 1, strlen(line), fout);
}
}

/* Flush if file ended while still inside Subject header */
if (in_subject) {
clean_subject_value(subj_raw, subj_clean, sizeof(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 0;
}

would have to figure how to have it called to modify message.


Chatting with google ai, and it came up with a program. to maybe filter out subject line. ```` #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; #define MAX_HDR_BUF 65536 static int hex_val(char c) { if (c &gt;= &#039;0&#039; &amp;&amp; c &lt;= &#039;9&#039;) return c - &#039;0&#039;; if (c &gt;= &#039;A&#039; &amp;&amp; c &lt;= &#039;F&#039;) return c - &#039;A&#039; + 10; if (c &gt;= &#039;a&#039; &amp;&amp; c &lt;= &#039;f&#039;) return c - &#039;a&#039; + 10; return -1; } static int b64_val(char c) { if (c &gt;= &#039;A&#039; &amp;&amp; c &lt;= &#039;Z&#039;) return c - &#039;A&#039;; if (c &gt;= &#039;a&#039; &amp;&amp; c &lt;= &#039;z&#039;) return c - &#039;a&#039; + 26; if (c &gt;= &#039;0&#039; &amp;&amp; c &lt;= &#039;9&#039;) return c - &#039;0&#039; + 52; if (c == &#039;+&#039;) return 62; if (c == &#039;/&#039;) 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 &lt; len; i++) { if (src[i] == &#039;_&#039;) { dst[out++] = &#039; &#039;; } else if (src[i] == &#039;=&#039; &amp;&amp; i + 2 &lt; len) { int h1 = hex_val(src[i + 1]); int h2 = hex_val(src[i + 2]); if (h1 &gt;= 0 &amp;&amp; h2 &gt;= 0) { dst[out++] = (unsigned char)((h1 &lt;&lt; 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 &lt; len; i++) { if (src[i] == &#039;=&#039;) break; int d = b64_val(src[i]); if (d &lt; 0) continue; val = (val &lt;&lt; 6) | d; bits += 6; if (bits &gt;= 0) { dst[out++] = (unsigned char)((val &gt;&gt; bits) &amp; 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 void clean_subject_value(const char *in, char *out, size_t out_max) { size_t oi = 0; const char *p = in; int prev_was_encoded = 0; while (*p &amp;&amp; oi + 1 &lt; out_max) { if (p[0] == &#039;=&#039; &amp;&amp; p[1] == &#039;?&#039;) { const char *q1 = strchr(p + 2, &#039;?&#039;); if (q1 &amp;&amp; (q1[1] == &#039;Q&#039; || q1[1] == &#039;q&#039; || q1[1] == &#039;B&#039; || q1[1] == &#039;b&#039;) &amp;&amp; q1[2] == &#039;?&#039;) { char enc = (char)toupper((unsigned char)q1[1]); const char *payload = q1 + 3; const char *end = strstr(payload, &quot;?=&quot;); if (end) { /* Strip inter-word whitespace between adjacent encoded words */ if (prev_was_encoded) { while (oi &gt; 0 &amp;&amp; (out[oi - 1] == &#039; &#039; || out[oi - 1] == &#039;\t&#039;)) { oi--; } } size_t plen = (size_t)(end - payload); unsigned char *tmp = (unsigned char *)malloc(plen + 4); if (tmp) { size_t dlen = (enc == &#039;Q&#039;) ? decode_qp(payload, plen, tmp) : decode_b64(payload, plen, tmp); for (size_t k = 0; k &lt; dlen &amp;&amp; oi + 1 &lt; out_max; k++) { if (tmp[k] &gt;= 0x20 &amp;&amp; tmp[k] &lt;= 0x7E) { out[oi++] = (char)tmp[k]; } } free(tmp); } p = end + 2; prev_was_encoded = 1; continue; } } } unsigned char ch = (unsigned char)*p; if (ch != &#039; &#039; &amp;&amp; ch != &#039;\t&#039;) { prev_was_encoded = 0; } if (ch &gt;= 0x20 &amp;&amp; ch &lt;= 0x7E) { out[oi++] = (char)ch; } p++; } /* Trim trailing spaces left over after stripping trailing emojis/NBSP */ while (oi &gt; 0 &amp;&amp; (out[oi - 1] == &#039; &#039; || out[oi - 1] == &#039;\t&#039;)) { oi--; } out[oi] = &#039;\0&#039;; } int main(int argc, char *argv[]) { if (argc &lt; 2) { fprintf(stderr, &quot;Usage: %s &lt;message.cnm&gt;\n&quot;, argv[0]); return 1; } const char *path = argv[1]; FILE *fin = fopen(path, &quot;rb&quot;); if (!fin) return 1; char tmp_path[1024]; snprintf(tmp_path, sizeof(tmp_path), &quot;%s.tmp&quot;, path); FILE *fout = fopen(tmp_path, &quot;wb&quot;); if (!fout) { fclose(fin); return 1; } char line[8192]; char subj_raw[MAX_HDR_BUF]; char subj_clean[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 &gt;= 2 &amp;&amp; line[len - 2] == &#039;\r&#039; &amp;&amp; line[len - 1] == &#039;\n&#039;); if (len &gt;= 1) detected_crlf = has_crlf; /* Strip trailing \r and \n for inspection */ while (len &gt; 0 &amp;&amp; (line[len - 1] == &#039;\r&#039; || line[len - 1] == &#039;\n&#039;)) { line[--len] = &#039;\0&#039;; } /* Check if we are currently accumulating a folded Subject header */ if (in_subject) { if (len &gt; 0 &amp;&amp; (line[0] == &#039; &#039; || line[0] == &#039;\t&#039;)) { const char *cont = line; while (*cont == &#039; &#039; || *cont == &#039;\t&#039;) cont++; size_t cur = strlen(subj_raw); if (cur + strlen(cont) + 2 &lt; sizeof(subj_raw)) { if (cur &gt; 0) strcat(subj_raw, &quot; &quot;); strcat(subj_raw, cont); } continue; } else { clean_subject_value(subj_raw, subj_clean, sizeof(subj_clean)); fprintf(fout, &quot;Subject: %s%s&quot;, subj_clean, detected_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); in_subject = 0; } } /* Blank line marks end of headers */ if (len == 0) { headers_done = 1; fprintf(fout, &quot;%s&quot;, detected_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); continue; } if (starts_with_ci(line, &quot;Subject:&quot;)) { const char *val = line + 8; while (*val == &#039; &#039; || *val == &#039;\t&#039;) val++; snprintf(subj_raw, sizeof(subj_raw), &quot;%s&quot;, val); in_subject = 1; continue; } fprintf(fout, &quot;%s%s&quot;, line, has_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); } else { /* Copy body untouched */ fwrite(line, 1, strlen(line), fout); } } /* Flush if file ended while still inside Subject header */ if (in_subject) { clean_subject_value(subj_raw, subj_clean, sizeof(subj_clean)); fprintf(fout, &quot;Subject: %s%s&quot;, subj_clean, detected_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); } fclose(fin); fclose(fout); remove(path); if (rename(tmp_path, path) != 0) { return 1; } return 0; } ```` would have to figure how to have it called to modify message.

mikes@guam.net

Mr. Harris added some support for utf-8 in message body part but the subject line currently has no utf-8 support? That is why utf-8 in Subject/other header lines causes issues.


An explanation that made some sense to me was that UTF-8 support exists for the subject line in all places except a folder list (other than the NMF) because the current design of the .PMI index can't support it. I the case of the sample message I posted about earlier, the subject content of "Best watched with leftovers" is visible when the message is in the NMF but is not once the message is moved to another folder. So, this makes sense to me. What doesn't make sense is why a .cnm that contains a copy of the raw view of the original message does not exhibit the issue. Apparently the SaveAs done by Notepad to create the .cnm resulted in a file that is altered in a way that fixes the problem. I am clueless about that.


[quote=&quot;pid:59422, uid:2546&quot;]Mr. Harris added some support for utf-8 in message body part but the subject line currently has no utf-8 support? That is why utf-8 in Subject/other header lines causes issues.[/quote] An explanation that made some sense to me was that UTF-8 support exists for the subject line in all places except a folder list (other than the NMF) because the current design of the .PMI index can&#039;t support it. I the case of the sample message I posted about earlier, the subject content of &quot;Best watched with leftovers&quot; is visible when the message is in the NMF but is not once the message is moved to another folder. So, this makes sense to me. What doesn&#039;t make sense is why a .cnm that contains a copy of the raw view of the original message does not exhibit the issue. Apparently the SaveAs done by Notepad to create the .cnm resulted in a file that is altered in a way that fixes the problem. I am clueless about that.

Did a little test.
Created a dummy file TEST2.CNM with utf in subject


From: sender@example.com
To: recipient@example.com
Date: Thu, 26 Nov 2026 11:45:00 -0800
Subject: =?UTF-8?Q?R=C3=A9sum=C3=A9_and_Cover_Letter?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit


Please find my documents attached.


Ran the google ai program on it.
stripchar.exe TEST2.CNM


File after run.


From: sender@example.com
To: recipient@example.com
Date: Thu, 26 Nov 2026 11:45:00 -0800
Subject: Rsum and Cover Letter
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit


Looks like in this example the utf is an e with accent, so for those things might be better to change character to be regular one without accent. Seeing Resume instead of Rsum? But had asked for program to strip utf, so that is what I got.


Did a little test. Created a dummy file TEST2.CNM with utf in subject From: sender@example.com To: recipient@example.com Date: Thu, 26 Nov 2026 11:45:00 -0800 Subject: =?UTF-8?Q?R=C3=A9sum=C3=A9_and_Cover_Letter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Please find my documents attached. Ran the google ai program on it. stripchar.exe TEST2.CNM File after run. From: sender@example.com To: recipient@example.com Date: Thu, 26 Nov 2026 11:45:00 -0800 Subject: Rsum and Cover Letter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Looks like in this example the utf is an e with accent, so for those things might be better to change character to be regular one without accent. Seeing Resume instead of Rsum? But had asked for program to strip utf, so that is what I got.

mikes@guam.net

asked google ai for version that replaces characters with base characters, and it created the version that convert to Resume instead of Rsum.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_HDR_BUF 65536

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;
}

/* 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 a buffer of decoded UTF-8 bytes into out, transliterating accents and stripping emojis */
static void append_transliterated_utf8(const unsigned char *src, size_t slen, char *out, size_t *oi, size_t out_max) {
size_t k = 0;
while (k < slen && *oi + 1 < out_max) {
unsigned char b1 = src[k];
if (b1 < 0x80) {
if (b1 >= 0x20 && b1 <= 0x7E) {
out[(*oi)++] = (char)b1;
}
k++;
} else if ((b1 & 0xE0) == 0xC0 && k + 1 < slen) {
if (b1 == 0xC3) {
char mapped = map_latin1_c3(src[k + 1]);
if (mapped) out[(*oi)++] = mapped;
} else if (b1 == 0xC2 && src[k + 1] == 0xA0) {
/* Non-breaking space -> regular space */
out[(*oi)++] = ' ';
}
k += 2;
} else if ((b1 & 0xF0) == 0xE0 && k + 2 < slen) {
/* 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[(*oi)++] = '-';
} else if (b3 == 0x98 || b3 == 0x99) {
out[(*oi)++] = '\'';
} else if (b3 == 0x9C || b3 == 0x9D) {
out[(*oi)++] = '"';
} else if (b3 == 0xA6 && *oi + 3 < out_max) {
out[(*oi)++] = '.';
out[(*oi)++] = '.';
out[(*oi)++] = '.';
}
}
k += 3;
} else if ((b1 & 0xF8) == 0xF0 && k + 3 < slen) {
/* 4-byte UTF-8 (emojis) -> skip */
k += 4;
} else {
/* Stray/invalid byte -> skip */
k++;
}
}
}

static void clean_subject_value(const char *in, char *out, size_t out_max) {
size_t oi = 0;
const char *p = in;
int prev_was_encoded = 0;

while (*p && oi + 1 < out_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 > 0 && (out[oi - 1] == ' ' || out[oi - 1] == '\t')) {
oi--;
}
}

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);

append_transliterated_utf8(tmp, dlen, out, &oi, out_max);
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[oi++] = (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;

append_transliterated_utf8((const unsigned char *)p, seq_len, out, &oi, out_max);
p += seq_len;
}
}

/* Trim trailing spaces left over after stripping trailing emojis/NBSP */
while (oi > 0 && (out[oi - 1] == ' ' || out[oi - 1] == '\t')) {
oi--;
}
out[oi] = '\0';
}

int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <message.cnm>\n", argv[0]);
return 1;
}

const char *path = argv[1];
FILE *fin = fopen(path, "rb");
if (!fin) return 1;

char tmp_path[1024];
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];
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;

/* Strip trailing \r and \n for inspection */
while (len > 0 && (line[len - 1] == '\r' || line[len - 1] == '\n')) {
line[--len] = '\0';
}

/* Check if we are currently accumulating a folded Subject header */
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 {
clean_subject_value(subj_raw, subj_clean, sizeof(subj_clean));
fprintf(fout, "Subject: %s%s", subj_clean, detected_crlf ? "\r\n" : "\n");
in_subject = 0;
}
}

/* Blank line marks end of headers */
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 {
/* Copy body untouched */
fwrite(line, 1, strlen(line), fout);
}
}

/* Flush if file ended while still inside Subject header */
if (in_subject) {
clean_subject_value(subj_raw, subj_clean, sizeof(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 0;
}
asked google ai for version that replaces characters with base characters, and it created the version that convert to Resume instead of Rsum. ```` #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; #define MAX_HDR_BUF 65536 static int hex_val(char c) { if (c &gt;= &#039;0&#039; &amp;&amp; c &lt;= &#039;9&#039;) return c - &#039;0&#039;; if (c &gt;= &#039;A&#039; &amp;&amp; c &lt;= &#039;F&#039;) return c - &#039;A&#039; + 10; if (c &gt;= &#039;a&#039; &amp;&amp; c &lt;= &#039;f&#039;) return c - &#039;a&#039; + 10; return -1; } static int b64_val(char c) { if (c &gt;= &#039;A&#039; &amp;&amp; c &lt;= &#039;Z&#039;) return c - &#039;A&#039;; if (c &gt;= &#039;a&#039; &amp;&amp; c &lt;= &#039;z&#039;) return c - &#039;a&#039; + 26; if (c &gt;= &#039;0&#039; &amp;&amp; c &lt;= &#039;9&#039;) return c - &#039;0&#039; + 52; if (c == &#039;+&#039;) return 62; if (c == &#039;/&#039;) 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 &lt; len; i++) { if (src[i] == &#039;_&#039;) { dst[out++] = &#039; &#039;; } else if (src[i] == &#039;=&#039; &amp;&amp; i + 2 &lt; len) { int h1 = hex_val(src[i + 1]); int h2 = hex_val(src[i + 2]); if (h1 &gt;= 0 &amp;&amp; h2 &gt;= 0) { dst[out++] = (unsigned char)((h1 &lt;&lt; 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 &lt; len; i++) { if (src[i] == &#039;=&#039;) break; int d = b64_val(src[i]); if (d &lt; 0) continue; val = (val &lt;&lt; 6) | d; bits += 6; if (bits &gt;= 0) { dst[out++] = (unsigned char)((val &gt;&gt; bits) &amp; 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; } /* Map UTF-8 0xC3 xx Latin-1 Supplement accented letters to base ASCII */ static char map_latin1_c3(unsigned char b2) { if (b2 &gt;= 0x80 &amp;&amp; b2 &lt;= 0x85) return &#039;A&#039;; if (b2 == 0x87) return &#039;C&#039;; if (b2 &gt;= 0x88 &amp;&amp; b2 &lt;= 0x8B) return &#039;E&#039;; if (b2 &gt;= 0x8C &amp;&amp; b2 &lt;= 0x8F) return &#039;I&#039;; if (b2 == 0x91) return &#039;N&#039;; if (b2 &gt;= 0x92 &amp;&amp; b2 &lt;= 0x96) return &#039;O&#039;; if (b2 &gt;= 0x99 &amp;&amp; b2 &lt;= 0x9C) return &#039;U&#039;; if (b2 == 0x9D) return &#039;Y&#039;; if (b2 &gt;= 0xA0 &amp;&amp; b2 &lt;= 0xA5) return &#039;a&#039;; if (b2 == 0xA7) return &#039;c&#039;; if (b2 &gt;= 0xA8 &amp;&amp; b2 &lt;= 0xAB) return &#039;e&#039;; if (b2 &gt;= 0xAC &amp;&amp; b2 &lt;= 0xAF) return &#039;i&#039;; if (b2 == 0xB1) return &#039;n&#039;; if (b2 &gt;= 0xB2 &amp;&amp; b2 &lt;= 0xB6) return &#039;o&#039;; if (b2 &gt;= 0xB9 &amp;&amp; b2 &lt;= 0xBC) return &#039;u&#039;; if (b2 == 0xBD || b2 == 0xBF) return &#039;y&#039;; return 0; } /* Append a buffer of decoded UTF-8 bytes into out, transliterating accents and stripping emojis */ static void append_transliterated_utf8(const unsigned char *src, size_t slen, char *out, size_t *oi, size_t out_max) { size_t k = 0; while (k &lt; slen &amp;&amp; *oi + 1 &lt; out_max) { unsigned char b1 = src[k]; if (b1 &lt; 0x80) { if (b1 &gt;= 0x20 &amp;&amp; b1 &lt;= 0x7E) { out[(*oi)++] = (char)b1; } k++; } else if ((b1 &amp; 0xE0) == 0xC0 &amp;&amp; k + 1 &lt; slen) { if (b1 == 0xC3) { char mapped = map_latin1_c3(src[k + 1]); if (mapped) out[(*oi)++] = mapped; } else if (b1 == 0xC2 &amp;&amp; src[k + 1] == 0xA0) { /* Non-breaking space -&gt; regular space */ out[(*oi)++] = &#039; &#039;; } k += 2; } else if ((b1 &amp; 0xF0) == 0xE0 &amp;&amp; k + 2 &lt; slen) { /* Common 3-byte smart punctuation (0xE2 0x80 xx) */ if (b1 == 0xE2 &amp;&amp; src[k + 1] == 0x80) { unsigned char b3 = src[k + 2]; if (b3 == 0x93 || b3 == 0x94) { out[(*oi)++] = &#039;-&#039;; } else if (b3 == 0x98 || b3 == 0x99) { out[(*oi)++] = &#039;\&#039;&#039;; } else if (b3 == 0x9C || b3 == 0x9D) { out[(*oi)++] = &#039;&quot;&#039;; } else if (b3 == 0xA6 &amp;&amp; *oi + 3 &lt; out_max) { out[(*oi)++] = &#039;.&#039;; out[(*oi)++] = &#039;.&#039;; out[(*oi)++] = &#039;.&#039;; } } k += 3; } else if ((b1 &amp; 0xF8) == 0xF0 &amp;&amp; k + 3 &lt; slen) { /* 4-byte UTF-8 (emojis) -&gt; skip */ k += 4; } else { /* Stray/invalid byte -&gt; skip */ k++; } } } static void clean_subject_value(const char *in, char *out, size_t out_max) { size_t oi = 0; const char *p = in; int prev_was_encoded = 0; while (*p &amp;&amp; oi + 1 &lt; out_max) { if (p[0] == &#039;=&#039; &amp;&amp; p[1] == &#039;?&#039;) { const char *q1 = strchr(p + 2, &#039;?&#039;); if (q1 &amp;&amp; (q1[1] == &#039;Q&#039; || q1[1] == &#039;q&#039; || q1[1] == &#039;B&#039; || q1[1] == &#039;b&#039;) &amp;&amp; q1[2] == &#039;?&#039;) { char enc = (char)toupper((unsigned char)q1[1]); const char *payload = q1 + 3; const char *end = strstr(payload, &quot;?=&quot;); if (end) { /* Strip inter-word whitespace between adjacent encoded words */ if (prev_was_encoded) { while (oi &gt; 0 &amp;&amp; (out[oi - 1] == &#039; &#039; || out[oi - 1] == &#039;\t&#039;)) { oi--; } } size_t plen = (size_t)(end - payload); unsigned char *tmp = (unsigned char *)malloc(plen + 4); if (tmp) { size_t dlen = (enc == &#039;Q&#039;) ? decode_qp(payload, plen, tmp) : decode_b64(payload, plen, tmp); append_transliterated_utf8(tmp, dlen, out, &amp;oi, out_max); free(tmp); } p = end + 2; prev_was_encoded = 1; continue; } } } unsigned char ch = (unsigned char)*p; if (ch != &#039; &#039; &amp;&amp; ch != &#039;\t&#039;) { prev_was_encoded = 0; } if (ch &lt; 0x80) { if (ch &gt;= 0x20 &amp;&amp; ch &lt;= 0x7E) { out[oi++] = (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 &amp; 0xE0) == 0xC0 &amp;&amp; avail &gt;= 2) seq_len = 2; else if ((ch &amp; 0xF0) == 0xE0 &amp;&amp; avail &gt;= 3) seq_len = 3; else if ((ch &amp; 0xF8) == 0xF0 &amp;&amp; avail &gt;= 4) seq_len = 4; append_transliterated_utf8((const unsigned char *)p, seq_len, out, &amp;oi, out_max); p += seq_len; } } /* Trim trailing spaces left over after stripping trailing emojis/NBSP */ while (oi &gt; 0 &amp;&amp; (out[oi - 1] == &#039; &#039; || out[oi - 1] == &#039;\t&#039;)) { oi--; } out[oi] = &#039;\0&#039;; } int main(int argc, char *argv[]) { if (argc &lt; 2) { fprintf(stderr, &quot;Usage: %s &lt;message.cnm&gt;\n&quot;, argv[0]); return 1; } const char *path = argv[1]; FILE *fin = fopen(path, &quot;rb&quot;); if (!fin) return 1; char tmp_path[1024]; snprintf(tmp_path, sizeof(tmp_path), &quot;%s.tmp&quot;, path); FILE *fout = fopen(tmp_path, &quot;wb&quot;); if (!fout) { fclose(fin); return 1; } char line[8192]; char subj_raw[MAX_HDR_BUF]; char subj_clean[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 &gt;= 2 &amp;&amp; line[len - 2] == &#039;\r&#039; &amp;&amp; line[len - 1] == &#039;\n&#039;); if (len &gt;= 1) detected_crlf = has_crlf; /* Strip trailing \r and \n for inspection */ while (len &gt; 0 &amp;&amp; (line[len - 1] == &#039;\r&#039; || line[len - 1] == &#039;\n&#039;)) { line[--len] = &#039;\0&#039;; } /* Check if we are currently accumulating a folded Subject header */ if (in_subject) { if (len &gt; 0 &amp;&amp; (line[0] == &#039; &#039; || line[0] == &#039;\t&#039;)) { const char *cont = line; while (*cont == &#039; &#039; || *cont == &#039;\t&#039;) cont++; size_t cur = strlen(subj_raw); if (cur + strlen(cont) + 2 &lt; sizeof(subj_raw)) { if (cur &gt; 0) strcat(subj_raw, &quot; &quot;); strcat(subj_raw, cont); } continue; } else { clean_subject_value(subj_raw, subj_clean, sizeof(subj_clean)); fprintf(fout, &quot;Subject: %s%s&quot;, subj_clean, detected_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); in_subject = 0; } } /* Blank line marks end of headers */ if (len == 0) { headers_done = 1; fprintf(fout, &quot;%s&quot;, detected_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); continue; } if (starts_with_ci(line, &quot;Subject:&quot;)) { const char *val = line + 8; while (*val == &#039; &#039; || *val == &#039;\t&#039;) val++; snprintf(subj_raw, sizeof(subj_raw), &quot;%s&quot;, val); in_subject = 1; continue; } fprintf(fout, &quot;%s%s&quot;, line, has_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); } else { /* Copy body untouched */ fwrite(line, 1, strlen(line), fout); } } /* Flush if file ended while still inside Subject header */ if (in_subject) { clean_subject_value(subj_raw, subj_clean, sizeof(subj_clean)); fprintf(fout, &quot;Subject: %s%s&quot;, subj_clean, detected_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); } fclose(fin); fclose(fout); remove(path); if (rename(tmp_path, path) != 0) { return 1; } return 0; } ````

mikes@guam.net

If the discussion is now about how to decode UTF-8 you need to know something about fonts: Please keep in mind that font charsets usually only cover a selected part of the whole Unicode universe. I've recently tried to figure out whether there is at least one charset capable of displaying all of Unicode, here's the first paragraph of a related Wikipedia article:



There are Unicode typefaces which are open-source and designed to contain glyphs of all Unicode characters, or at least a broad selection of Unicode scripts. There are also numerous projects aimed at providing only a certain script, such as the Arabeyes Arabic font. The advantage of targeting only some scripts with a font was that certain Unicode characters should be rendered differently depending on which language they are used in, and that a font that only includes the characters a certain user needs will be much smaller in file size compared to one with many glyphs. Unicode fonts in modern formats such as OpenType can in theory cover multiple languages by including multiple glyphs per character, though very few actually cover more than one language's forms of the unified Han characters.



Here's the best candidate mentioned in there, quote: "Noto is a font family designed to cover all the scripts encoded in the Unicode standard."


If the discussion is now about how to decode UTF-8 you need to know something about fonts: Please keep in mind that font charsets usually only cover a selected part of the whole Unicode universe. I&#039;ve recently tried to figure out whether there is at least _one_ charset capable of displaying all of Unicode, here&#039;s the first paragraph of a related Wikipedia article: &gt; There are Unicode typefaces which are open-source and designed to contain glyphs of all Unicode characters, or at least a broad selection of Unicode scripts. There are also numerous projects aimed at providing only a certain script, such as the Arabeyes Arabic font. The advantage of targeting only some scripts with a font was that certain Unicode characters should be rendered differently depending on which language they are used in, and that a font that only includes the characters a certain user needs will be much smaller in file size compared to one with many glyphs. Unicode fonts in modern formats such as OpenType can in theory cover multiple languages by including multiple glyphs per character, though very few actually cover more than one language&#039;s forms of the unified Han characters. Here&#039;s the best candidate mentioned in there, quote: &quot;Noto is a font family designed to cover all the scripts encoded in the Unicode standard.&quot;
			Michael
--
IERenderer's Homepage
PGP Key ID (RSA 2048): 0xC45D831B
S/MIME Fingerprint: 94C6B471 0C623088 A5B27701 742B8666 3B7E657C

Usually I don't have a problem with Pegasus Mail, but I tried to implement the program to modify the TEST2.CNM file, but it never changed.


If I manually run it it works perfectly stripcar2.exe TEST2.CNM perfect


Have it call program and tried the %s
Tried to run command if subject containg ?UTF-8, and it shows now rule run??
If I just put * it shows rule run but nothing changed?
tried the pause option, but then system window comes up, and it says running, but see no way to have it unpause??


Unchecked pause, but it keeps poping up with that screen?


Looked in Temp folder, and found random .TMP files that had the modified file? So at some point Pegasus was running the program, but rather than passing the path to the TEST2.CNM file it was creating a copy of the file in Temp and program is fixing it, which does no good. Need to modify the CNM file in the Mail directory.


I looked at the PMAIL.INI file, and the HIERARCH.PM but can find no change on the pause option??


One step forward two steps back?


Usually I don&#039;t have a problem with Pegasus Mail, but I tried to implement the program to modify the TEST2.CNM file, but it never changed. If I manually run it it works perfectly stripcar2.exe TEST2.CNM perfect Have it call program and tried the %s Tried to run command if subject containg ?UTF-8, and it shows now rule run?? If I just put * it shows rule run but nothing changed? tried the pause option, but then system window comes up, and it says running, but see no way to have it unpause?? Unchecked pause, but it keeps poping up with that screen? Looked in Temp folder, and found random .TMP files that had the modified file? So at some point Pegasus was running the program, but rather than passing the path to the TEST2.CNM file it was creating a copy of the file in Temp and program is fixing it, which does no good. Need to modify the CNM file in the Mail directory. I looked at the PMAIL.INI file, and the HIERARCH.PM but can find no change on the pause option?? One step forward two steps back?

mikes@guam.net

Confirmed a couple things.


  1. %s after modifying program is passing C:\users\msetzerii\Temp\9YLR3J3H.TMP Which is useless since in test the file to be modified would beC:\PMAIL\MAIL\TEST2.CNM

(have Single User Setup)


Came up somewhat solution. Couldn't figure how to get folder rules to get file.


Google AI looked at doing a compare with the temp file to the mail directory cnm files to find a match. But this is a simplier solution that seems to work.


Specific for my current single user setup.


cat fixutf8.sh
cd /home/msetzerii/.wine/drive_c/PMAIL/MAIL
for a in *.CNM; do if grep -qi "Subject: =?UTF-8" "$a"; then stripchar2 "$a"; fi; done


Changes to my mail folder and then runs modifies any CNM files with utf-8?


Changes regular accent characters to base character, and removes emoji.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_HDR_BUF 65536

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;
}

/* 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 a buffer of decoded UTF-8 bytes into out, transliterating accents and stripping emojis */
static void append_transliterated_utf8(const unsigned char *src, size_t slen, char *out, size_t *oi, size_t out_max) {
size_t k = 0;
while (k < slen && *oi + 1 < out_max) {
unsigned char b1 = src[k];
if (b1 < 0x80) {
if (b1 >= 0x20 && b1 <= 0x7E) {
out[(*oi)++] = (char)b1;
}
k++;
} else if ((b1 & 0xE0) == 0xC0 && k + 1 < slen) {
if (b1 == 0xC3) {
char mapped = map_latin1_c3(src[k + 1]);
if (mapped) out[(*oi)++] = mapped;
} else if (b1 == 0xC2 && src[k + 1] == 0xA0) {
/* Non-breaking space -> regular space */
out[(*oi)++] = ' ';
}
k += 2;
} else if ((b1 & 0xF0) == 0xE0 && k + 2 < slen) {
/* 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[(*oi)++] = '-';
} else if (b3 == 0x98 || b3 == 0x99) {
out[(*oi)++] = '\'';
} else if (b3 == 0x9C || b3 == 0x9D) {
out[(*oi)++] = '"';
} else if (b3 == 0xA6 && *oi + 3 < out_max) {
out[(*oi)++] = '.';
out[(*oi)++] = '.';
out[(*oi)++] = '.';
}
}
k += 3;
} else if ((b1 & 0xF8) == 0xF0 && k + 3 < slen) {
/* 4-byte UTF-8 (emojis) -> skip */
k += 4;
} else {
/* Stray/invalid byte -> skip */
k++;
}
}
}

static void clean_subject_value(const char *in, char *out, size_t out_max) {
size_t oi = 0;
const char *p = in;
int prev_was_encoded = 0;

while (*p && oi + 1 < out_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 > 0 && (out[oi - 1] == ' ' || out[oi - 1] == '\t')) {
oi--;
}
}

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);

append_transliterated_utf8(tmp, dlen, out, &oi, out_max);
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[oi++] = (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;

append_transliterated_utf8((const unsigned char *)p, seq_len, out, &oi, out_max);
p += seq_len;
}
}

/* Trim trailing spaces left over after stripping trailing emojis/NBSP */
while (oi > 0 && (out[oi - 1] == ' ' || out[oi - 1] == '\t')) {
oi--;
}
out[oi] = '\0';
}

int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <message.cnm>\n", argv[0]);
return 1;
}

const char *path = argv[1];
FILE *fin = fopen(path, "rb");
if (!fin) return 1;

char tmp_path[1024];
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];
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;

/* Strip trailing \r and \n for inspection */
while (len > 0 && (line[len - 1] == '\r' || line[len - 1] == '\n')) {
line[--len] = '\0';
}

/* Check if we are currently accumulating a folded Subject header */
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 {
clean_subject_value(subj_raw, subj_clean, sizeof(subj_clean));
fprintf(fout, "Subject: %s%s", subj_clean, detected_crlf ? "\r\n" : "\n");
in_subject = 0;
}
}

/* Blank line marks end of headers */
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 {
/* Copy body untouched */
fwrite(line, 1, strlen(line), fout);
}
}

/* Flush if file ended while still inside Subject header */
if (in_subject) {
clean_subject_value(subj_raw, subj_clean, sizeof(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 0;
}

sure a version for windows is possible.


Confirmed a couple things. 1. %s after modifying program is passing C:\users\msetzerii\Temp\9YLR3J3H.TMP Which is useless since in test the file to be modified would be C:\PMAIL\MAIL\TEST2.CNM (have Single User Setup) Came up somewhat solution. Couldn&#039;t figure how to get folder rules to get file. Google AI looked at doing a compare with the temp file to the mail directory cnm files to find a match. But this is a simplier solution that seems to work. Specific for my current single user setup. cat fixutf8.sh cd /home/msetzerii/.wine/drive_c/PMAIL/MAIL for a in *.CNM; do if grep -qi &quot;Subject: =?UTF-8&quot; &quot;$a&quot;; then stripchar2 &quot;$a&quot;; fi; done Changes to my mail folder and then runs modifies any CNM files with utf-8? Changes regular accent characters to base character, and removes emoji. ```` #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; #define MAX_HDR_BUF 65536 static int hex_val(char c) { if (c &gt;= &#039;0&#039; &amp;&amp; c &lt;= &#039;9&#039;) return c - &#039;0&#039;; if (c &gt;= &#039;A&#039; &amp;&amp; c &lt;= &#039;F&#039;) return c - &#039;A&#039; + 10; if (c &gt;= &#039;a&#039; &amp;&amp; c &lt;= &#039;f&#039;) return c - &#039;a&#039; + 10; return -1; } static int b64_val(char c) { if (c &gt;= &#039;A&#039; &amp;&amp; c &lt;= &#039;Z&#039;) return c - &#039;A&#039;; if (c &gt;= &#039;a&#039; &amp;&amp; c &lt;= &#039;z&#039;) return c - &#039;a&#039; + 26; if (c &gt;= &#039;0&#039; &amp;&amp; c &lt;= &#039;9&#039;) return c - &#039;0&#039; + 52; if (c == &#039;+&#039;) return 62; if (c == &#039;/&#039;) 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 &lt; len; i++) { if (src[i] == &#039;_&#039;) { dst[out++] = &#039; &#039;; } else if (src[i] == &#039;=&#039; &amp;&amp; i + 2 &lt; len) { int h1 = hex_val(src[i + 1]); int h2 = hex_val(src[i + 2]); if (h1 &gt;= 0 &amp;&amp; h2 &gt;= 0) { dst[out++] = (unsigned char)((h1 &lt;&lt; 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 &lt; len; i++) { if (src[i] == &#039;=&#039;) break; int d = b64_val(src[i]); if (d &lt; 0) continue; val = (val &lt;&lt; 6) | d; bits += 6; if (bits &gt;= 0) { dst[out++] = (unsigned char)((val &gt;&gt; bits) &amp; 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; } /* Map UTF-8 0xC3 xx Latin-1 Supplement accented letters to base ASCII */ static char map_latin1_c3(unsigned char b2) { if (b2 &gt;= 0x80 &amp;&amp; b2 &lt;= 0x85) return &#039;A&#039;; if (b2 == 0x87) return &#039;C&#039;; if (b2 &gt;= 0x88 &amp;&amp; b2 &lt;= 0x8B) return &#039;E&#039;; if (b2 &gt;= 0x8C &amp;&amp; b2 &lt;= 0x8F) return &#039;I&#039;; if (b2 == 0x91) return &#039;N&#039;; if (b2 &gt;= 0x92 &amp;&amp; b2 &lt;= 0x96) return &#039;O&#039;; if (b2 &gt;= 0x99 &amp;&amp; b2 &lt;= 0x9C) return &#039;U&#039;; if (b2 == 0x9D) return &#039;Y&#039;; if (b2 &gt;= 0xA0 &amp;&amp; b2 &lt;= 0xA5) return &#039;a&#039;; if (b2 == 0xA7) return &#039;c&#039;; if (b2 &gt;= 0xA8 &amp;&amp; b2 &lt;= 0xAB) return &#039;e&#039;; if (b2 &gt;= 0xAC &amp;&amp; b2 &lt;= 0xAF) return &#039;i&#039;; if (b2 == 0xB1) return &#039;n&#039;; if (b2 &gt;= 0xB2 &amp;&amp; b2 &lt;= 0xB6) return &#039;o&#039;; if (b2 &gt;= 0xB9 &amp;&amp; b2 &lt;= 0xBC) return &#039;u&#039;; if (b2 == 0xBD || b2 == 0xBF) return &#039;y&#039;; return 0; } /* Append a buffer of decoded UTF-8 bytes into out, transliterating accents and stripping emojis */ static void append_transliterated_utf8(const unsigned char *src, size_t slen, char *out, size_t *oi, size_t out_max) { size_t k = 0; while (k &lt; slen &amp;&amp; *oi + 1 &lt; out_max) { unsigned char b1 = src[k]; if (b1 &lt; 0x80) { if (b1 &gt;= 0x20 &amp;&amp; b1 &lt;= 0x7E) { out[(*oi)++] = (char)b1; } k++; } else if ((b1 &amp; 0xE0) == 0xC0 &amp;&amp; k + 1 &lt; slen) { if (b1 == 0xC3) { char mapped = map_latin1_c3(src[k + 1]); if (mapped) out[(*oi)++] = mapped; } else if (b1 == 0xC2 &amp;&amp; src[k + 1] == 0xA0) { /* Non-breaking space -&gt; regular space */ out[(*oi)++] = &#039; &#039;; } k += 2; } else if ((b1 &amp; 0xF0) == 0xE0 &amp;&amp; k + 2 &lt; slen) { /* Common 3-byte smart punctuation (0xE2 0x80 xx) */ if (b1 == 0xE2 &amp;&amp; src[k + 1] == 0x80) { unsigned char b3 = src[k + 2]; if (b3 == 0x93 || b3 == 0x94) { out[(*oi)++] = &#039;-&#039;; } else if (b3 == 0x98 || b3 == 0x99) { out[(*oi)++] = &#039;\&#039;&#039;; } else if (b3 == 0x9C || b3 == 0x9D) { out[(*oi)++] = &#039;&quot;&#039;; } else if (b3 == 0xA6 &amp;&amp; *oi + 3 &lt; out_max) { out[(*oi)++] = &#039;.&#039;; out[(*oi)++] = &#039;.&#039;; out[(*oi)++] = &#039;.&#039;; } } k += 3; } else if ((b1 &amp; 0xF8) == 0xF0 &amp;&amp; k + 3 &lt; slen) { /* 4-byte UTF-8 (emojis) -&gt; skip */ k += 4; } else { /* Stray/invalid byte -&gt; skip */ k++; } } } static void clean_subject_value(const char *in, char *out, size_t out_max) { size_t oi = 0; const char *p = in; int prev_was_encoded = 0; while (*p &amp;&amp; oi + 1 &lt; out_max) { if (p[0] == &#039;=&#039; &amp;&amp; p[1] == &#039;?&#039;) { const char *q1 = strchr(p + 2, &#039;?&#039;); if (q1 &amp;&amp; (q1[1] == &#039;Q&#039; || q1[1] == &#039;q&#039; || q1[1] == &#039;B&#039; || q1[1] == &#039;b&#039;) &amp;&amp; q1[2] == &#039;?&#039;) { char enc = (char)toupper((unsigned char)q1[1]); const char *payload = q1 + 3; const char *end = strstr(payload, &quot;?=&quot;); if (end) { /* Strip inter-word whitespace between adjacent encoded words */ if (prev_was_encoded) { while (oi &gt; 0 &amp;&amp; (out[oi - 1] == &#039; &#039; || out[oi - 1] == &#039;\t&#039;)) { oi--; } } size_t plen = (size_t)(end - payload); unsigned char *tmp = (unsigned char *)malloc(plen + 4); if (tmp) { size_t dlen = (enc == &#039;Q&#039;) ? decode_qp(payload, plen, tmp) : decode_b64(payload, plen, tmp); append_transliterated_utf8(tmp, dlen, out, &amp;oi, out_max); free(tmp); } p = end + 2; prev_was_encoded = 1; continue; } } } unsigned char ch = (unsigned char)*p; if (ch != &#039; &#039; &amp;&amp; ch != &#039;\t&#039;) { prev_was_encoded = 0; } if (ch &lt; 0x80) { if (ch &gt;= 0x20 &amp;&amp; ch &lt;= 0x7E) { out[oi++] = (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 &amp; 0xE0) == 0xC0 &amp;&amp; avail &gt;= 2) seq_len = 2; else if ((ch &amp; 0xF0) == 0xE0 &amp;&amp; avail &gt;= 3) seq_len = 3; else if ((ch &amp; 0xF8) == 0xF0 &amp;&amp; avail &gt;= 4) seq_len = 4; append_transliterated_utf8((const unsigned char *)p, seq_len, out, &amp;oi, out_max); p += seq_len; } } /* Trim trailing spaces left over after stripping trailing emojis/NBSP */ while (oi &gt; 0 &amp;&amp; (out[oi - 1] == &#039; &#039; || out[oi - 1] == &#039;\t&#039;)) { oi--; } out[oi] = &#039;\0&#039;; } int main(int argc, char *argv[]) { if (argc &lt; 2) { fprintf(stderr, &quot;Usage: %s &lt;message.cnm&gt;\n&quot;, argv[0]); return 1; } const char *path = argv[1]; FILE *fin = fopen(path, &quot;rb&quot;); if (!fin) return 1; char tmp_path[1024]; snprintf(tmp_path, sizeof(tmp_path), &quot;%s.tmp&quot;, path); FILE *fout = fopen(tmp_path, &quot;wb&quot;); if (!fout) { fclose(fin); return 1; } char line[8192]; char subj_raw[MAX_HDR_BUF]; char subj_clean[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 &gt;= 2 &amp;&amp; line[len - 2] == &#039;\r&#039; &amp;&amp; line[len - 1] == &#039;\n&#039;); if (len &gt;= 1) detected_crlf = has_crlf; /* Strip trailing \r and \n for inspection */ while (len &gt; 0 &amp;&amp; (line[len - 1] == &#039;\r&#039; || line[len - 1] == &#039;\n&#039;)) { line[--len] = &#039;\0&#039;; } /* Check if we are currently accumulating a folded Subject header */ if (in_subject) { if (len &gt; 0 &amp;&amp; (line[0] == &#039; &#039; || line[0] == &#039;\t&#039;)) { const char *cont = line; while (*cont == &#039; &#039; || *cont == &#039;\t&#039;) cont++; size_t cur = strlen(subj_raw); if (cur + strlen(cont) + 2 &lt; sizeof(subj_raw)) { if (cur &gt; 0) strcat(subj_raw, &quot; &quot;); strcat(subj_raw, cont); } continue; } else { clean_subject_value(subj_raw, subj_clean, sizeof(subj_clean)); fprintf(fout, &quot;Subject: %s%s&quot;, subj_clean, detected_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); in_subject = 0; } } /* Blank line marks end of headers */ if (len == 0) { headers_done = 1; fprintf(fout, &quot;%s&quot;, detected_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); continue; } if (starts_with_ci(line, &quot;Subject:&quot;)) { const char *val = line + 8; while (*val == &#039; &#039; || *val == &#039;\t&#039;) val++; snprintf(subj_raw, sizeof(subj_raw), &quot;%s&quot;, val); in_subject = 1; continue; } fprintf(fout, &quot;%s%s&quot;, line, has_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); } else { /* Copy body untouched */ fwrite(line, 1, strlen(line), fout); } } /* Flush if file ended while still inside Subject header */ if (in_subject) { clean_subject_value(subj_raw, subj_clean, sizeof(subj_clean)); fprintf(fout, &quot;Subject: %s%s&quot;, subj_clean, detected_crlf ? &quot;\r\n&quot; : &quot;\n&quot;); } fclose(fin); fclose(fout); remove(path); if (rename(tmp_path, path) != 0) { return 1; } return 0; } ```` sure a version for windows is possible.

mikes@guam.net

Now I think, that I can reasonably put our observations together.


UTF-8 trimming of MIME headers with a name and/or subject happens in
A) the Message view title bar
B) folder lists


The aim is to fit these potentially long strings into the tight space available for display (title or list columns).
In the case of A) this happens on the fly from the message's header, when the message gets loaded in the reader or preview.
In case B) it happens when a message is moved or copied to a folder (not the NMF) and the result is written into the message's folder index entry for a quick access when the list is opened in the future.


In the case of the disney subject given by @BrianFluet above,

Subject: Best watched with leftovers =?UTF-8?B?8J+Nve+4jw==?=

(the base-64 part of which decodes to utf-8 octets f0 9f 8d bd ef b8 8f)
the entire string consists of


28 ASCII characters 'Best watched with leftovers '
+ one 4-byte emoji character (codepoint U+1F37D, UTF-8 0xF0 0x9F 0x8D 0xBD)
+ one 3-byte invisible 'variation selector 16', meant to enforce the depiction of the preceeding character as an emoji, rather than text (codepoint U+FE0F, UTF-8 0xEF 0xB8 0x8F)


https://unicodeplus.com/U+1F37D
https://unicodeplus.com/U+FE0F


After concatenation the valid utf-8 string has 35 bytes.
When this gets truncated to 30 bytes we end up with an invalid string, because the emoji character is cut in two.
The invalid utf-8 is depicted with all question marks, a sure sign that something went wrong.


Now back to the topic on how to do this properly.
The easiest way would be to use a regex as given above.
If, for some reason one feels uncomfortable using regexes or your dev env doesn't give you this option (any advanced language does, what do we have here, C++ or C-Sharp?) you could implement exactly the same algorithm with a tiny loop:


If the byte string is too long
cut it off after the max length you can accommodate.
Take a look at the first byte of what you've cut off.
Is it a start byte for a multibyte sequence? (within either x00-x7F or xC0-xFF)
You'
re done.
While not,
cut off further bytes from the end, until it is.
End

That said, I think the basic purpose of trimming would be to restrict the display size, which means you don't fiddle with bytes but instead cut to characters, using any advanced utf-8-trimming-methods at your disposal.
These would of cause leave the multibyte sequences intact.
(How long the display of a string with mostly emoji 'characters' in it would come out to be, were still a gamble.)


But for storing them in the .pmi index in the file record's current format they must still be cut to a max byte length.
These files currently allow a mixture of strings in either your system's codepage (windows-1252 or similar) or utf-8.


Currently the name field (from/to) has 30 bytes and the subject has 36.
Both must end with a zero byte and start with 0x01 in the case of utf-8.
That means that a name in e.g. greek, cyrillic or many other non-latin languages, where every character has at least 2 bytes, is limited to 14 characters max.


I'd like to suggest changing the index files to a format with all strings utf-8 encoded, at flexible lengths.
Maybe even something like json or yaml.


Steffen


Now I think, that I can reasonably put our observations together. UTF-8 trimming of MIME headers with a name and/or subject happens in A) the Message view title bar B) folder lists The aim is to fit these potentially long strings into the tight space available for display (title or list columns). In the case of A) this happens on the fly from the message&#039;s header, when the message gets loaded in the reader or preview. In case B) it happens when a message is moved or copied to a folder (not the NMF) and the result is written into the message&#039;s folder index entry for a quick access when the list is opened in the future. In the case of the disney subject given by @BrianFluet above, ```` Subject: Best watched with leftovers =?UTF-8?B?8J+Nve+4jw==?= ```` (the base-64 part of which decodes to utf-8 octets **f0 9f 8d bd ef b8 8f**) the entire string consists of 28 ASCII characters &#039;**Best watched with leftovers** &#039; \+ one 4-byte emoji character (codepoint **U+1F37D**, UTF-8 **0xF0 0x9F 0x8D 0xBD**) \+ one 3-byte invisible &#039;variation selector 16&#039;, meant to enforce the depiction of the preceeding character as an emoji, rather than text (codepoint **U+FE0F**, UTF-8 **0xEF 0xB8 0x8F**) https://unicodeplus.com/U+1F37D https://unicodeplus.com/U+FE0F After concatenation the valid utf-8 string has 35 bytes. When this gets truncated to 30 bytes we end up with an invalid string, because the emoji character is cut in two. The invalid utf-8 is depicted with all question marks, a sure sign that something went wrong. Now back to the topic on how to do this properly. The easiest way would be to use a regex as given above. If, for some reason one feels uncomfortable using regexes or your dev env doesn&#039;t give you this option (any advanced language does, what do we have here, C++ or C-Sharp?) you could implement exactly the same algorithm with a tiny loop: ```` If the byte string is too long cut it off after the max length you can accommodate. Take a look at the first byte of what you&#039;ve cut off. Is it a start byte for a multibyte sequence? (within either x00-x7F or xC0-xFF) You&#039;re done. While not, cut off further bytes from the end, until it is. End ```` That said, I think the basic purpose of trimming would be to restrict the display size, which means you don&#039;t fiddle with bytes but instead cut to characters, using any advanced utf-8-trimming-methods at your disposal. These would of cause leave the multibyte sequences intact. (How long the display of a string with mostly emoji &#039;characters&#039; in it would come out to be, were still a gamble.) But for storing them in the .pmi index in the file record&#039;s current format they must still be cut to a max byte length. These files currently allow a mixture of strings in either your system&#039;s codepage (windows-1252 or similar) or utf-8. Currently the name field (from/to) has 30 bytes and the subject has 36. Both must end with a zero byte and start with 0x01 in the case of utf-8. That means that a name in e.g. greek, cyrillic or many other non-latin languages, where every character has at least 2 bytes, is limited to 14 characters max. I&#039;d like to suggest changing the index files to a format with all strings utf-8 encoded, at flexible lengths. Maybe even something like json or yaml. Steffen
12
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
With selected deselect posts show selected posts
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft