From 417455924d388f5e86d7a2d17b553ccc155b16db Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Sat, 28 Nov 2026 14:42:35 -0800 Subject: [PATCH 1/2] odfs: share the byte-order helpers Each backend had its own copy of the little- and big-endian loads: iso_read_le16/32 in the ISO 9660 backend, with Rock Ridge wrapping it, udf_le16/32/64 for UDF, hfs_be16/32/64 for HFS and HFS+, store helpers in the CDDA backend, and open-coded byte assembly in session.c, charset.c and the host file media. Only the ISO copy loaded its field with one move on the 68020. Put them in one place, include/odfs/byteorder.h, as odfs_le16/32/64, odfs_be16/32/64 and odfs_put_le16/32 and odfs_put_be16/32. The 68020 load is kept for odfs_le32, so the UDF backend now gets it too: the 68020 build shrinks by 832 bytes, all of it in udf.o. The 68000 and ROM builds differ only in charset.c, where the UTF-16 decode now takes the address of the code unit and GCC allocates registers a little differently. The endian tests move from test_iso9660.c to a new test_byteorder.c, which also covers the big-endian, 64-bit and store helpers and loads at odd addresses. handler_main.c still builds its SCSI fields by hand: its packet loop is sensitive to any change in code generation. --- backends/cdda/cdda.c | 63 +++++++--------------- backends/hfs/hfs.c | 36 ++++++------- backends/hfs/hfs_common.c | 64 +++++++++++----------- backends/hfs/hfs_common.h | 19 +------ backends/hfsplus/hfsplus.c | 50 ++++++++--------- backends/iso9660/iso9660.c | 16 +++--- backends/iso9660/iso9660.h | 27 +--------- backends/rock_ridge/rock_ridge.c | 19 +++---- backends/udf/udf.c | 64 +++++++++++----------- backends/udf/udf.h | 19 +------ core/charset.c | 6 +-- core/session.c | 6 +-- include/odfs/byteorder.h | 93 ++++++++++++++++++++++++++++++++ platform/host/file_media.c | 4 +- tests/unit/test_byteorder.c | 90 +++++++++++++++++++++++++++++++ tests/unit/test_iso9660.c | 26 --------- 16 files changed, 333 insertions(+), 269 deletions(-) create mode 100644 include/odfs/byteorder.h create mode 100644 tests/unit/test_byteorder.c diff --git a/backends/cdda/cdda.c b/backends/cdda/cdda.c index 704b768..a448ef3 100644 --- a/backends/cdda/cdda.c +++ b/backends/cdda/cdda.c @@ -17,6 +17,7 @@ #include "cdda.h" #include "odfs/alloc.h" +#include "odfs/byteorder.h" #include "odfs/cache.h" #include "odfs/charset.h" #include "odfs/log.h" @@ -40,34 +41,6 @@ /* Header generation */ /* ------------------------------------------------------------------ */ -static void cdda_write_le16(uint8_t *p, uint16_t v) -{ - p[0] = (uint8_t)(v); - p[1] = (uint8_t)(v >> 8); -} - -static void cdda_write_le32(uint8_t *p, uint32_t v) -{ - p[0] = (uint8_t)(v); - p[1] = (uint8_t)(v >> 8); - p[2] = (uint8_t)(v >> 16); - p[3] = (uint8_t)(v >> 24); -} - -static void cdda_write_be16(uint8_t *p, uint16_t v) -{ - p[0] = (uint8_t)(v >> 8); - p[1] = (uint8_t)(v); -} - -static void cdda_write_be32(uint8_t *p, uint32_t v) -{ - p[0] = (uint8_t)(v >> 24); - p[1] = (uint8_t)(v >> 16); - p[2] = (uint8_t)(v >> 8); - p[3] = (uint8_t)(v); -} - static uint32_t cdda_header_size(cdda_file_format_t format) { return (format == CDDA_FILE_FORMAT_AIFF) @@ -924,18 +897,18 @@ static void cdda_build_wav_header(uint8_t *hdr, uint32_t data_size) uint16_t block_align = CDDA_CHANNELS * (CDDA_BITS_PER_SAMPLE / 8); memcpy(&hdr[0], "RIFF", 4); - cdda_write_le32(&hdr[4], 36 + data_size); /* file size - 8 */ + odfs_put_le32(&hdr[4], 36 + data_size); /* file size - 8 */ memcpy(&hdr[8], "WAVE", 4); memcpy(&hdr[12], "fmt ", 4); - cdda_write_le32(&hdr[16], 16); /* fmt chunk size */ - cdda_write_le16(&hdr[20], 1); /* PCM format */ - cdda_write_le16(&hdr[22], CDDA_CHANNELS); - cdda_write_le32(&hdr[24], CDDA_SAMPLE_RATE); - cdda_write_le32(&hdr[28], byte_rate); - cdda_write_le16(&hdr[32], block_align); - cdda_write_le16(&hdr[34], CDDA_BITS_PER_SAMPLE); + odfs_put_le32(&hdr[16], 16); /* fmt chunk size */ + odfs_put_le16(&hdr[20], 1); /* PCM format */ + odfs_put_le16(&hdr[22], CDDA_CHANNELS); + odfs_put_le32(&hdr[24], CDDA_SAMPLE_RATE); + odfs_put_le32(&hdr[28], byte_rate); + odfs_put_le16(&hdr[32], block_align); + odfs_put_le16(&hdr[34], CDDA_BITS_PER_SAMPLE); memcpy(&hdr[36], "data", 4); - cdda_write_le32(&hdr[40], data_size); + odfs_put_le32(&hdr[40], data_size); } static void cdda_build_aiff_header(uint8_t *hdr, uint32_t data_size) @@ -947,18 +920,18 @@ static void cdda_build_aiff_header(uint8_t *hdr, uint32_t data_size) (CDDA_CHANNELS * (CDDA_BITS_PER_SAMPLE / 8)); memcpy(&hdr[0], "FORM", 4); - cdda_write_be32(&hdr[4], 46 + data_size); + odfs_put_be32(&hdr[4], 46 + data_size); memcpy(&hdr[8], "AIFF", 4); memcpy(&hdr[12], "COMM", 4); - cdda_write_be32(&hdr[16], 18); - cdda_write_be16(&hdr[20], CDDA_CHANNELS); - cdda_write_be32(&hdr[22], sample_frames); - cdda_write_be16(&hdr[26], CDDA_BITS_PER_SAMPLE); + odfs_put_be32(&hdr[16], 18); + odfs_put_be16(&hdr[20], CDDA_CHANNELS); + odfs_put_be32(&hdr[22], sample_frames); + odfs_put_be16(&hdr[26], CDDA_BITS_PER_SAMPLE); memcpy(&hdr[28], rate_44100, sizeof(rate_44100)); memcpy(&hdr[38], "SSND", 4); - cdda_write_be32(&hdr[42], 8 + data_size); - cdda_write_be32(&hdr[46], 0); - cdda_write_be32(&hdr[50], 0); + odfs_put_be32(&hdr[42], 8 + data_size); + odfs_put_be32(&hdr[46], 0); + odfs_put_be32(&hdr[50], 0); } static void cdda_build_header(cdda_file_format_t format, diff --git a/backends/hfs/hfs.c b/backends/hfs/hfs.c index f200c50..56c7fcb 100644 --- a/backends/hfs/hfs.c +++ b/backends/hfs/hfs.c @@ -44,7 +44,7 @@ static int hfs_check_part(odfs_cache_t *cache, uint64_t part_byte, void *ctx) { const uint8_t *mdb = odfs_hfs_read_block(cache, part_byte + 1024); - if (!mdb || hfs_be16(mdb) != HFS_MDB_SIG) + if (!mdb || odfs_be16(mdb) != HFS_MDB_SIG) return 0; *(uint64_t *)ctx = part_byte; return 1; @@ -96,10 +96,10 @@ static int hfs_parse_entry(const hfs_volume_t *vol, const odfs_node_t *dir, /* hide what the Finder hides: fdFlags of a file's FInfo, frFlags * of a directory's DInfo */ if (rec_type == HFS_CAT_FILE && data_len >= 14 && - (hfs_be16(&data[12]) & HFS_FINDER_INVISIBLE)) + (odfs_be16(&data[12]) & HFS_FINDER_INVISIBLE)) return 0; if (rec_type == HFS_CAT_DIR && data_len >= 32 && - (hfs_be16(&data[30]) & HFS_FINDER_INVISIBLE)) + (odfs_be16(&data[30]) & HFS_FINDER_INVISIBLE)) return 0; memset(node, 0, sizeof(*node)); @@ -117,20 +117,20 @@ static int hfs_parse_entry(const hfs_volume_t *vol, const odfs_node_t *dir, if (rec_type == HFS_CAT_DIR && data_len >= 22) { node->kind = ODFS_NODE_DIR; - node->id = hfs_be32(&data[6]); /* dirDirID */ + node->id = odfs_be32(&data[6]); /* dirDirID */ node->extent.lba = node->id; /* store CNID for readdir */ - odfs_timestamp_from_mac_time(hfs_be32(&data[14]), &node->mtime); + odfs_timestamp_from_mac_time(odfs_be32(&data[14]), &node->mtime); } else if (rec_type == HFS_CAT_FILE && data_len >= 52) { node->kind = ODFS_NODE_FILE; - node->id = hfs_be32(&data[20]); /* filFlNum */ - node->size = hfs_be32(&data[26]); /* data fork logical length */ + node->id = odfs_be32(&data[20]); /* filFlNum */ + node->size = odfs_be32(&data[26]); /* data fork logical length */ /* data fork first extent: at offset 74 in file record */ if (data_len >= 78) { - node->extent.lba = hfs_be16(&data[74]); /* first extent start AB */ + node->extent.lba = odfs_be16(&data[74]); /* first extent start AB */ node->extent.length = - odfs_hfs_extent_bytes(vol, hfs_be16(&data[76])); + odfs_hfs_extent_bytes(vol, odfs_be16(&data[76])); } - odfs_timestamp_from_mac_time(hfs_be32(&data[48]), &node->mtime); + odfs_timestamp_from_mac_time(odfs_be32(&data[48]), &node->mtime); } node->ctime = node->mtime; return 1; @@ -168,9 +168,9 @@ static odfs_err_t hfs_mount(odfs_cache_t *cache, } /* parse MDB */ - vol->total_blocks = hfs_be16(&mdb[HFS_MDB_NMALBLKS]); - vol->block_size = hfs_be32(&mdb[HFS_MDB_ALBLKSIZ]); - vol->alloc_base = vol_byte + (uint64_t)hfs_be16(&mdb[HFS_MDB_ALBLST]) * 512; + vol->total_blocks = odfs_be16(&mdb[HFS_MDB_NMALBLKS]); + vol->block_size = odfs_be32(&mdb[HFS_MDB_ALBLKSIZ]); + vol->alloc_base = vol_byte + (uint64_t)odfs_be16(&mdb[HFS_MDB_ALBLST]) * 512; uint8_t vnamelen = mdb[HFS_MDB_VOLNAMELEN]; if (vnamelen > 27) vnamelen = 27; @@ -178,14 +178,14 @@ static odfs_err_t hfs_mount(odfs_cache_t *cache, vol->volume_name, HFS_VOLNAME_SIZE); for (int i = 0; i < 3; i++) { - vol->cat.extents[i].start_block = hfs_be16(&mdb[HFS_MDB_CTEXTREC + i * 4]); - vol->cat.extents[i].block_count = hfs_be16(&mdb[HFS_MDB_CTEXTREC + i * 4 + 2]); - vol->ext.extents[i].start_block = hfs_be16(&mdb[HFS_MDB_XTEXTREC + i * 4]); - vol->ext.extents[i].block_count = hfs_be16(&mdb[HFS_MDB_XTEXTREC + i * 4 + 2]); + vol->cat.extents[i].start_block = odfs_be16(&mdb[HFS_MDB_CTEXTREC + i * 4]); + vol->cat.extents[i].block_count = odfs_be16(&mdb[HFS_MDB_CTEXTREC + i * 4 + 2]); + vol->ext.extents[i].start_block = odfs_be16(&mdb[HFS_MDB_XTEXTREC + i * 4]); + vol->ext.extents[i].block_count = odfs_be16(&mdb[HFS_MDB_XTEXTREC + i * 4 + 2]); } /* MDB modification date for root timestamp */ - mtime = hfs_be32(&mdb[HFS_MDB_LSMOD]); + mtime = odfs_be32(&mdb[HFS_MDB_LSMOD]); ODFS_INFO(log, ODFS_SUB_HFS, "volume: \"%s\", alloc blocks: %" PRIu32 ", block size: %" PRIu32, diff --git a/backends/hfs/hfs_common.c b/backends/hfs/hfs_common.c index 1c29fe0..940ff9a 100644 --- a/backends/hfs/hfs_common.c +++ b/backends/hfs/hfs_common.c @@ -75,14 +75,14 @@ int odfs_hfs_scan_apm(odfs_cache_t *cache, uint32_t session_start, const uint8_t *pm = odfs_hfs_read_block(cache, base + i * 512u); uint64_t part_byte; - if (!pm || hfs_be16(pm) != HFS_APM_SIG) + if (!pm || odfs_be16(pm) != HFS_APM_SIG) break; if (i == 1) - count = hfs_be32(&pm[4]); /* pmMapBlkCnt */ + count = odfs_be32(&pm[4]); /* pmMapBlkCnt */ if (memcmp(&pm[48], "Apple_HFS", 9) != 0) /* pmPartType */ continue; - part_byte = base + (uint64_t)hfs_be32(&pm[8]) * 512; /* pmPyPartStart */ + part_byte = base + (uint64_t)odfs_be32(&pm[8]) * 512; /* pmPyPartStart */ if (check(cache, part_byte, ctx)) return 1; } @@ -159,7 +159,7 @@ static uint32_t hfs_rec_offset(const uint8_t *node, uint32_t node_size, uint32_t rec_idx) { /* offset table is at end of node, growing backward: 2 bytes per entry */ - return hfs_be16(&node[node_size - 2u * (rec_idx + 1u)]); + return odfs_be16(&node[node_size - 2u * (rec_idx + 1u)]); } /* @@ -175,7 +175,7 @@ static int hfs_read_record_count(const uint8_t *node, uint32_t node_size, if (node_size < 14) return 0; - nrecs = hfs_be16(&node[10]); + nrecs = odfs_be16(&node[10]); if (((uint32_t)nrecs + 1u) * 2u + 14u > node_size) return 0; @@ -186,7 +186,7 @@ static int hfs_read_record_count(const uint8_t *node, uint32_t node_size, static uint32_t hfs_key_length(const hfs_volume_t *vol, const uint8_t *key) { - return vol->hfsplus ? 2u + hfs_be16(key) : 1u + key[0]; + return vol->hfsplus ? 2u + odfs_be16(key) : 1u + key[0]; } odfs_err_t odfs_hfs_init_btree(const hfs_volume_t *vol, hfs_btree_t *tree, @@ -203,9 +203,9 @@ odfs_err_t odfs_hfs_init_btree(const hfs_volume_t *vol, hfs_btree_t *tree, if (hdr[8] != HFS_NODE_HEADER) return ODFS_ERR_BAD_FORMAT; - tree->root_node = hfs_be32(&hdr[16]); - tree->node_size = hfs_be16(&hdr[32]); - tree->total_nodes = hfs_be32(&hdr[36]); + tree->root_node = odfs_be32(&hdr[16]); + tree->node_size = odfs_be16(&hdr[32]); + tree->total_nodes = odfs_be32(&hdr[36]); if (tree->node_size < 14 || tree->node_size > 32768) return ODFS_ERR_BAD_FORMAT; @@ -291,8 +291,8 @@ odfs_err_t odfs_hfs_walk_catalog(const hfs_volume_t *vol, if (ptr_off + 4 > limit) break; - key_parent = hfs_be32(&node[off + 2]); - ptr = hfs_be32(&node[ptr_off]); + key_parent = odfs_be32(&node[off + 2]); + ptr = odfs_be32(&node[ptr_off]); /* a directory's records can start in the child before the * first key naming it */ @@ -331,7 +331,7 @@ odfs_err_t odfs_hfs_walk_catalog(const hfs_volume_t *vol, if (key_len < 6 || key_len > limit - off) continue; - key_parent = hfs_be32(&node[off + 2]); + key_parent = odfs_be32(&node[off + 2]); if (key_parent < parent_cnid) continue; if (key_parent > parent_cnid) @@ -354,7 +354,7 @@ odfs_err_t odfs_hfs_walk_catalog(const hfs_volume_t *vol, /* follow forward link to next leaf */ prev_node = cur_node; - cur_node = hfs_be32(&node[0]); + cur_node = odfs_be32(&node[0]); if (cur_node == 0) return ODFS_OK; if (++steps >= tree->total_nodes) @@ -365,7 +365,7 @@ odfs_err_t odfs_hfs_walk_catalog(const hfs_volume_t *vol, return err; /* the leaf chain is doubly linked; a self-link or a jump out of * the chain shows up as a back link that does not match */ - if (hfs_be32(&node[4]) != prev_node) + if (odfs_be32(&node[4]) != prev_node) return ODFS_ERR_CORRUPT; } } @@ -486,21 +486,21 @@ static odfs_err_t hfs_inline_ext_cb(const uint8_t *key, size_t key_len, if (ie->hfsplus) { /* fileID at 8, data fork extents at 104 */ - if (data_len < 168 || hfs_be16(data) != HFS_CAT_FILE || - hfs_be32(&data[8]) != ie->file_id) + if (data_len < 168 || odfs_be16(data) != HFS_CAT_FILE || + odfs_be32(&data[8]) != ie->file_id) return ODFS_OK; for (int i = 0; i < 8; i++) { - ie->exts[i].start_block = hfs_be32(&data[104 + i * 8]); - ie->exts[i].block_count = hfs_be32(&data[108 + i * 8]); + ie->exts[i].start_block = odfs_be32(&data[104 + i * 8]); + ie->exts[i].block_count = odfs_be32(&data[108 + i * 8]); } } else { /* filFlNum at 20, filExtRec at 74 */ if (data_len < 86 || data[0] != HFS_CAT_FILE || - hfs_be32(&data[20]) != ie->file_id) + odfs_be32(&data[20]) != ie->file_id) return ODFS_OK; for (int i = 0; i < 3; i++) { - ie->exts[i].start_block = hfs_be16(&data[74 + i * 4]); - ie->exts[i].block_count = hfs_be16(&data[76 + i * 4]); + ie->exts[i].start_block = odfs_be16(&data[74 + i * 4]); + ie->exts[i].block_count = odfs_be16(&data[76 + i * 4]); } } @@ -580,9 +580,9 @@ static odfs_err_t hfs_find_overflow(const hfs_volume_t *vol, if (off < 14 || off + min_key > limit) break; - id = hfs_be32(&node[off + id_off]); - start = vol->hfsplus ? hfs_be32(&node[off + 8]) - : hfs_be16(&node[off + 6]); + id = odfs_be32(&node[off + id_off]); + start = vol->hfsplus ? odfs_be32(&node[off + 8]) + : odfs_be16(&node[off + 6]); if (id > file_id || (id == file_id && (node[off + fork_off] != 0 || @@ -601,11 +601,11 @@ static odfs_err_t hfs_find_overflow(const hfs_volume_t *vol, if (node[8] == HFS_NODE_INDEX) { if (ptr_off < best + min_key || ptr_off + 4 > limit) return ODFS_ERR_CORRUPT; - cur_node = hfs_be32(&node[ptr_off]); + cur_node = odfs_be32(&node[ptr_off]); continue; } - if (hfs_be32(&node[best + id_off]) != file_id || + if (odfs_be32(&node[best + id_off]) != file_id || node[best + fork_off] != 0 || ptr_off < best + min_key || ptr_off + (vol->hfsplus ? 64u : 12u) > limit) return ODFS_ERR_CORRUPT; @@ -614,9 +614,9 @@ static odfs_err_t hfs_find_overflow(const hfs_volume_t *vol, for (uint32_t i = 0; i < (vol->hfsplus ? 8u : 3u); i++) { const uint8_t *e = &node[ptr_off + i * (vol->hfsplus ? 8u : 4u)]; - exts[i].start_block = vol->hfsplus ? hfs_be32(e) : hfs_be16(e); - exts[i].block_count = vol->hfsplus ? hfs_be32(e + 4) - : hfs_be16(e + 2); + exts[i].start_block = vol->hfsplus ? odfs_be32(e) : odfs_be16(e); + exts[i].block_count = vol->hfsplus ? odfs_be32(e + 4) + : odfs_be16(e + 2); } *base = (uint64_t)best_start * vol->block_size; return ODFS_OK; @@ -806,14 +806,14 @@ static odfs_err_t hfs_thread_cb(const uint8_t *key, size_t key_len, if (info->hfsplus) { /* type(2) reserved(2) parentID(4) nodeName */ - if (data_len < 8 || hfs_be16(data) != HFS_CAT_DTHREAD) + if (data_len < 8 || odfs_be16(data) != HFS_CAT_DTHREAD) return ODFS_OK; - info->parent_cnid = hfs_be32(&data[4]); + info->parent_cnid = odfs_be32(&data[4]); } else { /* type(1) reserved(1) reserved(8) parentID(4) nodeName */ if (data_len < 14 || data[0] != HFS_CAT_DTHREAD) return ODFS_OK; - info->parent_cnid = hfs_be32(&data[10]); + info->parent_cnid = odfs_be32(&data[10]); } info->found = 1; diff --git a/backends/hfs/hfs_common.h b/backends/hfs/hfs_common.h index cd5dac3..c49a550 100644 --- a/backends/hfs/hfs_common.h +++ b/backends/hfs/hfs_common.h @@ -10,6 +10,7 @@ #define ODFS_HFS_COMMON_H #include "odfs/backend.h" +#include "odfs/byteorder.h" #include #include @@ -37,24 +38,6 @@ #define HFS_CNID_ROOT_DIR 2 /* root directory ID */ -/* ---- big-endian helpers ---- */ - -static inline uint16_t hfs_be16(const uint8_t *p) -{ - return (uint16_t)(((uint16_t)p[0] << 8) | p[1]); -} - -static inline uint32_t hfs_be32(const uint8_t *p) -{ - return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | - ((uint32_t)p[2] << 8) | p[3]; -} - -static inline uint64_t hfs_be64(const uint8_t *p) -{ - return ((uint64_t)hfs_be32(p) << 32) | hfs_be32(p + 4); -} - /* ---- extents and B-trees ---- */ typedef struct hfs_extent { diff --git a/backends/hfsplus/hfsplus.c b/backends/hfsplus/hfsplus.c index e56a1bb..c2dd109 100644 --- a/backends/hfsplus/hfsplus.c +++ b/backends/hfsplus/hfsplus.c @@ -48,9 +48,9 @@ static void hfsp_decode_name(const uint8_t *uname, uint16_t ulen, */ static int hfsp_valid_vh(const uint8_t *vh) { - uint16_t sig = hfs_be16(&vh[0]); - uint16_t version = hfs_be16(&vh[2]); - uint32_t block_size = hfs_be32(&vh[40]); + uint16_t sig = odfs_be16(&vh[0]); + uint16_t version = odfs_be16(&vh[2]); + uint32_t block_size = odfs_be32(&vh[40]); if ((sig != HFSPLUS_SIG && sig != HFSX_SIG) || (version != 4 && version != 5)) @@ -70,11 +70,11 @@ static int hfsp_check_part(odfs_cache_t *cache, uint64_t part_byte, void *ctx) * An HFS wrapper: an HFS volume whose MDB points at an HFS+ volume * embedded in its allocation blocks (drEmbedExtent). */ - if (hfs_be16(vh) == HFS_MDB_SIG && - hfs_be16(&vh[HFS_MDB_EMBEDSIG]) == HFSPLUS_SIG) { - part_byte += (uint64_t)hfs_be16(&vh[HFS_MDB_ALBLST]) * 512 + - (uint64_t)hfs_be16(&vh[HFS_MDB_EMBEDEXT]) * - hfs_be32(&vh[HFS_MDB_ALBLKSIZ]); + if (odfs_be16(vh) == HFS_MDB_SIG && + odfs_be16(&vh[HFS_MDB_EMBEDSIG]) == HFSPLUS_SIG) { + part_byte += (uint64_t)odfs_be16(&vh[HFS_MDB_ALBLST]) * 512 + + (uint64_t)odfs_be16(&vh[HFS_MDB_EMBEDEXT]) * + odfs_be32(&vh[HFS_MDB_ALBLKSIZ]); vh = odfs_hfs_read_block(cache, part_byte + HFSPLUS_VH_OFFSET); if (!vh) return 0; @@ -169,7 +169,7 @@ static int hfsp_parse_entry(const hfs_volume_t *vol, const odfs_node_t *dir, if (key_len < 8 || data_len < 2) return 0; - rec_type = hfs_be16(data); + rec_type = odfs_be16(data); if (rec_type == HFS_CAT_DIR) { if (data_len < 24) return 0; @@ -180,7 +180,7 @@ static int hfsp_parse_entry(const hfs_volume_t *vol, const odfs_node_t *dir, return 0; } - key_namelen = hfs_be16(&key[6]); + key_namelen = odfs_be16(&key[6]); if (key_namelen == 0 || key_namelen > (key_len - 8) / 2) return 0; @@ -190,7 +190,7 @@ static int hfsp_parse_entry(const hfs_volume_t *vol, const odfs_node_t *dir, * Finder-invisible entries (finderFlags in userInfo) and the * system files of the root folder. */ - if (data_len >= 58 && (hfs_be16(&data[56]) & HFS_FINDER_INVISIBLE)) + if (data_len >= 58 && (odfs_be16(&data[56]) & HFS_FINDER_INVISIBLE)) return 0; if (dir->extent.lba == HFS_CNID_ROOT_DIR) { for (size_t i = 0; i < sizeof(hfsp_root_private) / @@ -210,7 +210,7 @@ static int hfsp_parse_entry(const hfs_volume_t *vol, const odfs_node_t *dir, return 0; /* folderID or fileID */ - fnode->id = hfs_be32(&data[8]); + fnode->id = odfs_be32(&data[8]); if (rec_type == HFS_CAT_DIR) { fnode->kind = ODFS_NODE_DIR; fnode->extent.lba = fnode->id; @@ -219,13 +219,13 @@ static int hfsp_parse_entry(const hfs_volume_t *vol, const odfs_node_t *dir, if (data_len >= 168) { /* data fork: logicalSize(8) clumpSize(4) totalBlocks(4) extents[8] at record offset 88 */ - fnode->size = hfs_be64(&data[88]); - fnode->extent.lba = hfs_be32(&data[104]); + fnode->size = odfs_be64(&data[88]); + fnode->extent.lba = odfs_be32(&data[104]); fnode->extent.length = - odfs_hfs_extent_bytes(vol, hfs_be32(&data[108])); + odfs_hfs_extent_bytes(vol, odfs_be32(&data[108])); } } - odfs_timestamp_from_mac_time(hfs_be32(&data[16]), &fnode->mtime); + odfs_timestamp_from_mac_time(odfs_be32(&data[16]), &fnode->mtime); fnode->ctime = fnode->mtime; return 1; } @@ -246,10 +246,10 @@ static odfs_err_t hfsp_root_thread_cb(const uint8_t *key, size_t key_len, (void)key_len; /* thread record: type(2) reserved(2) parentID(4) namelen(2) name(...) */ - if (data_len < 10 || hfs_be16(data) != HFS_CAT_DTHREAD) + if (data_len < 10 || odfs_be16(data) != HFS_CAT_DTHREAD) return ODFS_OK; - tnlen = hfs_be16(&data[8]); + tnlen = odfs_be16(&data[8]); if (tnlen > 0 && tnlen <= (data_len - 10) / 2) hfsp_decode_name(&data[10], tnlen, vol->volume_name, HFSPLUS_VOLNAME_SIZE); @@ -301,8 +301,8 @@ static odfs_err_t hfsp_mount(odfs_cache_t *cache, odfs_free(vol); return ODFS_ERR_BAD_FORMAT; } - vol->block_size = hfs_be32(&vh[40]); - vol->total_blocks = hfs_be32(&vh[44]); + vol->block_size = odfs_be32(&vh[40]); + vol->total_blocks = odfs_be32(&vh[44]); ODFS_INFO(log, ODFS_SUB_HFSPLUS, "HFS+ block size: %" PRIu32 ", total: %" PRIu32, @@ -312,10 +312,10 @@ static odfs_err_t hfsp_mount(odfs_cache_t *cache, (80 bytes each): logicalSize(8) clumpSize(4) totalBlocks(4) extents[8] */ for (int i = 0; i < 8; i++) { - vol->ext.extents[i].start_block = hfs_be32(&vh[192 + 16 + i * 8]); - vol->ext.extents[i].block_count = hfs_be32(&vh[192 + 20 + i * 8]); - vol->cat.extents[i].start_block = hfs_be32(&vh[272 + 16 + i * 8]); - vol->cat.extents[i].block_count = hfs_be32(&vh[272 + 20 + i * 8]); + vol->ext.extents[i].start_block = odfs_be32(&vh[192 + 16 + i * 8]); + vol->ext.extents[i].block_count = odfs_be32(&vh[192 + 20 + i * 8]); + vol->cat.extents[i].start_block = odfs_be32(&vh[272 + 16 + i * 8]); + vol->cat.extents[i].block_count = odfs_be32(&vh[272 + 20 + i * 8]); } /* read catalog B-Tree header node */ @@ -360,7 +360,7 @@ static odfs_err_t hfsp_mount(odfs_cache_t *cache, root_out->extent.lba = HFS_CNID_ROOT_DIR; root_out->extent.length = 0; - odfs_timestamp_from_mac_time(hfs_be32(&vh[20]), + odfs_timestamp_from_mac_time(odfs_be32(&vh[20]), &root_out->mtime); root_out->ctime = root_out->mtime; vol->root = *root_out; diff --git a/backends/iso9660/iso9660.c b/backends/iso9660/iso9660.c index fad1fe5..83202b0 100644 --- a/backends/iso9660/iso9660.c +++ b/backends/iso9660/iso9660.c @@ -62,7 +62,7 @@ static void iso_read_dot_size(odfs_cache_t *cache, uint32_t lba, if (odfs_cache_read(cache, lba, §or) == ODFS_OK && sector[ISO_DR_LENGTH] >= 34) - *size_out = iso_read_le32(§or[ISO_DR_DATA_LENGTH]); + *size_out = odfs_le32(§or[ISO_DR_DATA_LENGTH]); } static void iso_apply_rr(odfs_node_t *node, const rr_info_t *rr, @@ -119,7 +119,7 @@ static void iso_apply_rr(odfs_node_t *node, const rr_info_t *rr, */ static inline uint32_t iso_rec_lba(const uint8_t *rec) { - return iso_read_le32(&rec[ISO_DR_EXTENT_LBA]) + rec[ISO_DR_EXT_ATTR_LENGTH]; + return odfs_le32(&rec[ISO_DR_EXTENT_LBA]) + rec[ISO_DR_EXT_ATTR_LENGTH]; } /* @@ -205,7 +205,7 @@ static int iso_parse_dir_record(const uint8_t *data, size_t avail, if (!iso_blk_to_sector(ctx, &lba)) return -(int)rec_len; node->extent.lba = lba + ctx->session_start; - node->extent.length = iso_read_le32(&data[ISO_DR_DATA_LENGTH]); + node->extent.length = odfs_le32(&data[ISO_DR_DATA_LENGTH]); node->size = node->extent.length; /* flags */ @@ -371,9 +371,9 @@ odfs_err_t odfs_iso_mount_vd(odfs_cache_t *cache, } ctx->pvd.volume_space_size = - iso_read_le32(§or[ISO_PVD_VOLUME_SPACE_SIZE + hs_off]); + odfs_le32(§or[ISO_PVD_VOLUME_SPACE_SIZE + hs_off]); ctx->pvd.logical_block_size = - iso_read_le16(§or[ISO_PVD_LOGICAL_BLK_SIZE + hs_off]); + odfs_le16(§or[ISO_PVD_LOGICAL_BLK_SIZE + hs_off]); root_rec = §or[ISO_IS_HS(ctx) ? HS_PVD_ROOT_DIR_RECORD : ISO_PVD_ROOT_DIR_RECORD]; @@ -390,7 +390,7 @@ odfs_err_t odfs_iso_mount_vd(odfs_cache_t *cache, /* extent and length offsets inside the record match in both variants */ ctx->pvd.root_dir_lba = iso_rec_lba(root_rec); - ctx->pvd.root_dir_size = iso_read_le32(&root_rec[ISO_DR_DATA_LENGTH]); + ctx->pvd.root_dir_size = odfs_le32(&root_rec[ISO_DR_DATA_LENGTH]); if (!iso_blk_to_sector(ctx, &ctx->pvd.root_dir_lba)) { ODFS_WARN(log, ODFS_SUB_ISO, "root directory not sector-aligned"); odfs_free(ctx); @@ -947,7 +947,7 @@ static odfs_err_t iso_read_parent_extent(const iso_context_t *ctx, if (!iso_blk_to_sector(ctx, &lba)) return ODFS_ERR_BAD_FORMAT; *parent_lba_out = lba + ctx->session_start; - *parent_size_out = iso_read_le32(&dotdot_rec[ISO_DR_DATA_LENGTH]); + *parent_size_out = odfs_le32(&dotdot_rec[ISO_DR_DATA_LENGTH]); #if ODFS_FEATURE_ROCK_RIDGE if (ctx->has_rock_ridge && dotdot_rec[ISO_DR_LENGTH] > 34) { @@ -1027,7 +1027,7 @@ odfs_err_t odfs_iso_merge_multi_extent(const iso_context_t *ctx, } part_lba = iso_rec_lba(rec); - part_len = iso_read_le32(&rec[ISO_DR_DATA_LENGTH]); + part_len = odfs_le32(&rec[ISO_DR_DATA_LENGTH]); /* A part continues the byte stream only if the merged length so * far is sector-aligned and the part starts in the next sector. */ diff --git a/backends/iso9660/iso9660.h b/backends/iso9660/iso9660.h index 6006815..d8b2eb6 100644 --- a/backends/iso9660/iso9660.h +++ b/backends/iso9660/iso9660.h @@ -10,6 +10,7 @@ #define ODFS_ISO9660_H #include "odfs/backend.h" +#include "odfs/byteorder.h" #include /* --- on-disc constants --- */ @@ -27,32 +28,6 @@ #define ISO_MAX_DIR_DEPTH 256 /* loop/recursion guard */ -/* --- both-byte order helpers (ECMA-119 7.3.3) --- */ - -/* read little-endian 32 from a both-byte-order 8-byte field */ -static inline uint32_t iso_read_le32(const uint8_t *p) -{ -#if defined(ODFS_M68K_UNALIGNED_OK) - /* GCC keeps to aligned accesses on every m68k and would assemble - * the value byte by byte; the 68020 and later load it unaligned. - * Rock Ridge fields can sit at odd addresses, so only a build that - * never runs on a 68000 may do this (see the Makefile). */ - uint32_t v; - - __asm__("move.l %1,%0" : "=d" (v) : "m" (*(const uint8_t (*)[4])p)); - return __builtin_bswap32(v); -#else - return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | - ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); -#endif -} - -/* read little-endian 16 from a both-byte-order 4-byte field */ -static inline uint16_t iso_read_le16(const uint8_t *p) -{ - return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8)); -} - /* copy a fixed-length string field, trimming trailing spaces and NUL */ static inline void iso_copy_strfield(const uint8_t *src, size_t src_len, char *dst, size_t dst_size) diff --git a/backends/rock_ridge/rock_ridge.c b/backends/rock_ridge/rock_ridge.c index 24433cb..85608d5 100644 --- a/backends/rock_ridge/rock_ridge.c +++ b/backends/rock_ridge/rock_ridge.c @@ -21,12 +21,7 @@ static inline uint16_t rr_sig(const uint8_t *p) { - return (uint16_t)((p[0] << 8) | p[1]); -} - -static inline uint32_t rr_read_le32(const uint8_t *p) -{ - return iso_read_le32(p); + return odfs_be16(p); } static void rr_parse_as(const uint8_t *entry, size_t entry_len, rr_info_t *info) @@ -168,9 +163,9 @@ typedef struct rr_state { /* note the continuation area of CE entry e in *st; 0 if out of bounds */ static int rr_set_ce(const uint8_t *e, rr_state_t *st) { - uint32_t lba = rr_read_le32(&e[4]); - uint32_t off = rr_read_le32(&e[12]); - uint32_t len = rr_read_le32(&e[20]); + uint32_t lba = odfs_le32(&e[4]); + uint32_t off = odfs_le32(&e[12]); + uint32_t len = odfs_le32(&e[20]); if (off >= ISO_SECTOR_SIZE) return 0; @@ -246,7 +241,7 @@ static int rr_parse_entries(const uint8_t *sua, size_t sua_len, case RR_SIG_PX: if (len >= 36) { - info->mode = rr_read_le32(&sua[pos + 4]); + info->mode = odfs_le32(&sua[pos + 4]); info->has_posix = 1; } break; @@ -363,7 +358,7 @@ static int rr_parse_entries(const uint8_t *sua, size_t sua_len, case RR_SIG_CL: if (len >= 12) { - info->child_link_lba = rr_read_le32(&sua[pos + 4]); + info->child_link_lba = odfs_le32(&sua[pos + 4]); info->has_child_link = 1; } break; @@ -456,7 +451,7 @@ int rr_find_parent_link(const uint8_t *sua, size_t sua_len, int skip, if (len < 4 || pos + len > sua_len || sig == RR_SIG_ST) break; if (sig == RR_SIG_PL && len >= 12) { - *lba_out = rr_read_le32(&sua[pos + 4]); + *lba_out = odfs_le32(&sua[pos + 4]); return 1; } if (sig == RR_SIG_CE && len >= 28 && rr_set_ce(&sua[pos], &st)) diff --git a/backends/udf/udf.c b/backends/udf/udf.c index a57fead..c9511db 100644 --- a/backends/udf/udf.c +++ b/backends/udf/udf.c @@ -42,7 +42,7 @@ static __attribute__((noinline)) uint16_t udf_tag_id(const uint8_t *data) if (i != 4) sum += data[i]; } - return ((uint8_t)sum == data[4]) ? udf_le16(&data[0]) : 0; + return ((uint8_t)sum == data[4]) ? odfs_le16(&data[0]) : 0; } /* ------------------------------------------------------------------ */ @@ -128,10 +128,10 @@ static void udf_parse_timestamp(const uint8_t *ts, odfs_timestamp_t *out) * zone is the low 12 bits of the first field: minutes from UTC as * a signed number, or -2047 for "not specified". */ - int tz = (int)((udf_le16(&ts[0]) & 0x0FFFu) ^ 0x0800u) - 0x0800; + int tz = (int)((odfs_le16(&ts[0]) & 0x0FFFu) ^ 0x0800u) - 0x0800; memset(out, 0, sizeof(*out)); - out->year = (int32_t)(int16_t)udf_le16(&ts[2]); + out->year = (int32_t)(int16_t)odfs_le16(&ts[2]); out->month = ts[4]; out->day = ts[5]; out->hour = ts[6]; @@ -272,8 +272,8 @@ static odfs_err_t udf_mount(odfs_cache_t *cache, } /* AVDP: main VDS extent at offset 16 */ - uint32_t mvds_lba = udf_le32(§or[16 + 4]); - uint32_t mvds_len = udf_le32(§or[16]); + uint32_t mvds_lba = odfs_le32(§or[16 + 4]); + uint32_t mvds_len = odfs_le32(§or[16]); uint32_t mvds_sectors = mvds_len / UDF_BLOCK_SIZE; ODFS_DEBUG(log, ODFS_SUB_UDF, @@ -305,9 +305,9 @@ static odfs_err_t udf_mount(odfs_cache_t *cache, /* Partition Descriptor; the LVD's maps say which one is used */ if (n_pds == UDF_MAX_PDS) break; - pd_numbers[n_pds] = udf_le16(§or[22]); - pd_starts[n_pds] = udf_le32(§or[188]); - pd_lengths[n_pds] = udf_le32(§or[192]); + pd_numbers[n_pds] = odfs_le16(§or[22]); + pd_starts[n_pds] = odfs_le32(§or[188]); + pd_lengths[n_pds] = odfs_le32(§or[192]); ODFS_INFO(log, ODFS_SUB_UDF, "partition %u: start LBA %" PRIu32 ", %" PRIu32 " sectors", pd_numbers[n_pds], pd_starts[n_pds], pd_lengths[n_pds]); @@ -316,7 +316,7 @@ static odfs_err_t udf_mount(odfs_cache_t *cache, case UDF_TAG_LVD: /* Logical Volume Descriptor */ - lv_block_size = udf_le32(§or[212]); + lv_block_size = odfs_le32(§or[212]); /* LVD volume id at offset 84, 128 bytes d-string */ if (ctx->volume_id[0] == '\0') udf_decode_dstring(§or[84], 128, @@ -327,9 +327,9 @@ static odfs_err_t udf_mount(odfs_cache_t *cache, * The FSD long_ad is at a fixed offset in the LVD: * ECMA-167: Logical Volume Contents Use at offset 248, 16 bytes long_ad. */ - fsd_ad.length = udf_le32(§or[248]); - fsd_ad.lba = udf_le32(§or[252]); - fsd_ad.partition = udf_le16(§or[256]); + fsd_ad.length = odfs_le32(§or[248]); + fsd_ad.lba = odfs_le32(§or[252]); + fsd_ad.partition = odfs_le16(§or[256]); found_lvd = 1; ODFS_INFO(log, ODFS_SUB_UDF, "LVD block size: %" PRIu32 ", FSD at part LBA %" PRIu32, @@ -343,8 +343,8 @@ static odfs_err_t udf_mount(odfs_cache_t *cache, * the same physical one through the Metadata File. */ { - uint32_t mt_len = udf_le32(§or[264]); - uint32_t n_maps = udf_le32(§or[268]); + uint32_t mt_len = odfs_le32(§or[264]); + uint32_t n_maps = odfs_le32(§or[268]); uint32_t pm_off = 0; if (mt_len > UDF_BLOCK_SIZE - 440u) @@ -360,7 +360,7 @@ static odfs_err_t udf_mount(odfs_cache_t *cache, if (pm[0] == 1) { if (!have_part_number) { - part_number = udf_le16(&pm[4]); + part_number = odfs_le16(&pm[4]); have_part_number = 1; ctx->phys_part_ref = (uint16_t)ref; } @@ -369,14 +369,14 @@ static odfs_err_t udf_mount(odfs_cache_t *cache, 23) == 0; if (is_meta || !have_part_number) { - part_number = udf_le16(&pm[38]); + part_number = odfs_le16(&pm[38]); have_part_number = 1; } if (is_meta) { ctx->has_meta_part = 1; ctx->meta_part_ref = (uint16_t)ref; - meta_file_lba = udf_le32(&pm[40]); - meta_mirror_lba = udf_le32(&pm[44]); + meta_file_lba = odfs_le32(&pm[40]); + meta_mirror_lba = odfs_le32(&pm[44]); } } pm_off += pm_len; @@ -452,8 +452,8 @@ static odfs_err_t udf_mount(odfs_cache_t *cache, } /* root directory ICB: long_ad at FSD offset 400 */ - ctx->root_icb_lba = udf_le32(§or[404]); - ctx->root_icb_part = udf_le16(§or[408]); + ctx->root_icb_lba = odfs_le32(§or[404]); + ctx->root_icb_part = odfs_le16(§or[408]); ODFS_INFO(log, ODFS_SUB_UDF, "root ICB at part LBA %" PRIu32, ctx->root_icb_lba); @@ -485,7 +485,7 @@ static odfs_err_t udf_mount(odfs_cache_t *cache, /* the Modification Time is at 84 in a File Entry and at 92 in an * Extended File Entry, which has the Object Size before it */ - root_out->size = udf_le64(§or[56]); + root_out->size = odfs_le64(§or[56]); udf_parse_timestamp(§or[tag == UDF_TAG_FE ? 84 : 92], &root_out->mtime); root_out->ctime = root_out->mtime; @@ -554,16 +554,16 @@ static odfs_err_t udf_parse_icb(odfs_cache_t *cache, else return ODFS_ERR_BAD_FORMAT; - ea_len = udf_le32(§or[ad_base - 8]); - ad_length = udf_le32(§or[ad_base - 4]); + ea_len = odfs_le32(§or[ad_base - 8]); + ad_length = odfs_le32(§or[ad_base - 4]); if (ea_len > UDF_BLOCK_SIZE - ad_base || ad_length > UDF_BLOCK_SIZE - (ad_base + ea_len)) return ODFS_ERR_CORRUPT; - out->data_size = udf_le64(§or[56]); + out->data_size = odfs_le64(§or[56]); out->ad_offset = ad_base + ea_len; out->ad_length = ad_length; - out->alloc_type = (uint8_t)(udf_le16(§or[16 + 18]) & 0x07); + out->alloc_type = (uint8_t)(odfs_le16(§or[16 + 18]) & 0x07); out->file_type = sector[16 + 11]; if (mtime) @@ -677,11 +677,11 @@ static odfs_err_t udf_ad_next(udf_ad_cursor_t *cur) } ad = &cur->win[cur->pos - cur->win_start]; - len_field = udf_le32(&ad[0]); + len_field = odfs_le32(&ad[0]); etype = len_field >> 30; elen = len_field & UDF_EXT_LENGTH_MASK; - elba = udf_le32(&ad[4]); /* LBA field, same offset for short/long */ - epart = (cur->ad_size == 16) ? udf_le16(&ad[8]) : cur->short_part; + elba = odfs_le32(&ad[4]); /* LBA field, same offset for short/long */ + epart = (cur->ad_size == 16) ? odfs_le16(&ad[8]) : cur->short_part; if (etype == UDF_EXT_CONTINUATION) { const uint8_t *aed; @@ -700,7 +700,7 @@ static odfs_err_t udf_ad_next(udf_ad_cursor_t *cur) if (udf_tag_id(aed) != UDF_TAG_AED) return ODFS_ERR_CORRUPT; - alen = udf_le32(&aed[20]); /* Length of Allocation Descriptors */ + alen = odfs_le32(&aed[20]); /* Length of Allocation Descriptors */ if (alen > UDF_BLOCK_SIZE - 24u) return ODFS_ERR_CORRUPT; @@ -1134,9 +1134,9 @@ static odfs_err_t udf_walk_fids(udf_context_t *ctx, fid.start = offset; fid.flags = hdr[18]; fid.name_len = hdr[19]; - fid.icb_lba = udf_le32(&hdr[20 + 4]); - fid.icb_part = udf_le16(&hdr[20 + 8]); - fid.impl_len = udf_le16(&hdr[36]); + fid.icb_lba = odfs_le32(&hdr[20 + 4]); + fid.icb_part = odfs_le16(&hdr[20 + 8]); + fid.impl_len = odfs_le16(&hdr[36]); fid.length = (38u + fid.impl_len + fid.name_len + 3u) & ~3u; diff --git a/backends/udf/udf.h b/backends/udf/udf.h index 5fc4ae5..6b36c1d 100644 --- a/backends/udf/udf.h +++ b/backends/udf/udf.h @@ -10,6 +10,7 @@ #define ODFS_UDF_H #include "odfs/backend.h" +#include "odfs/byteorder.h" #include /* ---- tag identifiers (ECMA-167 Table 3/7) ---- */ @@ -64,24 +65,6 @@ #define UDF_EXT_CONTINUATION 3 /* extent holds the next allocation descriptors */ #define UDF_EXT_LENGTH_MASK 0x3FFFFFFFu -/* ---- helpers for reading UDF LE fields ---- */ - -static inline uint16_t udf_le16(const uint8_t *p) -{ - return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8)); -} - -static inline uint32_t udf_le32(const uint8_t *p) -{ - return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | - ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); -} - -static inline uint64_t udf_le64(const uint8_t *p) -{ - return (uint64_t)udf_le32(p) | ((uint64_t)udf_le32(p + 4) << 32); -} - /* ---- extent (ECMA-167 7.1) ---- */ typedef struct udf_extent { diff --git a/core/charset.c b/core/charset.c index 5067eee..a052b2f 100644 --- a/core/charset.c +++ b/core/charset.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include "odfs/byteorder.h" #include "odfs/charset.h" #include @@ -58,7 +59,7 @@ odfs_err_t odfs_ucs2be_to_utf8(const uint8_t *src, size_t src_len, return ODFS_ERR_INVAL; for (si = 0; si + 1 < src_len; si += 2) { - uint16_t u1 = (uint16_t)(((uint16_t)src[si] << 8) | src[si + 1]); + uint16_t u1 = odfs_be16(&src[si]); uint32_t cp = u1; if (u1 == 0) @@ -69,8 +70,7 @@ odfs_err_t odfs_ucs2be_to_utf8(const uint8_t *src, size_t src_len, * point; any other surrogate stands for nothing */ 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]); + uint16_t u2 = odfs_be16(&src[si + 2]); if (u2 >= 0xDC00u && u2 <= 0xDFFFu) { cp = 0x10000u + (((uint32_t)(u1 - 0xD800u) << 10) | diff --git a/core/session.c b/core/session.c index 06259eb..9dac098 100644 --- a/core/session.c +++ b/core/session.c @@ -10,6 +10,7 @@ */ #include "odfs/api.h" +#include "odfs/byteorder.h" #include "odfs/media.h" #include "odfs/log.h" #include "odfs/config.h" @@ -148,10 +149,7 @@ odfs_err_t odfs_find_last_session_toc(odfs_media_t *media, best_lba = session_start; /* read volume space size to jump past this session */ - uint32_t vol_size = (uint32_t)buf[80] | - ((uint32_t)buf[81] << 8) | - ((uint32_t)buf[82] << 16) | - ((uint32_t)buf[83] << 24); + uint32_t vol_size = odfs_le32(&buf[80]); /* the next PVD is at lba + vol_size; compare without * forming the sum, which can wrap back to an earlier * session and loop forever */ diff --git a/include/odfs/byteorder.h b/include/odfs/byteorder.h new file mode 100644 index 0000000..9e7748c --- /dev/null +++ b/include/odfs/byteorder.h @@ -0,0 +1,93 @@ +/* + * byteorder.h - load and store little- and big-endian fields + * + * SPDX-License-Identifier: BSD-2-Clause + * + * On-disc and SCSI structures put their fields at any byte offset, so + * these take a byte pointer and make no assumption about alignment. + * + * ODFS_M68K_UNALIGNED_OK says the CPU allows word and long accesses at + * odd addresses (68020 and later); the Makefile sets it for the 68020 + * build only. GCC keeps to aligned accesses on every m68k and would + * assemble such a value byte by byte, so the loads below use one move + * where they may. GCC's own __mc680x0__ macros cannot decide this: they + * follow -mtune, not -m. + */ + +#ifndef ODFS_BYTEORDER_H +#define ODFS_BYTEORDER_H + +#include + +/* ---- loads ---- */ + +static inline uint16_t odfs_le16(const uint8_t *p) +{ + return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8)); +} + +static inline uint32_t odfs_le32(const uint8_t *p) +{ +#if defined(ODFS_M68K_UNALIGNED_OK) + uint32_t v; + + __asm__("move.l %1,%0" : "=d" (v) : "m" (*(const uint8_t (*)[4])p)); + return __builtin_bswap32(v); +#else + return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | + ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); +#endif +} + +static inline uint64_t odfs_le64(const uint8_t *p) +{ + return (uint64_t)odfs_le32(p) | ((uint64_t)odfs_le32(p + 4) << 32); +} + +static inline uint16_t odfs_be16(const uint8_t *p) +{ + return (uint16_t)(((uint16_t)p[0] << 8) | p[1]); +} + +static inline uint32_t odfs_be32(const uint8_t *p) +{ + return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | + ((uint32_t)p[2] << 8) | p[3]; +} + +static inline uint64_t odfs_be64(const uint8_t *p) +{ + return ((uint64_t)odfs_be32(p) << 32) | odfs_be32(p + 4); +} + +/* ---- stores ---- */ + +static inline void odfs_put_le16(uint8_t *p, uint16_t v) +{ + p[0] = (uint8_t)v; + p[1] = (uint8_t)(v >> 8); +} + +static inline void odfs_put_le32(uint8_t *p, uint32_t v) +{ + p[0] = (uint8_t)v; + p[1] = (uint8_t)(v >> 8); + p[2] = (uint8_t)(v >> 16); + p[3] = (uint8_t)(v >> 24); +} + +static inline void odfs_put_be16(uint8_t *p, uint16_t v) +{ + p[0] = (uint8_t)(v >> 8); + p[1] = (uint8_t)v; +} + +static inline void odfs_put_be32(uint8_t *p, uint32_t v) +{ + p[0] = (uint8_t)(v >> 24); + p[1] = (uint8_t)(v >> 16); + p[2] = (uint8_t)(v >> 8); + p[3] = (uint8_t)v; +} + +#endif /* ODFS_BYTEORDER_H */ diff --git a/platform/host/file_media.c b/platform/host/file_media.c index 660a9d4..96c9d79 100644 --- a/platform/host/file_media.c +++ b/platform/host/file_media.c @@ -7,6 +7,7 @@ #define _POSIX_C_SOURCE 200809L #define _FILE_OFFSET_BITS 64 +#include "odfs/byteorder.h" #include "odfs/media.h" #include #include @@ -292,8 +293,7 @@ static int host_riff_data_range(FILE *fp, uint64_t file_size, if (fread(chdr, 1, sizeof(chdr), fp) != sizeof(chdr)) return 0; - csize = (uint32_t)chdr[4] | ((uint32_t)chdr[5] << 8) | - ((uint32_t)chdr[6] << 16) | ((uint32_t)chdr[7] << 24); + csize = odfs_le32(&chdr[4]); pos = ftello(fp); if (pos < 0) return 0; diff --git a/tests/unit/test_byteorder.c b/tests/unit/test_byteorder.c new file mode 100644 index 0000000..fd66a85 --- /dev/null +++ b/tests/unit/test_byteorder.c @@ -0,0 +1,90 @@ +/* + * test_byteorder.c — tests for the byte-order loads and stores + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "odfs/byteorder.h" +#include "test_harness.h" + +#include + +static const uint8_t seq[] = { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 +}; + +TEST(byteorder_le) +{ + ASSERT_EQ(odfs_le16(seq), 0x0201); + ASSERT_EQ(odfs_le32(seq), 0x04030201); + ASSERT(odfs_le64(seq) == 0x0807060504030201ull); +} + +TEST(byteorder_be) +{ + ASSERT_EQ(odfs_be16(seq), 0x0102); + ASSERT_EQ(odfs_be32(seq), 0x01020304); + ASSERT(odfs_be64(seq) == 0x0102030405060708ull); +} + +/* fields sit at odd offsets in Rock Ridge and SCSI replies */ +TEST(byteorder_odd_address) +{ + ASSERT_EQ(odfs_le16(seq + 1), 0x0302); + ASSERT_EQ(odfs_le32(seq + 1), 0x05040302); + ASSERT(odfs_le64(seq + 1) == 0x0908070605040302ull); + ASSERT_EQ(odfs_be16(seq + 1), 0x0203); + ASSERT_EQ(odfs_be32(seq + 1), 0x02030405); + ASSERT(odfs_be64(seq + 1) == 0x0203040506070809ull); +} + +TEST(byteorder_all_ones) +{ + static const uint8_t ff[8] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF + }; + + ASSERT_EQ(odfs_le16(ff), 0xFFFF); + ASSERT(odfs_le32(ff) == 0xFFFFFFFFu); + ASSERT(odfs_be32(ff) == 0xFFFFFFFFu); + ASSERT(odfs_le64(ff) == ~0ull); +} + +/* an ECMA-119 both-byte-order field: the little-endian half comes first */ +TEST(byteorder_both_byte_field) +{ + static const uint8_t f[8] = { 0x78, 0x56, 0x34, 0x12, + 0x12, 0x34, 0x56, 0x78 }; + + ASSERT_EQ(odfs_le32(f), 0x12345678); + ASSERT_EQ(odfs_be32(f + 4), 0x12345678); +} + +TEST(byteorder_stores) +{ + uint8_t b[5]; + + memset(b, 0xAA, sizeof(b)); + odfs_put_le16(b + 1, 0x1234); + ASSERT_EQ(b[0], 0xAA); + ASSERT_EQ(b[1], 0x34); + ASSERT_EQ(b[2], 0x12); + ASSERT_EQ(b[3], 0xAA); + + odfs_put_be16(b + 1, 0x1234); + ASSERT_EQ(b[1], 0x12); + ASSERT_EQ(b[2], 0x34); + + odfs_put_le32(b + 1, 0x12345678); + ASSERT_EQ(odfs_le32(b + 1), 0x12345678); + ASSERT_EQ(b[1], 0x78); + ASSERT_EQ(b[4], 0x12); + ASSERT_EQ(b[0], 0xAA); + + odfs_put_be32(b + 1, 0x12345678); + ASSERT_EQ(odfs_be32(b + 1), 0x12345678); + ASSERT_EQ(b[1], 0x12); + ASSERT_EQ(b[4], 0x78); +} + +TEST_MAIN() diff --git a/tests/unit/test_iso9660.c b/tests/unit/test_iso9660.c index 60a78c9..5958ccb 100644 --- a/tests/unit/test_iso9660.c +++ b/tests/unit/test_iso9660.c @@ -12,32 +12,6 @@ #include -/* ---- endian / field helpers ---- */ - -TEST(iso_read_le16_basic) -{ - uint8_t buf[] = { 0x34, 0x12, 0x12, 0x34 }; - ASSERT_EQ(iso_read_le16(buf), 0x1234); -} - -TEST(iso_read_le32_basic) -{ - uint8_t buf[] = { 0x78, 0x56, 0x34, 0x12, 0x12, 0x34, 0x56, 0x78 }; - ASSERT_EQ(iso_read_le32(buf), 0x12345678); -} - -TEST(iso_read_le32_zero) -{ - uint8_t buf[8] = { 0 }; - ASSERT_EQ(iso_read_le32(buf), 0); -} - -TEST(iso_read_le32_max) -{ - uint8_t buf[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; - ASSERT_EQ(iso_read_le32(buf), 0xFFFFFFFF); -} - /* ---- PVD signature recognition ---- */ TEST(iso_pvd_signature) From 50974c0aba0016c507ac51440a0fe27beb652dc2 Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Sat, 28 Nov 2026 14:49:52 -0800 Subject: [PATCH 2/2] odfs: load 16-bit and big-endian fields with one move on the 68020 GCC keeps to aligned accesses on every m68k, so a field at an arbitrary offset is assembled byte by byte, about seven instructions for a long. The 68020 and later allow word and long accesses at any address, and odfs_le32() already used one move.l there. Do the same for odfs_le16() (move.w and a byte swap), odfs_be16() and odfs_be32() (a plain move), and so for the 64-bit loads built from them. That mostly helps HFS and HFS+, which are big-endian throughout, and UDF. The 68020 build shrinks by another 1848 bytes, 2680 against main. The 68000 and ROM builds do not change. CDBench on the 68040 rig, 68020 build, main -> this: UDF fixture seq read 168676 -> 172411 KiB/s Discovery 5498 -> 5617 entries/s same offset 85 -> 80 us HFS fixture Discovery 6119 -> 6477 entries/s small o+r 390 -> 384 us ISO fixture unchanged on every figure The HFS fixture is the CDBench tree in an Apple partition map, made with hdiutil makehybrid -hfs. --- include/odfs/byteorder.h | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/include/odfs/byteorder.h b/include/odfs/byteorder.h index 9e7748c..852e93c 100644 --- a/include/odfs/byteorder.h +++ b/include/odfs/byteorder.h @@ -21,18 +21,37 @@ /* ---- loads ---- */ +#if defined(ODFS_M68K_UNALIGNED_OK) +static inline uint16_t odfs_load16_(const uint8_t *p) +{ + uint16_t v; + + __asm__("move.w %1,%0" : "=d" (v) : "m" (*(const uint8_t (*)[2])p)); + return v; +} + +static inline uint32_t odfs_load32_(const uint8_t *p) +{ + uint32_t v; + + __asm__("move.l %1,%0" : "=d" (v) : "m" (*(const uint8_t (*)[4])p)); + return v; +} +#endif + static inline uint16_t odfs_le16(const uint8_t *p) { +#if defined(ODFS_M68K_UNALIGNED_OK) + return __builtin_bswap16(odfs_load16_(p)); +#else return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8)); +#endif } static inline uint32_t odfs_le32(const uint8_t *p) { #if defined(ODFS_M68K_UNALIGNED_OK) - uint32_t v; - - __asm__("move.l %1,%0" : "=d" (v) : "m" (*(const uint8_t (*)[4])p)); - return __builtin_bswap32(v); + return __builtin_bswap32(odfs_load32_(p)); #else return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); @@ -46,13 +65,21 @@ static inline uint64_t odfs_le64(const uint8_t *p) static inline uint16_t odfs_be16(const uint8_t *p) { +#if defined(ODFS_M68K_UNALIGNED_OK) + return odfs_load16_(p); +#else return (uint16_t)(((uint16_t)p[0] << 8) | p[1]); +#endif } static inline uint32_t odfs_be32(const uint8_t *p) { +#if defined(ODFS_M68K_UNALIGNED_OK) + return odfs_load32_(p); +#else return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3]; +#endif } static inline uint64_t odfs_be64(const uint8_t *p)