/* 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
* 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];
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);
}
/*
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)) {
/*
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);
}
}
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
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) {
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
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);
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",
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)))
/* 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);
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 *