Index: trunk/src/VBox/Runtime/common/misc/tar.cpp
===================================================================
--- trunk/src/VBox/Runtime/common/misc/tar.cpp	(revision 33540)
+++ trunk/src/VBox/Runtime/common/misc/tar.cpp	(revision 33804)
@@ -41,4 +41,5 @@
 #include <iprt/string.h>
 
+
 /******************************************************************************
  *   Structures and Typedefs                                                  *
@@ -58,4 +59,7 @@
 /** @} */
 
+/**
+ * A tar file header.
+ */
 typedef union RTTARRECORD
 {
@@ -79,7 +83,9 @@
     } h;
 } RTTARRECORD;
-typedef RTTARRECORD *PRTTARRECORD;
 AssertCompileSize(RTTARRECORD, 512);
 AssertCompileMemberOffset(RTTARRECORD, h.size, 100+8*3);
+/** Pointer to a tar file header. */
+typedef RTTARRECORD *PRTTARRECORD;
+
 
 #if 0 /* not currently used */
@@ -92,28 +98,54 @@
 #endif
 
-typedef struct RTTARFILEINTERNAL* PRTTARFILEINTERNAL;
+/** Pointer to a tar file handle. */
+typedef struct RTTARFILEINTERNAL *PRTTARFILEINTERNAL;
+
+/**
+ * The internal data of a tar handle.
+ */
 typedef struct RTTARINTERNAL
 {
+    /** The magic (RTTAR_MAGIC). */
+    uint32_t            u32Magic;
+    /** The handle to the tar file. */
+    RTFILE              hTarFile;
+    /** The open mode for hTarFile. */
+    uint32_t            fOpenMode;
+    /** Whether a file within the archive is currently open for writing.
+     * Only one can be open.  */
+    bool                fFileOpenForWrite;
+    /** Whether operating in stream mode. */
+    bool                fStreamMode;
+    /** The file cache of one file.  */
+    PRTTARFILEINTERNAL  pFileCache;
+} RTTARINTERNAL;
+/** Pointer to a the internal data of a tar handle.  */
+typedef RTTARINTERNAL* PRTTARINTERNAL;
+
+/**
+ * The internal data of a file within a tar file.
+ */
+typedef struct RTTARFILEINTERNAL
+{
+    /** The magic (RTTARFILE_MAGIC). */
     uint32_t        u32Magic;
-    RTFILE          hTarFile;
-    bool            fFileOpenForWrite;
-    uint32_t        fOpenMode;
-    bool            fStreamMode;
-    PRTTARFILEINTERNAL pFileCache;
-} RTTARINTERNAL;
-typedef RTTARINTERNAL* PRTTARINTERNAL;
-
-typedef struct RTTARFILEINTERNAL
-{
-    uint32_t        u32Magic;
+    /** Pointer to back to the tar file. */
     PRTTARINTERNAL  pTar;
+    /** The name of the file. */
     char           *pszFilename;
-    uint64_t        uStart;
+    /** The offset into the archive where the file header starts. */
+    uint64_t        offStart;
+    /** The size of the file. */
     uint64_t        cbSize;
+    /** The size set by RTTarFileSetSize(). */
     uint64_t        cbSetSize;
-    uint64_t        uCurrentPos;
+    /** The current offset within this file. */
+    uint64_t        offCurrent;
+    /** The open mode. */
     uint32_t        fOpenMode;
 } RTTARFILEINTERNAL;
-typedef RTTARFILEINTERNAL* PRTTARFILEINTERNAL;
+/** Pointer to the internal data of a tar file.  */
+typedef RTTARFILEINTERNAL *PRTTARFILEINTERNAL;
+
 
 /******************************************************************************
@@ -155,4 +187,5 @@
     } while (0)
 
+
 /******************************************************************************
  *   Internal Functions                                                       *
@@ -220,6 +253,8 @@
 }
 
-DECLINLINE(int) rtTarCreateHeaderRecord(PRTTARRECORD pRecord, const char *pszSrcName, uint64_t cbSize, RTUID uid, RTGID gid, RTFMODE fmode, int64_t mtime)
-{
+DECLINLINE(int) rtTarCreateHeaderRecord(PRTTARRECORD pRecord, const char *pszSrcName, uint64_t cbSize,
+                                        RTUID uid, RTGID gid, RTFMODE fmode, int64_t mtime)
+{
+    /** @todo check for field overflows. */
     /* Fill the header record */
 //    RT_ZERO(pRecord);
@@ -236,19 +271,19 @@
 
     /* Create the checksum out of the new header */
-    uint32_t chksum = 0;
-    int rc = rtTarCalcChkSum(pRecord, &chksum);
+    uint32_t uChkSum = 0;
+    int rc = rtTarCalcChkSum(pRecord, &uChkSum);
     if (RT_FAILURE(rc))
         return rc;
     /* Format the checksum */
-    RTStrPrintf(pRecord->h.chksum, sizeof(pRecord->h.chksum), "%0.7o", chksum);
+    RTStrPrintf(pRecord->h.chksum, sizeof(pRecord->h.chksum), "%0.7o", uChkSum);
 
     return VINF_SUCCESS;
 }
 
-DECLINLINE(void*) rtTarMemTmpAlloc(size_t *pcbSize)
+DECLINLINE(void *) rtTarMemTmpAlloc(size_t *pcbSize)
 {
     *pcbSize = 0;
-    /* Allocate a reasonably large buffer, fall back on a tiny one. Note:
-     * has to be 512 byte aligned and >= 512 byte. */
+    /* Allocate a reasonably large buffer, fall back on a tiny one.
+     * Note: has to be 512 byte aligned and >= 512 byte. */
     size_t cbTmp = _1M;
     void *pvTmp = RTMemTmpAlloc(cbTmp);
@@ -298,6 +333,11 @@
     pFileInt->u32Magic = RTTARFILE_MAGIC;
     pFileInt->pTar = pInt;
+    pFileInt->fOpenMode = fOpen;
     pFileInt->pszFilename = RTStrDup(pszFilename);
-    pFileInt->fOpenMode = fOpen;
+    if (!pFileInt->pszFilename)
+    {
+        RTMemFree(pFileInt);
+        return NULL;
+    }
 
     return pFileInt;
@@ -312,4 +352,9 @@
     memcpy(pNewInt, pInt, sizeof(RTTARFILEINTERNAL));
     pNewInt->pszFilename = RTStrDup(pInt->pszFilename);
+    if (!pNewInt->pszFilename)
+    {
+        RTMemFree(pNewInt);
+        return NULL;
+    }
 
     return pNewInt;
@@ -329,11 +374,11 @@
 static int rtTarExtractFileToFile(RTTARFILE hFile, const char *pszTargetName, const uint64_t cbOverallSize, uint64_t &cbOverallWritten, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
 {
+    /* Open the target file */
     RTFILE hNewFile;
-    /* Open the target file */
     int rc = RTFileOpen(&hNewFile, pszTargetName, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
     if (RT_FAILURE(rc))
         return rc;
 
-    void *pvTmp = 0;
+    void *pvTmp = NULL;
     do
     {
@@ -346,4 +391,5 @@
             break;
         }
+
         /* Get the size of the source file */
         uint64_t cbToCopy = 0;
@@ -351,4 +397,5 @@
         if (RT_FAILURE(rc))
             break;
+
         /* Copy the content from hFile over to pszTargetName. */
         uint64_t cbAllWritten = 0; /* Already copied */
@@ -358,7 +405,9 @@
             if (pfnProgressCallback)
                 pfnProgressCallback((unsigned)(100.0 / cbOverallSize * cbOverallWritten), pvUser);
+
             /* Finished already? */
             if (cbAllWritten == cbToCopy)
                 break;
+
             /* Read one block. */
             cbRead = RT_MIN(cbToCopy - cbAllWritten, cbTmp);
@@ -366,8 +415,10 @@
             if (RT_FAILURE(rc))
                 break;
+
             /* Write the block */
             rc = RTFileWrite(hNewFile, pvTmp, cbRead, NULL);
             if (RT_FAILURE(rc))
                 break;
+
             /* Count how many bytes are written already */
             cbAllWritten += cbRead;
@@ -375,6 +426,5 @@
         }
 
-    }
-    while(0);
+    } while(0);
 
     /* Cleanup */
@@ -406,6 +456,6 @@
 static int rtTarAppendFileFromFile(RTTAR hTar, const char *pszSrcName, const uint64_t cbOverallSize, uint64_t &cbOverallWritten, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
 {
+    /* Open the source file */
     RTFILE hOldFile;
-    /* Open the source file */
     int rc = RTFileOpen(&hOldFile, pszSrcName, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
     if (RT_FAILURE(rc))
@@ -413,5 +463,5 @@
 
     RTTARFILE hFile = NIL_RTTARFILE;
-    void *pvTmp = 0;
+    void *pvTmp = NULL;
     do
     {
@@ -432,4 +482,5 @@
         RTFMODE fmode = 0600; /* Make some save default */
         int64_t mtime = 0;
+
         /* This isn't critical. Use the defaults if it fails. */
         rc = RTFileQueryInfo(hOldFile, &info, RTFSOBJATTRADD_UNIX);
@@ -441,8 +492,10 @@
             mtime = RTTimeSpecGetSeconds(&info.ModificationTime);
         }
+
         /* Set the mode from the other file */
         rc = RTTarFileSetMode(hFile, fmode);
         if (RT_FAILURE(rc))
             break;
+
         /* Set the modification time from the other file */
         RTTIMESPEC time;
@@ -451,4 +504,5 @@
         if (RT_FAILURE(rc))
             break;
+
         /* Set the owner from the other file */
         rc = RTTarFileSetOwner(hFile, uid, gid);
@@ -465,11 +519,11 @@
         }
 
-        uint64_t cbAllWritten = 0; /* Already copied */
-        uint64_t cbRead = 0; /* Actually read in the last step */
-        uint64_t cbWrite = 0; /* Actually write in the last step */
         /* Copy the content from pszSrcName over to hFile. This is done block
          * wise in 512 byte steps. After this copying is finished hFile will be
          * on a 512 byte boundary, regardless if the file copied is 512 byte
          * size aligned. */
+        uint64_t cbAllWritten = 0; /* Already copied */
+        uint64_t cbRead       = 0; /* Actually read in the last step */
+        uint64_t cbWrite      = 0; /* Actually write in the last step */
         for (;;)
         {
@@ -478,21 +532,22 @@
             if (cbAllWritten >= cbToCopy)
                 break;
+
             /* Read one block. Either its the buffer size or the rest of the
              * file. */
             cbRead = RT_MIN(cbToCopy - cbAllWritten, cbTmp);
-            /* Read one block */
             rc = RTFileRead(hOldFile, pvTmp, cbRead, NULL);
             if (RT_FAILURE(rc))
                 break;
+
             /* Write one block. */
             rc = RTTarFileWriteAt(hFile, cbAllWritten, pvTmp, cbRead, NULL);
             if (RT_FAILURE(rc))
                 break;
+
             /* Count how many bytes (of the original file) are written already */
             cbAllWritten += cbRead;
             cbOverallWritten += cbRead;
         }
-    }
-    while(0);
+    } while (0);
 
     /* Cleanup */
@@ -518,9 +573,9 @@
 }
 
-static int rtTarFindFile(RTFILE hFile, const char *pszFile, uint64_t *puOffset, uint64_t *pcbSize)
+static int rtTarFindFile(RTFILE hFile, const char *pszFile, uint64_t *poff, uint64_t *pcbSize)
 {
     /* Assume we are on the file head. */
-    int rc = VINF_SUCCESS;
-    bool fFound = false;
+    int         rc      = VINF_SUCCESS;
+    bool        fFound  = false;
     RTTARRECORD record;
     for (;;)
@@ -531,4 +586,5 @@
         if (RT_FAILURE(rc))
             break;
+
         /* We support normal files only */
         if (   record.h.linkflag == LF_OLDNORMAL
@@ -541,6 +597,7 @@
                 if (RT_FAILURE(rc))
                     break;
+
                 /* Seek back, to position the file pointer at the start of the header. */
-                rc = RTFileSeek(hFile, -(int64_t)sizeof(RTTARRECORD), RTFILE_SEEK_CURRENT, puOffset);
+                rc = RTFileSeek(hFile, -(int64_t)sizeof(RTTARRECORD), RTFILE_SEEK_CURRENT, poff);
                 fFound = true;
                 break;
@@ -575,4 +632,5 @@
         if (RT_FAILURE(rc))
             break;
+
         /* We support normal files only */
         if (   record.h.linkflag == LF_OLDNORMAL
@@ -583,7 +641,8 @@
                 if (!RTStrCmp(record.h.name, papszFiles[i]))
                 {
-                    uint64_t cbSize;
                     /* Get the file size */
+                    uint64_t cbSize = 0;
                     rc = RTStrToUInt64Full(record.h.size, 8, &cbSize);
+
                     /* Sum up the overall size */
                     *pcbOverallSize += cbSize;
@@ -613,33 +672,28 @@
  ******************************************************************************/
 
-RTR3DECL(int) RTTarOpen(PRTTAR phTar, const char* pszTarname, uint32_t fMode, bool fStream)
-{
-    PRTTARINTERNAL pInt = (PRTTARINTERNAL)RTMemAllocZ(sizeof(RTTARINTERNAL));
-    if (!pInt)
+RTR3DECL(int) RTTarOpen(PRTTAR phTar, const char *pszTarname, uint32_t fMode, bool fStream)
+{
+    /*
+     * Create a tar instance.
+     */
+    PRTTARINTERNAL pThis = (PRTTARINTERNAL)RTMemAllocZ(sizeof(RTTARINTERNAL));
+    if (!pThis)
         return VERR_NO_MEMORY;
 
-    pInt->u32Magic = RTTAR_MAGIC;
-    pInt->fOpenMode = fMode;
-    pInt->fStreamMode = fStream && (fMode & RTFILE_O_READ);
-
-    int rc = VINF_SUCCESS;
-    do
-    {
-        /* Open the tar file. */
-        rc = RTFileOpen(&pInt->hTarFile, pszTarname, fMode);
-        if (RT_FAILURE(rc))
-            break;
-    }
-    while(0);
-
-    if (RT_FAILURE(rc))
-    {
-        /* Todo: remove if created by us */
-        if (pInt->hTarFile)
-            RTFileClose(pInt->hTarFile);
-        RTMemFree(pInt);
-    }else
-        *phTar = (RTTAR)pInt;
-
+    pThis->u32Magic    = RTTAR_MAGIC;
+    pThis->fOpenMode   = fMode;
+    pThis->fStreamMode = fStream && (fMode & RTFILE_O_READ);
+
+    /*
+     * Open the tar file.
+     */
+    int rc = RTFileOpen(&pThis->hTarFile, pszTarname, fMode);
+    if (RT_SUCCESS(rc))
+    {
+        *phTar = pThis;
+        return VINF_SUCCESS;
+    }
+
+    RTMemFree(pThis);
     return rc;
 }
@@ -666,5 +720,5 @@
 #endif
 
-    if (pInt->hTarFile)
+    if (pInt->hTarFile != NIL_RTFILE)
         rc = RTFileClose(pInt->hTarFile);
 
@@ -673,5 +727,5 @@
     {
         rtDeleteTarFileInternal(pInt->pFileCache);
-        pInt->pFileCache = 0;
+        pInt->pFileCache = NULL;
     }
 
@@ -709,13 +763,14 @@
 
     int rc = VINF_SUCCESS;
-    do
+    do /* break loop */
     {
         if (pFileInt->fOpenMode & RTFILE_O_WRITE)
         {
             pInt->fFileOpenForWrite = true;
+
             /* If we are in write mode, we also in append mode. Add an dummy
              * header at the end of the current file. It will be filled by the
              * close operation. */
-            rc = RTFileSeek(pFileInt->pTar->hTarFile, 0, RTFILE_SEEK_END, &pFileInt->uStart);
+            rc = RTFileSeek(pFileInt->pTar->hTarFile, 0, RTFILE_SEEK_END, &pFileInt->offStart);
             if (RT_FAILURE(rc))
                 break;
@@ -732,6 +787,7 @@
             if (RT_FAILURE(rc))
                 break;
+
             /* Search for the file. */
-            rc = rtTarFindFile(pFileInt->pTar->hTarFile, pszFilename, &pFileInt->uStart, &pFileInt->cbSize);
+            rc = rtTarFindFile(pFileInt->pTar->hTarFile, pszFilename, &pFileInt->offStart, &pFileInt->cbSize);
             if (RT_FAILURE(rc))
                 break;
@@ -739,8 +795,8 @@
         else
         {
-        }
-
-    }
-    while(0);
+            /** @todo is something missing here? */
+        }
+
+    } while (0);
 
     /* Cleanup on failure */
@@ -773,14 +829,16 @@
         /* In read mode, we want to make sure to stay at the aligned end of this
          * file, so the next file could be read immediately. */
-        uint64_t uCurPos = RTFileTell(pFileInt->pTar->hTarFile);
+        uint64_t offCur = RTFileTell(pFileInt->pTar->hTarFile);
+
         /* Check that the file pointer is somewhere within the last open file.
          * If we are at the beginning (nothing read yet) nothing will be done.
          * A user could open/close a file more than once, without reading
          * something. */
-        if (pFileInt->uStart + sizeof(RTTARRECORD) < uCurPos && uCurPos < RT_ALIGN(pFileInt->uStart + sizeof(RTTARRECORD) + pFileInt->cbSize, sizeof(RTTARRECORD)))
+        if (   pFileInt->offStart + sizeof(RTTARRECORD) < offCur
+            && offCur < RT_ALIGN(pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize, sizeof(RTTARRECORD)))
         {
             /* Seek to the next file header. */
-            uint64_t uNextPos = RT_ALIGN(pFileInt->uStart + sizeof(RTTARRECORD) + pFileInt->cbSize, sizeof(RTTARRECORD));
-            rc = RTFileSeek(pFileInt->pTar->hTarFile, uNextPos - uCurPos, RTFILE_SEEK_CURRENT, NULL);
+            uint64_t offNext = RT_ALIGN(pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize, sizeof(RTTARRECORD));
+            rc = RTFileSeek(pFileInt->pTar->hTarFile, offNext - offCur, RTFILE_SEEK_CURRENT, NULL);
         }
     }
@@ -798,4 +856,5 @@
                     break;
             }
+
             /* If the written size isn't 512 byte aligned, we need to fix this. */
             RTTARRECORD record;
@@ -805,17 +864,24 @@
             {
                 /* Note the RTFile method. We didn't increase the cbSize or cbCurrentPos here. */
-                rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->uStart + sizeof(RTTARRECORD) + pFileInt->cbSize, &record, cbSizeAligned - pFileInt->cbSize, NULL);
+                rc = RTFileWriteAt(pFileInt->pTar->hTarFile,
+                                   pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize,
+                                   &record,
+                                   cbSizeAligned - pFileInt->cbSize,
+                                   NULL);
                 if (RT_FAILURE(rc))
                     break;
             }
+
             /* Create a header record for the file */
             /* Todo: mode, gid, uid, mtime should be setable (or detected myself) */
             RTTIMESPEC time;
             RTTimeNow(&time);
-            rc = rtTarCreateHeaderRecord(&record, pFileInt->pszFilename, pFileInt->cbSize, 0, 0, 0600, RTTimeSpecGetSeconds(&time));
+            rc = rtTarCreateHeaderRecord(&record, pFileInt->pszFilename, pFileInt->cbSize,
+                                         0, 0, 0600, RTTimeSpecGetSeconds(&time));
             if (RT_FAILURE(rc))
                 break;
+
             /* Write this at the start of the file data */
-            rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->uStart, &record, sizeof(RTTARRECORD), NULL);
+            rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart, &record, sizeof(RTTARRECORD), NULL);
             if (RT_FAILURE(rc))
                 break;
@@ -830,5 +896,5 @@
 }
 
-RTR3DECL(int) RTTarFileSeek(RTTARFILE hFile, uint64_t uOffset,  unsigned uMethod, uint64_t *poffActual)
+RTR3DECL(int) RTTarFileSeek(RTTARFILE hFile, uint64_t offSeek, unsigned uMethod, uint64_t *poffActual)
 {
     PRTTARFILEINTERNAL pFileInt = hFile;
@@ -842,21 +908,21 @@
         case RTFILE_SEEK_BEGIN:
         {
-            if (uOffset > pFileInt->cbSize)
+            if (offSeek > pFileInt->cbSize)
                 return VERR_SEEK_ON_DEVICE;
-            pFileInt->uCurrentPos = uOffset;
+            pFileInt->offCurrent = offSeek;
             break;
         }
         case RTFILE_SEEK_CURRENT:
         {
-            if (pFileInt->uCurrentPos + uOffset > pFileInt->cbSize)
+            if (pFileInt->offCurrent + offSeek > pFileInt->cbSize)
                 return VERR_SEEK_ON_DEVICE;
-            pFileInt->uCurrentPos += uOffset;
+            pFileInt->offCurrent += offSeek;
             break;
         }
         case RTFILE_SEEK_END:
         {
-            if ((int64_t)pFileInt->cbSize - (int64_t)uOffset < 0)
+            if ((int64_t)pFileInt->cbSize - (int64_t)offSeek < 0)
                 return VERR_NEGATIVE_SEEK;
-            pFileInt->uCurrentPos = pFileInt->cbSize - uOffset;
+            pFileInt->offCurrent = pFileInt->cbSize - offSeek;
             break;
         }
@@ -870,7 +936,7 @@
 {
     PRTTARFILEINTERNAL pFileInt = hFile;
-    RTTARFILE_VALID_RETURN_RC(pFileInt, ~0ULL);
-
-    return pFileInt->uCurrentPos;
+    RTTARFILE_VALID_RETURN_RC(pFileInt, UINT64_MAX);
+
+    return pFileInt->offCurrent;
 }
 
@@ -881,8 +947,8 @@
 
     /* Todo: optimize this, by checking the current pos */
-    return RTTarFileReadAt(hFile, pFileInt->uCurrentPos, pvBuf, cbToRead, pcbRead);
-}
-
-RTR3DECL(int) RTTarFileReadAt(RTTARFILE hFile, uint64_t uOffset, void *pvBuf, size_t cbToRead, size_t *pcbRead)
+    return RTTarFileReadAt(hFile, pFileInt->offCurrent, pvBuf, cbToRead, pcbRead);
+}
+
+RTR3DECL(int) RTTarFileReadAt(RTTARFILE hFile, uint64_t off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
 {
     PRTTARFILEINTERNAL pFileInt = hFile;
@@ -890,5 +956,5 @@
 
     /* Check that we not read behind the end of file. If so return immediately. */
-    if (uOffset > pFileInt->cbSize)
+    if (off > pFileInt->cbSize)
     {
         if (pcbRead)
@@ -897,8 +963,8 @@
     }
 
-    size_t cbToCopy = RT_MIN(pFileInt->cbSize - uOffset, cbToRead);
+    size_t cbToCopy = RT_MIN(pFileInt->cbSize - off, cbToRead);
     size_t cbTmpRead = 0;
-    int rc = RTFileReadAt(pFileInt->pTar->hTarFile, pFileInt->uStart + 512 + uOffset, pvBuf, cbToCopy, &cbTmpRead);
-    pFileInt->uCurrentPos = uOffset + cbTmpRead;
+    int rc = RTFileReadAt(pFileInt->pTar->hTarFile, pFileInt->offStart + 512 + off, pvBuf, cbToCopy, &cbTmpRead);
+    pFileInt->offCurrent = off + cbTmpRead;
     if (pcbRead)
         *pcbRead = cbTmpRead;
@@ -912,9 +978,9 @@
     RTTARFILE_VALID_RETURN(pFileInt);
 
-    /* Todo: optimize this, by checking the current pos */
-    return RTTarFileWriteAt(hFile, pFileInt->uCurrentPos, pvBuf, cbToWrite, pcbWritten);
-}
-
-RTR3DECL(int) RTTarFileWriteAt(RTTARFILE hFile, uint64_t uOffset, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
+    /** @todo Optimize this, by checking the current pos */
+    return RTTarFileWriteAt(hFile, pFileInt->offCurrent, pvBuf, cbToWrite, pcbWritten);
+}
+
+RTR3DECL(int) RTTarFileWriteAt(RTTARFILE hFile, uint64_t off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
 {
     PRTTARFILEINTERNAL pFileInt = hFile;
@@ -925,7 +991,7 @@
 
     size_t cbTmpWritten = 0;
-    int rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->uStart + 512 + uOffset, pvBuf, cbToWrite, &cbTmpWritten);
+    int rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart + 512 + off, pvBuf, cbToWrite, &cbTmpWritten);
     pFileInt->cbSize += cbTmpWritten;
-    pFileInt->uCurrentPos = uOffset + cbTmpWritten;
+    pFileInt->offCurrent = off + cbTmpWritten;
     if (pcbWritten)
         *pcbWritten = cbTmpWritten;
@@ -955,6 +1021,6 @@
         return VERR_WRITE_ERROR;
 
-    /* Todo: if cbSize is smaller than pFileInt->cbSize we have to truncate the
-       current file. */
+    /** @todo If cbSize is smaller than pFileInt->cbSize we have to
+     * truncate the current file. */
     pFileInt->cbSetSize = cbSize;
 
@@ -971,10 +1037,16 @@
 
     /* Read the mode out of the header entry */
-    char mode[8];
-    int rc = RTFileReadAt(pFileInt->pTar->hTarFile, pFileInt->uStart + RT_OFFSETOF(RTTARRECORD, h.mode), mode, 8, NULL);
+    char szMode[RT_SIZEOFMEMB(RTTARRECORD, h.mode)+1];
+    int rc = RTFileReadAt(pFileInt->pTar->hTarFile,
+                          pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.mode),
+                          szMode,
+                          RT_SIZEOFMEMB(RTTARRECORD, h.mode),
+                          NULL);
     if (RT_FAILURE(rc))
         return rc;
+    szMode[sizeof(szMode) - 1] = '\0';
+
     /* Convert it to an integer */
-    return RTStrToUInt32Full(mode, 8, pfMode);
+    return RTStrToUInt32Full(szMode, 8, pfMode);
 }
 
@@ -988,8 +1060,13 @@
 
     /* Convert the mode to an string. */
-    char mode[8];
-    RTStrPrintf(mode, sizeof(mode), "%0.7o", fMode);
+    char szMode[RT_SIZEOFMEMB(RTTARRECORD, h.mode)];
+    RTStrPrintf(szMode, sizeof(szMode), "%0.7o", fMode);
+
     /* Write it directly into the header */
-    return RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->uStart + RT_OFFSETOF(RTTARRECORD, h.mode), mode, 8, NULL);
+    return RTFileWriteAt(pFileInt->pTar->hTarFile,
+                         pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.mode),
+                         szMode,
+                         RT_SIZEOFMEMB(RTTARRECORD, h.mode),
+                         NULL);
 }
 
@@ -1000,14 +1077,21 @@
 
     /* Read the time out of the header entry */
-    char mtime[12];
-    int rc = RTFileReadAt(pFileInt->pTar->hTarFile, pFileInt->uStart + RT_OFFSETOF(RTTARRECORD, h.mtime), mtime, 12, NULL);
+    char szModTime[RT_SIZEOFMEMB(RTTARRECORD, h.mtime) + 1];
+    int rc = RTFileReadAt(pFileInt->pTar->hTarFile,
+                          pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.mtime),
+                          szModTime,
+                          RT_SIZEOFMEMB(RTTARRECORD, h.mtime),
+                          NULL);
     if (RT_FAILURE(rc))
         return rc;
+    szModTime[sizeof(szModTime) - 1] = '\0';
+
     /* Convert it to an integer */
-    int64_t tmpSeconds;
-    rc = RTStrToInt64Full(mtime, 12, &tmpSeconds);
+    int64_t cSeconds;
+    rc = RTStrToInt64Full(szModTime, 12, &cSeconds);
+
     /* And back to our time structure */
     if (RT_SUCCESS(rc))
-        RTTimeSpecSetSeconds(pTime, tmpSeconds);
+        RTTimeSpecSetSeconds(pTime, cSeconds);
 
     return rc;
@@ -1023,8 +1107,13 @@
 
     /* Convert the time to an string. */
-    char mtime[12];
-    RTStrPrintf(mtime, sizeof(mtime), "%0.11o", RTTimeSpecGetSeconds(pTime));
+    char szModTime[RT_SIZEOFMEMB(RTTARRECORD, h.mtime)];
+    RTStrPrintf(szModTime, sizeof(szModTime), "%0.11o", RTTimeSpecGetSeconds(pTime));
+
     /* Write it directly into the header */
-    return RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->uStart + RT_OFFSETOF(RTTARRECORD, h.mtime), mtime, 12, NULL);
+    return RTFileWriteAt(pFileInt->pTar->hTarFile,
+                         pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.mtime),
+                         szModTime,
+                         RT_SIZEOFMEMB(RTTARRECORD, h.mtime),
+                         NULL);
 }
 
@@ -1035,18 +1124,23 @@
 
     /* Read the uid and gid out of the header entry */
-    char uid[8];
-    int rc = RTFileReadAt(pFileInt->pTar->hTarFile, pFileInt->uStart + RT_OFFSETOF(RTTARRECORD, h.uid), uid, 8, NULL);
+    AssertCompileAdjacentMembers(RTTARRECORD, h.uid, h.gid);
+    char szUidGid[RT_SIZEOFMEMB(RTTARRECORD, h.uid) + RT_SIZEOFMEMB(RTTARRECORD, h.gid) + 1];
+    int rc = RTFileReadAt(pFileInt->pTar->hTarFile,
+                          pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.uid),
+                          szUidGid,
+                          sizeof(szUidGid) - 1,
+                          NULL);
     if (RT_FAILURE(rc))
         return rc;
-    char gid[8];
-    rc = RTFileReadAt(pFileInt->pTar->hTarFile, pFileInt->uStart + RT_OFFSETOF(RTTARRECORD, h.gid), gid, 8, NULL);
-    if (RT_FAILURE(rc))
-        return rc;
+    szUidGid[sizeof(szUidGid) - 1] = '\0';
+
     /* Convert it to integer */
-    rc = RTStrToUInt32Full(uid, 8, pUid);
-    if (RT_FAILURE(rc))
-        return rc;
-
-    return RTStrToUInt32Full(gid, 8, pGid);
+    rc = RTStrToUInt32Full(&szUidGid[RT_SIZEOFMEMB(RTTARRECORD, h.uid)], 8, pGid);
+    if (RT_SUCCESS(rc))
+    {
+        szUidGid[RT_SIZEOFMEMB(RTTARRECORD, h.uid)] = '\0';
+        rc = RTStrToUInt32Full(szUidGid, 8, pUid);
+    }
+    return rc;
 }
 
@@ -1058,4 +1152,6 @@
     if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
         return VERR_WRITE_ERROR;
+    AssertReturn(uid == (uint32_t)-1 || uid <= 07777777, VERR_OUT_OF_RANGE);
+    AssertReturn(gid == (uint32_t)-1 || gid <= 07777777, VERR_OUT_OF_RANGE);
 
     int rc = VINF_SUCCESS;
@@ -1064,18 +1160,29 @@
     {
         /* Convert the uid to an string. */
-        char tmpUid[8];
-        RTStrPrintf(tmpUid, sizeof(tmpUid), "%0.7o", uid);
+        char szUid[RT_SIZEOFMEMB(RTTARRECORD, h.uid)];
+        RTStrPrintf(szUid, sizeof(szUid), "%0.7o", uid);
+
         /* Write it directly into the header */
-        rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->uStart + RT_OFFSETOF(RTTARRECORD, h.uid), tmpUid, 8, NULL);
+        rc = RTFileWriteAt(pFileInt->pTar->hTarFile,
+                           pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.uid),
+                           szUid,
+                           RT_SIZEOFMEMB(RTTARRECORD, h.uid),
+                           NULL);
         if (RT_FAILURE(rc))
             return rc;
     }
+
     if (gid != (uint32_t)-1)
     {
         /* Convert the gid to an string. */
-        char tmpGid[8];
-        RTStrPrintf(tmpGid, sizeof(tmpGid), "%0.7o", gid);
+        char szGid[RT_SIZEOFMEMB(RTTARRECORD, h.gid)];
+        RTStrPrintf(szGid, sizeof(szGid), "%0.7o", gid);
+
         /* Write it directly into the header */
-        rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->uStart + RT_OFFSETOF(RTTARRECORD, h.gid), tmpGid, 8, NULL);
+        rc = RTFileWriteAt(pFileInt->pTar->hTarFile,
+                           pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.gid),
+                           szGid,
+                           RT_SIZEOFMEMB(RTTARRECORD, h.gid),
+                           NULL);
         if (RT_FAILURE(rc))
             return rc;
@@ -1097,5 +1204,5 @@
     /* Open the tar file */
     RTTAR hTar;
-    int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, false);
+    int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, false /*fStream*/);
     if (RT_FAILURE(rc))
         return rc;
@@ -1121,5 +1228,5 @@
     /* Open the tar file */
     RTTAR hTar;
-    int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, false);
+    int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, false /*fStream*/);
     if (RT_FAILURE(rc))
         return rc;
@@ -1129,14 +1236,14 @@
 
     PRTTARINTERNAL pInt = hTar;
-    char **papszFiles = 0;
+    char **papszFiles = NULL;
     size_t cFiles = 0;
-    do
+    do /* break loop */
     {
         /* Initialize the file name array with one slot */
         size_t cFilesAlloc = 1;
-        papszFiles = (char**)RTMemAlloc(sizeof(char *));
+        papszFiles = (char **)RTMemAlloc(sizeof(char *));
         if (!papszFiles)
         {
-            return VERR_NO_MEMORY;
+            rc = VERR_NO_MEMORY;
             break;
         }
@@ -1185,6 +1292,5 @@
                 break;
         }
-    }
-    while(0);
+    } while(0);
 
     if (rc == VERR_TAR_END_OF_FILE)
@@ -1209,7 +1315,10 @@
 }
 
-RTR3DECL(int) RTTarExtractFileToBuf(const char *pszTarFile, void **ppvBuf, size_t *pcbSize, const char *pszFile, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
-{
-    /* Validate input */
+RTR3DECL(int) RTTarExtractFileToBuf(const char *pszTarFile, void **ppvBuf, size_t *pcbSize, const char *pszFile,
+                                    PFNRTPROGRESS pfnProgressCallback, void *pvUser)
+{
+    /*
+     * Validate input
+     */
     AssertPtrReturn(pszTarFile, VERR_INVALID_POINTER);
     AssertPtrReturn(ppvBuf, VERR_INVALID_POINTER);
@@ -1219,14 +1328,14 @@
     AssertPtrNullReturn(pvUser, VERR_INVALID_POINTER);
 
-    /* Todo: progress bar */
-
-    int rc = VINF_SUCCESS;
-    RTTAR hTar = NIL_RTTAR;
-    RTTARFILE hFile = NIL_RTTARFILE;
-    char *pvTmp = 0;
-    uint64_t cbToCopy = 0;
-    do
-    {
-        rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, false);
+    /** @todo progress bar - is this TODO still valid? */
+
+    int         rc      = VINF_SUCCESS;
+    RTTAR       hTar    = NIL_RTTAR;
+    RTTARFILE   hFile   = NIL_RTTARFILE;
+    char       *pvTmp   = NULL;
+    uint64_t    cbToCopy= 0;
+    do /* break loop */
+    {
+        rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, false /*fStream*/);
         if (RT_FAILURE(rc))
             break;
@@ -1237,6 +1346,7 @@
         if (RT_FAILURE(rc))
             break;
+
         /* Allocate the memory for the file content. */
-        pvTmp = (char*)RTMemAlloc(cbToCopy);
+        pvTmp = (char *)RTMemAlloc(cbToCopy);
         if (!pvTmp)
         {
@@ -1257,6 +1367,5 @@
             cbAllRead += cbRead;
         }
-    }
-    while(0);
+    } while (0);
 
     /* Set output values on success */
@@ -1279,5 +1388,6 @@
 }
 
-RTR3DECL(int) RTTarExtractFiles(const char *pszTarFile, const char *pszOutputDir, const char * const *papszFiles, size_t cFiles, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
+RTR3DECL(int) RTTarExtractFiles(const char *pszTarFile, const char *pszOutputDir, const char * const *papszFiles,
+                                size_t cFiles, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
 {
     /* Validate input */
@@ -1291,9 +1401,9 @@
     /* Open the tar file */
     RTTAR hTar;
-    int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, false);
+    int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, false /*fStream*/);
     if (RT_FAILURE(rc))
         return rc;
 
-    do
+    do /* break loop */
     {
         /* Get the overall size of all files to extract out of the tar archive
@@ -1315,10 +1425,8 @@
                 break;
             char *pszTargetFile = RTPathJoinA(pszOutputDir, papszFiles[i]);
-            if (!pszTargetFile)
-            {
+            if (pszTargetFile)
+                rc = rtTarExtractFileToFile(hFile, pszTargetFile, cbOverallSize, cbOverallWritten, pfnProgressCallback, pvUser);
+            else
                 rc = VERR_NO_STR_MEMORY;
-                break;
-            }
-            rc = rtTarExtractFileToFile(hFile, pszTargetFile, cbOverallSize, cbOverallWritten, pfnProgressCallback, pvUser);
             RTStrFree(pszTargetFile);
             RTTarFileClose(hFile);
@@ -1326,6 +1434,5 @@
                 break;
         }
-    }
-    while(0);
+    } while (0);
 
     RTTarClose(hTar);
@@ -1364,5 +1471,5 @@
 
     RTTAR hTar;
-    int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_CREATE | RTFILE_O_READWRITE | RTFILE_O_DENY_NONE, false);
+    int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_CREATE | RTFILE_O_READWRITE | RTFILE_O_DENY_NONE, false /*fStream*/);
     if (RT_FAILURE(rc))
         return rc;
@@ -1431,5 +1538,5 @@
     if (!pInt->pFileCache)
     {
-        rc = RTTarCurrentFile(hTar, 0);
+        rc = RTTarCurrentFile(hTar, NULL);
         if (RT_FAILURE(rc))
             return rc;
@@ -1438,16 +1545,17 @@
     /* Check that the file pointer is somewhere within the last open file.
      * If not we are somehow busted. */
-    uint64_t uCurPos = RTFileTell(pInt->hTarFile);
-    if (!(pInt->pFileCache->uStart <= uCurPos && uCurPos < pInt->pFileCache->uStart + sizeof(RTTARRECORD) + pInt->pFileCache->cbSize))
+    uint64_t offCur = RTFileTell(pInt->hTarFile);
+    if (!(   pInt->pFileCache->offStart <= offCur
+          && offCur < pInt->pFileCache->offStart + sizeof(RTTARRECORD) + pInt->pFileCache->cbSize))
         return VERR_INVALID_STATE;
 
     /* Seek to the next file header. */
-    uint64_t uNextPos = RT_ALIGN(pInt->pFileCache->uStart + sizeof(RTTARRECORD) + pInt->pFileCache->cbSize, sizeof(RTTARRECORD));
-    rc = RTFileSeek(pInt->hTarFile, uNextPos - uCurPos, RTFILE_SEEK_CURRENT, NULL);
+    uint64_t offNext = RT_ALIGN(pInt->pFileCache->offStart + sizeof(RTTARRECORD) + pInt->pFileCache->cbSize, sizeof(RTTARRECORD));
+    rc = RTFileSeek(pInt->hTarFile, offNext - offCur, RTFILE_SEEK_CURRENT, NULL);
     if (RT_FAILURE(rc))
         return rc;
 
     /* Again check the current filename to fill the cache with the new value. */
-    return RTTarCurrentFile(hTar, 0);
+    return RTTarCurrentFile(hTar, NULL);
 }
 
@@ -1471,5 +1579,5 @@
     {
         /* Are we still direct behind that header? */
-        if (pInt->pFileCache->uStart + sizeof(RTTARRECORD) == RTFileTell(pInt->hTarFile))
+        if (pInt->pFileCache->offStart + sizeof(RTTARRECORD) == RTFileTell(pInt->hTarFile))
         {
             /* Yes, so the streaming can start. Just return the cached file
@@ -1479,14 +1587,13 @@
                 *ppszFilename = RTStrDup(pInt->pFileCache->pszFilename);
             return VINF_SUCCESS;
-        }else
-        {
-            /* Else delete the last open file cache. Might be recreated below. */
-            rtDeleteTarFileInternal(pInt->pFileCache);
-            pInt->pFileCache = 0;
-        }
-    }
-
-    PRTTARFILEINTERNAL pFileInt = 0;
-    do
+        }
+
+        /* Else delete the last open file cache. Might be recreated below. */
+        rtDeleteTarFileInternal(pInt->pFileCache);
+        pInt->pFileCache = NULL;
+    }
+
+    PRTTARFILEINTERNAL pFileInt = NULL;
+    do /* break loop */
     {
         /* Try to read a header entry from the current position. If we aren't
@@ -1499,4 +1606,5 @@
         if (RT_FAILURE(rc))
             break;
+
         /* We support normal files only */
         if (   record.h.linkflag == LF_OLDNORMAL
@@ -1509,10 +1617,13 @@
                 break;
             }
+
             /* Get the file size */
             rc = RTStrToUInt64Full(record.h.size, 8, &pFileInt->cbSize);
             if (RT_FAILURE(rc))
                 break;
+
             /* The start is -512 from here. */
-            pFileInt->uStart = RTFileTell(pInt->hTarFile) - sizeof(RTTARRECORD);
+            pFileInt->offStart = RTFileTell(pInt->hTarFile) - sizeof(RTTARRECORD);
+
             /* Copy the new file structure to our cache. */
             pInt->pFileCache = rtCopyTarFileInternal(pFileInt);
@@ -1520,5 +1631,5 @@
                 *ppszFilename = RTStrDup(pFileInt->pszFilename);
         }
-    }while (0);
+    } while (0);
 
     if (RT_FAILURE(rc))
