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
63 changes: 18 additions & 45 deletions backends/cdda/cdda.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
36 changes: 18 additions & 18 deletions backends/hfs/hfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand Down Expand Up @@ -168,24 +168,24 @@ 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;
hfs_name_to_utf8(&mdb[HFS_MDB_VOLNAME], vnamelen,
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,
Expand Down
64 changes: 32 additions & 32 deletions backends/hfs/hfs_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)]);
}

/*
Expand All @@ -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;

Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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 ||
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 1 addition & 18 deletions backends/hfs/hfs_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define ODFS_HFS_COMMON_H

#include "odfs/backend.h"
#include "odfs/byteorder.h"
#include <stddef.h>
#include <stdint.h>

Expand Down Expand Up @@ -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 {
Expand Down
Loading
Loading