CMailMessage::NormalizeText Method  
 
LONG NormalizeText(
  LPCTSTR lpszInput,  
  LONG cchInput,  
  LPTSTR lpszOutput,  
  LONG nMaxLength  
);
LONG NormalizeText(
  LPCTSTR lpszInput,  
  LONG cchInput,  
  CString& strOutput  
);

The NormalizeText method normalizes the input text to replace confusable Unicode homoglyphs, remove symbols and trim whitespace.

Parameters

lpszInput
A null-terminated string which contains the Unicode text that will be normalized. The input may only contain text and cannot include non-printable control characters. If this parameter is NULL or if the input contains binary data, this method will fail. If the ANSI version of this method is called, the input string must be contain ASCII or UTF-8 encoded Unicode characters.
cchInput
An integer which specifies the number of characters in the input string. If this parameter is -1, the length of the string will be determined by counting the number of characters up to a terminating null character.
lpszOutput
A null-terminated string buffer which will contain the normalized text. The buffer must be large enough to contain the entire string and a terminating null character or the method will fail. If this parameter is NULL and the nMaxLength parameter is zero, the method will return the number of characters required to store the normalized text, not including the terminating null character. An alternate version of this method accepts a reference to a CString object which will contain the normalized text when the method returns.
nMaxLength
The maximum number of characters which can be copied into the lpszOutput string buffer. If this value is zero, no characters will be copied to the output buffer and the method will return the length of the filtered output, not including the terminating null character. This can be used to determine how much memory should be allocated to store the filtered text.

Return Value

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.

Remarks

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.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csmsgv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also