From fea949f1f46c27bf69b43dfb8ef1402e090e890f Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 1 Feb 2025 22:12:46 +0000 Subject: [PATCH] start packing some binary image structures --- src/bin.h | 86 +++++++++++++++++++++++++++++------------------------- src/misc.h | 12 ++++++++ 2 files changed, 58 insertions(+), 40 deletions(-) create mode 100644 src/misc.h diff --git a/src/bin.h b/src/bin.h index 0cc8fe9..b695c24 100644 --- a/src/bin.h +++ b/src/bin.h @@ -2,6 +2,7 @@ #define BIN_H_ #include +#include "misc.h" #define EC3_SIGNATURE 0x45433358 @@ -34,7 +35,7 @@ #define EC3_TAG_ENCRYPTED 0x00000004u /* 32K per cluster group */ -#define EC3_CLUSTERS_PER_GROUP 1635 +#define EC3_CLUSTERS_PER_GROUP 1637 /* 1K per extent group */ #define EC3_EXTENTS_PER_GROUP 36 @@ -67,47 +68,52 @@ typedef uint8_t ec3_chunk_id[EC3_CHUNK_ID_SIZE]; -struct ec3_header { - b_i32 h_magic; - b_i16 h_version; - b_i16 h_cluster_size; - b_i64 h_tag_table_offset; - b_i64 h_extent_table_offset; - b_i64 h_cluster_table_offset; - b_i32 h_tag_count; - b_i32 h_extent_count; - b_i32 h_cluster_group_count; - b_i16 h_compression; - b_i16 h_encryption; - b_i64 h_app_magic; - uint8_t h_reserved[8]; -}; +PACK( + struct ec3_header { + b_i32 h_magic; + b_i16 h_version; + b_i16 h_cluster_size; + b_i64 h_tag_table_offset; + b_i64 h_extent_table_offset; + b_i64 h_cluster_table_offset; + b_i32 h_tag_count; + b_i32 h_extent_count; + b_i32 h_cluster_group_count; + b_i16 h_compression; + b_i16 h_encryption; + b_i64 h_app_magic; + uint8_t h_reserved[8]; + } +); -struct ec3_cluster { - /* cluster identifier */ - b_i32 c_id; - /* lower 32-bits of the cluster bounds */ - b_i32 c_bounds0; - /* upper 32-bits of the cluster bounds */ - b_i32 c_bounds1; - /* CRC-16 of the on-disk cluster data */ - b_i16 c_checksum; - /* flags that apply to this cluster */ - b_i16 c_flags; -}; +PACK( + struct ec3_cluster { + /* cluster identifier */ + b_i32 c_id; + /* lower 32-bits of the cluster bounds */ + b_i32 c_bounds0; + /* upper 32-bits of the cluster bounds */ + b_i32 c_bounds1; + /* CRC-16 of the on-disk cluster data */ + b_i16 c_checksum; + /* flags that apply to this cluster */ + b_i16 c_flags; + } +); -struct ec3_cluster_group { - /* the number of clusters that this group contains */ - b_i16 g_nr_clusters; - uint8_t g_reserved[2]; - /* array of clusters contained within the group. */ - struct ec3_cluster g_clusters[EC3_CLUSTERS_PER_GROUP]; - /* offsets to other cluster groups, relative to the start of the - * cluster group table. the cluster groups form a B-tree. */ - b_i32 g_child_offsets[EC3_CLUSTERS_PER_GROUP + 1]; - /* pad the group out to an even 32K */ - uint8_t g_padding[20]; -}; +PACK( + struct ec3_cluster_group { + /* the number of clusters that this group contains */ + b_i16 g_nr_clusters; + uint8_t g_reserved[2]; + /* array of clusters contained within the group. */ + struct ec3_cluster g_clusters[EC3_CLUSTERS_PER_GROUP]; + /* offsets to other cluster groups, relative to the start of the + * cluster group table. the cluster groups form a B-tree. */ + b_i32 g_child_offsets[EC3_CLUSTERS_PER_GROUP + 1]; + uint8_t g_padding[20]; + } +); struct ec3_tag_table_entry { b_i32 tag_type; diff --git a/src/misc.h b/src/misc.h new file mode 100644 index 0000000..49eb2e2 --- /dev/null +++ b/src/misc.h @@ -0,0 +1,12 @@ +#ifndef MISC_H_ +#define MISC_H_ + +#ifdef __GNUC__ +#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__)) +#endif + +#ifdef _MSC_VER +#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop)) +#endif + +#endif \ No newline at end of file