Index: trunk/src/VBox/Devices/Storage/VDICore.h
===================================================================
--- trunk/src/VBox/Devices/Storage/VDICore.h	(revision 7230)
+++ trunk/src/VBox/Devices/Storage/VDICore.h	(revision 7231)
@@ -539,15 +539,19 @@
     /** Block shift value for converting byte hdd offset into paBlock index. */
     unsigned                uShiftOffset2Index;
+#ifndef VBOX_VDICORE_VD
     /** Block shift value for converting block index into offset in image. */
     unsigned                uShiftIndex2Offset;
+#endif /* !VBOX_VDICORE_VD */
     /** Offset of data from the beginning of block. */
     unsigned                offStartBlockData;
+#ifndef VBOX_VDICORE_VD
     /** Image is modified flags (VDI_IMAGE_MODIFIED*). */
     unsigned                fModified;
-#ifndef VBOX_VDICORE_VD
     /** Container filename. (UTF-8)
      * @todo Make this variable length to save a bunch of bytes. (low prio) */
     char                    szFilename[RTPATH_MAX];
-#else /* !VBOX_VDICORE_VD */
+#else /* VBOX_VDICORE_VD */
+    /** Total size of image block (including the extra data). */
+    unsigned                cbTotalBlockData;
     /** Container filename. (UTF-8) */
     const char             *pszFilename;
@@ -558,5 +562,5 @@
     /** Opaque data for error callback. */
     void                   *pvErrorUser;
-#endif /* !VBOX_VDICORE_VD */
+#endif /* VBOX_VDICORE_VD */
 } VDIIMAGEDESC, *PVDIIMAGEDESC;
 
Index: trunk/src/VBox/Devices/Storage/VDIHDDCore.cpp
===================================================================
--- trunk/src/VBox/Devices/Storage/VDIHDDCore.cpp	(revision 7230)
+++ trunk/src/VBox/Devices/Storage/VDIHDDCore.cpp	(revision 7231)
@@ -40,6 +40,7 @@
 static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
 static int  vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
-static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType, uint32_t uImageFlags,
-                          const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
+static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType,
+                          uint32_t uImageFlags, const char *pszComment,
+                          uint64_t cbDisk, uint32_t cbBlock,
                           uint32_t cbBlockExtra);
 static int  vdiValidateHeader(PVDIHEADER pHeader);
@@ -113,6 +114,7 @@
  * @param   pHeader     Assumes it was initially initialized to all zeros.
  */
-static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType, uint32_t uImageFlags,
-                          const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
+static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType,
+                          uint32_t uImageFlags, const char *pszComment,
+                          uint64_t cbDisk, uint32_t cbBlock,
                           uint32_t cbBlockExtra)
 {
@@ -296,9 +298,8 @@
     pImage->offStartData       = getImageDataOffset(&pImage->Header);
     pImage->uBlockMask         = getImageBlockSize(&pImage->Header) - 1;
-    pImage->uShiftIndex2Offset =
     pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
     pImage->offStartBlockData  = getImageExtraBlockSize(&pImage->Header);
-    if (pImage->offStartBlockData != 0)
-        pImage->uShiftIndex2Offset += getPowerOfTwo(pImage->offStartBlockData);
+    pImage->cbTotalBlockData   =   pImage->offStartBlockData
+                                 + getImageBlockSize(&pImage->Header);
 }
 
@@ -375,5 +376,5 @@
 
     cbTotal =   pImage->offStartData
-              + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
+              + (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
 
     if (enmType == VD_IMAGE_TYPE_FIXED)
@@ -456,5 +457,5 @@
         }
 
-        cbFill = (uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset;
+        cbFill = (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
         uOff = 0;
         /* do loop to fill all image. */
@@ -684,5 +685,5 @@
 static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
 {
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     vdiFlushImage(pImage);
@@ -879,10 +880,11 @@
     int rc;
 
-    Assert(pImage);
-    Assert(uOffset % 512 == 0);
-    Assert(cbToRead % 512 == 0);
+    Assert(VALID_PTR(pImage));
+    Assert(!(uOffset % 512));
+    Assert(!(cbToRead % 512));
 
     if (   uOffset + cbToRead > getImageDiskSize(&pImage->Header)
-        || cbToRead == 0)
+        || !VALID_PTR(pvBuf)
+        || !cbToRead)
     {
         rc = VERR_INVALID_PARAMETER;
@@ -907,6 +909,6 @@
     else
     {
-        /* block present in image file */
-        uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
+        /* Block present in image file, read relevant data. */
+        uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
                            + (pImage->offStartData + pImage->offStartBlockData + offRead);
         rc = RTFileReadAt(pImage->File, u64Offset, pvBuf, cbToRead, NULL);
@@ -932,7 +934,7 @@
     int rc = VINF_SUCCESS;
 
-    Assert(pImage);
-    Assert(uOffset % 512 == 0);
-    Assert(cbToWrite % 512 == 0);
+    Assert(VALID_PTR(pImage));
+    Assert(!(uOffset % 512));
+    Assert(!(cbToWrite % 512));
 
     if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
@@ -942,5 +944,5 @@
     }
 
-    if (cbToWrite == 0)
+    if (!VALID_PTR(pvBuf) || !cbToWrite)
     {
         rc = VERR_INVALID_PARAMETER;
@@ -971,5 +973,5 @@
              * (which also means that it's a zero block. Don't need to write
              * anything to this block  if the data consists of just zeroes. */
-            Assert(cbToWrite % 4 == 0);
+            Assert(!(cbToWrite % 4));
             Assert(cbToWrite * 8 <= UINT32_MAX);
             if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
@@ -984,9 +986,9 @@
             /* Full block write to previously unallocated block.
              * Allocate block and write data. */
+            Assert(!offWrite);
             unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
-            rc = RTFileWriteAt(pImage->File,
-                                 ((uint64_t)cBlocksAllocated << pImage->uShiftIndex2Offset)
-                               + pImage->offStartData,
-                               pvBuf, cbToWrite, NULL);
+            uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
+                               + (pImage->offStartData + pImage->offStartBlockData);
+            rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
             if (VBOX_FAILURE(rc))
                 goto out;
@@ -1011,8 +1013,10 @@
     }
     else
-        rc = RTFileWriteAt(pImage->File,
-                             ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
-                           + pImage->offStartData + pImage->offStartBlockData + offWrite,
-                           pvBuf, cbToWrite, NULL);
+    {
+        /* Block present in image file, write relevant data. */
+        uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
+                           + (pImage->offStartData + pImage->offStartBlockData + offWrite);
+        rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
+    }
 
 out:
@@ -1040,5 +1044,5 @@
     unsigned uVersion;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1058,6 +1062,6 @@
     int rc = VINF_SUCCESS;
 
-    Assert(pImage);
-    Assert(penmImageType);
+    Assert(VALID_PTR(pImage));
+    Assert(VALID_PTR(penmImageType));
 
     if (pImage)
@@ -1079,5 +1083,5 @@
     uint64_t cbSize;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1097,5 +1101,5 @@
     uint64_t cb = 0;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1122,5 +1126,5 @@
     int rc;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1149,5 +1153,5 @@
     int rc;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1178,5 +1182,5 @@
     int rc;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1215,5 +1219,5 @@
     int rc;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1253,5 +1257,5 @@
     unsigned uImageFlags;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1271,5 +1275,5 @@
     unsigned uOpenFlags;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1316,5 +1320,5 @@
     int rc = VINF_SUCCESS;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1344,5 +1348,5 @@
     int rc;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
@@ -1392,5 +1396,5 @@
     int rc;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1413,5 +1417,5 @@
     int rc = VINF_SUCCESS;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1447,5 +1451,5 @@
     int rc;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1468,5 +1472,5 @@
     int rc = VINF_SUCCESS;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1502,5 +1506,5 @@
     int rc;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1523,5 +1527,5 @@
     int rc = VINF_SUCCESS;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1557,5 +1561,5 @@
     int rc;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1578,5 +1582,5 @@
     int rc = VINF_SUCCESS;
 
-    Assert(pImage);
+    Assert(VALID_PTR(pImage));
 
     if (pImage)
@@ -1636,7 +1640,7 @@
     RTLogPrintf("Image:  fFlags=%08X offStartBlocks=%u offStartData=%u\n",
                 pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
-    RTLogPrintf("Image:  uBlockMask=%08X uShiftIndex2Offset=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
+    RTLogPrintf("Image:  uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
                 pImage->uBlockMask,
-                pImage->uShiftIndex2Offset,
+                pImage->cbTotalBlockData,
                 pImage->uShiftOffset2Index,
                 pImage->offStartBlockData);
