From f0ea84bca63515ead38b9199f3cb84d8b6bbdb5d Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Mon, 23 Nov 2026 22:57:59 -0800 Subject: [PATCH 1/6] charset: add an in-place UTF-8 to Latin-1 converter The backends decode every name to UTF-8, but AmigaOS shows and compares names in an 8-bit character set, so a Joliet, UDF or HFS name with an umlaut comes out as two wrong characters there. Add odfs_utf8_to_latin1(), which rewrites a string in place: Latin-1 never needs more bytes than UTF-8. A character outside Latin-1 becomes '?', and so do U+0080 to U+009F, which are control codes that the Amiga console would act on. A byte that is not part of a well-formed sequence is copied as it is. Rock Ridge names written by Amiga mastering tools are raw Latin-1, and they must keep working once the handler converts names. Overlong forms count as malformed, so that none can produce a NUL or a '/'. Nothing calls the function yet. It is built when ODFS_FEATURE_CHARSET_BUILTIN is set, which so far had no effect. --- core/charset.c | 37 ++++++++++++++ include/odfs/charset.h | 14 ++++++ tests/unit/test_charset.c | 101 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) diff --git a/core/charset.c b/core/charset.c index 6f82797..7d2aa75 100644 --- a/core/charset.c +++ b/core/charset.c @@ -179,6 +179,43 @@ odfs_err_t odfs_iso_name_to_display(const char *src, size_t src_len, return ODFS_OK; } +#if ODFS_FEATURE_CHARSET_BUILTIN +size_t odfs_utf8_to_latin1(char *s) +{ + const unsigned char *src = (const unsigned char *)s; + unsigned char *dst = (unsigned char *)s; + unsigned int c; + + while ((c = *src++) != 0) { + /* 0xC2-0xF4 start a sequence of two to four bytes */ + if (c >= 0xC2u && c <= 0xF4u) { + unsigned int more = c < 0xE0u ? 1u : c < 0xF0u ? 2u : 3u; + unsigned int cp = c & (0x3Fu >> more); + unsigned int i; + + for (i = 0; i < more && (src[i] & 0xC0u) == 0x80u; i++) + cp = (cp << 6) | (src[i] & 0x3Fu); + + /* + * Take the sequence only if it is complete and not an + * overlong form: one of three bytes encodes at least + * U+0800, one of four at least U+10000. Otherwise the + * lead byte is copied below like any other byte. + */ + if (i == more && (cp >> (5u * more + 1u)) != 0) { + src += more; + /* U+0080-U+009F are control codes, CSI among them */ + c = (cp >= 0xA0u && cp <= 0xFFu) ? cp : '?'; + } + } + *dst++ = (unsigned char)c; + } + + *dst = '\0'; + return (size_t)(dst - (unsigned char *)s); +} +#endif + #if !defined(AMIGA) void odfs_sanitize_name(char *name, size_t len, char replacement) { diff --git a/include/odfs/charset.h b/include/odfs/charset.h index 43c0f8e..1451866 100644 --- a/include/odfs/charset.h +++ b/include/odfs/charset.h @@ -7,6 +7,7 @@ #ifndef ODFS_CHARSET_H #define ODFS_CHARSET_H +#include "odfs/config.h" #include "odfs/error.h" #include #include @@ -54,6 +55,19 @@ odfs_err_t odfs_iso_name_to_display(const char *src, size_t src_len, char *dst, size_t dst_size, int lowercase); +#if ODFS_FEATURE_CHARSET_BUILTIN +/* + * Convert a UTF-8 string to ISO 8859-1 (Latin-1) in place and return + * its new length; the result is never longer than the input. + * + * A character Latin-1 does not have becomes '?'. A byte that is not + * part of a well-formed UTF-8 sequence is kept as it is, so a string + * that already is in an 8-bit character set, such as a Rock Ridge name + * written by an Amiga mastering tool, passes through unchanged. + */ +size_t odfs_utf8_to_latin1(char *s); +#endif + #if !defined(AMIGA) /* * Host-only fallback: replace non-printable / non-Amiga-safe diff --git a/tests/unit/test_charset.c b/tests/unit/test_charset.c index 30f981c..8e4a6b6 100644 --- a/tests/unit/test_charset.c +++ b/tests/unit/test_charset.c @@ -161,4 +161,105 @@ TEST(sanitize_name_controls) ASSERT_STR_EQ(name, "foo__bar_baz"); } +TEST(utf8_to_latin1_ascii) +{ + char s[] = "README.TXT"; + + ASSERT_EQ(odfs_utf8_to_latin1(s), 10); + ASSERT_STR_EQ(s, "README.TXT"); +} + +TEST(utf8_to_latin1_converts_latin1_range) +{ + /* "Gr" U+00FC U+00DF "e" U+00A0 U+00FF */ + char s[] = "Gr\xC3\xBC\xC3\x9F" "e\xC2\xA0\xC3\xBF"; + + ASSERT_EQ(odfs_utf8_to_latin1(s), 7); + ASSERT_STR_EQ(s, "Gr\xFC\xDF" "e\xA0\xFF"); +} + +TEST(utf8_to_latin1_replaces_other_characters) +{ + /* U+0100, U+20AC, U+65E5 U+672C, U+1F600: one '?' each */ + char s[] = "a\xC4\x80" "b\xE2\x82\xAC" "c\xE6\x97\xA5\xE6\x9C\xAC" + "d\xF0\x9F\x98\x80" "e"; + + ASSERT_EQ(odfs_utf8_to_latin1(s), 10); + ASSERT_STR_EQ(s, "a?b?c??d?e"); +} + +TEST(utf8_to_latin1_replaces_c1_controls) +{ + /* U+0080, U+009B (CSI), U+009F */ + char s[] = "\xC2\x80\xC2\x9B\xC2\x9F"; + + ASSERT_EQ(odfs_utf8_to_latin1(s), 3); + ASSERT_STR_EQ(s, "???"); +} + +/* a name that is Latin-1 already must come out as it went in */ +TEST(utf8_to_latin1_keeps_raw_8bit_names) +{ + /* "Grüße", "Ärger.txt", "Señor Ñ", "café" and "ü" at the very end */ + char a[] = "Gr\xFC\xDF" "e"; + char b[] = "\xC4rger.txt"; + char c[] = "Se\xF1or \xD1"; + char d[] = "caf\xE9"; + char e[] = "Men\xFC"; + + ASSERT_EQ(odfs_utf8_to_latin1(a), 5); + ASSERT_STR_EQ(a, "Gr\xFC\xDF" "e"); + odfs_utf8_to_latin1(b); + ASSERT_STR_EQ(b, "\xC4rger.txt"); + odfs_utf8_to_latin1(c); + ASSERT_STR_EQ(c, "Se\xF1or \xD1"); + odfs_utf8_to_latin1(d); + ASSERT_STR_EQ(d, "caf\xE9"); + odfs_utf8_to_latin1(e); + ASSERT_STR_EQ(e, "Men\xFC"); +} + +TEST(utf8_to_latin1_keeps_malformed_sequences) +{ + /* lone continuation, lead cut short by the end, lead cut short by + * ASCII, and the bytes UTF-8 never uses */ + char a[] = "a\x80" "b\xBF"; + char b[] = "x\xE2\x82"; + char c[] = "x\xF0\x9F\x98" "y"; + char d[] = "\xC0\x80\xC1\xBF\xF5\x80\xFF"; + + odfs_utf8_to_latin1(a); + ASSERT_STR_EQ(a, "a\x80" "b\xBF"); + odfs_utf8_to_latin1(b); + ASSERT_STR_EQ(b, "x\xE2\x82"); + odfs_utf8_to_latin1(c); + ASSERT_STR_EQ(c, "x\xF0\x9F\x98" "y"); + odfs_utf8_to_latin1(d); + ASSERT_STR_EQ(d, "\xC0\x80\xC1\xBF\xF5\x80\xFF"); +} + +/* an overlong form must not turn into the character it spells, least + * of all into a NUL or a '/' */ +TEST(utf8_to_latin1_keeps_overlong_forms) +{ + char a[] = "a\xE0\x80\x80" "b"; /* U+0000 in three bytes */ + char b[] = "a\xE0\x80\xAF" "b"; /* '/' in three bytes */ + char c[] = "a\xF0\x80\x83\xA9" "b"; /* U+00E9 in four bytes */ + + ASSERT_EQ(odfs_utf8_to_latin1(a), 5); + ASSERT_STR_EQ(a, "a\xE0\x80\x80" "b"); + ASSERT_EQ(odfs_utf8_to_latin1(b), 5); + ASSERT_STR_EQ(b, "a\xE0\x80\xAF" "b"); + ASSERT_EQ(odfs_utf8_to_latin1(c), 6); + ASSERT_STR_EQ(c, "a\xF0\x80\x83\xA9" "b"); +} + +TEST(utf8_to_latin1_empty) +{ + char s[] = ""; + + ASSERT_EQ(odfs_utf8_to_latin1(s), 0); + ASSERT_STR_EQ(s, ""); +} + TEST_MAIN() From be7b848a082ff994d96ff7ce0b2b3733217a3a3f Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Mon, 23 Nov 2026 23:22:48 -0800 Subject: [PATCH 2/6] core: add a mount option for names in Latin-1 Add odfs_mount_opts_t.name_charset. With ODFS_NAMES_LATIN1, node names, symbolic link targets and the volume name are converted from UTF-8 with odfs_utf8_to_latin1(); the default, ODFS_NAMES_UTF8, leaves them as the backends decode them. Node names are converted in odfs_namefix_apply(), the one place every backend's readdir and lookup pass a name through, and before it looks for duplicates. So two names that differ in UTF-8 but come out the same, like two names without a single Latin-1 character, are told apart with the usual "~2" suffix, the metadata cache holds converted names, and a lookup compares the name it is given directly. The function runs for every directory entry read and already walks the name to hash it. That loop now stops at a byte with bit 7 set, so a pure ASCII name costs no more than before; only other names are converted and then hashed again. The backends cannot see the mount options, so the choice is kept in the block cache, which every backend call is handed. Rock Ridge's readlink scans the directory records itself for the link's name and converts each candidate before comparing. The target is converted in odfs_readlink() for all backends. --- backends/hfs/hfs_common.c | 2 +- backends/iso9660/iso9660.c | 4 +- backends/udf/udf.c | 2 +- core/mount.c | 13 +++++- core/namefix.c | 36 +++++++++++++-- include/odfs/api.h | 1 + include/odfs/cache.h | 4 ++ include/odfs/charset.h | 20 ++++++++ include/odfs/namefix.h | 10 +++- tests/unit/test_joliet.c | 95 ++++++++++++++++++++++++++++++++++++++ tests/unit/test_namefix.c | 78 +++++++++++++++++++++++++++++-- tests/unit/test_symlink.c | 76 +++++++++++++++++++++++++++++- 12 files changed, 323 insertions(+), 18 deletions(-) diff --git a/backends/hfs/hfs_common.c b/backends/hfs/hfs_common.c index d48b01a..a7c1d4e 100644 --- a/backends/hfs/hfs_common.c +++ b/backends/hfs/hfs_common.c @@ -443,7 +443,7 @@ odfs_err_t odfs_hfs_readdir(void *backend_ctx, rc.skip_to = (resume_offset && *resume_offset) ? *resume_offset : 0; rc.resume_offset = resume_offset; rc.last_err = ODFS_OK; - odfs_namefix_init(&rc.namefix); + odfs_namefix_init(&rc.namefix, cache->name_charset); /* dir->extent.lba stores the directory's CNID */ err = odfs_hfs_walk_catalog(vol, cache, dir->extent.lba, diff --git a/backends/iso9660/iso9660.c b/backends/iso9660/iso9660.c index aacaaee..e52d23c 100644 --- a/backends/iso9660/iso9660.c +++ b/backends/iso9660/iso9660.c @@ -524,7 +524,7 @@ odfs_err_t odfs_iso_readdir(void *backend_ctx, ODFS_TRACE(log, ODFS_SUB_ISO, "readdir: LBA %" PRIu32 ", size %" PRIu32, dir_lba, dir_size); - odfs_namefix_init(&namefix); + odfs_namefix_init(&namefix, cache->name_charset); while (offset < dir_size) { uint32_t entry_start = offset; @@ -853,6 +853,8 @@ static odfs_err_t iso_readlink(void *backend_ctx, } } + /* name is the node's, which readdir converted */ + odfs_name_to_charset(node.name, cache->name_charset); if (odfs_strcasecmp(node.name, name) != 0) continue; diff --git a/backends/udf/udf.c b/backends/udf/udf.c index 4af7fed..4e9efc4 100644 --- a/backends/udf/udf.c +++ b/backends/udf/udf.c @@ -1255,7 +1255,7 @@ static odfs_err_t udf_walk_dir_entries(udf_context_t *ctx, walk.dir = dir; walk.callback = callback; walk.arg = arg; - odfs_namefix_init(&walk.namefix); + odfs_namefix_init(&walk.namefix, cache->name_charset); err = udf_walk_fids(ctx, cache, dir, udf_decode_dir_entry, &walk, dir_size_out); odfs_namefix_destroy(&walk.namefix); diff --git a/core/mount.c b/core/mount.c index 0a4bacd..f42e510 100644 --- a/core/mount.c +++ b/core/mount.c @@ -6,6 +6,7 @@ #include "odfs/api.h" #include "odfs/ancestry.h" +#include "odfs/charset.h" #include "odfs/string.h" #include #include @@ -373,6 +374,7 @@ void odfs_mount_opts_default(odfs_mount_opts_t *opts) opts->prefer_hfs = 0; opts->lowercase_iso = 0; /* preserve original case */ opts->prefer_aiff = 0; /* expose CDDA tracks as WAV by default */ + opts->name_charset = ODFS_NAMES_UTF8; opts->cache_blocks = 0; /* use default from config.h */ opts->meta_cache_kib = ODFS_META_CACHE_KIB; } @@ -443,6 +445,7 @@ odfs_err_t odfs_mount(odfs_media_t *media, err = odfs_cache_init(&mnt->cache, &mnt->media, cache_size); if (err != ODFS_OK) return err; + mnt->cache.name_charset = (uint8_t)mnt->opts.name_charset; ODFS_INFO(&mnt->log, ODFS_SUB_MOUNT, "cache initialized: %" PRIu32 " blocks", cache_size); @@ -504,6 +507,7 @@ odfs_err_t odfs_mount(odfs_media_t *media, mnt->backend_ops->get_volume_name(mnt->backend_ctx, mnt->volume_name, sizeof(mnt->volume_name)); + odfs_name_to_charset(mnt->volume_name, mnt->cache.name_charset); if (mnt->backend_ops->get_volume_size) mnt->total_blocks = mnt->backend_ops->get_volume_size(mnt->backend_ctx); @@ -697,6 +701,7 @@ odfs_err_t odfs_readlink(odfs_mount_t *mnt, { const odfs_backend_ops_t *ops; void *backend_ctx; + odfs_err_t err; if (!mnt || !dir || !name || !buf || buf_size == 0) return ODFS_ERR_INVAL; @@ -708,8 +713,12 @@ odfs_err_t odfs_readlink(odfs_mount_t *mnt, !ops || !ops->readlink) return ODFS_ERR_UNSUPPORTED; - return ops->readlink(backend_ctx, &mnt->cache, &mnt->log, dir, name, - buf, buf_size); + err = ops->readlink(backend_ctx, &mnt->cache, &mnt->log, dir, name, + buf, buf_size); + /* the target names nodes, so it has to be spelled as they are */ + if (err == ODFS_OK) + odfs_name_to_charset(buf, mnt->cache.name_charset); + return err; } odfs_err_t odfs_resolve_parent_node(odfs_mount_t *mnt, diff --git a/core/namefix.c b/core/namefix.c index 6f85201..013355a 100644 --- a/core/namefix.c +++ b/core/namefix.c @@ -7,6 +7,7 @@ #include "odfs/namefix.h" #include "odfs/alloc.h" +#include "odfs/charset.h" #include "odfs/node.h" #include "odfs/string.h" @@ -15,12 +16,13 @@ #define ODFS_NAMEFIX_CHUNK_SIZE 4096u -void odfs_namefix_init(odfs_namefix_state_t *state) +void odfs_namefix_init(odfs_namefix_state_t *state, unsigned int charset) { memset(state->buckets, 0, sizeof(state->buckets)); state->chunks = NULL; state->free = (unsigned char *)state->inline_buf; state->avail = sizeof(state->inline_buf); + state->charset = (unsigned char)charset; } void odfs_namefix_destroy(odfs_namefix_state_t *state) @@ -33,7 +35,7 @@ void odfs_namefix_destroy(odfs_namefix_state_t *state) chunk = next; } - odfs_namefix_init(state); + odfs_namefix_init(state, state->charset); } static void *odfs_namefix_alloc(odfs_namefix_state_t *state, size_t size) @@ -118,12 +120,35 @@ odfs_err_t odfs_namefix_apply(odfs_namefix_state_t *state, char base[ODFS_NAME_MAX]; size_t len; size_t base_len; - unsigned int hash; + unsigned int hash = 0; + const char *s = name; + signed char c; if (name_size == 0) return ODFS_ERR_INVAL; - hash = odfs_namefix_hash(name, &len); + /* + * This runs for every directory entry read. Hash the name as + * odfs_namefix_hash() does, but stop at a byte with bit 7 set: + * the loop then costs a pure ASCII name, which reads the same in + * every character set, no more than hashing it. + */ + while ((c = (signed char)*s++) > 0) { + hash = (hash << 5) | (hash >> 27); + hash ^= (unsigned int)c | 0x20u; + } + if (c != 0) { + /* + * Convert before looking for duplicates, so that two names + * which come out the same are told apart like any others. + */ + odfs_name_to_charset(name, state->charset); + hash = odfs_namefix_hash(name, &len); + } else { + hash ^= hash >> 16; + hash ^= hash >> 8; + len = (size_t)(s - name - 1); + } if (!odfs_namefix_contains(state, name, hash)) return odfs_namefix_remember(state, name, len, hash); @@ -157,7 +182,8 @@ odfs_err_t odfs_namefix_apply(odfs_namefix_state_t *state, if (keep + (size_t)suffix_len >= name_size) { keep = name_size - (size_t)suffix_len - 1; /* cut before a character, not inside its UTF-8 sequence */ - while (keep > 0 && ((unsigned char)base[keep] & 0xC0u) == 0x80u) + while (state->charset == ODFS_NAMES_UTF8 && keep > 0 && + ((unsigned char)base[keep] & 0xC0u) == 0x80u) keep--; } diff --git a/include/odfs/api.h b/include/odfs/api.h index f752732..c29d6ff 100644 --- a/include/odfs/api.h +++ b/include/odfs/api.h @@ -26,6 +26,7 @@ typedef struct odfs_mount_opts { int prefer_hfs; /* prefer HFS on hybrid discs */ int lowercase_iso; /* lowercase plain ISO names */ int prefer_aiff; /* expose CDDA tracks as AIFF instead of WAV */ + int name_charset; /* ODFS_NAMES_xxx: character set of names */ uint32_t cache_blocks; /* block cache size (0 = use default) */ uint32_t meta_cache_kib; /* parsed-dir cache budget in KiB (default from config.h; 0 disables) */ diff --git a/include/odfs/cache.h b/include/odfs/cache.h index d692512..9e3dde9 100644 --- a/include/odfs/cache.h +++ b/include/odfs/cache.h @@ -65,6 +65,10 @@ typedef struct odfs_cache { it still holds the same sector */ uint8_t *data_slab; /* all sector buffers, or NULL when each entry has its own */ + uint8_t name_charset; /* ODFS_NAMES_xxx the mount asked + for; kept here because the + cache is what every backend + call is handed */ } odfs_cache_t; /* lifecycle */ diff --git a/include/odfs/charset.h b/include/odfs/charset.h index 1451866..c6cbc3e 100644 --- a/include/odfs/charset.h +++ b/include/odfs/charset.h @@ -68,6 +68,26 @@ odfs_err_t odfs_iso_name_to_display(const char *src, size_t src_len, size_t odfs_utf8_to_latin1(char *s); #endif +/* + * The character set a mount gives names in. The backends decode every + * name to UTF-8; a mount that asks for another set has node names, + * symbolic link targets and the volume name converted to it. + */ +#define ODFS_NAMES_UTF8 0u +#define ODFS_NAMES_LATIN1 1u /* ISO 8859-1, see odfs_utf8_to_latin1() */ + +/* convert a UTF-8 name in place to character set charset */ +static inline void odfs_name_to_charset(char *name, unsigned int charset) +{ +#if ODFS_FEATURE_CHARSET_BUILTIN + if (charset == ODFS_NAMES_LATIN1) + odfs_utf8_to_latin1(name); +#else + (void)name; + (void)charset; +#endif +} + #if !defined(AMIGA) /* * Host-only fallback: replace non-printable / non-Amiga-safe diff --git a/include/odfs/namefix.h b/include/odfs/namefix.h index 3593e16..48757a5 100644 --- a/include/odfs/namefix.h +++ b/include/odfs/namefix.h @@ -31,11 +31,19 @@ typedef struct odfs_namefix_state { odfs_namefix_chunk_t *chunks; unsigned char *free; /* next unused byte */ size_t avail; /* bytes left at free */ + unsigned char charset; /* ODFS_NAMES_xxx of the fixed names */ void *inline_buf[ODFS_NAMEFIX_INLINE / sizeof(void *)]; } odfs_namefix_state_t; -void odfs_namefix_init(odfs_namefix_state_t *state); +/* charset is the ODFS_NAMES_xxx set the names are wanted in */ +void odfs_namefix_init(odfs_namefix_state_t *state, unsigned int charset); void odfs_namefix_destroy(odfs_namefix_state_t *state); + +/* + * Give one name of a directory its final form: convert it from UTF-8 + * to the state's character set, then, if an earlier name of the + * directory compares equal, add a "~" suffix that makes it unique. + */ odfs_err_t odfs_namefix_apply(odfs_namefix_state_t *state, char *name, size_t name_size); diff --git a/tests/unit/test_joliet.c b/tests/unit/test_joliet.c index d9ba6a7..ce48a96 100644 --- a/tests/unit/test_joliet.c +++ b/tests/unit/test_joliet.c @@ -6,6 +6,7 @@ #include "joliet/joliet.h" #include "odfs/cache.h" +#include "odfs/charset.h" #include "odfs/error.h" #include "test_harness.h" @@ -227,4 +228,98 @@ TEST(joliet_skips_odd_names_and_strips_version_suffix) odfs_cache_destroy(&cache); } +/* a Joliet tree read for a mount that wants Latin-1 names */ +TEST(joliet_names_in_latin1) +{ + static mem_media_t media_data; + odfs_media_t media; + odfs_cache_t cache; + odfs_log_state_t log; + odfs_node_t root; + odfs_node_t node; + void *ctx = NULL; + joliet_collect_t collect; + uint8_t dot = 0, dotdot = 1; + const uint32_t root_lba = 20; + uint8_t *vd = media_data.sectors[ISO_VD_START_LBA]; + size_t off = 0; + + /* "Gr" U+00FC U+00DF "e" */ + static const uint8_t gruesse[] = { + 0x00, 'G', 0x00, 'r', 0x00, 0xFC, 0x00, 0xDF, 0x00, 'e' + }; + /* U+65E5 U+672C and U+4E2D U+56FD: not in Latin-1 */ + static const uint8_t nihon[] = { 0x65, 0xE5, 0x67, 0x2C }; + static const uint8_t chugoku[] = { 0x4E, 0x2D, 0x56, 0xFD }; + static const uint8_t plain[] = { 0x00, 'a', 0x00, 'b', 0x00, 'c' }; + + memset(&media_data, 0, sizeof(media_data)); + media.ops = &mem_media_ops; + media.ctx = &media_data; + odfs_log_init(&log); + + vd[ISO_PVD_TYPE] = ISO_VD_TYPE_SUPPL; + memcpy(&vd[ISO_PVD_ID], ISO_STANDARD_ID, ISO_STANDARD_ID_LEN); + vd[6] = 1; + memcpy(&vd[88], JOLIET_ESC_UCS2_LEVEL3, 3); + write_le32(&vd[ISO_PVD_VOLUME_SPACE_SIZE], 40); + write_le16(&vd[ISO_PVD_LOGICAL_BLK_SIZE], TEST_SECTOR_SIZE); + write_dir_record(&vd[ISO_PVD_ROOT_DIR_RECORD], root_lba, + TEST_SECTOR_SIZE, ISO_DR_FLAG_DIRECTORY, &dot, 1); + + media_data.sectors[ISO_VD_START_LBA + 1][ISO_PVD_TYPE] = ISO_VD_TYPE_TERM; + memcpy(&media_data.sectors[ISO_VD_START_LBA + 1][ISO_PVD_ID], + ISO_STANDARD_ID, ISO_STANDARD_ID_LEN); + + off += write_dir_record(&media_data.sectors[root_lba][off], + root_lba, TEST_SECTOR_SIZE, ISO_DR_FLAG_DIRECTORY, + &dot, 1); + off += write_dir_record(&media_data.sectors[root_lba][off], + root_lba, TEST_SECTOR_SIZE, ISO_DR_FLAG_DIRECTORY, + &dotdot, 1); + off += write_dir_record(&media_data.sectors[root_lba][off], + 21, 10, 0, gruesse, sizeof(gruesse)); + off += write_dir_record(&media_data.sectors[root_lba][off], + 22, 10, 0, nihon, sizeof(nihon)); + off += write_dir_record(&media_data.sectors[root_lba][off], + 23, 10, 0, chugoku, sizeof(chugoku)); + off += write_dir_record(&media_data.sectors[root_lba][off], + 24, 10, 0, plain, sizeof(plain)); + + ASSERT_OK(odfs_cache_init(&cache, &media, 8)); + ASSERT_OK(joliet_backend_ops.mount(&cache, &log, 0, &root, &ctx)); + + /* as decoded: UTF-8 */ + memset(&collect, 0, sizeof(collect)); + ASSERT_OK(joliet_backend_ops.readdir(ctx, &cache, &log, &root, + joliet_collect_cb, &collect, NULL)); + ASSERT_EQ(collect.count, 4); + ASSERT_STR_EQ(collect.nodes[0].name, "Gr\xC3\xBC\xC3\x9F" "e"); + ASSERT_STR_EQ(collect.nodes[1].name, "\xE6\x97\xA5\xE6\x9C\xAC"); + + cache.name_charset = ODFS_NAMES_LATIN1; + memset(&collect, 0, sizeof(collect)); + ASSERT_OK(joliet_backend_ops.readdir(ctx, &cache, &log, &root, + joliet_collect_cb, &collect, NULL)); + ASSERT_EQ(collect.count, 4); + ASSERT_STR_EQ(collect.nodes[0].name, "Gr\xFC\xDF" "e"); + ASSERT_STR_EQ(collect.nodes[1].name, "??"); + ASSERT_STR_EQ(collect.nodes[2].name, "??~2"); + ASSERT_STR_EQ(collect.nodes[3].name, "abc"); + + /* a name is found the way readdir spelled it, and only that way */ + ASSERT_OK(joliet_backend_ops.lookup(ctx, &cache, &log, &root, + "Gr\xFC\xDF" "e", &node)); + ASSERT_EQ(node.extent.lba, 21); + ASSERT_OK(joliet_backend_ops.lookup(ctx, &cache, &log, &root, + "??~2", &node)); + ASSERT_EQ(node.extent.lba, 23); + ASSERT_ERR(joliet_backend_ops.lookup(ctx, &cache, &log, &root, + "Gr\xC3\xBC\xC3\x9F" "e", &node), + ODFS_ERR_NOT_FOUND); + + joliet_backend_ops.unmount(ctx); + odfs_cache_destroy(&cache); +} + TEST_MAIN() diff --git a/tests/unit/test_namefix.c b/tests/unit/test_namefix.c index 4084ef7..12b80ab 100644 --- a/tests/unit/test_namefix.c +++ b/tests/unit/test_namefix.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include "odfs/charset.h" #include "odfs/namefix.h" #include "odfs/node.h" #include "test_harness.h" @@ -13,7 +14,7 @@ TEST(namefix_keeps_unique_name) odfs_namefix_state_t state; char name[ODFS_NAME_MAX] = "README"; - odfs_namefix_init(&state); + odfs_namefix_init(&state, ODFS_NAMES_UTF8); ASSERT_OK(odfs_namefix_apply(&state, name, sizeof(name))); ASSERT_STR_EQ(name, "README"); odfs_namefix_destroy(&state); @@ -25,7 +26,7 @@ TEST(namefix_suffixes_duplicate_name) char first[ODFS_NAME_MAX] = "README"; char second[ODFS_NAME_MAX] = "README"; - odfs_namefix_init(&state); + odfs_namefix_init(&state, ODFS_NAMES_UTF8); ASSERT_OK(odfs_namefix_apply(&state, first, sizeof(first))); ASSERT_OK(odfs_namefix_apply(&state, second, sizeof(second))); ASSERT_STR_EQ(first, "README"); @@ -40,7 +41,7 @@ TEST(namefix_handles_suffix_collisions) char second[ODFS_NAME_MAX] = "README"; char third[ODFS_NAME_MAX] = "README~2"; - odfs_namefix_init(&state); + odfs_namefix_init(&state, ODFS_NAMES_UTF8); ASSERT_OK(odfs_namefix_apply(&state, first, sizeof(first))); ASSERT_OK(odfs_namefix_apply(&state, second, sizeof(second))); ASSERT_OK(odfs_namefix_apply(&state, third, sizeof(third))); @@ -55,7 +56,7 @@ TEST(namefix_is_case_insensitive) char first[ODFS_NAME_MAX] = "ReadMe"; char second[ODFS_NAME_MAX] = "README"; - odfs_namefix_init(&state); + odfs_namefix_init(&state, ODFS_NAMES_UTF8); ASSERT_OK(odfs_namefix_apply(&state, first, sizeof(first))); ASSERT_OK(odfs_namefix_apply(&state, second, sizeof(second))); ASSERT_STR_EQ(second, "README~2"); @@ -83,7 +84,7 @@ TEST(namefix_truncates_at_utf8_boundaries) strcpy(a, names[i]); memcpy(b, a, sizeof(b)); - odfs_namefix_init(&state); + odfs_namefix_init(&state, ODFS_NAMES_UTF8); ASSERT_OK(odfs_namefix_apply(&state, a, sizes[i])); ASSERT_OK(odfs_namefix_apply(&state, b, sizes[i])); ASSERT_STR_EQ(b, want[i]); @@ -91,4 +92,71 @@ TEST(namefix_truncates_at_utf8_boundaries) } } +TEST(namefix_converts_names_to_latin1) +{ + odfs_namefix_state_t state; + /* "Gr" U+00FC U+00DF "e.txt" */ + char name[ODFS_NAME_MAX] = "Gr\xC3\xBC\xC3\x9F" "e.txt"; + char ascii[ODFS_NAME_MAX] = "README"; + + odfs_namefix_init(&state, ODFS_NAMES_LATIN1); + ASSERT_OK(odfs_namefix_apply(&state, name, sizeof(name))); + ASSERT_STR_EQ(name, "Gr\xFC\xDF" "e.txt"); + ASSERT_OK(odfs_namefix_apply(&state, ascii, sizeof(ascii))); + ASSERT_STR_EQ(ascii, "README"); + odfs_namefix_destroy(&state); +} + +TEST(namefix_keeps_utf8_names_by_default) +{ + odfs_namefix_state_t state; + char name[ODFS_NAME_MAX] = "Gr\xC3\xBC\xC3\x9F" "e.txt"; + + odfs_namefix_init(&state, ODFS_NAMES_UTF8); + ASSERT_OK(odfs_namefix_apply(&state, name, sizeof(name))); + ASSERT_STR_EQ(name, "Gr\xC3\xBC\xC3\x9F" "e.txt"); + odfs_namefix_destroy(&state); +} + +/* names that differ in UTF-8 but come out the same must not collide */ +TEST(namefix_separates_names_that_convert_alike) +{ + odfs_namefix_state_t state; + /* U+65E5 U+672C ".txt", U+4E2D U+56FD ".txt", and a literal "??.txt" */ + char a[ODFS_NAME_MAX] = "\xE6\x97\xA5\xE6\x9C\xAC.txt"; + char b[ODFS_NAME_MAX] = "\xE4\xB8\xAD\xE5\x9B\xBD.txt"; + char c[ODFS_NAME_MAX] = "??.txt"; + /* the same name once as UTF-8 and once as raw Latin-1 */ + char d[ODFS_NAME_MAX] = "caf\xC3\xA9"; + char e[ODFS_NAME_MAX] = "caf\xE9"; + + odfs_namefix_init(&state, ODFS_NAMES_LATIN1); + ASSERT_OK(odfs_namefix_apply(&state, a, sizeof(a))); + ASSERT_OK(odfs_namefix_apply(&state, b, sizeof(b))); + ASSERT_OK(odfs_namefix_apply(&state, c, sizeof(c))); + ASSERT_STR_EQ(a, "??.txt"); + ASSERT_STR_EQ(b, "??.txt~2"); + ASSERT_STR_EQ(c, "??.txt~3"); + ASSERT_OK(odfs_namefix_apply(&state, d, sizeof(d))); + ASSERT_OK(odfs_namefix_apply(&state, e, sizeof(e))); + ASSERT_STR_EQ(d, "caf\xE9"); + ASSERT_STR_EQ(e, "caf\xE9~2"); + odfs_namefix_destroy(&state); +} + +/* in Latin-1, 0x80-0xBF are characters, not the tail of one */ +TEST(namefix_truncates_latin1_names_at_any_byte) +{ + odfs_namefix_state_t state; + char a[8] = "ab\xA9"; + char b[8] = "ab\xA9"; + + odfs_namefix_init(&state, ODFS_NAMES_LATIN1); + ASSERT_OK(odfs_namefix_apply(&state, a, 5)); + ASSERT_OK(odfs_namefix_apply(&state, b, 5)); + ASSERT_STR_EQ(a, "ab\xA9"); + ASSERT_STR_EQ(b, "ab~2"); + odfs_namefix_destroy(&state); +} + TEST_MAIN() diff --git a/tests/unit/test_symlink.c b/tests/unit/test_symlink.c index dc80e95..26da922 100644 --- a/tests/unit/test_symlink.c +++ b/tests/unit/test_symlink.c @@ -127,6 +127,17 @@ static size_t sl_sl(uint8_t *p, const uint8_t *comps, size_t comps_len) return 5 + comps_len; } +/* NM entry: the Rock Ridge name of the record */ +static size_t sl_nm(uint8_t *p, const char *name) +{ + size_t len = strlen(name); + + p[0] = 'N'; p[1] = 'M'; p[2] = (uint8_t)(5 + len); p[3] = 1; + p[4] = 0; /* flags */ + memcpy(&p[5], name, len); + return 5 + len; +} + static void sl_build_image(sl_media_t *media) { uint8_t *pvd = media->sectors[ISO_VD_START_LBA]; @@ -194,6 +205,20 @@ static void sl_build_image(sl_media_t *media) "UP.LNK;1", 8, sua, sua_len); } + /* "L" U+00E4 "nk" -> "z" U+00FC "l/" U+65E5: names in UTF-8, as + * current mastering tools write them */ + { + static const uint8_t comps[] = { + 0, 4, 'z', 0xC3, 0xBC, 'l', + 0, 3, 0xE6, 0x97, 0xA5 + }; + sua_len = sl_px(sua, 0120777); + sua_len += sl_nm(sua + sua_len, "L\xC3\xA4nk"); + sua_len += sl_sl(sua + sua_len, comps, sizeof(comps)); + off += sl_dir_record(&root[off], 0, 0, 0, + "LANK.;1", 7, sua, sua_len); + } + memcpy(media->sectors[SL_TARGET_LBA], sl_target_data, sizeof(sl_target_data) - 1); } @@ -235,13 +260,13 @@ TEST(symlink_nodes_have_symlink_kind) memset(&collect, 0, sizeof(collect)); ASSERT_OK(odfs_readdir(&mnt, &mnt.root, sl_collect_cb, &collect, NULL)); - ASSERT_EQ(collect.count, 3); + ASSERT_EQ(collect.count, 4); for (i = 0; i < collect.count; i++) { if (collect.entries[i].kind == ODFS_NODE_SYMLINK) links++; } - ASSERT_EQ(links, 2); + ASSERT_EQ(links, 3); odfs_unmount(&mnt); } @@ -288,6 +313,53 @@ TEST(readlink_rejects_non_links) odfs_unmount(&mnt); } +/* + * A mount that names nodes in Latin-1 finds a link by its converted + * name and spells the target the way it spells the nodes. + */ +TEST(readlink_follows_the_mount_charset) +{ + static sl_media_t media; + odfs_media_t m; + odfs_mount_opts_t opts; + odfs_mount_t mnt; + sl_collect_t collect; + char target[256]; + + /* as decoded */ + ASSERT_OK(sl_mount_image(&media, &mnt)); + ASSERT_OK(odfs_readlink(&mnt, &mnt.root, "L\xC3\xA4nk", + target, sizeof(target))); + ASSERT_STR_EQ(target, "z\xC3\xBCl/\xE6\x97\xA5"); + odfs_unmount(&mnt); + + sl_build_image(&media); + /* the volume identifier is copied as it is, so UTF-8 can be in it */ + memcpy(&media.sectors[ISO_VD_START_LBA][ISO_PVD_VOLUME_ID], + "SL\xC3\x9CTEST", 8); + m.ops = &sl_media_ops; + m.ctx = &media; + odfs_mount_opts_default(&opts); + opts.name_charset = ODFS_NAMES_LATIN1; + ASSERT_OK(odfs_mount(&m, &opts, NULL, &mnt)); + ASSERT_STR_EQ(mnt.volume_name, "SL\xDCTEST"); + + memset(&collect, 0, sizeof(collect)); + ASSERT_OK(odfs_readdir(&mnt, &mnt.root, sl_collect_cb, &collect, NULL)); + ASSERT_EQ(collect.count, 4); + ASSERT_STR_EQ(collect.entries[3].name, "L\xE4nk"); + ASSERT_EQ(collect.entries[3].kind, ODFS_NODE_SYMLINK); + + ASSERT_OK(odfs_readlink(&mnt, &mnt.root, "L\xE4nk", + target, sizeof(target))); + ASSERT_STR_EQ(target, "z\xFCl/?"); + ASSERT_ERR(odfs_readlink(&mnt, &mnt.root, "L\xC3\xA4nk", + target, sizeof(target)), + ODFS_ERR_NOT_FOUND); + + odfs_unmount(&mnt); +} + TEST(posix_to_amiga_conversion) { char out[128]; From 1e3afd086a46a6813ad7f62c789fc55f1689434a Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Mon, 23 Nov 2026 23:24:48 -0800 Subject: [PATCH 3/6] amiga: show names in Latin-1 instead of UTF-8 The backends decode Joliet, UDF, HFS and HFS+ names to UTF-8, and current mastering tools write Rock Ridge names in UTF-8 as well. The handler passed these bytes to AmigaDOS unchanged, so every character outside ASCII showed as two or three wrong ones, and such a name could not be typed. Mount with ODFS_NAMES_LATIN1. A character Latin-1 does not have is shown as '?'. Rock Ridge names that are raw Latin-1, as Amiga mastering tools write them, are not valid UTF-8 and stay as they are. Together with the two commits before it, this adds 332 bytes to the 68000 handler, 272 to the 68020 one and 324 to the ROM build. --- README.md | 7 +++++++ platform/amiga/handler_main.c | 2 ++ 2 files changed, 9 insertions(+) diff --git a/README.md b/README.md index cb281af..12c306b 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,13 @@ Control = "UDF" To prefer HFS or HFS+, use `Control = "HFSFIRST"`. Options can be combined, for example `Control = "UDF LOWERCASE"`. +### Character sets + +Names from Joliet, UDF, HFS and HFS+ volumes, and Rock Ridge names written in +UTF-8, are converted to ISO 8859-1 (Latin-1), the character set of AmigaOS. A +character that Latin-1 does not have is shown as `?`. Rock Ridge names that +already are in an 8-bit character set are left as they are. + ### Name collisions If multiple on-disc names become the same AmigaDOS name after name or character diff --git a/platform/amiga/handler_main.c b/platform/amiga/handler_main.c index 7cbd59b..2bd8859 100644 --- a/platform/amiga/handler_main.c +++ b/platform/amiga/handler_main.c @@ -6003,6 +6003,8 @@ static void mount_volume(handler_global_t *g) probe_scsi_reads(g); odfs_mount_opts_default(&opts); + /* AmigaDOS shows and compares names in an 8-bit character set */ + opts.name_charset = ODFS_NAMES_LATIN1; #if !defined(ODFS_PROFILE_ROM) || !ODFS_PROFILE_ROM if (g->envec) From e0e61ffec0b88bbe853c0ad8e4f01d87c6c3f32c Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Mon, 23 Nov 2026 12:10:35 -0800 Subject: [PATCH 4/6] udf: decode 8-bit CS0 names as Latin-1 to UTF-8 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. Host tools that expect UTF-8 now get valid names. On AmigaOS the handler converts names to Latin-1, so these names look the same as before, except for the control codes 0x80 to 0x9F, which become '?'. --- backends/udf/udf.c | 82 +++++++++++++++++++++---------------------- tests/unit/test_udf.c | 46 ++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 41 deletions(-) diff --git a/backends/udf/udf.c b/backends/udf/udf.c index 4e9efc4..294944f 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..b9505f9 100644 --- a/tests/unit/test_udf.c +++ b/tests/unit/test_udf.c @@ -6,6 +6,7 @@ #include "odfs/api.h" #include "odfs/cache.h" +#include "odfs/charset.h" #include "udf/udf.h" #include "test_harness.h" @@ -1631,4 +1632,49 @@ 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"); + + /* a mount that names nodes in Latin-1 gets the bytes on the disc */ + cache.name_charset = ODFS_NAMES_LATIN1; + ASSERT_OK(udf_backend_ops.lookup(&ctx, &cache, &log, &dir, + "caf\xE9", &out)); + ASSERT_STR_EQ(out.name, "caf\xE9"); + + odfs_cache_destroy(&cache); +} + TEST_MAIN() From 201fa41143e94dddc6fe1bce4f8b71e759d1dbbe Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Mon, 23 Nov 2026 23:13:21 -0800 Subject: [PATCH 5/6] charset: merge combining accents when converting to Latin-1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit HFS+ stores every name decomposed: "Ärger" is "A", U+0308 (combining diaeresis), "rger". Discs mastered on a Mac from such names carry the same form in their Joliet and Rock Ridge trees. Converted character by character, the accent has no Latin-1 equivalent, so the name came out as "A?rger". When a combining grave, acute, circumflex, tilde, diaeresis, ring or cedilla follows a letter and Latin-1 has the accented letter, replace the letter with it. Any other combination still gives the letter and a '?'. The new ODFS_FEATURE_CHARSET_COMPOSE controls this. It is off in the ROM profile, which has no HFS+ support, and costs 188 bytes on the 68000 elsewhere. --- core/charset.c | 38 +++++++++++++++++++++++++++ include/odfs/charset.h | 4 +++ include/odfs/config.h | 7 +++++ tests/unit/test_charset.c | 55 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) diff --git a/core/charset.c b/core/charset.c index 7d2aa75..332a460 100644 --- a/core/charset.c +++ b/core/charset.c @@ -180,6 +180,35 @@ odfs_err_t odfs_iso_name_to_display(const char *src, size_t src_len, } #if ODFS_FEATURE_CHARSET_BUILTIN +#if ODFS_FEATURE_CHARSET_COMPOSE +/* + * The Latin-1 letter that base and the combining character U+0300 + + * mark make, or 0. The tables give, for 0xC0 to 0xDD, the capital + * letter and the mark each one is made of; a small letter is 0x20 + * above its capital. + */ +static unsigned int odfs_latin1_compose(unsigned int base, unsigned int mark) +{ + static const char letters[] = + "AAAAAA\0CEEEEIIII\0NOOOOO\0\0UUUUY"; + static const uint8_t marks[30] = { + 0x00, 0x01, 0x02, 0x03, 0x08, 0x0A, 0xFF, 0x27, /* A, AE, C */ + 0x00, 0x01, 0x02, 0x08, 0x00, 0x01, 0x02, 0x08, /* E, I */ + 0xFF, 0x03, 0x00, 0x01, 0x02, 0x03, 0x08, 0xFF, /* ETH, N, O */ + 0xFF, 0x00, 0x01, 0x02, 0x08, 0x01 /* O/, U, Y */ + }; + unsigned int i; + + if (base == 'y' && mark == 0x08) + return 0xFF; + for (i = 0; i < 30; i++) { + if ((unsigned char)letters[i] == (base & ~0x20u) && marks[i] == mark) + return (0xC0u + i) | (base & 0x20u); + } + return 0; +} +#endif + size_t odfs_utf8_to_latin1(char *s) { const unsigned char *src = (const unsigned char *)s; @@ -204,6 +233,15 @@ size_t odfs_utf8_to_latin1(char *s) */ if (i == more && (cp >> (5u * more + 1u)) != 0) { src += more; +#if ODFS_FEATURE_CHARSET_COMPOSE + if (cp - 0x300u < 0x28u && dst != (unsigned char *)s) { + c = odfs_latin1_compose(dst[-1], cp - 0x300u); + if (c != 0) { + dst[-1] = (unsigned char)c; + continue; + } + } +#endif /* U+0080-U+009F are control codes, CSI among them */ c = (cp >= 0xA0u && cp <= 0xFFu) ? cp : '?'; } diff --git a/include/odfs/charset.h b/include/odfs/charset.h index c6cbc3e..f0f0539 100644 --- a/include/odfs/charset.h +++ b/include/odfs/charset.h @@ -64,6 +64,10 @@ odfs_err_t odfs_iso_name_to_display(const char *src, size_t src_len, * part of a well-formed UTF-8 sequence is kept as it is, so a string * that already is in an 8-bit character set, such as a Rock Ridge name * written by an Amiga mastering tool, passes through unchanged. + * + * With ODFS_FEATURE_CHARSET_COMPOSE, a combining accent merges with + * the letter before it where Latin-1 has the accented letter: "u" + * followed by U+0308 becomes u-diaeresis. */ size_t odfs_utf8_to_latin1(char *s); #endif diff --git a/include/odfs/config.h b/include/odfs/config.h index 27d652f..4dce4b5 100644 --- a/include/odfs/config.h +++ b/include/odfs/config.h @@ -133,11 +133,18 @@ /* ---------- charset feature flags ---------- */ +/* + * BUILTIN: convert names from UTF-8 to Latin-1, see odfs/charset.h. + * COMPOSE: on the way, merge a letter and a combining accent that + * follows it. HFS+ stores every name decomposed like that. + */ #ifdef ODFS_PROFILE_ROM #define ODFS_FEATURE_CHARSET_BUILTIN 1 + #define ODFS_FEATURE_CHARSET_COMPOSE 0 #define ODFS_FEATURE_CHARSET_EXTERNAL 0 #else #define ODFS_FEATURE_CHARSET_BUILTIN 1 + #define ODFS_FEATURE_CHARSET_COMPOSE 1 #define ODFS_FEATURE_CHARSET_EXTERNAL 1 #endif diff --git a/tests/unit/test_charset.c b/tests/unit/test_charset.c index 8e4a6b6..c30056e 100644 --- a/tests/unit/test_charset.c +++ b/tests/unit/test_charset.c @@ -254,6 +254,61 @@ TEST(utf8_to_latin1_keeps_overlong_forms) ASSERT_STR_EQ(c, "a\xF0\x80\x83\xA9" "b"); } +/* HFS+ stores "u-diaeresis" as "u" followed by U+0308 */ +TEST(utf8_to_latin1_composes_accents) +{ +#if ODFS_FEATURE_CHARSET_COMPOSE + /* A-diaeresis "rger", "Gru" + diaeresis + sharp s + "e", + * "cafe" + acute, "gar" c-cedilla "on", "A" + ring, "n" + tilde, + * "y" + diaeresis, "Y" + acute */ + char a[] = "A\xCC\x88rger"; + char b[] = "Gru\xCC\x88\xC3\x9F" "e"; + char c[] = "cafe\xCC\x81"; + char d[] = "garc\xCC\xA7on"; + char e[] = "A\xCC\x8A n\xCC\x83 y\xCC\x88 Y\xCC\x81"; + /* every capital with every mark it takes */ + char f[] = "A\xCC\x80" "A\xCC\x81" "A\xCC\x82" "A\xCC\x83" + "A\xCC\x88" "A\xCC\x8A" "C\xCC\xA7" + "E\xCC\x80" "E\xCC\x81" "E\xCC\x82" "E\xCC\x88" + "I\xCC\x80" "I\xCC\x81" "I\xCC\x82" "I\xCC\x88" + "N\xCC\x83" + "O\xCC\x80" "O\xCC\x81" "O\xCC\x82" "O\xCC\x83" + "O\xCC\x88" + "U\xCC\x80" "U\xCC\x81" "U\xCC\x82" "U\xCC\x88" + "Y\xCC\x81"; + /* no such letter in Latin-1: s + caron, Y + diaeresis, q + acute; + * a mark with nothing before it, and one after a non-letter */ + char g[] = "s\xCC\x8C Y\xCC\x88 q\xCC\x81"; + char h[] = "\xCC\x88" "a"; + char k[] = "1\xCC\x81 @\xCC\x80 `\xCC\x80"; + /* the mark only joins the letter right before it */ + char l[] = "a\xCC\x88\xCC\x81"; + + ASSERT_EQ(odfs_utf8_to_latin1(a), 5); + ASSERT_STR_EQ(a, "\xC4rger"); + odfs_utf8_to_latin1(b); + ASSERT_STR_EQ(b, "Gr\xFC\xDF" "e"); + odfs_utf8_to_latin1(c); + ASSERT_STR_EQ(c, "caf\xE9"); + odfs_utf8_to_latin1(d); + ASSERT_STR_EQ(d, "gar\xE7on"); + odfs_utf8_to_latin1(e); + ASSERT_STR_EQ(e, "\xC5 \xF1 \xFF \xDD"); + odfs_utf8_to_latin1(f); + ASSERT_STR_EQ(f, "\xC0\xC1\xC2\xC3\xC4\xC5\xC7\xC8\xC9\xCA\xCB" + "\xCC\xCD\xCE\xCF\xD1\xD2\xD3\xD4\xD5\xD6" + "\xD9\xDA\xDB\xDC\xDD"); + odfs_utf8_to_latin1(g); + ASSERT_STR_EQ(g, "s? Y? q?"); + odfs_utf8_to_latin1(h); + ASSERT_STR_EQ(h, "?a"); + odfs_utf8_to_latin1(k); + ASSERT_STR_EQ(k, "1? @? `?"); + odfs_utf8_to_latin1(l); + ASSERT_STR_EQ(l, "\xE4?"); +#endif +} + TEST(utf8_to_latin1_empty) { char s[] = ""; From 04e4a6ecb54b8cff8a1ecc19ceae78dff5731e17 Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Wed, 25 Nov 2026 09:32:25 -0800 Subject: [PATCH 6/6] charset: replace what cannot be converted with '_', not '?' A character Latin-1 does not have came out as '?'. So did a lone UTF-16 surrogate, and a control character in a Mac name or in CD-Text. '?' is an AmigaDOS pattern character. On AROS, List ALL does not come back from a directory whose name has one. Having listed the parent, it asks for a lock on an empty name where it means the directory, gets the parent again, lists that, and so on without end. Seen on AROS i386 with a Joliet directory named in Czech, which came out as "?e?tina". AmigaOS 3.2 and 4.1 list the same disc without trouble. Use '_' in all those places. It means nothing in a pattern. --- README.md | 2 +- backends/cdda/cdda.c | 3 ++- backends/hfs/hfs_common.c | 3 ++- core/charset.c | 8 ++++---- include/odfs/charset.h | 13 ++++++++++--- tests/unit/test_charset.c | 18 +++++++++--------- tests/unit/test_hfs.c | 2 +- tests/unit/test_joliet.c | 6 +++--- tests/unit/test_namefix.c | 10 +++++----- tests/unit/test_symlink.c | 2 +- 10 files changed, 38 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 12c306b..bd6fd6d 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,7 @@ To prefer HFS or HFS+, use `Control = "HFSFIRST"`. Options can be combined, for Names from Joliet, UDF, HFS and HFS+ volumes, and Rock Ridge names written in UTF-8, are converted to ISO 8859-1 (Latin-1), the character set of AmigaOS. A -character that Latin-1 does not have is shown as `?`. Rock Ridge names that +character that Latin-1 does not have is shown as `_`. Rock Ridge names that already are in an 8-bit character set are left as they are. ### Name collisions diff --git a/backends/cdda/cdda.c b/backends/cdda/cdda.c index 2c20638..704b768 100644 --- a/backends/cdda/cdda.c +++ b/backends/cdda/cdda.c @@ -18,6 +18,7 @@ #include "cdda.h" #include "odfs/alloc.h" #include "odfs/cache.h" +#include "odfs/charset.h" #include "odfs/log.h" #include "odfs/error.h" #include "odfs/string.h" @@ -389,7 +390,7 @@ static size_t cdda_sanitize_ascii(char *dst, size_t dst_size, if (ch == '\r' || ch == '\n' || ch == '\t') ch = ' '; if (ch < 0x20 || ch > 0x7e) - ch = '?'; + ch = ODFS_CHAR_REPLACEMENT; dst[used++] = (char)ch; } diff --git a/backends/hfs/hfs_common.c b/backends/hfs/hfs_common.c index a7c1d4e..e7d6d4f 100644 --- a/backends/hfs/hfs_common.c +++ b/backends/hfs/hfs_common.c @@ -16,6 +16,7 @@ #include "hfs/hfs_common.h" #include "odfs/alloc.h" #include "odfs/cache.h" +#include "odfs/charset.h" #include "odfs/error.h" #include "odfs/namefix.h" #include "odfs/string.h" @@ -37,7 +38,7 @@ void odfs_hfs_fix_name(char *name, size_t len) else if (c == '/') name[i] = '-'; else if (c < 0x20 || c == 0x7F) - name[i] = '?'; + name[i] = ODFS_CHAR_REPLACEMENT; } } diff --git a/core/charset.c b/core/charset.c index 332a460..3ad3672 100644 --- a/core/charset.c +++ b/core/charset.c @@ -34,7 +34,7 @@ static int odfs_utf8_append(uint32_t cp, char *dst, size_t dst_size, size_t *di) } else { if (*di + 1 >= dst_size) return 0; - dst[(*di)++] = '?'; + dst[(*di)++] = ODFS_CHAR_REPLACEMENT; } return 1; @@ -67,7 +67,7 @@ odfs_err_t odfs_ucs2be_to_utf8(const uint8_t *src, size_t src_len, if (u1 >= 0xD800u && u1 <= 0xDFFFu) { /* a high surrogate followed by a low one is one code * point; any other surrogate stands for nothing */ - cp = '?'; + cp = ODFS_CHAR_REPLACEMENT; if (u1 <= 0xDBFFu && si + 3 < src_len) { uint16_t u2 = (uint16_t)(((uint16_t)src[si + 2] << 8) | src[si + 3]); @@ -124,7 +124,7 @@ odfs_err_t odfs_mac_roman_to_utf8(const uint8_t *src, size_t src_len, uint32_t cp; if (c < 0x20) - cp = '?'; + cp = ODFS_CHAR_REPLACEMENT; else if (c < 0x80) cp = c; else @@ -243,7 +243,7 @@ size_t odfs_utf8_to_latin1(char *s) } #endif /* U+0080-U+009F are control codes, CSI among them */ - c = (cp >= 0xA0u && cp <= 0xFFu) ? cp : '?'; + c = (cp >= 0xA0u && cp <= 0xFFu) ? cp : ODFS_CHAR_REPLACEMENT; } } *dst++ = (unsigned char)c; diff --git a/include/odfs/charset.h b/include/odfs/charset.h index f0f0539..67e2498 100644 --- a/include/odfs/charset.h +++ b/include/odfs/charset.h @@ -12,6 +12,13 @@ #include #include +/* + * What a character that cannot be converted is replaced with. Not '?': + * that is an AmigaDOS pattern character, and List ALL on AROS never + * comes back from a directory that has one in its name. + */ +#define ODFS_CHAR_REPLACEMENT '_' + /* * Convert UCS-2 / UTF-16 big-endian to UTF-8. * src — UCS-2BE / UTF-16BE input buffer @@ -21,7 +28,7 @@ * out_len — receives number of bytes written (excluding NUL) * * Output is always NUL-terminated if dst_size > 0. - * Unpaired surrogates and unmappable characters are replaced with '?'. + * Unpaired surrogates and unmappable characters are replaced with '_'. */ odfs_err_t odfs_ucs2be_to_utf8(const uint8_t *src, size_t src_len, char *dst, size_t dst_size, @@ -36,7 +43,7 @@ odfs_err_t odfs_ucs2be_to_utf8(const uint8_t *src, size_t src_len, * out_len - receives number of bytes written (excluding NUL) * * Output is always NUL-terminated if dst_size > 0. - * Control characters are replaced with '?'. + * Control characters are replaced with '_'. */ odfs_err_t odfs_mac_roman_to_utf8(const uint8_t *src, size_t src_len, char *dst, size_t dst_size, @@ -60,7 +67,7 @@ odfs_err_t odfs_iso_name_to_display(const char *src, size_t src_len, * Convert a UTF-8 string to ISO 8859-1 (Latin-1) in place and return * its new length; the result is never longer than the input. * - * A character Latin-1 does not have becomes '?'. A byte that is not + * A character Latin-1 does not have becomes '_'. A byte that is not * part of a well-formed UTF-8 sequence is kept as it is, so a string * that already is in an 8-bit character set, such as a Rock Ridge name * written by an Amiga mastering tool, passes through unchanged. diff --git a/tests/unit/test_charset.c b/tests/unit/test_charset.c index c30056e..84f5c40 100644 --- a/tests/unit/test_charset.c +++ b/tests/unit/test_charset.c @@ -80,7 +80,7 @@ TEST(ucs2be_surrogate_pairs) ASSERT_EQ(dst[5], '\0'); ASSERT_OK(odfs_ucs2be_to_utf8(lone, sizeof(lone), dst, sizeof(dst), &len)); - ASSERT_STR_EQ(dst, "?B??"); + ASSERT_STR_EQ(dst, "_B__"); ASSERT_EQ(len, 4); /* a pair that does not fit is left out whole */ @@ -128,7 +128,7 @@ TEST(mac_roman_controls) size_t len; ASSERT_OK(odfs_mac_roman_to_utf8(src, sizeof(src), dst, sizeof(dst), &len)); - ASSERT_STR_EQ(dst, "A?B"); + ASSERT_STR_EQ(dst, "A_B"); ASSERT_EQ(len, 3); } @@ -180,12 +180,12 @@ TEST(utf8_to_latin1_converts_latin1_range) TEST(utf8_to_latin1_replaces_other_characters) { - /* U+0100, U+20AC, U+65E5 U+672C, U+1F600: one '?' each */ + /* U+0100, U+20AC, U+65E5 U+672C, U+1F600: one '_' each */ char s[] = "a\xC4\x80" "b\xE2\x82\xAC" "c\xE6\x97\xA5\xE6\x9C\xAC" "d\xF0\x9F\x98\x80" "e"; ASSERT_EQ(odfs_utf8_to_latin1(s), 10); - ASSERT_STR_EQ(s, "a?b?c??d?e"); + ASSERT_STR_EQ(s, "a_b_c__d_e"); } TEST(utf8_to_latin1_replaces_c1_controls) @@ -194,7 +194,7 @@ TEST(utf8_to_latin1_replaces_c1_controls) char s[] = "\xC2\x80\xC2\x9B\xC2\x9F"; ASSERT_EQ(odfs_utf8_to_latin1(s), 3); - ASSERT_STR_EQ(s, "???"); + ASSERT_STR_EQ(s, "___"); } /* a name that is Latin-1 already must come out as it went in */ @@ -299,13 +299,13 @@ TEST(utf8_to_latin1_composes_accents) "\xCC\xCD\xCE\xCF\xD1\xD2\xD3\xD4\xD5\xD6" "\xD9\xDA\xDB\xDC\xDD"); odfs_utf8_to_latin1(g); - ASSERT_STR_EQ(g, "s? Y? q?"); + ASSERT_STR_EQ(g, "s_ Y_ q_"); odfs_utf8_to_latin1(h); - ASSERT_STR_EQ(h, "?a"); + ASSERT_STR_EQ(h, "_a"); odfs_utf8_to_latin1(k); - ASSERT_STR_EQ(k, "1? @? `?"); + ASSERT_STR_EQ(k, "1_ @_ `_"); odfs_utf8_to_latin1(l); - ASSERT_STR_EQ(l, "\xE4?"); + ASSERT_STR_EQ(l, "\xE4_"); #endif } diff --git a/tests/unit/test_hfs.c b/tests/unit/test_hfs.c index dc7ab27..15531a6 100644 --- a/tests/unit/test_hfs.c +++ b/tests/unit/test_hfs.c @@ -595,7 +595,7 @@ TEST(hfsplus_names_drop_amiga_separators) ASSERT_OK(list_root(&mnt, &list)); ASSERT_EQ(list.count, 2); ASSERT_STR_EQ(list.nodes[0].name, "AC-DC"); - ASSERT_STR_EQ(list.nodes[1].name, "Icon?"); + ASSERT_STR_EQ(list.nodes[1].name, "Icon_"); odfs_unmount(&mnt); odfs_free(img); } diff --git a/tests/unit/test_joliet.c b/tests/unit/test_joliet.c index ce48a96..a55e28d 100644 --- a/tests/unit/test_joliet.c +++ b/tests/unit/test_joliet.c @@ -303,8 +303,8 @@ TEST(joliet_names_in_latin1) joliet_collect_cb, &collect, NULL)); ASSERT_EQ(collect.count, 4); ASSERT_STR_EQ(collect.nodes[0].name, "Gr\xFC\xDF" "e"); - ASSERT_STR_EQ(collect.nodes[1].name, "??"); - ASSERT_STR_EQ(collect.nodes[2].name, "??~2"); + ASSERT_STR_EQ(collect.nodes[1].name, "__"); + ASSERT_STR_EQ(collect.nodes[2].name, "__~2"); ASSERT_STR_EQ(collect.nodes[3].name, "abc"); /* a name is found the way readdir spelled it, and only that way */ @@ -312,7 +312,7 @@ TEST(joliet_names_in_latin1) "Gr\xFC\xDF" "e", &node)); ASSERT_EQ(node.extent.lba, 21); ASSERT_OK(joliet_backend_ops.lookup(ctx, &cache, &log, &root, - "??~2", &node)); + "__~2", &node)); ASSERT_EQ(node.extent.lba, 23); ASSERT_ERR(joliet_backend_ops.lookup(ctx, &cache, &log, &root, "Gr\xC3\xBC\xC3\x9F" "e", &node), diff --git a/tests/unit/test_namefix.c b/tests/unit/test_namefix.c index 12b80ab..2d36e67 100644 --- a/tests/unit/test_namefix.c +++ b/tests/unit/test_namefix.c @@ -122,10 +122,10 @@ TEST(namefix_keeps_utf8_names_by_default) TEST(namefix_separates_names_that_convert_alike) { odfs_namefix_state_t state; - /* U+65E5 U+672C ".txt", U+4E2D U+56FD ".txt", and a literal "??.txt" */ + /* U+65E5 U+672C ".txt", U+4E2D U+56FD ".txt", and a literal "__.txt" */ char a[ODFS_NAME_MAX] = "\xE6\x97\xA5\xE6\x9C\xAC.txt"; char b[ODFS_NAME_MAX] = "\xE4\xB8\xAD\xE5\x9B\xBD.txt"; - char c[ODFS_NAME_MAX] = "??.txt"; + char c[ODFS_NAME_MAX] = "__.txt"; /* the same name once as UTF-8 and once as raw Latin-1 */ char d[ODFS_NAME_MAX] = "caf\xC3\xA9"; char e[ODFS_NAME_MAX] = "caf\xE9"; @@ -134,9 +134,9 @@ TEST(namefix_separates_names_that_convert_alike) ASSERT_OK(odfs_namefix_apply(&state, a, sizeof(a))); ASSERT_OK(odfs_namefix_apply(&state, b, sizeof(b))); ASSERT_OK(odfs_namefix_apply(&state, c, sizeof(c))); - ASSERT_STR_EQ(a, "??.txt"); - ASSERT_STR_EQ(b, "??.txt~2"); - ASSERT_STR_EQ(c, "??.txt~3"); + ASSERT_STR_EQ(a, "__.txt"); + ASSERT_STR_EQ(b, "__.txt~2"); + ASSERT_STR_EQ(c, "__.txt~3"); ASSERT_OK(odfs_namefix_apply(&state, d, sizeof(d))); ASSERT_OK(odfs_namefix_apply(&state, e, sizeof(e))); ASSERT_STR_EQ(d, "caf\xE9"); diff --git a/tests/unit/test_symlink.c b/tests/unit/test_symlink.c index 26da922..14e669d 100644 --- a/tests/unit/test_symlink.c +++ b/tests/unit/test_symlink.c @@ -352,7 +352,7 @@ TEST(readlink_follows_the_mount_charset) ASSERT_OK(odfs_readlink(&mnt, &mnt.root, "L\xE4nk", target, sizeof(target))); - ASSERT_STR_EQ(target, "z\xFCl/?"); + ASSERT_STR_EQ(target, "z\xFCl/_"); ASSERT_ERR(odfs_readlink(&mnt, &mnt.root, "L\xC3\xA4nk", target, sizeof(target)), ODFS_ERR_NOT_FOUND);