initramfs chain handling: Accounting fixes for padding, etc.
authorShao Miller <sha0.miller@gmail.com>
Fri, 26 Oct 2012 06:14:28 +0000 (02:14 -0400)
committerShao Miller <sha0.miller@gmail.com>
Sat, 3 Nov 2012 05:08:37 +0000 (01:08 -0400)
Signed-off-by: Shao Miller <sha0.miller@gmail.com>
com32/lib/syslinux/initramfs_file.c

index 763eff2..7eb55b5 100644 (file)
@@ -65,7 +65,7 @@ static size_t initramfs_mkdirs(const char *filename, void *buffer,
     const char *p = filename;
     char *bp = buffer;
     int len;
-    size_t bytes = 0;
+    size_t bytes = 0, hdr_sz;
     int pad;
 
     while ((p = strchr(p, '/'))) {
@@ -81,15 +81,17 @@ static size_t initramfs_mkdirs(const char *filename, void *buffer,
        while ((p = strchr(p, '/'))) {
            if (p != filename && p[-1] != '/') {
                len = p - filename;
+               hdr_sz = ((sizeof(struct cpio_header) + len + 1) + 3) & ~3;
                bp += sprintf(bp, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x"
                              "%08x%08x%08x%08x", next_ino++, S_IFDIR | 0755,
                              0, 0, 1, 0, 0, 0, 1, 0, 1, len + 1, 0);
                memcpy(bp, filename, len);
                bp += len;
-               pad = (-(sizeof(struct cpio_header) + len) & 3) + 1;
+               pad = hdr_sz - (sizeof(struct cpio_header) + len);
                memset(bp, 0, pad);
                bp += pad;
            }
+           p++;
        }
     }
 
@@ -104,7 +106,7 @@ int initramfs_mknod(struct initramfs *ihead, const char *filename,
                    int do_mkdir,
                    uint16_t mode, size_t len, uint32_t major, uint32_t minor)
 {
-    size_t bytes;
+    size_t bytes, hdr_sz;
     int namelen = strlen(filename);
     int pad;
     char *buffer, *bp;
@@ -114,7 +116,8 @@ int initramfs_mknod(struct initramfs *ihead, const char *filename,
     else
        bytes = 0;
 
-    bytes += ((sizeof(struct cpio_header) + namelen + 1) + 3) & ~3;
+    hdr_sz = ((sizeof(struct cpio_header) + namelen + 1) + 3) & ~3;
+    bytes += hdr_sz;
 
     bp = buffer = malloc(bytes);
     if (!buffer)
@@ -127,8 +130,8 @@ int initramfs_mknod(struct initramfs *ihead, const char *filename,
                  "%08x%08x%08x%08x", next_ino++, mode,
                  0, 0, 1, 0, len, 0, 1, major, minor, namelen + 1, 0);
     memcpy(bp, filename, namelen);
-    bp += len;
-    pad = (-(sizeof(struct cpio_header) + namelen) & 3) + 1;
+    bp += namelen;
+    pad = hdr_sz - (sizeof(struct cpio_header) + namelen);
     memset(bp, 0, pad);
 
     if (initramfs_add_data(ihead, buffer, bytes, bytes, 4)) {