Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Side by Side Diff: third_party/sqlite/src/test_md5.c

Issue 3108030: Move bundled copy of sqlite one level deeper to better separate it... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/sqlite/src/test_malloc.c ('k') | third_party/sqlite/src/test_mutex.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 ** SQLite uses this code for testing only. It is not a part of
3 ** the SQLite library. This file implements two new TCL commands
4 ** "md5" and "md5file" that compute md5 checksums on arbitrary text
5 ** and on complete files. These commands are used by the "testfixture"
6 ** program to help verify the correct operation of the SQLite library.
7 **
8 ** The original use of these TCL commands was to test the ROLLBACK
9 ** feature of SQLite. First compute the MD5-checksum of the database.
10 ** Then make some changes but rollback the changes rather than commit
11 ** them. Compute a second MD5-checksum of the file and verify that the
12 ** two checksums are the same. Such is the original use of this code.
13 ** New uses may have been added since this comment was written.
14 **
15 ** $Id: test_md5.c,v 1.10 2009/02/03 19:52:59 shane Exp $
16 */
17 /*
18 * This code implements the MD5 message-digest algorithm.
19 * The algorithm is due to Ron Rivest. This code was
20 * written by Colin Plumb in 1993, no copyright is claimed.
21 * This code is in the public domain; do with it what you wish.
22 *
23 * Equivalent code is available from RSA Data Security, Inc.
24 * This code has been tested against that, and is equivalent,
25 * except that you don't need to include two pages of legalese
26 * with every copy.
27 *
28 * To compute the message digest of a chunk of bytes, declare an
29 * MD5Context structure, pass it to MD5Init, call MD5Update as
30 * needed on buffers full of bytes, and then call MD5Final, which
31 * will fill a supplied 16-byte array with the digest.
32 */
33 #include <tcl.h>
34 #include <string.h>
35 #include "sqlite3.h"
36
37 /*
38 * If compiled on a machine that doesn't have a 32-bit integer,
39 * you just set "uint32" to the appropriate datatype for an
40 * unsigned 32-bit integer. For example:
41 *
42 * cc -Duint32='unsigned long' md5.c
43 *
44 */
45 #ifndef uint32
46 # define uint32 unsigned int
47 #endif
48
49 struct Context {
50 int isInit;
51 uint32 buf[4];
52 uint32 bits[2];
53 unsigned char in[64];
54 };
55 typedef struct Context MD5Context;
56
57 /*
58 * Note: this code is harmless on little-endian machines.
59 */
60 static void byteReverse (unsigned char *buf, unsigned longs){
61 uint32 t;
62 do {
63 t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 |
64 ((unsigned)buf[1]<<8 | buf[0]);
65 *(uint32 *)buf = t;
66 buf += 4;
67 } while (--longs);
68 }
69 /* The four core functions - F1 is optimized somewhat */
70
71 /* #define F1(x, y, z) (x & y | ~x & z) */
72 #define F1(x, y, z) (z ^ (x & (y ^ z)))
73 #define F2(x, y, z) F1(z, x, y)
74 #define F3(x, y, z) (x ^ y ^ z)
75 #define F4(x, y, z) (y ^ (x | ~z))
76
77 /* This is the central step in the MD5 algorithm. */
78 #define MD5STEP(f, w, x, y, z, data, s) \
79 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
80
81 /*
82 * The core of the MD5 algorithm, this alters an existing MD5 hash to
83 * reflect the addition of 16 longwords of new data. MD5Update blocks
84 * the data and converts bytes into longwords for this routine.
85 */
86 static void MD5Transform(uint32 buf[4], const uint32 in[16]){
87 register uint32 a, b, c, d;
88
89 a = buf[0];
90 b = buf[1];
91 c = buf[2];
92 d = buf[3];
93
94 MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7);
95 MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
96 MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
97 MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
98 MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7);
99 MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
100 MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
101 MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
102 MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7);
103 MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
104 MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
105 MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
106 MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7);
107 MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
108 MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
109 MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
110
111 MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5);
112 MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9);
113 MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
114 MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
115 MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5);
116 MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9);
117 MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
118 MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
119 MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5);
120 MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9);
121 MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
122 MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
123 MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5);
124 MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9);
125 MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
126 MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
127
128 MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4);
129 MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
130 MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
131 MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
132 MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4);
133 MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
134 MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
135 MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
136 MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4);
137 MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
138 MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
139 MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
140 MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4);
141 MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
142 MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
143 MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
144
145 MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6);
146 MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
147 MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
148 MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
149 MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6);
150 MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
151 MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
152 MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
153 MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6);
154 MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
155 MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
156 MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
157 MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6);
158 MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
159 MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
160 MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
161
162 buf[0] += a;
163 buf[1] += b;
164 buf[2] += c;
165 buf[3] += d;
166 }
167
168 /*
169 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
170 * initialization constants.
171 */
172 static void MD5Init(MD5Context *ctx){
173 ctx->isInit = 1;
174 ctx->buf[0] = 0x67452301;
175 ctx->buf[1] = 0xefcdab89;
176 ctx->buf[2] = 0x98badcfe;
177 ctx->buf[3] = 0x10325476;
178 ctx->bits[0] = 0;
179 ctx->bits[1] = 0;
180 }
181
182 /*
183 * Update context to reflect the concatenation of another buffer full
184 * of bytes.
185 */
186 static
187 void MD5Update(MD5Context *pCtx, const unsigned char *buf, unsigned int len){
188 struct Context *ctx = (struct Context *)pCtx;
189 uint32 t;
190
191 /* Update bitcount */
192
193 t = ctx->bits[0];
194 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t)
195 ctx->bits[1]++; /* Carry from low to high */
196 ctx->bits[1] += len >> 29;
197
198 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
199
200 /* Handle any leading odd-sized chunks */
201
202 if ( t ) {
203 unsigned char *p = (unsigned char *)ctx->in + t;
204
205 t = 64-t;
206 if (len < t) {
207 memcpy(p, buf, len);
208 return;
209 }
210 memcpy(p, buf, t);
211 byteReverse(ctx->in, 16);
212 MD5Transform(ctx->buf, (uint32 *)ctx->in);
213 buf += t;
214 len -= t;
215 }
216
217 /* Process data in 64-byte chunks */
218
219 while (len >= 64) {
220 memcpy(ctx->in, buf, 64);
221 byteReverse(ctx->in, 16);
222 MD5Transform(ctx->buf, (uint32 *)ctx->in);
223 buf += 64;
224 len -= 64;
225 }
226
227 /* Handle any remaining bytes of data. */
228
229 memcpy(ctx->in, buf, len);
230 }
231
232 /*
233 * Final wrapup - pad to 64-byte boundary with the bit pattern
234 * 1 0* (64-bit count of bits processed, MSB-first)
235 */
236 static void MD5Final(unsigned char digest[16], MD5Context *pCtx){
237 struct Context *ctx = (struct Context *)pCtx;
238 unsigned count;
239 unsigned char *p;
240
241 /* Compute number of bytes mod 64 */
242 count = (ctx->bits[0] >> 3) & 0x3F;
243
244 /* Set the first char of padding to 0x80. This is safe since there is
245 always at least one byte free */
246 p = ctx->in + count;
247 *p++ = 0x80;
248
249 /* Bytes of padding needed to make 64 bytes */
250 count = 64 - 1 - count;
251
252 /* Pad out to 56 mod 64 */
253 if (count < 8) {
254 /* Two lots of padding: Pad the first block to 64 bytes */
255 memset(p, 0, count);
256 byteReverse(ctx->in, 16);
257 MD5Transform(ctx->buf, (uint32 *)ctx->in);
258
259 /* Now fill the next block with 56 bytes */
260 memset(ctx->in, 0, 56);
261 } else {
262 /* Pad block to 56 bytes */
263 memset(p, 0, count-8);
264 }
265 byteReverse(ctx->in, 14);
266
267 /* Append length in bits and transform */
268 ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0];
269 ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1];
270
271 MD5Transform(ctx->buf, (uint32 *)ctx->in);
272 byteReverse((unsigned char *)ctx->buf, 4);
273 memcpy(digest, ctx->buf, 16);
274 memset(ctx, 0, sizeof(ctx)); /* In case it is sensitive */
275 }
276
277 /*
278 ** Convert a digest into base-16. digest should be declared as
279 ** "unsigned char digest[16]" in the calling function. The MD5
280 ** digest is stored in the first 16 bytes. zBuf should
281 ** be "char zBuf[33]".
282 */
283 static void DigestToBase16(unsigned char *digest, char *zBuf){
284 static char const zEncode[] = "0123456789abcdef";
285 int i, j;
286
287 for(j=i=0; i<16; i++){
288 int a = digest[i];
289 zBuf[j++] = zEncode[(a>>4)&0xf];
290 zBuf[j++] = zEncode[a & 0xf];
291 }
292 zBuf[j] = 0;
293 }
294
295 /*
296 ** A TCL command for md5. The argument is the text to be hashed. The
297 ** Result is the hash in base64.
298 */
299 static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){
300 MD5Context ctx;
301 unsigned char digest[16];
302 char zBuf[33];
303
304 if( argc!=2 ){
305 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
306 " TEXT\"", 0);
307 return TCL_ERROR;
308 }
309 MD5Init(&ctx);
310 MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1]));
311 MD5Final(digest, &ctx);
312 DigestToBase16(digest, zBuf);
313 Tcl_AppendResult(interp, zBuf, (char*)0);
314 return TCL_OK;
315 }
316
317 /*
318 ** A TCL command to take the md5 hash of a file. The argument is the
319 ** name of the file.
320 */
321 static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){
322 FILE *in;
323 MD5Context ctx;
324 unsigned char digest[16];
325 char zBuf[10240];
326
327 if( argc!=2 ){
328 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
329 " FILENAME\"", 0);
330 return TCL_ERROR;
331 }
332 in = fopen(argv[1],"rb");
333 if( in==0 ){
334 Tcl_AppendResult(interp,"unable to open file \"", argv[1],
335 "\" for reading", 0);
336 return TCL_ERROR;
337 }
338 MD5Init(&ctx);
339 for(;;){
340 int n;
341 n = fread(zBuf, 1, sizeof(zBuf), in);
342 if( n<=0 ) break;
343 MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
344 }
345 fclose(in);
346 MD5Final(digest, &ctx);
347 DigestToBase16(digest, zBuf);
348 Tcl_AppendResult(interp, zBuf, (char*)0);
349 return TCL_OK;
350 }
351
352 /*
353 ** Register the two TCL commands above with the TCL interpreter.
354 */
355 int Md5_Init(Tcl_Interp *interp){
356 Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, 0, 0);
357 Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, 0, 0);
358 return TCL_OK;
359 }
360
361 /*
362 ** During testing, the special md5sum() aggregate function is available.
363 ** inside SQLite. The following routines implement that function.
364 */
365 static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){
366 MD5Context *p;
367 int i;
368 if( argc<1 ) return;
369 p = sqlite3_aggregate_context(context, sizeof(*p));
370 if( p==0 ) return;
371 if( !p->isInit ){
372 MD5Init(p);
373 }
374 for(i=0; i<argc; i++){
375 const char *zData = (char*)sqlite3_value_text(argv[i]);
376 if( zData ){
377 MD5Update(p, (unsigned char*)zData, strlen(zData));
378 }
379 }
380 }
381 static void md5finalize(sqlite3_context *context){
382 MD5Context *p;
383 unsigned char digest[16];
384 char zBuf[33];
385 p = sqlite3_aggregate_context(context, sizeof(*p));
386 MD5Final(digest,p);
387 DigestToBase16(digest, zBuf);
388 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
389 }
390 int Md5_Register(sqlite3 *db){
391 int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0,
392 md5step, md5finalize);
393 sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */
394 return rc;
395 }
OLDNEW
« no previous file with comments | « third_party/sqlite/src/test_malloc.c ('k') | third_party/sqlite/src/test_mutex.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698