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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion backends/cdda/cdda.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions backends/hfs/hfs_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -443,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);
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,
Expand Down
4 changes: 3 additions & 1 deletion 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);
odfs_namefix_init(&namefix, cache->name_charset);

while (offset < dir_size) {
uint32_t entry_start = offset;
Expand Down Expand Up @@ -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;

Expand Down
84 changes: 42 additions & 42 deletions backends/udf/udf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/* ------------------------------------------------------------------ */
Expand Down Expand Up @@ -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);
Expand Down
81 changes: 78 additions & 3 deletions core/charset.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -179,6 +179,81 @@ odfs_err_t odfs_iso_name_to_display(const char *src, size_t src_len,
return ODFS_OK;
}

#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;
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;
#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 : ODFS_CHAR_REPLACEMENT;
}
}
*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)
{
Expand Down
13 changes: 11 additions & 2 deletions core/mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "odfs/api.h"
#include "odfs/ancestry.h"
#include "odfs/charset.h"
#include "odfs/string.h"
#include <string.h>
#include <inttypes.h>
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
Loading
Loading