If the method succeeds, it will return the number of characters copied
into the output buffer, not including the terminating null character.
If the lpszOutput parameter is NULL and nMaxLength is
zero, the method will return the number of characters in the
filtered text, not including the terminating null. If the method
fails, or there is no text in the input buffer, the method will
return zero.
The NormalizeText method performs a multi-stage normalization
and cleanup of Unicode text, intended to simplify and standardize input
that may contain visually similar (confusable) characters, diacritical marks,
or inconsistent whitespace. It applies NFKD (Compatibility Decomposition)
normalization converting characters into their canonical forms and
then replaces any confusable Unicode characters with their ASCII equivalents
based on an internal mapping table. This method is similar to
calling the NormalizeString function in the Windows API,
however it performs additional checks and is more strict with how the
input is validated.
If your project is configured to use a multibyte character set
(ANSI), the input string is checked to ensure that it only contains valid
UTF-8 encoding and will reject strings which contain invalid sequences.
For example, it will reject multibyte UTF-8 sequences which are over-long
or truncated, invalid continuation sequences or characters outside of
valid Unicode code points. You should never provide a localized ANSI
string composed using the current code page. If you wish to normalize
a localized string, you should convert it to Unicode using the
MultiByteToWideChar function and then call the Unicode version of
this function.
If your project is configured to use Unicode, the input string is
validated to ensure that it only contains valid UTF-16 encoding and will
reject strings which contain invalid sequences. This includes checking
to ensure there are no unpaired surrogates, invalid code points,
unprintable control characters or private-use characters in the string.
If the input string does not contain valid Unicode character sequences,
the method will return zero and the last error code will be set to
ST_ERROR_INVALID_UNICODE.
This method helps mitigate issues with characters that are visually
indistinguishable from standard Latin letters, such as using the Cyrillic
"a" (U+0430) instead of the Latin "a" (U+0061). It also removes symbols
(such as emojis, flags and pictographs) which can confuse pattern matching
and removes combining marks that can be used to visually modify characters
or obscure text. Unicode whitespace is trimmed from both ends of the string,
including zero-width and non-breaking spaces.
This method is particularly useful in security-sensitive applications
where input normalization is critical, such as sanitizing user-provided domain
names, filenames, or login credentials to prevent spoofing, phishing, or
bypassing filters through the use of homoglyphs or invisible characters.
If you want a normalized version of the text in a message, you can
use the GetFilteredText method. It performs many of the
same normalizations that this method does, but allows for more flexibility
through options which allow you to control how the message text is
processed and filtered. It is particularly useful for filtering extraneous
HTML from the message, enabling you to display a version of the message
that is safer for the user (such as removing unsafe links from the message
contents).
This method is deliberately biased toward security rather than readability
and can affect the output when applied to text containing legitimate non-Latin
characters. It is intended for use on text that is expected to contain only
printable ASCII characters. For example, it will filter and normalize text
containing certain Greek or Cyrillic characters that could be used to spoof
ASCII letters or URL delimiters. While this helps mitigate obfuscated or
malicious content, it can also make legitimate text in other languages unreadable.