Changeset 32568 in vbox
- Timestamp:
- Sep 16, 2010 3:26:13 PM (14 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/sha.h
r30079 r32568 114 114 115 115 /** 116 * Creates a SHA1 digest for the given memory buffer. 117 * 118 * @returns iprt status code. 119 * 120 * @param pvBuf Memory buffer to create a SHA1 digest for. 121 * @param cbBuf The amount of data (in bytes). 122 * @param ppszDigest On success the SHA1 digest. 123 * @param pfnProgressCallback optional callback for the progress indication 124 * @param pvUser user defined pointer for the callback 125 */ 126 RTR3DECL(int) RTSha1Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, FNRTPROGRESS pfnProgressCallback, void *pvUser); 127 128 /** 116 129 * Creates a SHA1 digest for the given file. 117 130 * … … 123 136 * @param pvUser user defined pointer for the callback 124 137 */ 125 RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest, FNRTPROGRESS pfnProgressCallback, void *pvUser); 126 138 RTR3DECL(int) RTSha1DigestFromFile(const char *pszFile, char **ppszDigest, FNRTPROGRESS pfnProgressCallback, void *pvUser); 127 139 128 140 -
trunk/src/VBox/Main/ApplianceImplImport.cpp
r31676 r32568 691 691 /* Create the SHA1 sum of the OVF file for later validation */ 692 692 char *pszDigest; 693 int vrc = RTSha1Digest (locInfo.strPath.c_str(), &pszDigest, NULL, NULL);693 int vrc = RTSha1DigestFromFile(locInfo.strPath.c_str(), &pszDigest, NULL, NULL); 694 694 if (RT_FAILURE(vrc)) 695 695 DebugBreakThrow(setError(VBOX_E_FILE_ERROR, … … 724 724 725 725 AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS); 726 727 726 HRESULT rc = S_OK; 728 int vrc = VINF_SUCCESS; 729 char szOSTmpDir[RTPATH_MAX]; 730 RTPathTemp(szOSTmpDir, sizeof(szOSTmpDir)); 731 /* The template for the temporary directory created below */ 732 char *pszTmpDir; 733 RTStrAPrintf(&pszTmpDir, "%s"RTPATH_SLASH_STR"vbox-ovf-XXXXXX", szOSTmpDir); 734 list< pair<Utf8Str, ULONG> > filesList; 735 Utf8Str strTmpOvf; 727 void *pvBuf = 0; 736 728 737 729 try 738 730 { 739 /* Extract the path */740 731 Utf8Str tmpPath = locInfo.strPath; 741 732 /* Remove the ova extension */ 742 733 tmpPath.stripExt(); 734 /* add the ovf extension. */ 743 735 tmpPath += ".ovf"; 744 745 /* We need a temporary directory which we can put the OVF file & all 746 * disk images in */ 747 vrc = RTDirCreateTemp(pszTmpDir); 736 char* pcszOVFName = RTPathFilename(tmpPath.c_str()); 737 738 /* Read the OVF into a memory buffer */ 739 uint64_t cbSize; 740 int vrc = RTTarExtractFileToBuf(locInfo.strPath.c_str(), &pvBuf, &cbSize, pcszOVFName, 0, 0); 748 741 if (RT_FAILURE(vrc)) 749 DebugBreakThrow(setError(VBOX_E_FILE_ERROR, 750 tr("Cannot create temporary directory '%s' (%Rrc)"), pszTmpDir, vrc)); 751 752 /* The temporary name of the target OVF file */ 753 strTmpOvf = Utf8StrFmt("%s/%s", pszTmpDir, RTPathFilename(tmpPath.c_str())); 754 755 /* Next we have to download the OVF */ 756 char *papszFile = RTPathFilename(strTmpOvf.c_str()); 757 vrc = RTTarExtractFiles(locInfo.strPath.c_str(), pszTmpDir, &papszFile, 1, 0, 0); 742 { 743 if (vrc == VERR_FILE_NOT_FOUND) 744 throw setError(VBOX_E_IPRT_ERROR, 745 tr("Can't find ovf file '%s' in archive '%s' (%Rrc)"), pcszOVFName, locInfo.strPath.c_str(), vrc); 746 else 747 throw setError(VBOX_E_IPRT_ERROR, 748 tr("Can't unpack the archive file '%s' (%Rrc)"), locInfo.strPath.c_str(), vrc); 749 } 750 751 /* Read & parse the XML structure of the OVF file */ 752 m->pReader = new ovf::OVFReader(pvBuf, cbSize, locInfo.strPath); 753 /* Create the SHA1 sum of the OVF file for later validation */ 754 char *pszDigest; 755 vrc = RTSha1Digest(pvBuf, cbSize, &pszDigest, 0, 0); 758 756 if (RT_FAILURE(vrc)) 759 { 760 if (vrc == VERR_FILE_NOT_FOUND) 761 DebugBreakThrow(setError(VBOX_E_IPRT_ERROR, 762 tr("Can't find ovf file '%s' in archive '%s' (%Rrc)"), papszFile, locInfo.strPath.c_str(), vrc)); 763 else 764 DebugBreakThrow(setError(VBOX_E_IPRT_ERROR, 765 tr("Can't unpack the archive file '%s' (%Rrc)"), locInfo.strPath.c_str(), vrc)); 766 } 767 768 // todo: check this out 769 // pTask->pProgress->SetNextOperation(Bstr(tr("Reading")), 1); 770 771 /* Prepare the temporary reading of the OVF */ 772 ComObjPtr<Progress> progress; 773 LocationInfo li; 774 li.strPath = strTmpOvf; 775 /* Start the reading from the fs */ 776 rc = readImpl(li, progress); 777 if (FAILED(rc)) DebugBreakThrow(rc); 778 779 /* Unlock the appliance for the reading thread */ 780 appLock.release(); 781 /* Wait until the reading is done, but report the progress back to the 782 caller */ 783 ComPtr<IProgress> progressInt(progress); 784 waitForAsyncProgress(pProgress, progressInt); /* Any errors will be thrown */ 785 786 /* Again lock the appliance for the next steps */ 787 appLock.acquire(); 788 } 789 catch(HRESULT aRC) 757 throw setError(VBOX_E_FILE_ERROR, 758 tr("Couldn't calculate SHA1 digest for file '%s' (%Rrc)"), 759 RTPathFilename(locInfo.strPath.c_str()), vrc); 760 m->strOVFSHA1Digest = pszDigest; 761 RTStrFree(pszDigest); 762 763 } 764 catch (iprt::Error &x) // includes all XML exceptions 765 { 766 rc = setError(VBOX_E_FILE_ERROR, 767 x.what()); 768 } 769 catch (HRESULT aRC) 790 770 { 791 771 rc = aRC; 792 772 } 793 /* Delete all files which where temporary created */ 794 if (RTPathExists(strTmpOvf.c_str())) 795 { 796 vrc = RTFileDelete(strTmpOvf.c_str()); 797 if (RT_FAILURE(vrc)) 798 rc = setError(VBOX_E_FILE_ERROR, 799 tr("Cannot delete file '%s' (%Rrc)"), strTmpOvf.c_str(), vrc); 800 } 801 /* Delete the temporary directory */ 802 if (RTPathExists(pszTmpDir)) 803 { 804 vrc = RTDirRemove(pszTmpDir); 805 if (RT_FAILURE(vrc)) 806 rc = setError(VBOX_E_FILE_ERROR, 807 tr("Cannot delete temporary directory '%s' (%Rrc)"), pszTmpDir, vrc); 808 } 809 if (pszTmpDir) 810 RTStrFree(pszTmpDir); 773 774 /* Cleanup the OVF memory buffer */ 775 if (pvBuf) 776 RTMemFree(pvBuf); 811 777 812 778 LogFlowFunc(("rc=%Rhrc\n", rc)); … … 1157 1123 { 1158 1124 char* pszDigest; 1159 vrc = RTSha1Digest ((*it1).c_str(), &pszDigest, NULL, NULL);1125 vrc = RTSha1DigestFromFile((*it1).c_str(), &pszDigest, NULL, NULL); 1160 1126 pTestList[i].pszTestFile = (char*)(*it1).c_str(); 1161 1127 pTestList[i].pszTestDigest = pszDigest; -
trunk/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp
r30080 r32568 41 41 42 42 43 RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser) 43 RTR3DECL(int) RTSha1Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, FNRTPROGRESS pfnProgressCallback, void *pvUser) 44 { 45 /* Validate input */ 46 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER); 47 AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER); 48 AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER); 49 50 int rc = VINF_SUCCESS; 51 *ppszDigest = NULL; 52 53 /* Initialize OpenSSL. */ 54 SHA_CTX ctx; 55 if (!SHA1_Init(&ctx)) 56 return VERR_INTERNAL_ERROR; 57 58 /* Buffer size for progress callback */ 59 double rdMulti = 100.0 / cbBuf; 60 61 /* Working buffer */ 62 char *pvTmp = (char*)pvBuf; 63 64 /* Process the memory in blocks */ 65 size_t cbRead; 66 size_t cbReadTotal = 0; 67 for (;;) 68 { 69 cbRead = RT_MIN(cbBuf - cbReadTotal, _1M); 70 if(!SHA1_Update(&ctx, pvTmp, cbRead)) 71 { 72 rc = VERR_INTERNAL_ERROR; 73 break; 74 } 75 cbReadTotal += cbRead; 76 pvTmp += cbRead; 77 78 /* Call the progress callback if one is defined */ 79 if (pfnProgressCallback) 80 { 81 rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser); 82 if (RT_FAILURE(rc)) 83 break; /* canceled */ 84 } 85 /* Finished? */ 86 if (cbReadTotal == cbBuf) 87 break; 88 } 89 if (RT_SUCCESS(rc)) 90 { 91 /* Finally calculate & format the SHA1 sum */ 92 unsigned char auchDig[RTSHA1_HASH_SIZE]; 93 if (!SHA1_Final(auchDig, &ctx)) 94 return VERR_INTERNAL_ERROR; 95 96 char *pszDigest; 97 rc = RTStrAllocEx(&pszDigest, RTSHA1_DIGEST_LEN + 1); 98 if (RT_SUCCESS(rc)) 99 { 100 rc = RTSha1ToString(auchDig, pszDigest, RTSHA1_DIGEST_LEN + 1); 101 if (RT_SUCCESS(rc)) 102 *ppszDigest = pszDigest; 103 else 104 RTStrFree(pszDigest); 105 } 106 } 107 108 return rc; 109 } 110 111 RTR3DECL(int) RTSha1DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser) 44 112 { 45 113 /* Validate input */ -
trunk/src/VBox/Runtime/common/checksum/manifest.cpp
r30079 r32568 253 253 { 254 254 callback.cCurrentFile = i; 255 rc = RTSha1Digest (papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);255 rc = RTSha1DigestFromFile(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback); 256 256 } 257 257 else 258 rc = RTSha1Digest (papszFiles[i], &pszDigest, NULL, NULL);258 rc = RTSha1DigestFromFile(papszFiles[i], &pszDigest, NULL, NULL); 259 259 if (RT_FAILURE(rc)) 260 260 break; … … 301 301 { 302 302 callback.cCurrentFile = i; 303 rc = RTSha1Digest (papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);303 rc = RTSha1DigestFromFile(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback); 304 304 } 305 305 else 306 rc = RTSha1Digest (papszFiles[i], &pszDigest, NULL, NULL);306 rc = RTSha1DigestFromFile(papszFiles[i], &pszDigest, NULL, NULL); 307 307 if (RT_FAILURE(rc)) 308 308 break; -
trunk/src/VBox/Runtime/testcase/tstRTDigest.cpp
r31847 r32568 152 152 { 153 153 char *pszDigest; 154 int rc = RTSha1Digest (ValueUnion.psz, &pszDigest, NULL, NULL);154 int rc = RTSha1DigestFromFile(ValueUnion.psz, &pszDigest, NULL, NULL); 155 155 if (RT_FAILURE(rc)) 156 156 return Error("RTSha1Digest(%s,) -> %Rrc\n", ValueUnion.psz, rc);
Note:
See TracChangeset
for help on using the changeset viewer.