From: Eric Biggers Date: Sat, 30 May 2015 20:48:16 +0000 (-0500) Subject: New helper macro: ALIGN() X-Git-Tag: v1.8.2~105 X-Git-Url: https://wimlib.net/git/?p=wimlib;a=commitdiff_plain;h=f50557a7095444c554a066b3837c2999ecd1be31 New helper macro: ALIGN() --- diff --git a/include/wimlib/util.h b/include/wimlib/util.h index 7f34cbf3..c3bac7ab 100644 --- a/include/wimlib/util.h +++ b/include/wimlib/util.h @@ -24,6 +24,10 @@ /* Get the number of elements of an array type. */ #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0])) +/* Round 'v' up to the next 'alignment'-byte aligned boundary. 'alignment' must + * be a power of 2. */ +#define ALIGN(v, alignment) (((v) + ((alignment) - 1)) & ~((alignment) - 1)) + /* Maximum number of bytes that can be allocated on the stack. * * Note: this isn't a hard bound on the stack space used, since this is just for diff --git a/src/compress_common.c b/src/compress_common.c index e2ad298e..8b85d9d5 100644 --- a/src/compress_common.c +++ b/src/compress_common.c @@ -125,7 +125,7 @@ sort_symbols(unsigned num_syms, const u32 freqs[restrict], * Tests were done with building the codes for LZX. Results may * vary for different compression algorithms...! */ - num_counters = (DIV_ROUND_UP(num_syms, 4) + 3) & ~3; + num_counters = ALIGN(DIV_ROUND_UP(num_syms, 4), 4); unsigned counters[num_counters]; diff --git a/src/dentry.c b/src/dentry.c index 6a759388..a19e02c8 100644 --- a/src/dentry.c +++ b/src/dentry.c @@ -347,7 +347,7 @@ stream_out_total_length(const struct wim_inode_stream *strm) len += utf16le_len_bytes(strm->stream_name) + 2; /* Account for any necessary padding to the next 8-byte boundary. */ - return (len + 7) & ~7; + return ALIGN(len, 8); } /* @@ -364,12 +364,9 @@ dentry_out_total_length(const struct wim_dentry *dentry) len = dentry_min_len_with_names(dentry->file_name_nbytes, dentry->short_name_nbytes); - len = (len + 7) & ~7; + len = ALIGN(len, 8); - if (inode->i_extra_size) { - len += inode->i_extra_size; - len = (len + 7) & ~7; - } + len += ALIGN(inode->i_extra_size, 8); if (!(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED)) { /* @@ -396,8 +393,8 @@ dentry_out_total_length(const struct wim_dentry *dentry) if (have_named_data_stream || have_reparse_point_stream) { if (have_reparse_point_stream) - len += (sizeof(struct wim_extra_stream_entry_on_disk) + 7) & ~7; - len += (sizeof(struct wim_extra_stream_entry_on_disk) + 7) & ~7; + len += ALIGN(sizeof(struct wim_extra_stream_entry_on_disk), 8); + len += ALIGN(sizeof(struct wim_extra_stream_entry_on_disk), 8); } } @@ -1331,10 +1328,7 @@ setup_inode_streams(const u8 *p, const u8 *end, struct wim_inode *inode, disk_strm = (const struct wim_extra_stream_entry_on_disk *)p; /* Read the length field */ - length = le64_to_cpu(disk_strm->length); - - /* 8-byte align the length */ - length = (length + 7) & ~7; + length = ALIGN(le64_to_cpu(disk_strm->length), 8); /* Make sure the length field is neither so small it doesn't * include all the fixed-length data nor so large it overflows @@ -1424,7 +1418,7 @@ read_dentry(const u8 * restrict buf, size_t buf_len, disk_dentry = (const struct wim_dentry_on_disk*)p; /* Get dentry length. */ - length = (le64_to_cpu(disk_dentry->length) + 7) & ~7; + length = ALIGN(le64_to_cpu(disk_dentry->length), 8); /* Check for end-of-directory. */ if (length <= 8) { diff --git a/src/metadata_resource.c b/src/metadata_resource.c index a3faa9c9..014d16b9 100644 --- a/src/metadata_resource.c +++ b/src/metadata_resource.c @@ -150,7 +150,7 @@ recalculate_security_data_length(struct wim_security_data *sd) u32 total_length = sizeof(u64) * sd->num_entries + 2 * sizeof(u32); for (u32 i = 0; i < sd->num_entries; i++) total_length += sd->sizes[i]; - sd->total_length = (total_length + 7) & ~7; + sd->total_length = ALIGN(total_length, 8); } static int @@ -185,16 +185,12 @@ prepare_metadata_resource(WIMStruct *wim, int image, imd->root_dentry = root; } - /* Offset of first child of the root dentry. It's equal to: - * - The total length of the security data, rounded to the next 8-byte - * boundary, - * - plus the total length of the root dentry, - * - plus 8 bytes for an end-of-directory entry following the root - * dentry (shouldn't really be needed, but just in case...) - */ + /* The offset of the first child of the root dentry is equal to the + * total length of the security data, plus the total length of the root + * dentry, plus 8 bytes for an end-of-directory entry following the root + * dentry (shouldn't really be needed, but just in case...) */ recalculate_security_data_length(sd); - subdir_offset = (((u64)sd->total_length + 7) & ~7) + - dentry_out_total_length(root) + 8; + subdir_offset = sd->total_length + dentry_out_total_length(root) + 8; /* Calculate the subdirectory offsets for the entire dentry tree. */ calculate_subdir_offsets(root, &subdir_offset); diff --git a/src/security.c b/src/security.c index b49ba69c..f160d683 100644 --- a/src/security.c +++ b/src/security.c @@ -157,8 +157,8 @@ read_wim_security_data(const u8 *buf, size_t buf_len, p += sd->sizes[i]; } out_align_total_length: - total_len = (total_len + 7) & ~7; - sd->total_length = (sd->total_length + 7) & ~7; + total_len = ALIGN(total_len, 8); + sd->total_length = ALIGN(sd->total_length, 8); if (total_len != sd->total_length) { WARNING("Expected WIM security data total length of " "%u bytes, but calculated %u bytes", diff --git a/src/tagged_items.c b/src/tagged_items.c index 1ae227c8..1ed3b4a9 100644 --- a/src/tagged_items.c +++ b/src/tagged_items.c @@ -85,7 +85,7 @@ inode_get_tagged_item(const struct wim_inode *inode, hdr = (struct tagged_item_header *)p; tag = le32_to_cpu(hdr->tag); - len = (le32_to_cpu(hdr->length) + 7) & ~7; + len = ALIGN(le32_to_cpu(hdr->length), 8); /* Length overflow? */ if (unlikely(len > len_remaining - sizeof(struct tagged_item_header))) @@ -113,7 +113,7 @@ inode_add_tagged_item(struct wim_inode *inode, u32 tag, u32 len) /* We prepend the item instead of appending it because it's easier. */ - itemsize = sizeof(struct tagged_item_header) + ((len + 7) & ~7); + itemsize = sizeof(struct tagged_item_header) + ALIGN(len, 8); newsize = itemsize + inode->i_extra_size; buf = MALLOC(newsize); @@ -130,7 +130,7 @@ inode_add_tagged_item(struct wim_inode *inode, u32 tag, u32 len) hdr = (struct tagged_item_header *)buf; hdr->tag = cpu_to_le32(tag); hdr->length = cpu_to_le32(len); - return memset(hdr->data, 0, (len + 7) & ~7); + return memset(hdr->data, 0, ALIGN(len, 8)); } static inline struct wimlib_unix_data_disk *