Skip to content
Closed
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
82 changes: 41 additions & 41 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
39 changes: 39 additions & 0 deletions tests/unit/test_udf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading