Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ AMIGA_PLATFORM_INCLUDES = -I platform/amiga \
-I platform/amiga/$(AMIGA_OSDIR)
AMIGA_INCLUDES = $(INCLUDES) $(AMIGA_PLATFORM_INCLUDES) \
$(if $(NDK_PATH),-I$(NDK_PATH))
# codesets.library headers, after the system's own
AMIGA_INCLUDES += -idirafter 3rdparty/libcodesets/include

# ---- optional 3rdparty submodules ----

Expand Down Expand Up @@ -217,8 +219,12 @@ CORE_SRCS = $(ODFS_CORE_SRCS)
# Host-only sources
HOST_SRCS = platform/host/file_media.c

# Amiga handler sources
AMIGA_SRCS = platform/amiga/handler_main.c \
# Amiga handler sources. The linker places handler_main.o and the
# objects after it in front of the core, so that code added to them moves
# every routine of the core. system_charset.c is listed first to end up
# behind the core and the backends instead.
AMIGA_SRCS = platform/amiga/system_charset.c \
platform/amiga/handler_main.c \
platform/amiga/printf_local.c \
platform/amiga/$(AMIGA_OSDIR)/sys_compat.c
ifeq ($(AMIGA_TARGET),os4)
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,15 @@ To prefer HFS or HFS+, use `Control = "HFSFIRST"`. Options can be combined, for
### 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.
UTF-8, are converted to the 8-bit character set of the system. A character
that set does not have is shown as `_`. Rock Ridge names that already are in
an 8-bit character set are left as they are.

The character set is ISO 8859-1 (Latin-1), unless `codesets.library` is
installed. The handler then asks it, the first time the drive is used, for the
system's default character set: a system set up for Polish gets names in
ISO 8859-2, for example. Names stay in Latin-1 in the ROM build, and when the
system was booted from the disc in the drive.

### Name collisions

Expand Down
2 changes: 1 addition & 1 deletion backends/hfs/hfs_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,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, cache->name_charset);
odfs_namefix_init(&rc.namefix, &cache->names);

/* dir->extent.lba stores the directory's CNID */
err = odfs_hfs_walk_catalog(vol, cache, dir->extent.lba,
Expand Down
4 changes: 2 additions & 2 deletions backends/iso9660/iso9660.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, cache->name_charset);
odfs_namefix_init(&namefix, &cache->names);

while (offset < dir_size) {
uint32_t entry_start = offset;
Expand Down Expand Up @@ -854,7 +854,7 @@ 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);
odfs_name_to_charset(node.name, &cache->names);
if (odfs_strcasecmp(node.name, name) != 0)
continue;

Expand Down
2 changes: 1 addition & 1 deletion backends/udf/udf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,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, cache->name_charset);
odfs_namefix_init(&walk.namefix, &cache->names);
err = udf_walk_fids(ctx, cache, dir, udf_decode_dir_entry, &walk,
dir_size_out);
odfs_namefix_destroy(&walk.namefix);
Expand Down
30 changes: 27 additions & 3 deletions core/charset.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,27 @@ static unsigned int odfs_latin1_compose(unsigned int base, unsigned int mark)
}
#endif

size_t odfs_utf8_to_latin1(char *s)
/* the byte character cp has in the 8-bit set of map, or 0 for none */
static unsigned int odfs_8bit_code(unsigned int cp, const uint16_t *map)
{
#if ODFS_FEATURE_CHARSET_EXTERNAL
if (map) {
unsigned int i;

for (i = 0; i < ODFS_CHARSET_MAP_SIZE; i++) {
if (map[i] == cp)
return ODFS_CHARSET_MAP_FIRST + i;
}
return 0;
}
#else
(void)map;
#endif
/* U+0080-U+009F are control codes, CSI among them */
return (cp >= 0xA0u && cp <= 0xFFu) ? cp : 0;
}

size_t odfs_utf8_to_8bit(char *s, const uint16_t *map)
{
const unsigned char *src = (const unsigned char *)s;
unsigned char *dst = (unsigned char *)s;
Expand All @@ -235,15 +255,19 @@ size_t odfs_utf8_to_latin1(char *s)
src += more;
#if ODFS_FEATURE_CHARSET_COMPOSE
if (cp - 0x300u < 0x28u && dst != (unsigned char *)s) {
/* a Latin-1 code is the letter's code point too */
c = odfs_latin1_compose(dst[-1], cp - 0x300u);
if (c != 0)
c = odfs_8bit_code(c, map);
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 : ODFS_CHAR_REPLACEMENT;
c = odfs_8bit_code(cp, map);
if (c == 0)
c = ODFS_CHAR_REPLACEMENT;
}
}
*dst++ = (unsigned char)c;
Expand Down
7 changes: 4 additions & 3 deletions core/mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ 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;
mnt->cache.names.charset = (uint8_t)mnt->opts.name_charset;
mnt->cache.names.map = mnt->opts.name_map;

ODFS_INFO(&mnt->log, ODFS_SUB_MOUNT,
"cache initialized: %" PRIu32 " blocks", cache_size);
Expand Down Expand Up @@ -507,7 +508,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);
odfs_name_to_charset(mnt->volume_name, &mnt->cache.names);
if (mnt->backend_ops->get_volume_size)
mnt->total_blocks =
mnt->backend_ops->get_volume_size(mnt->backend_ctx);
Expand Down Expand Up @@ -717,7 +718,7 @@ odfs_err_t odfs_readlink(odfs_mount_t *mnt,
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);
odfs_name_to_charset(buf, &mnt->cache.names);
return err;
}

Expand Down
11 changes: 6 additions & 5 deletions core/namefix.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

#define ODFS_NAMEFIX_CHUNK_SIZE 4096u

void odfs_namefix_init(odfs_namefix_state_t *state, unsigned int charset)
void odfs_namefix_init(odfs_namefix_state_t *state,
const odfs_names_t *names)
{
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;
state->names = names;
}

void odfs_namefix_destroy(odfs_namefix_state_t *state)
Expand All @@ -35,7 +36,7 @@ void odfs_namefix_destroy(odfs_namefix_state_t *state)
chunk = next;
}

odfs_namefix_init(state, state->charset);
odfs_namefix_init(state, state->names);
}

static void *odfs_namefix_alloc(odfs_namefix_state_t *state, size_t size)
Expand Down Expand Up @@ -142,7 +143,7 @@ odfs_err_t odfs_namefix_apply(odfs_namefix_state_t *state,
* 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);
odfs_name_to_charset(name, state->names);
hash = odfs_namefix_hash(name, &len);
} else {
hash ^= hash >> 16;
Expand Down Expand Up @@ -182,7 +183,7 @@ 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 (state->charset == ODFS_NAMES_UTF8 && keep > 0 &&
while (state->names->charset == ODFS_NAMES_UTF8 && keep > 0 &&
((unsigned char)base[keep] & 0xC0u) == 0x80u)
keep--;
}
Expand Down
2 changes: 2 additions & 0 deletions include/odfs/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ typedef struct odfs_mount_opts {
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 */
const uint16_t *name_map; /* ODFS_NAMES_MAPPED: its map, which must
stay valid while mounted */
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) */
Expand Down
9 changes: 5 additions & 4 deletions include/odfs/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define ODFS_CACHE_H

#include "odfs/config.h"
#include "odfs/charset.h"
#include "odfs/error.h"
#include "odfs/media.h"
#include <stdint.h>
Expand Down Expand Up @@ -65,10 +66,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_names_t names; /* character set the mount asked
for; kept here because the
cache is what every backend
call is handed */
} odfs_cache_t;

/* lifecycle */
Expand Down
43 changes: 31 additions & 12 deletions include/odfs/charset.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,32 @@ odfs_err_t odfs_iso_name_to_display(const char *src, size_t src_len,
char *dst, size_t dst_size,
int lowercase);

/*
* An 8-bit character set that has ASCII in its lower half is described
* by a map: the Unicode code point of each byte from 0xA0 to 0xFF, or 0
* for a byte without a character. Bytes 0x80 to 0x9F are left out: the
* Amiga console takes them as control codes in every character set.
*/
#define ODFS_CHARSET_MAP_FIRST 0xA0u
#define ODFS_CHARSET_MAP_SIZE 96u

#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.
* Convert a UTF-8 string to an 8-bit character set in place and return
* its new length; the result is never longer than the input. A NULL
* map stands for ISO 8859-1 (Latin-1); only a build with
* ODFS_FEATURE_CHARSET_EXTERNAL takes another one.
*
* A character Latin-1 does not have becomes '_'. A byte that is not
* A character the set 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.
*
* 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.
* the letter before it where Latin-1 and the set have the accented
* letter: "u" followed by U+0308 becomes u-diaeresis.
*/
size_t odfs_utf8_to_latin1(char *s);
size_t odfs_utf8_to_8bit(char *s, const uint16_t *map);
#endif

/*
Expand All @@ -85,17 +96,25 @@ size_t odfs_utf8_to_latin1(char *s);
* 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() */
#define ODFS_NAMES_LATIN1 1u /* ISO 8859-1 */
#define ODFS_NAMES_MAPPED 2u /* the 8-bit set of odfs_names_t.map */

typedef struct odfs_names {
uint8_t charset; /* ODFS_NAMES_xxx */
const uint16_t *map; /* for ODFS_NAMES_MAPPED; the mount's
owner keeps it while mounted */
} odfs_names_t;

/* convert a UTF-8 name in place to character set charset */
static inline void odfs_name_to_charset(char *name, unsigned int charset)
/* convert a UTF-8 name in place to the character set of names */
static inline void odfs_name_to_charset(char *name, const odfs_names_t *names)
{
#if ODFS_FEATURE_CHARSET_BUILTIN
if (charset == ODFS_NAMES_LATIN1)
odfs_utf8_to_latin1(name);
if (names->charset != ODFS_NAMES_UTF8)
odfs_utf8_to_8bit(name, names->charset == ODFS_NAMES_MAPPED ?
names->map : NULL);
#else
(void)name;
(void)charset;
(void)names;
#endif
}

Expand Down
13 changes: 9 additions & 4 deletions include/odfs/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@
/* ---------- 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.
* 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.
* EXTERNAL: convert to another 8-bit character set instead when the
* mount hands in its map. The Amiga handler asks
* codesets.library for the map of the system's set.
*/
#ifdef ODFS_PROFILE_ROM
#define ODFS_FEATURE_CHARSET_BUILTIN 1
Expand All @@ -116,7 +119,9 @@
#else
#define ODFS_FEATURE_CHARSET_BUILTIN 1
#define ODFS_FEATURE_CHARSET_COMPOSE 1
#define ODFS_FEATURE_CHARSET_EXTERNAL 1
#ifndef ODFS_FEATURE_CHARSET_EXTERNAL
#define ODFS_FEATURE_CHARSET_EXTERNAL 1
#endif
#endif

/* ---------- cache sizing defaults ---------- */
Expand Down
6 changes: 4 additions & 2 deletions include/odfs/namefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef ODFS_NAMEFIX_H
#define ODFS_NAMEFIX_H

#include "odfs/charset.h"
#include "odfs/error.h"
#include <stddef.h>

Expand All @@ -31,12 +32,13 @@ 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 */
const odfs_names_t *names; /* character set of the fixed names */
void *inline_buf[ODFS_NAMEFIX_INLINE / sizeof(void *)];
} odfs_namefix_state_t;

/* 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_init(odfs_namefix_state_t *state,
const odfs_names_t *names);
void odfs_namefix_destroy(odfs_namefix_state_t *state);

/*
Expand Down
3 changes: 3 additions & 0 deletions platform/amiga/aros/amiga_target_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
struct ExecBase;
extern struct ExecBase *SysBase;

/* the process that loads libraries and devices from disk */
#define ODFS_AMIGA_LOADER_TASK "Lib & Dev Loader Daemon"

#ifndef ACTION_SHUTDOWN
#define ACTION_SHUTDOWN 3000
#endif
Expand Down
Loading
Loading