]> wimlib.net Git - wimlib/commitdiff
New helper macro: ALIGN()
authorEric Biggers <ebiggers3@gmail.com>
Sat, 30 May 2015 20:48:16 +0000 (15:48 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Fri, 5 Jun 2015 03:05:37 +0000 (22:05 -0500)
include/wimlib/util.h
src/compress_common.c
src/dentry.c
src/metadata_resource.c
src/security.c
src/tagged_items.c

index 7f34cbf39721f0a6e411595f568474bf5db5f6c9..c3bac7abe46be8d0c55f105989f318860e78dd8a 100644 (file)
 /* 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
index e2ad298e3f33d8ec88536df26c3c9e52d48a0428..8b85d9d55d84703ebfa12a99c4240494937bcc29 100644 (file)
@@ -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];
 
index 6a7593887caeb2c042d8ae1ca93e5b3f5ae3eefa..a19e02c8f3df54dde88ad1b563d491dd1c5f309f 100644 (file)
@@ -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) {
index a3faa9c904e41d48a6c73a689a5d906b0e47a9de..014d16b999388267ff5eec1e17025d0b43b1c15e 100644 (file)
@@ -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);
index b49ba69cd459f5f2640173eb49a3339a8b74ca9a..f160d683f6cbf4755bc92f8ef28b96fa9f8dfcb4 100644 (file)
@@ -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",
index 1ae227c85f551b6afb65a98aa9c6b694674fb9c3..1ed3b4a914454b6938dd193bb31b2d400dd8eba3 100644 (file)
@@ -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 *