Description

utf8rewind is a cross-platform and open source C library designed to extend the default string handling functions and add support for UTF-8 encoded text.

Download

utf8rewind-1.2.0.zip (3.15 MB)

Clone in Mercurial

hg clone https://bitbucket.org/knight666/utf8rewind utf8rewind

Summary

This major release of utf8rewind adds case mapping and normalization functionality. Both case mapping and normalization make use of the mappings outlined in the Unicode 7.0 standard.

Case mapping

When case mapping, every codepoint must be checked against a database and replaced by one or more codepoints if it has an entry. This "one or more codepoints" is what trips most developers up. For example, when uppercasing U+00DF (LATIN SMALL LETTER SHARP S), the codepoint should not be not converted to a single "LATIN CAPITAL LETTER SHARP S", but two "LATIN CAPITAL LETTER S" instead. As a rule of thumb, codepoints will never expand to more than three times its original size in memory when case mapping. In practice, it's advised to either use a large fixed-size buffer or to determine the buffer size first before attempting to do a casemapping conversion.

Version 1.2.0 of utf8rewind adds three forms of case mapping: uppercase, lowercase and titlecase.

Normalization

Normalization of Unicode strings is one of the most difficult subjects in string handling. It is a whirlwind of unfamiliar terms, requirements that make little sense at first inspection and maddening edge-cases. Of the five months utf8rewind 1.2.0 has been in development, most of that time was spent on writing and rewriting the normalization implementation.

In simple terms, normalization allows you to compare strings even if their combining marks are out of order. A string like "U+00C8" is equivalent to "U+0045 U+0300" when both are normalized, because they are canonically equivalent.

What's important to note is the current implementation does not allocate any memory on the heap. Normalization uses only stack memory, which means that it can be used even if you don't use a standard memory allocator like malloc .

The library allows you to normalize strings in four ways: composition (NFC), decomposition(NFD), compatibility composition (NFKC) and compatibility decomposition (NFKD). Before normalizing a string, you can use utf8isnormalized to check whether a string needs to be normalized.

Back-end

On the back-end, most functions have been refactored and the library has been split into multiple source files. This was necessary due to the inclusion of a Unicode database, which is generated from Unicode data and stored as static const arrays. The database would have dominated the line count in the library's implementation if it had been stored in the same source file. Each private function has received its own test suite, causing the number of tests to increase dramatically. While utf8rewind 1.1.1 had 467 unit and integration tests, utf8rewind 1.2.0 has 1586. The upside is that the library is now much more secure. A number of bugs have been found and fixed in these private functions, which were used by the previous version as well.

Running the (backwards-compatible) 1.2.0 tests on the 1.1.1 implementation paints a positive picture. Out of 646 compatible tests, 85% pass muster.

Of the failed tests, the vast majority (79%) relate to the encoding and decoding of codepoints. This is due to the addition of checks for overlapping parameters.

Bug fixes

A major bug was found in the implementation of codepoint_read . Bytes below 0x80 were not treated as invalid continuation bytes. This allowed the creation of an invalid multi byte sequence which would "eat up" valid bytes. Similarly, utf8len did not check for invalid continuation bytes above 0xBF.

An important addition for this release is the inclusion of checks for overlapping parameters. This check makes sure that the input and output buffers specified by the users don't overlap in memory, which could cause heap corruptions or the execution of arbitrary code.

Another bug (reported by Marcel Smit) was that error codes were not reset if execution succeeded. This meant that users could not rely on the error code result in all cases. This has been fixed by the inclusion of an UTF8_ERR_NONE error code, which is set when the function succeeds.

Interface

Added utf8toupper function.

function. Added utf8tolower function.

function. Added utf8totitle function.

function. Added utf8isnormalized function.

function. Added utf8normalize function.

function. Added UTF8_ERR_NONE error code.

error code. Added UTF8_ERR_INVALID_FLAG error code.

error code. Added UTF8_ERR_OVERLAPPING_PARAMETERS error code.

error code. Added UTF8_NORMALIZE_COMPOSE flag.

flag. Added UTF8_NORMALIZE_DECOMPOSE flag.

flag. Added UTF8_NORMALIZE_COMPATIBILITY flag.

flag. Added UTF8_NORMALIZATION_RESULT_YES result.

result. Added UTF8_NORMALIZATION_RESULT_MAYBE result.

result. Added UTF8_NORMALIZATION_RESULT_NO result.

result. Added UTF8_API define.

define. Removed UTF8_ERR_UNMATCHED_HIGH_SURROGATE_PAIR error code.

error code. Removed UTF8_ERR_UNMATCHED_LOW_SURROGATE_PAIR error code.

Bug fixes

utf16toutf8: The input size can no longer be overridden with a 0 byte in the input.

utf16toutf8: Invalid surrogate pairs no longer stop the conversion, but are converted to replacement characters.

utf16toutf8: Fixed bug where overlapping parameters were not detected.

utf16toutf8: Errors parameter is now always set to UTF8_ERR_NONE if the function succeeded.

if the function succeeded. utf32toutf8: The input size can no longer be overridden with a 0 byte in the input.

utf32toutf8: Invalid surrogate pairs no longer stop the conversion, but are converted to replacement characters.

utf32toutf8: Fixed bug where overlapping parameters were not detected.

utf32toutf8: Errors parameter is now always set to UTF8_ERR_NONE if the function succeeded.

if the function succeeded. utf8toutf16: Fixed bug where overlapping parameters were not detected.

utf8toutf16: Errors parameter is now always set to UTF8_ERR_NONE if the function succeeded.

if the function succeeded. utf8toutf32: Fixed bug where overlapping parameters were not detected.

utf8toutf32: Errors parameter is now always set to UTF8_ERR_NONE if the function succeeded.

Test suites