diff options
| author | Michael Meskes <meskes@debian.org> | 2010-06-04 09:49:50 +0200 |
|---|---|---|
| committer | Michael Meskes <meskes@debian.org> | 2010-06-04 09:49:50 +0200 |
| commit | e13debb062071c46f2707d0d0e59c57675b49360 (patch) | |
| tree | 922f54068563b5cf3274bae8ba8122ce4b4ede1d /src/VBox/Runtime/common | |
| parent | abd0051802e55207e88435a185ff8d6e6b8d17d5 (diff) | |
| download | virtualbox-upstream/3.2.2-dfsg.tar.gz | |
Imported Upstream version 3.2.2-dfsgupstream/3.2.2-dfsg
Diffstat (limited to 'src/VBox/Runtime/common')
| -rw-r--r-- | src/VBox/Runtime/common/checksum/RTSha1Digest.cpp | 76 | ||||
| -rw-r--r-- | src/VBox/Runtime/common/checksum/manifest.cpp | 47 | ||||
| -rw-r--r-- | src/VBox/Runtime/common/misc/cidr.cpp | 143 | ||||
| -rw-r--r-- | src/VBox/Runtime/common/string/strformat.cpp | 11 | ||||
| -rw-r--r-- | src/VBox/Runtime/common/string/strformatrt.cpp | 33 |
5 files changed, 176 insertions, 134 deletions
diff --git a/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp b/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp index 5a9d6955f..97d9ed268 100644 --- a/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp +++ b/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp @@ -1,4 +1,4 @@ -/* $Id: RTSha1Digest.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */ +/* $Id: RTSha1Digest.cpp 29901 2010-05-31 12:53:25Z vboxsync $ */ /** @file * IPRT - SHA1 digest creation */ @@ -32,69 +32,95 @@ #include <iprt/sha.h> #include <iprt/assert.h> -#include <iprt/err.h> -#include <iprt/stream.h> +#include <iprt/mem.h> #include <iprt/string.h> +#include <iprt/file.h> #include <openssl/sha.h> - - -RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest) +RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest, PFNRTSHAPROGRESS pfnProgressCallback, void *pvUser) { /* Validate input */ AssertPtrReturn(pszFile, VERR_INVALID_POINTER); AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER); + AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER); *ppszDigest = NULL; + int rc = VINF_SUCCESS; /* Initialize OpenSSL */ SHA_CTX ctx; if (!SHA1_Init(&ctx)) return VERR_INTERNAL_ERROR; - /** @todo r=bird: Using a stream here doesn't really serve much purpose as - * few stream implementations uses a buffer much larger than 4KB. (The - * only I'm aware of is libc on OS/2, which uses 8KB.) */ + /* Fetch the file size. Only needed if there is a progress callback. */ + float multi = 0; + if (pfnProgressCallback) + { + uint64_t cbFile; + rc = RTFileQuerySize(pszFile, &cbFile); + if (RT_FAILURE(rc)) + return rc; + multi = 100.0 / cbFile; + } /* Open the file to calculate a SHA1 sum of */ - PRTSTREAM pStream; - int rc = RTStrmOpen(pszFile, "rb", &pStream); + RTFILE file; + rc = RTFileOpen(&file, pszFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE); if (RT_FAILURE(rc)) return rc; /* Read that file in blocks */ - void *pvBuf[4096]; + void *pvBuf = RTMemTmpAlloc(_1M); + if (!pvBuf) + { + RTFileClose(file); + rc = VERR_NO_MEMORY; + } size_t cbRead; - do + size_t cbReadFull = 0; + for (;;) { cbRead = 0; - rc = RTStrmReadEx(pStream, pvBuf, 4096, &cbRead); - if (RT_FAILURE(rc)) + rc = RTFileRead(file, pvBuf, _1M, &cbRead); + if (RT_FAILURE(rc) || !cbRead) break; if(!SHA1_Update(&ctx, pvBuf, cbRead)) { rc = VERR_INTERNAL_ERROR; break; } - } while (cbRead > 0); - RTStrmClose(pStream); + cbReadFull += cbRead; + /* Call progress callback if some is defined */ + if ( pfnProgressCallback + && RT_FAILURE(pfnProgressCallback((unsigned)(cbReadFull * multi), pvUser))) + { + /* Cancel support */ + rc = VERR_CANCELLED; + break; + } + } + RTMemTmpFree(pvBuf); + RTFileClose(file); if (RT_FAILURE(rc)) return rc; /* Finally calculate & format the SHA1 sum */ - unsigned char auchDig[20]; + unsigned char auchDig[RTSHA1_HASH_SIZE]; if (!SHA1_Final(auchDig, &ctx)) return VERR_INTERNAL_ERROR; - int cch = RTStrAPrintf(ppszDigest, "%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", - auchDig[0] , auchDig[1] , auchDig[2] , auchDig[3] , auchDig[4], - auchDig[5] , auchDig[6] , auchDig[7] , auchDig[8] , auchDig[9], - auchDig[10], auchDig[11], auchDig[12], auchDig[13], auchDig[14], - auchDig[15], auchDig[16], auchDig[17], auchDig[18], auchDig[19]); - if (RT_UNLIKELY(cch == -1)) - rc = VERR_INTERNAL_ERROR; + char *pszDigest; + rc = RTStrAllocEx(&pszDigest, RTSHA1_DIGEST_LEN + 1); + if (RT_SUCCESS(rc)) + { + rc = RTSha1ToString(auchDig, pszDigest, RTSHA1_DIGEST_LEN + 1); + if (RT_SUCCESS(rc)) + *ppszDigest = pszDigest; + else + RTStrFree(pszDigest); + } return rc; } diff --git a/src/VBox/Runtime/common/checksum/manifest.cpp b/src/VBox/Runtime/common/checksum/manifest.cpp index bc5d5b970..968e83b2d 100644 --- a/src/VBox/Runtime/common/checksum/manifest.cpp +++ b/src/VBox/Runtime/common/checksum/manifest.cpp @@ -1,4 +1,4 @@ -/* $Id: manifest.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */ +/* $Id: manifest.cpp 29904 2010-05-31 12:57:38Z vboxsync $ */ /** @file * IPRT - Manifest file handling. */ @@ -54,7 +54,23 @@ typedef struct RTMANIFESTFILEENTRY } RTMANIFESTFILEENTRY; typedef RTMANIFESTFILEENTRY* PRTMANIFESTFILEENTRY; - +/** + * Internal structure used for the progress callback + */ +typedef struct RTMANIFESTCALLBACKDATA +{ + PFNRTMANIFESTPROGRESS pfnProgressCallback; + void *pvUser; + size_t cMaxFiles; + size_t cCurrentFile; +} RTMANIFESTCALLBACKDATA; +typedef RTMANIFESTCALLBACKDATA* PRTMANIFESTCALLBACKDATA; + +int rtSHAProgressCallback(unsigned uPercent, void *pvUser) +{ + PRTMANIFESTCALLBACKDATA pData = (PRTMANIFESTCALLBACKDATA)pvUser; + return pData->pfnProgressCallback((unsigned)((uPercent + (float)pData->cCurrentFile * 100.0) / (float)pData->cMaxFiles), pData->pvUser); +} RTR3DECL(int) RTManifestVerify(const char *pszManifestFile, PRTMANIFESTTEST paTests, size_t cTests, size_t *piFailed) { @@ -210,23 +226,32 @@ RTR3DECL(int) RTManifestVerify(const char *pszManifestFile, PRTMANIFESTTEST paTe } -RTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, size_t *piFailed) +RTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, size_t *piFailed, PFNRTMANIFESTPROGRESS pfnProgressCallback, void *pvUser) { /* Validate input */ AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER); AssertPtrReturn(papszFiles, VERR_INVALID_POINTER); + AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER); + + int rc = VINF_SUCCESS; /* Create our compare list */ PRTMANIFESTTEST paFiles = (PRTMANIFESTTEST)RTMemTmpAllocZ(sizeof(RTMANIFESTTEST) * cFiles); if (!paFiles) return VERR_NO_MEMORY; + RTMANIFESTCALLBACKDATA callback = { pfnProgressCallback, pvUser, cFiles, 0 }; /* Fill our compare list */ - int rc = VINF_SUCCESS; for (size_t i = 0; i < cFiles; ++i) { char *pszDigest; - rc = RTSha1Digest(papszFiles[i], &pszDigest); + if (pfnProgressCallback) + { + callback.cCurrentFile = i; + rc = RTSha1Digest(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback); + } + else + rc = RTSha1Digest(papszFiles[i], &pszDigest, NULL, NULL); if (RT_FAILURE(rc)) break; paFiles[i].pszTestFile = (char*)papszFiles[i]; @@ -249,11 +274,12 @@ RTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * co } -RTR3DECL(int) RTManifestWriteFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles) +RTR3DECL(int) RTManifestWriteFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, PFNRTMANIFESTPROGRESS pfnProgressCallback, void *pvUser) { /* Validate input */ AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER); AssertPtrReturn(papszFiles, VERR_INVALID_POINTER); + AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER); /* Open a file to stream in */ PRTSTREAM pStream; @@ -261,11 +287,18 @@ RTR3DECL(int) RTManifestWriteFiles(const char *pszManifestFile, const char * con if (RT_FAILURE(rc)) return rc; + RTMANIFESTCALLBACKDATA callback = { pfnProgressCallback, pvUser, cFiles, 0 }; for (size_t i = 0; i < cFiles; ++i) { /* Calculate the SHA1 digest of every file */ char *pszDigest; - rc = RTSha1Digest(papszFiles[i], &pszDigest); + if (pfnProgressCallback) + { + callback.cCurrentFile = i; + rc = RTSha1Digest(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback); + } + else + rc = RTSha1Digest(papszFiles[i], &pszDigest, NULL, NULL); if (RT_FAILURE(rc)) break; diff --git a/src/VBox/Runtime/common/misc/cidr.cpp b/src/VBox/Runtime/common/misc/cidr.cpp index ae5ed728f..08aec3c1a 100644 --- a/src/VBox/Runtime/common/misc/cidr.cpp +++ b/src/VBox/Runtime/common/misc/cidr.cpp @@ -1,4 +1,4 @@ -/* $Id: cidr.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */ +/* $Id: cidr.cpp 29845 2010-05-27 11:29:36Z vboxsync $ */ /** @file * IPRT - IPv4 address parsing. */ @@ -31,106 +31,87 @@ #include <iprt/cidr.h> #include "internal/iprt.h" +#include <iprt/assert.h> #include <iprt/ctype.h> #include <iprt/string.h> #include <iprt/stream.h> -/** - * Scan a digit of an IPv4 address. - * - * @returns IPRT status code. - * - * @param iDigit The index of the IPv4 digit to scan. - * @param psz Pointer to the begin of the string. - * @param ppszNext Pointer to variable that should be set pointing to the first invalid character. (output) - * @param pu8 Pointer to the digit to write (output). - */ -static int scanIPv4Digit(int iDigit, const char *psz, char **ppszNext, uint8_t *pu8) -{ - int rc = RTStrToUInt8Ex(psz, ppszNext, 10, pu8); - if ( ( rc != VINF_SUCCESS - && rc != VWRN_TRAILING_CHARS) - || *pu8 > 254) - return VERR_INVALID_PARAMETER; - - /* first digit cannot be 0 */ - if ( iDigit == 1 - && *pu8 < 1) - return VERR_INVALID_PARAMETER; - - if (**ppszNext == '/') - return VINF_SUCCESS; - - if ( iDigit != 4 - && ( **ppszNext == '\0' - || **ppszNext != '.')) - return VERR_INVALID_PARAMETER; - - return VINF_SUCCESS; -} - - RTDECL(int) RTCidrStrToIPv4(const char *pszAddress, PRTIPV4ADDR pNetwork, PRTIPV4ADDR pNetmask) { uint8_t cBits; - uint8_t a; - uint8_t b = 0; - uint8_t c = 0; - uint8_t d = 0; + uint8_t addr[4]; + uint32_t u32Netmask; + uint32_t u32Network; const char *psz = pszAddress; + const char *pszNetmask; char *pszNext; - int rc; - - do + int rc = VINF_SUCCESS; + int cDelimiter = 0; + int cDelimiterLimit = 0; + + AssertPtrReturn(pszAddress, VERR_INVALID_PARAMETER); + AssertPtrReturn(pNetwork, VERR_INVALID_PARAMETER); + AssertPtrReturn(pNetmask, VERR_INVALID_PARAMETER); + + pszNetmask = RTStrStr(psz, "/"); + *(uint32_t *)addr = 0; + if (!pszNetmask) + cBits = 32; + else { - /* 1st digit */ - rc = scanIPv4Digit(1, psz, &pszNext, &a); - if (RT_FAILURE(rc)) - return rc; - if (*pszNext == '/') - break; - psz = pszNext + 1; - - /* 2nd digit */ - rc = scanIPv4Digit(2, psz, &pszNext, &b); - if (RT_FAILURE(rc)) - return rc; - if (*pszNext == '/') - break; - psz = pszNext + 1; + rc = RTStrToUInt8Ex(pszNetmask + 1, &pszNext, 10, &cBits); + if ( RT_FAILURE(rc) + || cBits > 32 + || rc != VINF_SUCCESS) /* No trailing symbols are accptable after the digit */ + return VERR_INVALID_PARAMETER; + } + u32Netmask = ~(uint32_t)((1<< (32 - cBits)) - 1); + + if (cBits <= 8) + cDelimiterLimit = 0; + else if (cBits <= 16) + cDelimiterLimit = 1; + else if (cBits <= 24) + cDelimiterLimit = 2; + else if (cBits <= 32) + cDelimiterLimit = 3; + + for (;;) + { + rc = RTStrToUInt8Ex(psz, &pszNext, 10, &addr[cDelimiter]); + if ( RT_FAILURE(rc) + || rc == VWRN_NUMBER_TOO_BIG) + return VERR_INVALID_PARAMETER; - /* 3rd digit */ - rc = scanIPv4Digit(3, psz, &pszNext, &c); - if (RT_FAILURE(rc)) - return rc; - if (*pszNext == '/') + if (*pszNext == '.') + cDelimiter++; + else if ( cDelimiter >= cDelimiterLimit + && ( *pszNext == '\0' + || *pszNext == '/')) break; - psz = pszNext + 1; + else + return VERR_INVALID_PARAMETER; - /* 4th digit */ - rc = scanIPv4Digit(4, psz, &pszNext, &d); - if (RT_FAILURE(rc)) - return rc; - } while (0); + if (cDelimiter > 3) + /* not more than four octets */ + return VERR_INVALID_PARAMETER; - if (*pszNext == '/') - { psz = pszNext + 1; - rc = RTStrToUInt8Ex(psz, &pszNext, 10, &cBits); - if (rc != VINF_SUCCESS || cBits < 8 || cBits > 28) - return VERR_INVALID_PARAMETER; } - else - cBits = 0; + u32Network = RT_MAKE_U32_FROM_U8(addr[3], addr[2], addr[1], addr[0]); + + /* Corner case: see RFC 790 page 2 and RFC 4632 page 6. */ + if ( addr[0] == 0 + && ( *(uint32_t *)addr != 0 + || u32Netmask == (uint32_t)~0)) + return VERR_INVALID_PARAMETER; - for (psz = pszNext; RT_C_IS_SPACE(*psz); psz++) - /* nothing */; - if (*psz != '\0') + if ((u32Network & ~u32Netmask) != 0) return VERR_INVALID_PARAMETER; - *pNetwork = RT_MAKE_U32_FROM_U8(d, c, b, a); - *pNetmask = ~(((uint32_t)1 << (32 - cBits)) - 1); + *pNetmask = u32Netmask; + *pNetwork = u32Network; return VINF_SUCCESS; } RT_EXPORT_SYMBOL(RTCidrStrToIPv4); diff --git a/src/VBox/Runtime/common/string/strformat.cpp b/src/VBox/Runtime/common/string/strformat.cpp index 9c0b7aa9d..11ed8f4f1 100644 --- a/src/VBox/Runtime/common/string/strformat.cpp +++ b/src/VBox/Runtime/common/string/strformat.cpp @@ -1,4 +1,4 @@ -/* $Id: strformat.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */ +/* $Id: strformat.cpp 29783 2010-05-25 13:13:35Z vboxsync $ */ /** @file * IPRT - String Formatter. */ @@ -351,7 +351,8 @@ static int rtStrFormatNumber(char *psz, KSIZE64 ullValue, unsigned int uiBase, s * @param pszFormat Format string. * @param InArgs Argument list. */ -RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, va_list InArgs) +RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, + const char *pszFormat, va_list InArgs) { va_list args; KSIZE cch = 0; @@ -832,12 +833,12 @@ RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRF if (*pszFormat != '[') { pszFormat--; - cch += rtstrFormatRt(pfnOutput, pvArgOutput, &pszFormat, &args, cchPrecision, cchWidth, fFlags, chArgSize); + cch += rtstrFormatRt(pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize); } else { pszFormat--; - cch += rtstrFormatType(pfnOutput, pvArgOutput, &pszFormat, &args, cchPrecision, cchWidth, fFlags, chArgSize); + cch += rtstrFormatType(pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize); } break; } @@ -850,7 +851,7 @@ RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRF if (pfnFormat) { pszFormat--; - cch += pfnFormat(pvArgFormat, pfnOutput, pvArgOutput, &pszFormat, &args, cchPrecision, cchWidth, fFlags, chArgSize); + cch += pfnFormat(pvArgFormat, pfnOutput, pvArgOutput, &pszFormat, &args, cchWidth, cchPrecision, fFlags, chArgSize); } break; } diff --git a/src/VBox/Runtime/common/string/strformatrt.cpp b/src/VBox/Runtime/common/string/strformatrt.cpp index 69a5b2d0a..21c82e2a6 100644 --- a/src/VBox/Runtime/common/string/strformatrt.cpp +++ b/src/VBox/Runtime/common/string/strformatrt.cpp @@ -1,4 +1,4 @@ -/* $Id: strformatrt.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */ +/* $Id: strformatrt.cpp 29963 2010-06-01 16:43:09Z vboxsync $ */ /** @file * IPRT - IPRT String Formatter Extensions. */ @@ -96,11 +96,12 @@ * * Group 3, hex dumpers and other complex stuff which requires more than simple formatting. * - \%Rhxd - Takes a pointer to the memory which is to be dumped in typical - * hex format. Use the width to specify the length, and the precision to + * hex format. Use the precision to specify the length, and the width to * set the number of bytes per line. Default width and precision is 16. * - \%Rhxs - Takes a pointer to the memory to be displayed as a hex string, * i.e. a series of space separated bytes formatted as two digit hex value. - * Use the width to specify the length. Default length is 16 bytes. + * Use the precision to specify the length. Default length is 16 bytes. + * The width, if specified, is ignored. * - \%Rrc - Takes an integer iprt status code as argument. Will insert the * status code define corresponding to the iprt status code. * - \%Rrs - Takes an integer iprt status code as argument. Will insert the @@ -755,8 +756,8 @@ size_t rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **p case 'x': { uint8_t *pu8 = va_arg(*pArgs, uint8_t *); - if (cchWidth <= 0) - cchWidth = 16; + if (cchPrecision <= 0) + cchPrecision = 16; if (pu8) { switch (*(*ppszFormat)++) @@ -769,30 +770,30 @@ size_t rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **p size_t cch = 0; int off = 0; - if (cchPrecision <= 0) - cchPrecision = 16; + if (cchWidth <= 0) + cchWidth = 16; - while (off < cchWidth) + while (off < cchPrecision) { int i; cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%0*x %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off); - for (i = 0; i < cchPrecision && off + i < cchWidth ; i++) + for (i = 0; i < cchWidth && off + i < cchPrecision ; i++) cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, - off + i < cchWidth ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pu8[i]); - while (i++ < cchPrecision) + off + i < cchPrecision ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pu8[i]); + while (i++ < cchWidth) cch += pfnOutput(pvArgOutput, " ", 3); cch += pfnOutput(pvArgOutput, " ", 1); - for (i = 0; i < cchPrecision && off + i < cchWidth; i++) + for (i = 0; i < cchWidth && off + i < cchPrecision; i++) { uint8_t u8 = pu8[i]; cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1); } /* next */ - pu8 += cchPrecision; - off += cchPrecision; + pu8 += cchWidth; + off += cchWidth; } return cch; } @@ -802,10 +803,10 @@ size_t rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **p */ case 's': { - if (cchWidth-- > 0) + if (cchPrecision-- > 0) { size_t cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++); - for (; cchWidth > 0; cchWidth--, pu8++) + for (; cchPrecision > 0; cchPrecision--, pu8++) cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8); return cch; } |
