- Timestamp:
- Jul 2, 2014 7:32:59 PM (11 years ago)
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 3 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/crypto/digest-builtin.cpp
r51770 r51820 308 308 static PCRTCRDIGESTDESC const g_apDigestOps[] = 309 309 { 310 &g_rtCrDigestMd2Desc, 310 311 &g_rtCrDigestMd5Desc, 311 312 &g_rtCrDigestSha1Desc, -
trunk/src/VBox/Runtime/testcase/Makefile.kmk
r51770 r51820 55 55 tstRTCType \ 56 56 tstRTDigest \ 57 tstRTDigest-2 \ 57 58 tstDir \ 58 59 tstDir-2 \ … … 236 237 tstRTCType_SOURCES = tstRTCType.cpp 237 238 239 tstRTDigest_TEMPLATE = VBOXR3TSTEXE 238 240 tstRTDigest_SOURCES = tstRTDigest.cpp 241 242 tstRTDigest-2_TEMPLATE = VBOXR3TSTEXE 243 tstRTDigest-2_SOURCES = tstRTDigest-2.cpp 239 244 240 245 ifdef VBOX_WITH_LIBCURL -
trunk/src/VBox/Runtime/testcase/tstRTDigest.cpp
r45227 r51820 30 30 *******************************************************************************/ 31 31 #include <iprt/sha.h> 32 #include <iprt/md2.h> 32 33 #include <iprt/md5.h> 33 34 #include <iprt/crc.h> … … 61 62 62 63 64 static int MyReadFile(RTFILE hFile, void *pvBuf, size_t cbToRead, size_t *pcbRead, uint64_t *pcbMaxLeft) 65 { 66 int rc = VINF_SUCCESS; 67 if (*pcbMaxLeft > 0) 68 { 69 if (cbToRead > *pcbMaxLeft) 70 cbToRead = (size_t)*pcbMaxLeft; 71 rc = RTFileRead(hFile, pvBuf, cbToRead, pcbRead); 72 if (RT_SUCCESS(rc)) 73 *pcbMaxLeft -= *pcbRead; 74 } 75 else 76 *pcbRead = 0; 77 return rc; 78 } 79 80 81 63 82 int main(int argc, char **argv) 64 83 { … … 70 89 kDigestType_CRC32, 71 90 kDigestType_CRC64, 91 kDigestType_MD2, 72 92 kDigestType_MD5, 73 93 kDigestType_SHA1, … … 75 95 kDigestType_SHA512 76 96 } enmDigestType = kDigestType_NotSpecified; 97 const char *pszDigestType = "NotSpecified"; 77 98 78 99 enum … … 83 104 } enmMethod = kMethod_Block; 84 105 106 uint64_t offStart = 0; 107 uint64_t cbMax = UINT64_MAX; 108 bool fTestcase = false; 109 85 110 static const RTGETOPTDEF s_aOptions[] = 86 111 { … … 88 113 { "--method", 'm', RTGETOPT_REQ_STRING }, 89 114 { "--help", 'h', RTGETOPT_REQ_NOTHING }, 115 { "--length", 'l', RTGETOPT_REQ_UINT64 }, 116 { "--offset", 'o', RTGETOPT_REQ_UINT64 }, 117 { "--testcase", 'x', RTGETOPT_REQ_NOTHING }, 90 118 }; 91 119 … … 93 121 RTGETOPTUNION ValueUnion; 94 122 RTGETOPTSTATE GetState; 95 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);123 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST); 96 124 while ((ch = RTGetOpt(&GetState, &ValueUnion))) 97 125 { … … 100 128 case 't': 101 129 if (!RTStrICmp(ValueUnion.psz, "crc32")) 130 { 131 pszDigestType = "CRC32"; 102 132 enmDigestType = kDigestType_CRC32; 133 } 103 134 else if (!RTStrICmp(ValueUnion.psz, "crc64")) 135 { 136 pszDigestType = "CRC64"; 104 137 enmDigestType = kDigestType_CRC64; 138 } 139 else if (!RTStrICmp(ValueUnion.psz, "md2")) 140 { 141 pszDigestType = "MD2"; 142 enmDigestType = kDigestType_MD2; 143 } 105 144 else if (!RTStrICmp(ValueUnion.psz, "md5")) 145 { 146 pszDigestType = "MD5"; 106 147 enmDigestType = kDigestType_MD5; 148 } 107 149 else if (!RTStrICmp(ValueUnion.psz, "sha1")) 150 { 151 pszDigestType = "SHA-1"; 108 152 enmDigestType = kDigestType_SHA1; 153 } 109 154 else if (!RTStrICmp(ValueUnion.psz, "sha256")) 155 { 156 pszDigestType = "SHA-256"; 110 157 enmDigestType = kDigestType_SHA256; 158 } 111 159 else if (!RTStrICmp(ValueUnion.psz, "sha512")) 160 { 161 pszDigestType = "SHA-512"; 112 162 enmDigestType = kDigestType_SHA512; 163 } 113 164 else 114 165 { … … 132 183 break; 133 184 185 case 'l': 186 cbMax = ValueUnion.u64; 187 break; 188 189 case 'o': 190 offStart = ValueUnion.u64; 191 break; 192 193 case 'x': 194 fTestcase = true; 195 break; 196 134 197 case 'h': 135 RTPrintf(" syntax: tstRTDigest -t <digest-type>file [file2 [..]]\n");198 RTPrintf("usage: tstRTDigest -t <digest-type> [-o <offset>] [-l <length>] [-x] file [file2 [..]]\n"); 136 199 return 1; 137 200 … … 147 210 148 211 case kMethod_File: 212 if (offStart != 0 || cbMax != UINT64_MAX) 213 return Error("The -l and -o options do not work with the 'file' method."); 149 214 switch (enmDigestType) 150 215 { … … 181 246 if (RT_FAILURE(rc)) 182 247 return Error("RTFileOpen(,%s,) -> %Rrc\n", ValueUnion.psz, rc); 183 248 if (offStart != 0) 249 { 250 rc = RTFileSeek(hFile, offStart, RTFILE_SEEK_BEGIN, NULL); 251 if (RT_FAILURE(rc)) 252 return Error("RTFileSeek(%s,%ull) -> %Rrc\n", ValueUnion.psz, offStart, rc); 253 } 254 255 uint64_t cbMaxLeft = cbMax; 184 256 size_t cbRead; 185 257 uint8_t abBuf[_64K]; … … 192 264 for (;;) 193 265 { 194 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);266 rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); 195 267 if (RT_FAILURE(rc) || !cbRead) 196 268 break; … … 207 279 for (;;) 208 280 { 209 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);281 rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); 210 282 if (RT_FAILURE(rc) || !cbRead) 211 283 break; … … 217 289 } 218 290 291 case kDigestType_MD2: 292 { 293 RTMD2CONTEXT Ctx; 294 RTMd2Init(&Ctx); 295 for (;;) 296 { 297 rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); 298 if (RT_FAILURE(rc) || !cbRead) 299 break; 300 RTMd2Update(&Ctx, abBuf, cbRead); 301 } 302 uint8_t abDigest[RTMD2_HASH_SIZE]; 303 RTMd2Final(&Ctx, abDigest); 304 RTMd2ToString(abDigest, pszDigest, sizeof(abBuf)); 305 break; 306 } 307 219 308 case kDigestType_MD5: 220 309 { … … 223 312 for (;;) 224 313 { 225 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);314 rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); 226 315 if (RT_FAILURE(rc) || !cbRead) 227 316 break; … … 240 329 for (;;) 241 330 { 242 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);331 rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); 243 332 if (RT_FAILURE(rc) || !cbRead) 244 333 break; … … 257 346 for (;;) 258 347 { 259 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);348 rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); 260 349 if (RT_FAILURE(rc) || !cbRead) 261 350 break; … … 274 363 for (;;) 275 364 { 276 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);365 rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); 277 366 if (RT_FAILURE(rc) || !cbRead) 278 367 break; … … 294 383 return Error("RTFileRead(%s) -> %Rrc\n", ValueUnion.psz, rc); 295 384 } 296 RTPrintf("%s %s\n", pszDigest, ValueUnion.psz); 385 386 if (!fTestcase) 387 RTPrintf("%s %s\n", pszDigest, ValueUnion.psz); 388 else if (offStart) 389 RTPrintf(" { &g_abRandom72KB[%#4llx], %5llu, \"%s\", \"%s %llu bytes @%llu\" },\n", 390 offStart, cbMax - cbMaxLeft, pszDigest, pszDigestType, offStart, cbMax - cbMaxLeft); 391 else 392 RTPrintf(" { &g_abRandom72KB[0], %5llu, \"%s\", \"%s %llu bytes\" },\n", 393 cbMax - cbMaxLeft, pszDigest, pszDigestType, cbMax - cbMaxLeft); 297 394 break; 298 395 }
Note:
See TracChangeset
for help on using the changeset viewer.