[PATCH v2 2/6] util: use helper functions for extracting image headers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Rename/retype the little endian functions used for qed images and add
corresponding big endian ones.
---
 src/util/virstoragefile.c | 152 ++++++++++++++++++++--------------------------
 1 file changed, 66 insertions(+), 86 deletions(-)

diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 6e2d61e..9e50ccb 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -208,6 +208,51 @@ static struct FileTypeInfo const fileTypeInfo[] = {
 };
 verify(ARRAY_CARDINALITY(fileTypeInfo) == VIR_STORAGE_FILE_LAST);
 
+
+static uint64_t
+getBEHeader64(const unsigned char *buf)
+{
+    return (((uint64_t)buf[0] << 56)
+            | ((uint64_t)buf[1] << 48)
+            | ((uint64_t)buf[2] << 40)
+            | ((uint64_t)buf[3] << 32)
+            | ((uint64_t)buf[4] << 24)
+            | ((uint64_t)buf[5] << 16)
+            | ((uint64_t)buf[6] << 8)
+            | ((uint64_t)buf[7]));
+}
+
+static uint32_t
+getBEHeader32(const unsigned char *buf)
+{
+    return (((uint32_t)buf[0] << 24)
+            | ((uint32_t)buf[1] << 16)
+            | ((uint32_t)buf[2] << 8)
+            | ((uint32_t)buf[3]));
+}
+
+static uint32_t
+getLEHeader32(const unsigned char *loc)
+{
+    return (((uint32_t)loc[3] << 24) |
+            ((uint32_t)loc[2] << 16) |
+            ((uint32_t)loc[1] << 8) |
+            ((uint32_t)loc[0] << 0));
+}
+
+static uint64_t
+getLEHeader64(const unsigned char *loc)
+{
+    return (((uint64_t)loc[7] << 56) |
+            ((uint64_t)loc[6] << 48) |
+            ((uint64_t)loc[5] << 40) |
+            ((uint64_t)loc[4] << 32) |
+            ((uint64_t)loc[3] << 24) |
+            ((uint64_t)loc[2] << 16) |
+            ((uint64_t)loc[1] << 8) |
+            ((uint64_t)loc[0] << 0));
+}
+
 static int
 cowGetBackingStore(char **res,
                    int *format,
@@ -255,16 +300,8 @@ qcow2GetBackingStoreFormat(int *format,
      */
     while (offset < (buf_size-8) &&
            offset < (extension_end-8)) {
-        unsigned int magic =
-            (buf[offset] << 24) +
-            (buf[offset+1] << 16) +
-            (buf[offset+2] << 8) +
-            (buf[offset+3]);
-        unsigned int len =
-            (buf[offset+4] << 24) +
-            (buf[offset+5] << 16) +
-            (buf[offset+6] << 8) +
-            (buf[offset+7]);
+        unsigned int magic = getBEHeader32(buf + offset);
+        unsigned int len = getBEHeader32(buf + offset + 4);
 
         offset += 8;
 
@@ -312,20 +349,12 @@ qcowXGetBackingStore(char **res,
 
     if (buf_size < QCOWX_HDR_BACKING_FILE_OFFSET+8+4)
         return BACKING_STORE_INVALID;
-    offset = (((unsigned long long)buf[QCOWX_HDR_BACKING_FILE_OFFSET] << 56)
-              | ((unsigned long long)buf[QCOWX_HDR_BACKING_FILE_OFFSET+1] << 48)
-              | ((unsigned long long)buf[QCOWX_HDR_BACKING_FILE_OFFSET+2] << 40)
-              | ((unsigned long long)buf[QCOWX_HDR_BACKING_FILE_OFFSET+3] << 32)
-              | ((unsigned long long)buf[QCOWX_HDR_BACKING_FILE_OFFSET+4] << 24)
-              | ((unsigned long long)buf[QCOWX_HDR_BACKING_FILE_OFFSET+5] << 16)
-              | ((unsigned long long)buf[QCOWX_HDR_BACKING_FILE_OFFSET+6] << 8)
-              | buf[QCOWX_HDR_BACKING_FILE_OFFSET+7]); /* QCowHeader.backing_file_offset */
+    /* QCowHeader.backing_file_offset */
+    offset = getBEHeader64(buf + QCOWX_HDR_BACKING_FILE_OFFSET);
     if (offset > buf_size)
         return BACKING_STORE_INVALID;
-    size = ((buf[QCOWX_HDR_BACKING_FILE_SIZE] << 24)
-            | (buf[QCOWX_HDR_BACKING_FILE_SIZE+1] << 16)
-            | (buf[QCOWX_HDR_BACKING_FILE_SIZE+2] << 8)
-            | buf[QCOWX_HDR_BACKING_FILE_SIZE+3]); /* QCowHeader.backing_file_size */
+    /* QCowHeader.backing_file_size */
+    size = getBEHeader32(buf + QCOWX_HDR_BACKING_FILE_SIZE);
     if (size == 0) {
         if (format)
             *format = VIR_STORAGE_FILE_NONE;
@@ -468,28 +497,6 @@ cleanup:
     return ret;
 }
 
-static unsigned long
-qedGetHeaderUL(const unsigned char *loc)
-{
-    return (((unsigned long)loc[3] << 24) |
-            ((unsigned long)loc[2] << 16) |
-            ((unsigned long)loc[1] << 8) |
-            ((unsigned long)loc[0] << 0));
-}
-
-static unsigned long long
-qedGetHeaderULL(const unsigned char *loc)
-{
-    return (((unsigned long long)loc[7] << 56) |
-            ((unsigned long long)loc[6] << 48) |
-            ((unsigned long long)loc[5] << 40) |
-            ((unsigned long long)loc[4] << 32) |
-            ((unsigned long long)loc[3] << 24) |
-            ((unsigned long long)loc[2] << 16) |
-            ((unsigned long long)loc[1] << 8) |
-            ((unsigned long long)loc[0] << 0));
-}
-
 static int
 qedGetBackingStore(char **res,
                    int *format,
@@ -503,7 +510,7 @@ qedGetBackingStore(char **res,
     /* Check if this image has a backing file */
     if (buf_size < QED_HDR_FEATURES_OFFSET+8)
         return BACKING_STORE_INVALID;
-    flags = qedGetHeaderULL(buf + QED_HDR_FEATURES_OFFSET);
+    flags = getLEHeader64(buf + QED_HDR_FEATURES_OFFSET);
     if (!(flags & QED_F_BACKING_FILE)) {
         *format = VIR_STORAGE_FILE_NONE;
         return BACKING_STORE_OK;
@@ -512,10 +519,10 @@ qedGetBackingStore(char **res,
     /* Parse the backing file */
     if (buf_size < QED_HDR_BACKING_FILE_OFFSET+8)
         return BACKING_STORE_INVALID;
-    offset = qedGetHeaderUL(buf + QED_HDR_BACKING_FILE_OFFSET);
+    offset = getLEHeader32(buf + QED_HDR_BACKING_FILE_OFFSET);
     if (offset > buf_size)
         return BACKING_STORE_INVALID;
-    size = qedGetHeaderUL(buf + QED_HDR_BACKING_FILE_SIZE);
+    size = getLEHeader32(buf + QED_HDR_BACKING_FILE_SIZE);
     if (size == 0)
         return BACKING_STORE_OK;
     if (offset + size > buf_size || offset + size < offset)
@@ -633,19 +640,10 @@ virStorageFileMatchesVersion(int format,
     if ((fileTypeInfo[format].versionOffset + 4) > buflen)
         return false;
 
-    if (fileTypeInfo[format].endian == LV_LITTLE_ENDIAN) {
-        version =
-            (buf[fileTypeInfo[format].versionOffset+3] << 24) |
-            (buf[fileTypeInfo[format].versionOffset+2] << 16) |
-            (buf[fileTypeInfo[format].versionOffset+1] << 8) |
-            (buf[fileTypeInfo[format].versionOffset]);
-    } else {
-        version =
-            (buf[fileTypeInfo[format].versionOffset] << 24) |
-            (buf[fileTypeInfo[format].versionOffset+1] << 16) |
-            (buf[fileTypeInfo[format].versionOffset+2] << 8) |
-            (buf[fileTypeInfo[format].versionOffset+3]);
-    }
+    if (fileTypeInfo[format].endian == LV_LITTLE_ENDIAN)
+        version = getLEHeader32(buf + fileTypeInfo[format].versionOffset);
+    else
+        version = getBEHeader32(buf + fileTypeInfo[format].versionOffset);
 
     VIR_DEBUG("Compare detected version %d vs expected version %d",
               version, fileTypeInfo[format].versionNumber);
@@ -688,25 +686,11 @@ virStorageFileGetMetadataFromBuf(int format,
             return 1;
 
         if (fileTypeInfo[format].endian == LV_LITTLE_ENDIAN) {
-            meta->capacity =
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+7] << 56) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+6] << 48) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+5] << 40) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+4] << 32) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+3] << 24) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+2] << 16) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+1] << 8) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset]);
+            meta->capacity = getLEHeader64(buf +
+                                            fileTypeInfo[format].sizeOffset);
         } else {
-            meta->capacity =
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset] << 56) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+1] << 48) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+2] << 40) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+3] << 32) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+4] << 24) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+5] << 16) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+6] << 8) |
-                ((unsigned long long)buf[fileTypeInfo[format].sizeOffset+7]);
+            meta->capacity = getBEHeader64(buf +
+                                            fileTypeInfo[format].sizeOffset);
         }
         /* Avoid unlikely, but theoretically possible overflow */
         if (meta->capacity > (ULLONG_MAX / fileTypeInfo[format].sizeMultiplier))
@@ -715,14 +699,10 @@ virStorageFileGetMetadataFromBuf(int format,
     }
 
     if (fileTypeInfo[format].qcowCryptOffset != -1) {
-        int crypt_format;
-
-        crypt_format =
-            (buf[fileTypeInfo[format].qcowCryptOffset] << 24) |
-            (buf[fileTypeInfo[format].qcowCryptOffset+1] << 16) |
-            (buf[fileTypeInfo[format].qcowCryptOffset+2] << 8) |
-            (buf[fileTypeInfo[format].qcowCryptOffset+3]);
-        meta->encrypted = crypt_format != 0;
+        int crypt_fmt;
+
+        crypt_fmt = getBEHeader32(buf + fileTypeInfo[format].qcowCryptOffset);
+        meta->encrypted = crypt_fmt != 0;
     }
 
     if (fileTypeInfo[format].getBackingStore != NULL) {
-- 
1.7.12.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]