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