Files
mojibake/tests/encoding.c
T
Francesco Bigiarini 6c06fa9150 Add NULL terminated strings (#39)
* Remove MJB_ALLOW_EMBEDDED_NULLS

* Fix: doc

* Add mjb_resolve_input_byte_length and MJB_NUL_TERMINATED

* Remove obsolete tests

* New test

* Fix: example test

* Minor change

* Fix: lint
2026-07-22 22:56:39 +02:00

627 lines
29 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The Mojibake library
*
* This file is distributed under the MIT License. See LICENSE for details.
*/
#include "test.h"
static void assert_encoding_conversion(const char *input, size_t input_size,
mjb_encoding input_encoding, mjb_encoding output_encoding, const char *expected,
size_t expected_size, const char *message) {
mjb_result result;
ATT_ASSERT_STATUS(mjb_convert_encoding(input, input_size, input_encoding,
output_encoding, &result),
MJB_STATUS_OK, message)
ATT_ASSERT(result.output_size, expected_size, message)
ATT_ASSERT(memcmp(result.output, expected, expected_size), 0, message)
if(result.transformed) {
mjb_free(result.output);
}
}
// See API.md for the example.
static mjb_status api_md_examples(void) {
const char *input = "caf\xC3\xA9";
size_t required = 0;
mjb_status status;
// Get the length.
status = mjb_convert_encoding_into(input, strlen(input), MJB_ENC_UTF_8, MJB_ENC_UTF_16LE, NULL,
&required);
if(status != MJB_STATUS_OK) {
return status;
}
char *output = (char *)malloc(required);
if(output == NULL) {
return MJB_STATUS_NO_MEMORY;
}
size_t capacity = required;
status = mjb_convert_encoding_into(input, strlen(input), MJB_ENC_UTF_8, MJB_ENC_UTF_16LE,
output, &capacity);
if(status != MJB_STATUS_OK) {
free(output);
return status;
}
MJB_TEST_COVERAGE(mjb_convert_encoding_into);
ATT_ASSERT(capacity, required, "mjb_convert_encoding_into API.md example")
ATT_ASSERT(capacity, 8, "mjb_convert_encoding_into API.md example capacity")
free(output);
return MJB_STATUS_OK;
}
int test_encoding(void *arg) {
ATT_ASSERT((unsigned int)mjb_detect_encoding(0, 10), (unsigned int)MJB_ENC_UNKNOWN,
"Void unknown string")
ATT_ASSERT((unsigned int)mjb_detect_encoding("", 0), (unsigned int)MJB_ENC_UNKNOWN,
"Void unknown length")
ATT_ASSERT((unsigned int)mjb_detect_encoding(0, 0), (unsigned int)MJB_ENC_UNKNOWN,
"Void unknown string and length")
const char *test1 = "The quick brown fox jumps over the lazy dog";
ATT_ASSERT((unsigned int)mjb_detect_encoding(test1, 43),
(unsigned int)(MJB_ENC_ASCII | MJB_ENC_UTF_8), "Plain ASCII (and UTF-8)")
const char *test2 = "\xEF\xBB\xBFThe quick brown fox jumps over the lazy dog";
ATT_ASSERT((unsigned int)mjb_detect_encoding(test2, 43 + 3), (unsigned int)MJB_ENC_UTF_8,
"UTF-8 BOM")
const char *test3 = "\xFE\xFFThe quick brown fox jumps over the lazy dog";
ATT_ASSERT((unsigned int)mjb_detect_encoding(test3, 43 + 2),
(unsigned int)(MJB_ENC_UTF_16 | MJB_ENC_UTF_16BE), "UTF-16-BE BOM")
const char *test4 = "\xFF\xFEThe quick brown fox jumps over the lazy dog";
ATT_ASSERT((unsigned int)mjb_detect_encoding(test4, 43 + 2),
(unsigned int)(MJB_ENC_UTF_16 | MJB_ENC_UTF_16LE), "UTF-16-LE BOM")
const char *test5 = "\x00\x00\xFE\xFFThe quick brown fox jumps over the lazy dog";
ATT_ASSERT((unsigned int)mjb_detect_encoding(test5, 43 + 4),
(unsigned int)MJB_ENC_UTF_32 | MJB_ENC_UTF_32BE, "UTF-32-BE BOM")
const char *test6 = "\xFF\xFE\x00\x00The quick brown fox jumps over the lazy dog";
ATT_ASSERT((unsigned int)mjb_detect_encoding(test6, 43 + 4),
(unsigned int)(MJB_ENC_UTF_32 | MJB_ENC_UTF_32LE | MJB_ENC_UTF_16 | MJB_ENC_UTF_16LE),
"UTF-32-LE BOM")
ATT_ASSERT(mjb_is_ascii("", 0), false, "Void ASCII string")
ATT_ASSERT(mjb_is_ascii("", 0), false, "Void ASCII length")
ATT_ASSERT(mjb_is_ascii(0, 0), false, "Void ASCII string and length")
const char *test10 = "The quick brown fox jumps over the lazy dog";
ATT_ASSERT(mjb_is_ascii(test10, 43), true, "Valid string and length")
const unsigned char ascii_null_invalid[] = { 'A', '\0', 0x80 };
ATT_ASSERT(mjb_is_ascii((const char *)ascii_null_invalid, sizeof(ascii_null_invalid)),
false, "ASCII rejects non-ASCII after embedded NULL")
ATT_ASSERT(mjb_is_ascii((const char *)ascii_null_invalid, MJB_NUL_TERMINATED), true,
"NUL-terminated ASCII excludes bytes after the terminator")
// \xF0\x9F\x99\x82 = 🙂
const char *test11 = "\xF0\x9F\x99\x82";
ATT_ASSERT(mjb_is_ascii(test11, 5), false, "String with emoji")
const char *test12 = "\x80";
ATT_ASSERT(mjb_is_ascii(test12, 2), false, "Lone continuation byte")
const char *test13 = "\xC0";
ATT_ASSERT(mjb_is_ascii(test13, 2), false, "Lone first 2-bytes sequence")
const char *test14 = "\xE0";
ATT_ASSERT(mjb_is_ascii(test14, 2), false, "Lone first 3-bytes sequence")
const char *test15 = "\xF0";
ATT_ASSERT(mjb_is_ascii(test15, 2), false, "Lone first 4-bytes sequence")
const char *utf8_test = "";
ATT_ASSERT(mjb_is_utf8(NULL, 0), false, "Void UTF-8 string")
ATT_ASSERT(mjb_is_utf8("", 0), false, "Empty UTF-8 \"\" string")
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), false, "Empty UTF-8 string")
utf8_test = "Hello, world!";
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "Simple ASCII")
utf8_test = "Hell\xC3\xB6 w\xC3\xB6rld";
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "Hell[o] wörld")
utf8_test = "\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1\xE3\x81\xAF\xE4\xB8\x96\xE7\x95"
"\x8C"; // こんにちは世界
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "Japanese")
utf8_test = "Hello \xF0\x9F\x8C\x8D";
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "Hello (world)")
utf8_test = "a\xC2\xA2\xE2\x82\xAC\xF0\x90\x8D\x88";
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
"1-byte, 2-byte, 3-byte, and 4-byte characters")
utf8_test = "\xF4\x8F\xBF\xBF";
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
"U+10FFFF, maximum code point")
utf8_test = "\xEF\xBB\xBF"
"Hello";
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
"BOM (Byte Order Mark) followed by \"Hello\"")
utf8_test = "Hello\xC2\xA0World\xE2\x80\x83Test\xE2\x80\x8B";
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
"Various Unicode spaces and invisible characters")
utf8_test = "n\xCC\x83";
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "N combined with tilde")
utf8_test = "A\xCE\x91\xE2\x98\x83\xF0\x9D\x84\x9E";
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
"Characters from various Unicode planes")
utf8_test = "Hello\0World";
ATT_ASSERT(mjb_is_utf8(utf8_test, 11), true, "String with NULL character")
const unsigned char utf8_null_invalid[] = { 'A', '\0', 0xFF };
ATT_ASSERT(mjb_is_utf8((const char *)utf8_null_invalid, sizeof(utf8_null_invalid)),
false, "UTF-8 rejects invalid byte after embedded NULL")
ATT_ASSERT((unsigned int)mjb_detect_encoding((const char *)utf8_null_invalid,
sizeof(utf8_null_invalid)),
(unsigned int)MJB_ENC_UNKNOWN, "Encoding rejects invalid byte after embedded NULL")
ATT_ASSERT(mjb_is_utf8((const char *)utf8_null_invalid, MJB_NUL_TERMINATED), true,
"NUL-terminated UTF-8 excludes bytes after the terminator")
utf8_test = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14"
"\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F"; //
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
"All ASCII control characters")
utf8_test = "Hello\xE2\x80\x94World\xE2\x80\xA2Test\xE2\x99\xA5Unicode\xE2\x98\xAE";
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
"Various Unicode punctuation and symbols")
// UTF-16 tests
ATT_ASSERT(mjb_is_utf16(NULL, 0), false, "Void UTF-16 string")
ATT_ASSERT(mjb_is_utf16("", 0), false, "Empty UTF-16 string")
ATT_ASSERT(mjb_is_utf16("A", 1), false, "Odd-length UTF-16 string")
ATT_ASSERT(mjb_is_utf16("", 1), false, "Odd-length empty UTF-16 string")
// UTF-16BE tests (Big Endian)
const char *utf16be_hello = "\x00H\x00e\x00l\x00l\x00o"; // "Hello" in UTF-16BE
ATT_ASSERT(mjb_is_utf16(utf16be_hello, 10), true, "UTF-16BE Hello")
const char *utf16be_with_bmp = "\x00A\x03\x91\x00Z"; // "AΑZ" (A, Greek Alpha, Z) in UTF-16BE
ATT_ASSERT(mjb_is_utf16(utf16be_with_bmp, 6), true, "UTF-16BE with BMP characters")
// UTF-16LE tests (Little Endian)
const char *utf16le_hello = "H\x00e\x00l\x00l\x00o\x00"; // "Hello" in UTF-16LE
ATT_ASSERT(mjb_is_utf16(utf16le_hello, 10), true, "UTF-16LE Hello")
const char utf16le_terminated[] = { 'H', '\0', 'i', '\0', '\0', '\0' };
ATT_ASSERT(mjb_is_utf16(utf16le_terminated, MJB_NUL_TERMINATED), true,
"NUL-terminated UTF-16LE")
const char *utf16le_with_bmp = "A\x00\x91\x03Z\x00"; // "AΑZ" (A, Greek Alpha, Z) in UTF-16LE
ATT_ASSERT(mjb_is_utf16(utf16le_with_bmp, 6), true, "UTF-16LE with BMP characters")
// UTF-16BE with surrogate pairs (emoji: 🙂 U+1F642)
const char *utf16be_emoji = "\xD8\x3D\xDE\x42"; // 🙂 in UTF-16BE surrogate pair
ATT_ASSERT(mjb_is_utf16(utf16be_emoji, 4), true, "UTF-16BE with surrogate pair")
// UTF-16LE with surrogate pairs (emoji: 🙂 U+1F642)
const char *utf16le_emoji = "\x3D\xD8\x42\xDE"; // 🙂 in UTF-16LE surrogate pair
ATT_ASSERT(mjb_is_utf16(utf16le_emoji, 4), true, "UTF-16LE with surrogate pair")
// UTF-16 with BOM markers
const char *utf16be_bom = "\xFE\xFF\x00H\x00i"; // BOM + "Hi" in UTF-16BE
ATT_ASSERT(mjb_is_utf16(utf16be_bom, 6), true, "UTF-16BE with BOM")
const char *utf16le_bom = "\xFF\xFEH\x00i\x00"; // BOM + "Hi" in UTF-16LE
ATT_ASSERT(mjb_is_utf16(utf16le_bom, 6), true, "UTF-16LE with BOM")
// Edge cases - maximum valid codepoints
const char *utf16be_max = "\xDB\xFF\xDF\xFF"; // U+10FFFF in UTF-16BE
ATT_ASSERT(mjb_is_utf16(utf16be_max, 4), true, "UTF-16BE maximum codepoint")
const char *utf16le_max = "\xFF\xDB\xFF\xDF"; // U+10FFFF in UTF-16LE
ATT_ASSERT(mjb_is_utf16(utf16le_max, 4), true, "UTF-16LE maximum codepoint")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)0, 0, MJB_ENC_UTF_8), 0, "Void buffer")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 1, MJB_ENC_UTF_8), 0, "Wrong size")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)0, 0, MJB_ENC_UTF_16LE), 0, "Void buffer UTF-16LE")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 1, MJB_ENC_UTF_16LE), 0, "Wrong size UTF-16LE")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)0, 0, MJB_ENC_UTF_16BE), 0, "Void buffer UTF-16BE")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 1, MJB_ENC_UTF_16BE), 0, "Wrong size UTF-16BE")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 4, MJB_ENC_UTF_32), 0, "Invalid encoding")
char buffer[5];
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MAX + 1, (char *)buffer, 5, MJB_ENC_UTF_8), 0,
"Noncharacter max")
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MIN - 1, (char *)buffer, 5, MJB_ENC_UTF_8), 0,
"Noncharacter min")
ATT_ASSERT(mjb_codepoint_encode(0xD800, (char *)buffer, 5, MJB_ENC_UTF_8), 0,
"High surrogate UTF-8")
ATT_ASSERT(mjb_codepoint_encode(0xDFFF, (char *)buffer, 5, MJB_ENC_UTF_8), 0,
"Low surrogate UTF-8")
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MAX + 1, (char *)buffer, 5, MJB_ENC_UTF_16LE), 0,
"Noncharacter max UTF-16LE")
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MIN - 1, (char *)buffer, 5, MJB_ENC_UTF_16LE), 0,
"Noncharacter min UTF-16LE")
ATT_ASSERT(mjb_codepoint_encode(0xD800, (char *)buffer, 5, MJB_ENC_UTF_16LE), 0,
"High surrogate UTF-16LE")
ATT_ASSERT(mjb_codepoint_encode(0xDFFF, (char *)buffer, 5, MJB_ENC_UTF_16LE), 0,
"Low surrogate UTF-16LE")
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MAX + 1, (char *)buffer, 5, MJB_ENC_UTF_16BE), 0,
"Noncharacter max UTF-16BE")
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MIN - 1, (char *)buffer, 5, MJB_ENC_UTF_16BE), 0,
"Noncharacter min UTF-16BE")
ATT_ASSERT(mjb_codepoint_encode(0xD800, (char *)buffer, 5, MJB_ENC_UTF_16BE), 0,
"High surrogate UTF-16BE")
ATT_ASSERT(mjb_codepoint_encode(0xDFFF, (char *)buffer, 5, MJB_ENC_UTF_16BE), 0,
"Low surrogate UTF-16BE")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)0, 0, MJB_ENC_UTF_32LE), 0, "Void buffer UTF-32LE")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 4, MJB_ENC_UTF_32LE), 0, "Wrong size UTF-32LE")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)0, 0, MJB_ENC_UTF_32BE), 0, "Void buffer UTF-32BE")
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 4, MJB_ENC_UTF_32BE), 0, "Wrong size UTF-32BE")
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MAX + 1, (char *)buffer, 5, MJB_ENC_UTF_32LE), 0,
"Noncharacter max UTF-32LE")
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MIN - 1, (char *)buffer, 5, MJB_ENC_UTF_32LE), 0,
"Noncharacter min UTF-32LE")
ATT_ASSERT(mjb_codepoint_encode(0xD800, (char *)buffer, 5, MJB_ENC_UTF_32LE), 0,
"High surrogate UTF-32LE")
ATT_ASSERT(mjb_codepoint_encode(0xDFFF, (char *)buffer, 5, MJB_ENC_UTF_32LE), 0,
"Low surrogate UTF-32LE")
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MAX + 1, (char *)buffer, 5, MJB_ENC_UTF_32BE), 0,
"Noncharacter max UTF-32BE")
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MIN - 1, (char *)buffer, 5, MJB_ENC_UTF_32BE), 0,
"Noncharacter min UTF-32BE")
ATT_ASSERT(mjb_codepoint_encode(0xD800, (char *)buffer, 5, MJB_ENC_UTF_32BE), 0,
"High surrogate UTF-32BE")
ATT_ASSERT(mjb_codepoint_encode(0xDFFF, (char *)buffer, 5, MJB_ENC_UTF_32BE), 0,
"Low surrogate UTF-32BE")
ATT_ASSERT(mjb_codepoint_encode(0x0000, (char *)buffer, 5, MJB_ENC_UTF_8), 1, "0x0000 UTF-8")
ATT_ASSERT(buffer[0], 0, "0x0000 UTF-8")
ATT_ASSERT(mjb_codepoint_encode(0x0000, (char *)buffer, 5, MJB_ENC_UTF_16LE), 2,
"0x0000 UTF-16LE")
ATT_ASSERT(buffer[0], 0, "0x0000 UTF-16LE")
ATT_ASSERT(mjb_codepoint_encode(0x0000, (char *)buffer, 5, MJB_ENC_UTF_16BE), 2,
"0x0000 UTF-16BE")
ATT_ASSERT(buffer[0], 0, "0x0000 UTF-16BE")
ATT_ASSERT(mjb_codepoint_encode(0x0000, (char *)buffer, 5, MJB_ENC_UTF_32LE), 4,
"0x0000 UTF-32LE")
ATT_ASSERT(buffer[0], 0, "0x0000 UTF-32LE")
ATT_ASSERT(mjb_codepoint_encode(0x0000, (char *)buffer, 5, MJB_ENC_UTF_32BE), 4,
"0x0000 UTF-32BE")
ATT_ASSERT(buffer[0], 0, "0x0000 UTF-32BE")
ATT_ASSERT(mjb_codepoint_encode(0x007F, (char *)buffer, 5, MJB_ENC_ASCII), 1,
"ASCII encode limit")
ATT_ASSERT(buffer[0], (char)0x7F, "ASCII encode limit value")
ATT_ASSERT(mjb_codepoint_encode(0x0080, (char *)buffer, 5, MJB_ENC_ASCII), 0,
"ASCII encode rejects non-ASCII")
ATT_ASSERT(mjb_codepoint_encode(0x00E9, (char *)buffer, 5, MJB_ENC_ASCII), 0,
"ASCII encode rejects Latin-1")
mjb_encoding encoding;
#define TEST_UTF(CHAR, STR, RES, COMMENT) \
ATT_ASSERT(mjb_codepoint_encode(CHAR, (char *)buffer, 5, encoding), RES, COMMENT) \
ATT_ASSERT((const char *)buffer, STR, COMMENT)
encoding = MJB_ENC_UTF_8;
// UTF-8 tests
TEST_UTF(0x007F, "\x7F", 1, "ASCII limit");
TEST_UTF(0x07FF, "\xDF\xBF", 2, "2-bytes limit");
TEST_UTF(0x1E0A, "\xE1\xB8\x8A", 3, "LATIN CAPITAL LETTER D WITH DOT ABOVE");
TEST_UTF(0xFFFD, "\xEF\xBF\xBD", 3, "3-bytes limit");
TEST_UTF(0x10FFFE, "\xF4\x8F\xBF\xBE", 4, "4-bytes limit");
TEST_UTF(0x1F642, "\xF0\x9F\x99\x82", 4, "SLIGHTLY SMILING FACE");
encoding = MJB_ENC_UTF_16LE;
TEST_UTF(0x007F, "\x7F\x00", 2, "ASCII limit UTF-16LE");
TEST_UTF(0x07FF, "\xFF\x07", 2, "2-bytes limit UTF-16LE");
TEST_UTF(0x1E0A, "\x0A\x1E", 2, "LATIN CAPITAL LETTER D WITH DOT ABOVE UTF-16LE");
TEST_UTF(0xFFFD, "\xFD\xFF", 2, "3-bytes limit UTF-16LE");
TEST_UTF(0x10FFFE, "\xFF\xDB\xFE\xDF", 4, "4-bytes limit UTF-16LE");
TEST_UTF(0x1F642, "\x3D\xD8\x42\xDE", 4, "SLIGHTLY SMILING FACE UTF-16LE");
encoding = MJB_ENC_UTF_16BE;
TEST_UTF(0x007F, "\x00\x7F", 2, "ASCII limit UTF-16BE");
TEST_UTF(0x07FF, "\x07\xFF", 2, "2-bytes limit UTF-16BE");
TEST_UTF(0x1E0A, "\x1E\x0A", 2, "LATIN CAPITAL LETTER D WITH DOT ABOVE UTF-16BE");
TEST_UTF(0xFFFD, "\xFF\xFD", 2, "3-bytes limit UTF-16BE");
TEST_UTF(0x10FFFE, "\xDB\xFF\xDF\xFE", 4, "4-bytes limit UTF-16BE");
TEST_UTF(0x1F642, "\xD8\x3D\xDE\x42", 4, "SLIGHTLY SMILING FACE UTF-16BE");
encoding = MJB_ENC_UTF_32LE;
TEST_UTF(0x007F, "\x7F\x00\x00\x00", 4, "ASCII limit UTF-32LE");
TEST_UTF(0x07FF, "\xFF\x07\x00\x00", 4, "2-bytes limit UTF-32LE");
TEST_UTF(0x1E0A, "\x0A\x1E\x00\x00", 4, "LATIN CAPITAL LETTER D WITH DOT ABOVE UTF-32LE");
TEST_UTF(0xFFFD, "\xFD\xFF\x00\x00", 4, "3-bytes limit UTF-32LE");
TEST_UTF(0x10FFFE, "\xFE\xFF\x10\x00", 4, "4-bytes limit UTF-32LE");
TEST_UTF(0x1F642, "\x42\xF6\x01\x00", 4, "SLIGHTLY SMILING FACE UTF-32LE");
encoding = MJB_ENC_UTF_32BE;
TEST_UTF(0x007F, "\x00\x00\x00\x7F", 4, "ASCII limit UTF-32BE");
TEST_UTF(0x07FF, "\x00\x00\x07\xFF", 4, "2-bytes limit UTF-32BE");
TEST_UTF(0x1E0A, "\x00\x00\x1E\x0A", 4, "LATIN CAPITAL LETTER D WITH DOT ABOVE UTF-32BE");
TEST_UTF(0xFFFD, "\x00\x00\xFF\xFD", 4, "3-bytes limit UTF-32BE");
TEST_UTF(0x10FFFE, "\x00\x10\xFF\xFE", 4, "4-bytes limit UTF-32BE");
TEST_UTF(0x1F642, "\x00\x01\xF6\x42", 4, "SLIGHTLY SMILING FACE UTF-32BE");
#undef TEST_UTF
const char *hello_strings[] = {
"Hello", // UTF-8
"\x48\x00\x65\x00\x6C\x00\x6C\x00\x6F\x00", // UTF-16LE
"\x00\x48\x00\x65\x00\x6C\x00\x6C\x00\x6F", // UTF-16BE
"\x48\x00\x00\x00\x65\x00\x00\x00\x6C\x00\x00\x00\x6C\x00\x00\x00\x6F\x00\x00"
"\x00", // UTF-32LE
"\x00\x00\x00\x48\x00\x00\x00\x65\x00\x00\x00\x6C\x00\x00\x00\x6C\x00\x00\x00"
"\x6F" // UTF-32BE
};
size_t hello_strings_sizes[] = { 5, 10, 10, 20, 20 };
mjb_encoding encodings[] = { MJB_ENC_UTF_8, MJB_ENC_UTF_16LE, MJB_ENC_UTF_16BE,
MJB_ENC_UTF_32LE, MJB_ENC_UTF_32BE };
const char *output_encodings[] = { "UTF-8", "UTF-16LE", "UTF-16BE", "UTF-32LE", "UTF-32BE" };
char test_description[64];
mjb_result boundary_result;
ATT_ASSERT_STATUS(mjb_convert_encoding(NULL, 1, MJB_ENC_UTF_8, MJB_ENC_UTF_16LE,
&boundary_result),
MJB_STATUS_INVALID_ARGUMENT, "Convert encoding rejects NULL buffer")
ATT_ASSERT_STATUS(mjb_convert_encoding("", 0, MJB_ENC_UTF_8, MJB_ENC_UTF_16LE, NULL),
MJB_STATUS_INVALID_ARGUMENT, "Convert encoding rejects NULL result")
MJB_TEST_COVERAGE(mjb_convert_encoding_into);
size_t into_size = 123;
ATT_ASSERT_STATUS(mjb_convert_encoding_into(NULL, 1, MJB_ENC_UTF_8, MJB_ENC_UTF_16LE, NULL,
&into_size),
MJB_STATUS_INVALID_ARGUMENT, "Convert encoding into rejects NULL buffer")
ATT_ASSERT(into_size, (size_t)0, "Convert encoding into resets size for invalid input")
ATT_ASSERT_STATUS(mjb_convert_encoding_into("A", 1, MJB_ENC_UTF_8, MJB_ENC_UTF_16LE, NULL,
NULL),
MJB_STATUS_INVALID_ARGUMENT, "Convert encoding into rejects NULL output size")
const char into_input[] = "caf\xC3\xA9";
const unsigned char into_expected[] = { 'c', 0, 'a', 0, 'f', 0, 0xE9, 0 };
into_size = 0;
ATT_ASSERT_STATUS(mjb_convert_encoding_into(into_input, strlen(into_input), MJB_ENC_UTF_8,
MJB_ENC_UTF_16LE, NULL, &into_size),
MJB_STATUS_OK, "Query encoding conversion output size")
ATT_ASSERT(into_size, sizeof(into_expected), "Encoding conversion required payload size")
unsigned char into_output[sizeof(into_expected) + 1];
memset(into_output, 0xA5, sizeof(into_output));
into_size = sizeof(into_expected) - 1;
ATT_ASSERT_STATUS(mjb_convert_encoding_into(into_input, strlen(into_input), MJB_ENC_UTF_8,
MJB_ENC_UTF_16LE, into_output, &into_size),
MJB_STATUS_OUTPUT_TOO_SMALL, "Convert encoding reports a small output buffer")
ATT_ASSERT(into_size, sizeof(into_expected), "Small output buffer reports required size")
unsigned char untouched_output[sizeof(into_output)];
memset(untouched_output, 0xA5, sizeof(untouched_output));
ATT_ASSERT(memcmp(into_output, untouched_output, sizeof(into_output)), 0,
"Small output buffer is not modified")
into_size = sizeof(into_expected);
ATT_ASSERT_STATUS(mjb_convert_encoding_into(into_input, strlen(into_input), MJB_ENC_UTF_8,
MJB_ENC_UTF_16LE, into_output, &into_size),
MJB_STATUS_OK, "Convert encoding into exact-size output buffer")
ATT_ASSERT(into_size, sizeof(into_expected), "Convert encoding into written payload size")
ATT_ASSERT(memcmp(into_output, into_expected, sizeof(into_expected)), 0,
"Convert encoding into output bytes")
ATT_ASSERT((unsigned int)into_output[sizeof(into_expected)], 0xA5u,
"Convert encoding into does not write a terminator")
unsigned char same_encoding_output[] = { 0, 0xA5 };
into_size = 1;
ATT_ASSERT_STATUS(mjb_convert_encoding_into("A", 1, MJB_ENC_UTF_8, MJB_ENC_UTF_8,
same_encoding_output, &into_size),
MJB_STATUS_OK, "Convert encoding into copies unchanged encoding")
ATT_ASSERT((unsigned int)same_encoding_output[0], (unsigned int)'A',
"Convert encoding into unchanged byte")
ATT_ASSERT((unsigned int)same_encoding_output[1], 0xA5u,
"Unchanged encoding does not write a terminator")
const char utf16le_ascii[] = { 'e', '\0', 'n', '\0' };
mjb_result ascii_result;
ATT_ASSERT_STATUS(mjb_convert_encoding(utf16le_ascii, sizeof(utf16le_ascii),
MJB_ENC_UTF_16LE, MJB_ENC_ASCII, &ascii_result),
MJB_STATUS_OK, "Convert UTF-16LE ASCII text to ASCII")
ATT_ASSERT(ascii_result.transformed, true, "Convert UTF-16LE ASCII text is transformed")
ATT_ASSERT(ascii_result.output_size, (size_t)2, "Convert UTF-16LE ASCII text size")
ATT_ASSERT(ascii_result.output, "en", "Convert UTF-16LE ASCII text output")
mjb_free(ascii_result.output);
ATT_ASSERT_STATUS(mjb_convert_encoding("\xC3\xA9", 2, MJB_ENC_UTF_8, MJB_ENC_ASCII,
&ascii_result),
MJB_STATUS_UNSUPPORTED, "Convert UTF-8 non-ASCII text to ASCII")
const char utf16le_smile[] = { '\x3D', '\xD8', '\x42', '\xDE' };
const char utf16be_smile[] = { '\xD8', '\x3D', '\xDE', '\x42' };
ATT_ASSERT_STATUS(mjb_convert_encoding(utf16le_smile, sizeof(utf16le_smile),
MJB_ENC_UTF_16LE, MJB_ENC_UTF_8, &ascii_result),
MJB_STATUS_OK, "Convert UTF-16LE surrogate pair to UTF-8")
ATT_ASSERT(ascii_result.output_size, (size_t)4, "Convert UTF-16LE surrogate pair to UTF-8 size")
ATT_ASSERT(memcmp(ascii_result.output, "\xF0\x9F\x99\x82", ascii_result.output_size), 0,
"Convert UTF-16LE surrogate pair to UTF-8 output")
mjb_free(ascii_result.output);
ATT_ASSERT_STATUS(mjb_convert_encoding(utf16be_smile, sizeof(utf16be_smile),
MJB_ENC_UTF_16BE, MJB_ENC_UTF_8, &ascii_result),
MJB_STATUS_OK, "Convert UTF-16BE surrogate pair to UTF-8")
ATT_ASSERT(ascii_result.output_size, (size_t)4, "Convert UTF-16BE surrogate pair to UTF-8 size")
ATT_ASSERT(memcmp(ascii_result.output, "\xF0\x9F\x99\x82", ascii_result.output_size), 0,
"Convert UTF-16BE surrogate pair to UTF-8 output")
mjb_free(ascii_result.output);
const char utf8_bom_a[] = { '\xEF', '\xBB', '\xBF', 'A' };
const char utf16be_bom_a[] = { '\xFE', '\xFF', '\x00', 'A' };
const char utf16le_bom_a[] = { '\xFF', '\xFE', 'A', '\x00' };
const char utf16be_bom_a_inner_bom_b[] = {
'\xFE',
'\xFF',
'\x00',
'A',
'\xFE',
'\xFF',
'\x00',
'B',
};
const char utf32be_bom_a[] = {
'\x00',
'\x00',
'\xFE',
'\xFF',
'\x00',
'\x00',
'\x00',
'A',
};
const char utf32le_bom_a[] = {
'\xFF',
'\xFE',
'\x00',
'\x00',
'A',
'\x00',
'\x00',
'\x00',
};
const char utf16be_plain_a[] = { '\x00', 'A' };
mjb_encoding detected_utf16be = mjb_detect_encoding(utf16be_bom_a, sizeof(utf16be_bom_a));
mjb_encoding detected_utf16le = mjb_detect_encoding(utf16le_bom_a, sizeof(utf16le_bom_a));
mjb_encoding detected_utf32be = mjb_detect_encoding(utf32be_bom_a, sizeof(utf32be_bom_a));
mjb_encoding detected_utf32le = mjb_detect_encoding(utf32le_bom_a, sizeof(utf32le_bom_a));
assert_encoding_conversion(utf16be_bom_a, sizeof(utf16be_bom_a), detected_utf16be,
MJB_ENC_UTF_8, "A", 1, "Convert detected UTF-16BE BOM consumes signature");
assert_encoding_conversion(utf16le_bom_a, sizeof(utf16le_bom_a), detected_utf16le,
MJB_ENC_UTF_8, "A", 1, "Convert detected UTF-16LE BOM consumes signature");
assert_encoding_conversion(utf32be_bom_a, sizeof(utf32be_bom_a), detected_utf32be,
MJB_ENC_UTF_8, "A", 1, "Convert detected UTF-32BE BOM consumes signature");
assert_encoding_conversion(utf32le_bom_a, sizeof(utf32le_bom_a), detected_utf32le,
MJB_ENC_UTF_8, "A", 1, "Convert detected UTF-32LE BOM consumes signature");
assert_encoding_conversion(utf16be_bom_a, sizeof(utf16be_bom_a), MJB_ENC_UTF_16, MJB_ENC_UTF_8,
"A", 1, "Convert generic UTF-16BE BOM consumes signature");
assert_encoding_conversion(utf16le_bom_a, sizeof(utf16le_bom_a), MJB_ENC_UTF_16, MJB_ENC_UTF_8,
"A", 1, "Convert generic UTF-16LE BOM consumes signature");
assert_encoding_conversion(utf16be_bom_a_inner_bom_b, sizeof(utf16be_bom_a_inner_bom_b),
MJB_ENC_UTF_16, MJB_ENC_UTF_8,
"A\xEF\xBB\xBF"
"B",
5, "Convert generic UTF-16 preserves inner U+FEFF");
assert_encoding_conversion(utf32be_bom_a, sizeof(utf32be_bom_a), MJB_ENC_UTF_32, MJB_ENC_UTF_8,
"A", 1, "Convert generic UTF-32BE BOM consumes signature");
assert_encoding_conversion(utf32le_bom_a, sizeof(utf32le_bom_a), MJB_ENC_UTF_32, MJB_ENC_UTF_8,
"A", 1, "Convert generic UTF-32LE BOM consumes signature");
assert_encoding_conversion(utf16be_bom_a, sizeof(utf16be_bom_a), MJB_ENC_UTF_16BE,
MJB_ENC_UTF_8, utf8_bom_a, sizeof(utf8_bom_a),
"Convert explicit UTF-16BE preserves U+FEFF");
assert_encoding_conversion(utf16le_bom_a, sizeof(utf16le_bom_a), MJB_ENC_UTF_16LE,
MJB_ENC_UTF_8, utf8_bom_a, sizeof(utf8_bom_a),
"Convert explicit UTF-16LE preserves U+FEFF");
assert_encoding_conversion(utf32be_bom_a, sizeof(utf32be_bom_a), MJB_ENC_UTF_32BE,
MJB_ENC_UTF_8, utf8_bom_a, sizeof(utf8_bom_a),
"Convert explicit UTF-32BE preserves U+FEFF");
assert_encoding_conversion(utf32le_bom_a, sizeof(utf32le_bom_a), MJB_ENC_UTF_32LE,
MJB_ENC_UTF_8, utf8_bom_a, sizeof(utf8_bom_a),
"Convert explicit UTF-32LE preserves U+FEFF");
MJB_TEST_COVERAGE(mjb_count_codepoints);
ATT_ASSERT(mjb_count_codepoints(utf16be_bom_a, sizeof(utf16be_bom_a), MJB_ENC_UTF_16), (size_t)1,
"Length generic UTF-16BE BOM consumes signature")
ATT_ASSERT(mjb_count_codepoints(utf16be_bom_a, sizeof(utf16be_bom_a), MJB_ENC_UTF_16BE), (size_t)2,
"Length explicit UTF-16BE preserves U+FEFF")
ATT_ASSERT(mjb_count_codepoints(utf32le_bom_a, sizeof(utf32le_bom_a), detected_utf32le), (size_t)1,
"Length detected UTF-32LE BOM consumes signature")
ATT_ASSERT(mjb_count_codepoints(utf32le_bom_a, sizeof(utf32le_bom_a), MJB_ENC_UTF_32LE), (size_t)2,
"Length explicit UTF-32LE preserves U+FEFF")
ATT_ASSERT_STATUS(mjb_convert_encoding(utf16be_plain_a, sizeof(utf16be_plain_a),
MJB_ENC_UTF_16, MJB_ENC_UTF_8, &ascii_result),
MJB_STATUS_INVALID_ENCODING,
"Convert generic UTF-16 without BOM rejects unknown byte order")
ATT_ASSERT_STATUS(mjb_convert_encoding("A", 1, MJB_ENC_UTF_8, MJB_ENC_UTF_16,
&ascii_result),
MJB_STATUS_INVALID_ENCODING, "Convert generic UTF-16 output rejects unknown byte order")
MJB_TEST_COVERAGE(mjb_convert_encoding);
for(size_t from = 0; from < 5; ++from) {
for(size_t to = 0; to < 5; ++to) {
mjb_result convert_result;
mjb_status status = mjb_convert_encoding(hello_strings[from], hello_strings_sizes[from],
encodings[from], encodings[to], &convert_result);
snprintf(test_description, 64, "%s to %s", output_encodings[from],
output_encodings[to]);
ATT_ASSERT_STATUS(status, MJB_STATUS_OK, test_description)
if(from == to) {
snprintf(test_description, 64, "%s to %s is not transformed",
output_encodings[from], output_encodings[to]);
ATT_ASSERT(convert_result.transformed, false, test_description)
ATT_ASSERT((void *)convert_result.output, (void *)hello_strings[from],
test_description)
ATT_ASSERT(convert_result.output_size, hello_strings_sizes[from], test_description)
} else {
snprintf(test_description, 64, "%s to %s is transformed", output_encodings[from],
output_encodings[to]);
ATT_ASSERT(convert_result.transformed, true, test_description)
ATT_ASSERT(memcmp(convert_result.output, hello_strings[to],
convert_result.output_size),
0, test_description)
}
if(convert_result.transformed) {
mjb_free(convert_result.output);
}
}
}
ATT_ASSERT_STATUS(api_md_examples(), MJB_STATUS_OK, "API MD examples");
return 0;
}