]> git.jsancho.org Git - lugaru.git/blob - Source/DRIVER.CC
Added GPL license and headers.
[lugaru.git] / Source / DRIVER.CC
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22 // DRIVER.CC - test driver for the C++/object oriented translation and 
23 //             modification of MD5.
24
25 // Translation and modification (c) 1995 by Mordechai T. Abzug 
26
27 // This translation/ modification is provided "as is," without express or 
28 // implied warranty of any kind.
29
30 // The translator/ modifier does not claim (1) that MD5 will do what you think 
31 // it does; (2) that this translation/ modification is accurate; or (3) that 
32 // this software is "merchantible."  (Language for this disclaimer partially 
33 // copied from the disclaimer below).
34
35 /* based on:
36
37    MDDRIVER.C - test driver for MD2, MD4 and MD5
38
39   Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
40 rights reserved.
41
42 RSA Data Security, Inc. makes no representations concerning either
43 the merchantability of this software or the suitability of this
44 software for any particular purpose. It is provided "as is"
45 without express or implied warranty of any kind.
46
47 These notices must be retained in any copies of any part of this
48 documentation and/or software.
49  */
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <time.h>
54 #include <string.h>
55
56 #include "md5.h"
57
58
59 // Length of test block, number of test blocks.
60
61 #define TEST_BLOCK_LEN 1000
62 #define TEST_BLOCK_COUNT 1000
63
64 static void  MD5_timeTrial (void);
65 static void  MD5_testSuite (void);
66 static void  MD5_file (char *);
67 static void  MD5_filter (void);
68 static void  MD5_string (unsigned char *string);
69 static char *MD5_usage (void);
70
71 // Main driver.
72 /*
73 int main (int argc, char *argv[]){
74
75   int i;
76
77   MD5_testSuite();
78
79   if (argc > 1)
80     for (i = 1; i < argc; i++)
81       if (argv[i][0] == '-' && argv[i][1] == 's')
82         MD5_string ( (unsigned char *) argv[i] + 2);
83       else if (strcmp (argv[i], "-t") == 0)
84         MD5_timeTrial ();
85       else if (strcmp (argv[i], "-x") == 0)
86         MD5_testSuite ();
87       else if (strcmp (argv[i], "-h") == 0)
88         cout << MD5_usage()<< flush;
89       else if (strcmp (argv[i], "-help")==0)
90         cout << MD5_usage()<< flush;
91       else if (argv[i][0] == '-'){
92         cerr << argv[i] << " is an unknown option.\n" << MD5_usage()<< flush;
93         exit (1);
94       }
95       else
96         MD5_file (argv[i]);
97   else
98     MD5_filter ();
99
100   return (0);
101 }
102 */
103
104 // Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
105
106 static void MD5_timeTrial ()
107 {
108   MD5 context;
109   time_t endTime, startTime;
110   unsigned char block[TEST_BLOCK_LEN];
111   unsigned int i;
112
113
114   cout << "MD5 time trial. Digesting "<< TEST_BLOCK_LEN << " ";
115   cout << TEST_BLOCK_COUNT << "-byte blocks ...";
116
117   // Initialize block
118   for (i = 0; i < TEST_BLOCK_LEN; i++)
119     block[i] = (unsigned char)(i & 0xff);
120
121   // Start timer
122   time (&startTime);
123
124   // Digest blocks
125   for (i = 0; i < TEST_BLOCK_COUNT; i++)
126     context.update (block, TEST_BLOCK_LEN);
127
128   context.finalize();
129
130   // Stop timer
131   time (&endTime);
132
133   cout << " done" << endl;
134
135   cout << "Digest = " << context << endl;
136
137   cout << "Time = "<< (long)(endTime-startTime) << " seconds" << endl;
138
139   cout << "Speed = ";
140   cout << (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime);
141   cout << "bytes/second" <<endl;
142
143 }
144
145 // Digests a reference suite of strings and prints the results.
146 /*
147 static void MD5_testSuite ()
148 {
149   cout << "MD5 test suite:" << endl;
150
151   MD5_string ( (unsigned char*) "");
152   MD5_string ( (unsigned char*) "a");
153   MD5_string ( (unsigned char*) "abc");
154   MD5_string ( (unsigned char*) "message digest");
155   MD5_string ( (unsigned char*) "abcdefghijklmnopqrstuvwxyz");
156   MD5_string
157  ( (unsigned char*) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
158   MD5_string
159  ( (unsigned char*) "1234567890123456789012345678901234567890\
160 1234567890123456789012345678901234567890");
161 }
162 */
163
164 // Digests a file and prints the result.
165
166 static void MD5_file (char *filename){
167
168   ifstream file(ConvertFileName(filename));
169
170   if (!file)
171     cerr << filename <<" can't be opened" << endl;
172   else {
173     MD5 context(file);
174     cout <<  "MD5 ("  << filename <<  ") = "  <<  context << endl;
175   }
176 }
177
178 // Digests the standard input and prints the result.
179
180 static void MD5_filter ()
181 {
182
183   MD5 context(cin);  // ie. run istream version of MD5 on cin.
184                      // Could also run file version of MD5 on stdin.
185
186   cout << context << endl;
187 }
188
189
190
191 // Digests a string and prints the result.
192 /*
193 void MD5_string (unsigned char *string){
194
195   MD5 context;
196   unsigned int len = strlen ( (char *)string);
197
198   context.update   (string, len);
199   context.finalize ();
200
201   cout << "MD5 (\"" << (char *)string << "\") = " << context <<endl;
202 }*/
203
204
205
206 static char *MD5_usage(){
207
208   return "\n\
209   MD5\n\n\
210 USAGE:\n\n\
211   MD5 [-sstring] [-t] [-x] [-h] [-help] [filename]\n\n\
212 Arguments (may be any combination):\n\
213   -sstring - digests string\n\
214   -t       - runs time trial\n\
215   -x       - runs test script\n\
216   -h       - displays this message\n\
217   -help    - displays this message\n\
218   filename - digests file\n\
219   (none)   - digests standard input\n\n";
220 }
221
222
223
224