From f9292a847726df597a40672c0a2fd6a2358a2a2b Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Mon, 23 Nov 2026 12:10:35 -0800 Subject: [PATCH] udf: decode 8-bit CS0 names as Latin-1 to UTF-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OSTA CS0 with compression ID 8 stores one byte per character, and those bytes are the Unicode code points U+0000..U+00FF, i.e. Latin-1. udf_decode_cs0() and udf_decode_dstring() copied them verbatim into node names and the volume name, so a non-ASCII name produced invalid UTF-8 even though odfs_node_t names are UTF-8 and the 16-bit CS0 path already converts to UTF-8. Encode bytes >= 0x80 as two-byte UTF-8 sequences, and let udf_decode_dstring() reuse udf_decode_cs0() instead of carrying its own copy of the decoder. This is a user-visible change on AmigaOS: the Amiga frontend copies node names into the FileInfoBlock and ExAllData unconverted, and the system charset there is Latin-1. 8-bit UDF names, which display correctly today, would then show as "Müller" instead of "Müller", the same way Joliet, HFS and 16-bit CS0 names already appear. Host tools that expect UTF-8 get valid names. --- backends/udf/udf.c | 82 +++++++++++++++++++++---------------------- tests/unit/test_udf.c | 39 ++++++++++++++++++++ 2 files changed, 80 insertions(+), 41 deletions(-) diff --git a/backends/udf/udf.c b/backends/udf/udf.c index 4af7fed..90f8ff3 100644 --- a/backends/udf/udf.c +++ b/backends/udf/udf.c @@ -49,71 +49,71 @@ static __attribute__((noinline)) uint16_t udf_tag_id(const uint8_t *data) /* OSTA CS0 compressed Unicode → UTF-8 */ /* ------------------------------------------------------------------ */ -static void udf_decode_dstring(const uint8_t *src, size_t field_len, - char *dst, size_t dst_size) +/* + * Decode a OSTA CS0 identifier (from FID name field). + * Unlike d-strings, these don't have the length byte at the end. + * Returns the length of the UTF-8 result. + */ +static size_t udf_decode_cs0(const uint8_t *src, size_t len, + char *dst, size_t dst_size) { - if (field_len == 0 || dst_size == 0) { + if (len == 0 || dst_size == 0) { if (dst_size > 0) dst[0] = '\0'; - return; - } - - /* last byte of d-string is the used length */ - size_t used = src[field_len - 1]; - if (used == 0 || used > field_len - 1) { - dst[0] = '\0'; - return; + return 0; } uint8_t comp_id = src[0]; /* 8 = Latin-1, 16 = UCS-2 */ size_t di = 0; if (comp_id == 8) { - /* 8-bit characters (Latin-1) */ - for (size_t i = 1; i < used && di + 1 < dst_size; i++) - dst[di++] = (char)src[i]; + /* 8-bit characters are Latin-1, i.e. U+0000..U+00FF */ + for (size_t i = 1; i < len; i++) { + uint8_t c = src[i]; + + if (c < 0x80) { + if (di + 1 >= dst_size) + break; + dst[di++] = (char)c; + } else { + if (di + 2 >= dst_size) + break; + dst[di++] = (char)(0xC0u | (c >> 6)); + dst[di++] = (char)(0x80u | (c & 0x3Fu)); + } + } } else if (comp_id == 16) { /* 16-bit UCS-2 BE */ - odfs_ucs2be_to_utf8(src + 1, used - 1, + odfs_ucs2be_to_utf8(src + 1, len - 1, dst, dst_size, &di); } else { /* unknown compression, copy raw */ - for (size_t i = 1; i < used && di + 1 < dst_size; i++) + for (size_t i = 1; i < len && di + 1 < dst_size; i++) dst[di++] = (char)src[i]; } - dst[di] = '\0'; - - /* trim trailing spaces */ - while (di > 0 && dst[di - 1] == ' ') - dst[--di] = '\0'; + return di; } -/* - * Decode a OSTA CS0 identifier (from FID name field). - * Unlike d-strings, these don't have the length byte at the end. - */ -static void udf_decode_cs0(const uint8_t *src, size_t len, - char *dst, size_t dst_size) +static void udf_decode_dstring(const uint8_t *src, size_t field_len, + char *dst, size_t dst_size) { - if (len == 0 || dst_size == 0) { + if (field_len == 0 || dst_size == 0) { if (dst_size > 0) dst[0] = '\0'; return; } - uint8_t comp_id = src[0]; - size_t di = 0; - - if (comp_id == 8) { - for (size_t i = 1; i < len && di + 1 < dst_size; i++) - dst[di++] = (char)src[i]; - } else if (comp_id == 16) { - odfs_ucs2be_to_utf8(src + 1, len - 1, - dst, dst_size, &di); - } else { - for (size_t i = 1; i < len && di + 1 < dst_size; i++) - dst[di++] = (char)src[i]; + /* last byte of d-string is the used length */ + size_t used = src[field_len - 1]; + if (used == 0 || used > field_len - 1) { + dst[0] = '\0'; + return; } - dst[di] = '\0'; + + size_t di = udf_decode_cs0(src, used, dst, dst_size); + + /* trim trailing spaces */ + while (di > 0 && dst[di - 1] == ' ') + dst[--di] = '\0'; } /* ------------------------------------------------------------------ */ diff --git a/tests/unit/test_udf.c b/tests/unit/test_udf.c index a7a8e03..4003825 100644 --- a/tests/unit/test_udf.c +++ b/tests/unit/test_udf.c @@ -1631,4 +1631,43 @@ TEST(udf_metadata_file_too_fragmented_uses_mirror) odfs_cache_destroy(&cache); } +TEST(udf_cs0_8bit_name_decodes_latin1_to_utf8) +{ + udf_mock_media_t mock; + odfs_media_t media; + odfs_cache_t cache; + odfs_log_state_t log; + udf_context_t ctx; + odfs_node_t dir; + odfs_node_t out; + uint8_t fid[44]; + + memset(&mock, 0, sizeof(mock)); + memset(&ctx, 0, sizeof(ctx)); + memset(&dir, 0, sizeof(dir)); + memset(&out, 0, sizeof(out)); + + udf_make_media(&media, &mock); + ASSERT_OK(odfs_cache_init(&cache, &media, 4)); + odfs_log_init(&log); + + udf_make_fe(mock.sectors[0], UDF_ICB_FILETYPE_DIR, sizeof(fid), 1); + udf_make_fe(mock.sectors[3], UDF_ICB_FILETYPE_FILE, 5, 5); + + /* 8-bit CS0 "caf\xE9" (Latin-1 e-acute) */ + memset(fid, 0, sizeof(fid)); + udf_make_fid(fid, "caf\xE9", 3); + memcpy(&mock.sectors[1][0], fid, sizeof(fid)); + + dir.backend = ODFS_BACKEND_UDF; + dir.kind = ODFS_NODE_DIR; + dir.extent.lba = 0; + + ASSERT_OK(udf_backend_ops.lookup(&ctx, &cache, &log, &dir, + "caf\xC3\xA9", &out)); + ASSERT_STR_EQ(out.name, "caf\xC3\xA9"); + + odfs_cache_destroy(&cache); +} + TEST_MAIN()