VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTDigest.cpp@ 44535

Last change on this file since 44535 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/* $Id: tstRTDigest.cpp 44529 2013-02-04 15:54:15Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTSha*, RTMd5, RTCrc*.
4 */
5
6/*
7 * Copyright (C) 2009-2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/sha.h>
32#include <iprt/md5.h>
33#include <iprt/crc.h>
34
35#include <iprt/ctype.h>
36#include <iprt/err.h>
37#include <iprt/file.h>
38#include <iprt/getopt.h>
39#include <iprt/initterm.h>
40#include <iprt/param.h>
41#include <iprt/path.h>
42#include <iprt/process.h>
43#include <iprt/string.h>
44#include <iprt/stream.h>
45
46
47static int Error(const char *pszFormat, ...)
48{
49 char szName[RTPATH_MAX];
50 if (!RTProcGetExecutablePath(szName, sizeof(szName)))
51 strcpy(szName, "tstRTDigest");
52
53 RTStrmPrintf(g_pStdErr, "%s: error: ", RTPathFilename(szName));
54 va_list va;
55 va_start(va, pszFormat);
56 RTStrmPrintfV(g_pStdErr, pszFormat, va);
57 va_end(va);
58
59 return 1;
60}
61
62
63int main(int argc, char **argv)
64{
65 RTR3InitExe(argc, &argv, 0);
66
67 enum
68 {
69 kDigestType_NotSpecified,
70 kDigestType_CRC32,
71 kDigestType_CRC64,
72 kDigestType_MD5,
73 kDigestType_SHA1,
74 kDigestType_SHA256,
75 kDigestType_SHA512
76 } enmDigestType = kDigestType_NotSpecified;
77
78 enum
79 {
80 kMethod_Full,
81 kMethod_Block,
82 kMethod_File
83 } enmMethod = kMethod_Block;
84
85 static const RTGETOPTDEF s_aOptions[] =
86 {
87 { "--type", 't', RTGETOPT_REQ_STRING },
88 { "--method", 'm', RTGETOPT_REQ_STRING },
89 { "--help", 'h', RTGETOPT_REQ_NOTHING },
90 };
91
92 int ch;
93 RTGETOPTUNION ValueUnion;
94 RTGETOPTSTATE GetState;
95 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
96 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
97 {
98 switch (ch)
99 {
100 case 't':
101 if (!RTStrICmp(ValueUnion.psz, "crc32"))
102 enmDigestType = kDigestType_CRC32;
103 else if (!RTStrICmp(ValueUnion.psz, "crc64"))
104 enmDigestType = kDigestType_CRC64;
105 else if (!RTStrICmp(ValueUnion.psz, "md5"))
106 enmDigestType = kDigestType_MD5;
107 else if (!RTStrICmp(ValueUnion.psz, "sha1"))
108 enmDigestType = kDigestType_SHA1;
109 else if (!RTStrICmp(ValueUnion.psz, "sha256"))
110 enmDigestType = kDigestType_SHA256;
111 else if (!RTStrICmp(ValueUnion.psz, "sha512"))
112 enmDigestType = kDigestType_SHA512;
113 else
114 {
115 Error("Invalid digest type: %s\n", ValueUnion.psz);
116 return 1;
117 }
118 break;
119
120 case 'm':
121 if (!RTStrICmp(ValueUnion.psz, "full"))
122 enmMethod = kMethod_Full;
123 else if (!RTStrICmp(ValueUnion.psz, "block"))
124 enmMethod = kMethod_Block;
125 else if (!RTStrICmp(ValueUnion.psz, "file"))
126 enmMethod = kMethod_File;
127 else
128 {
129 Error("Invalid digest method: %s\n", ValueUnion.psz);
130 return 1;
131 }
132 break;
133
134 case 'h':
135 RTPrintf("syntax: tstRTDigest -t <digest-type> file [file2 [..]]\n");
136 return 1;
137
138 case VINF_GETOPT_NOT_OPTION:
139 {
140 if (enmDigestType == kDigestType_NotSpecified)
141 return Error("No digest type was specified\n");
142
143 switch (enmMethod)
144 {
145 case kMethod_Full:
146 return Error("Full file method is not implemented\n");
147
148 case kMethod_File:
149 switch (enmDigestType)
150 {
151 case kDigestType_SHA1:
152 {
153 char *pszDigest;
154 int rc = RTSha1DigestFromFile(ValueUnion.psz, &pszDigest, NULL, NULL);
155 if (RT_FAILURE(rc))
156 return Error("RTSha1Digest(%s,) -> %Rrc\n", ValueUnion.psz, rc);
157 RTPrintf("%s %s\n", pszDigest, ValueUnion.psz);
158 RTStrFree(pszDigest);
159 break;
160 }
161
162 default:
163 return Error("The file method isn't implemented for this digest\n");
164 }
165 break;
166
167 case kMethod_Block:
168 {
169 RTFILE hFile;
170 int rc = RTFileOpen(&hFile, ValueUnion.psz, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
171 if (RT_FAILURE(rc))
172 return Error("RTFileOpen(,%s,) -> %Rrc\n", ValueUnion.psz, rc);
173
174 size_t cbRead;
175 uint8_t abBuf[_64K];
176 char *pszDigest = (char *)&abBuf[0];
177 switch (enmDigestType)
178 {
179 case kDigestType_CRC32:
180 {
181 uint32_t uCRC32 = RTCrc32Start();
182 for (;;)
183 {
184 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
185 if (RT_FAILURE(rc) || !cbRead)
186 break;
187 uCRC32 = RTCrc32Process(uCRC32, abBuf, cbRead);
188 }
189 uCRC32 = RTCrc32Finish(uCRC32);
190 RTStrPrintf(pszDigest, sizeof(abBuf), "%08RX32", uCRC32);
191 break;
192 }
193
194 case kDigestType_CRC64:
195 {
196 uint64_t uCRC64 = RTCrc64Start();
197 for (;;)
198 {
199 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
200 if (RT_FAILURE(rc) || !cbRead)
201 break;
202 uCRC64 = RTCrc64Process(uCRC64, abBuf, cbRead);
203 }
204 uCRC64 = RTCrc64Finish(uCRC64);
205 RTStrPrintf(pszDigest, sizeof(abBuf), "%016RX64", uCRC64);
206 break;
207 }
208
209 case kDigestType_MD5:
210 {
211 RTMD5CONTEXT Ctx;
212 RTMd5Init(&Ctx);
213 for (;;)
214 {
215 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
216 if (RT_FAILURE(rc) || !cbRead)
217 break;
218 RTMd5Update(&Ctx, abBuf, cbRead);
219 }
220 uint8_t abDigest[RTMD5HASHSIZE];
221 RTMd5Final(abDigest, &Ctx);
222 RTMd5ToString(abDigest, pszDigest, sizeof(abBuf));
223 break;
224 }
225
226 case kDigestType_SHA1:
227 {
228 RTSHA1CONTEXT Ctx;
229 RTSha1Init(&Ctx);
230 for (;;)
231 {
232 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
233 if (RT_FAILURE(rc) || !cbRead)
234 break;
235 RTSha1Update(&Ctx, abBuf, cbRead);
236 }
237 uint8_t abDigest[RTSHA1_HASH_SIZE];
238 RTSha1Final(&Ctx, abDigest);
239 RTSha1ToString(abDigest, pszDigest, sizeof(abBuf));
240 break;
241 }
242
243 case kDigestType_SHA256:
244 {
245 RTSHA256CONTEXT Ctx;
246 RTSha256Init(&Ctx);
247 for (;;)
248 {
249 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
250 if (RT_FAILURE(rc) || !cbRead)
251 break;
252 RTSha256Update(&Ctx, abBuf, cbRead);
253 }
254 uint8_t abDigest[RTSHA256_HASH_SIZE];
255 RTSha256Final(&Ctx, abDigest);
256 RTSha256ToString(abDigest, pszDigest, sizeof(abBuf));
257 break;
258 }
259
260 case kDigestType_SHA512:
261 {
262 RTSHA512CONTEXT Ctx;
263 RTSha512Init(&Ctx);
264 for (;;)
265 {
266 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
267 if (RT_FAILURE(rc) || !cbRead)
268 break;
269 RTSha512Update(&Ctx, abBuf, cbRead);
270 }
271 uint8_t abDigest[RTSHA512_HASH_SIZE];
272 RTSha512Final(&Ctx, abDigest);
273 RTSha512ToString(abDigest, pszDigest, sizeof(abBuf));
274 break;
275 }
276
277 default:
278 return Error("Internal error #1\n");
279 }
280 RTFileClose(hFile);
281 if (RT_FAILURE(rc) && rc != VERR_EOF)
282 {
283 RTPrintf("Partial: %s %s\n", pszDigest, ValueUnion.psz);
284 return Error("RTFileRead(%s) -> %Rrc\n", ValueUnion.psz, rc);
285 }
286 RTPrintf("%s %s\n", pszDigest, ValueUnion.psz);
287 break;
288 }
289
290 default:
291 return Error("Internal error #2\n");
292 }
293 break;
294 }
295
296 default:
297 return RTGetOptPrintError(ch, &ValueUnion);
298 }
299 }
300
301 return 0;
302}
303
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette