1*c318f730Sdrh /*
2*c318f730Sdrh ** 2017-10-13
3*c318f730Sdrh **
4*c318f730Sdrh ** The author disclaims copyright to this source code. In place of
5*c318f730Sdrh ** a legal notice, here is a blessing:
6*c318f730Sdrh **
7*c318f730Sdrh ** May you do good and not evil.
8*c318f730Sdrh ** May you find forgiveness for yourself and forgive others.
9*c318f730Sdrh ** May you share freely, never taking more than you give.
10*c318f730Sdrh **
11*c318f730Sdrh *************************************************************************
12*c318f730Sdrh **
13*c318f730Sdrh ** This file contains code to implement an MD5 extension to TCL.
14*c318f730Sdrh */
15*c318f730Sdrh #include "sqlite3.h"
16*c318f730Sdrh #include <stdlib.h>
17*c318f730Sdrh #include <string.h>
18*c318f730Sdrh #include "sqlite3.h"
19*c318f730Sdrh #if defined(INCLUDE_SQLITE_TCL_H)
20*c318f730Sdrh # include "sqlite_tcl.h"
21*c318f730Sdrh #else
22*c318f730Sdrh # include "tcl.h"
23*c318f730Sdrh # ifndef SQLITE_TCLAPI
24*c318f730Sdrh # define SQLITE_TCLAPI
25*c318f730Sdrh # endif
26*c318f730Sdrh #endif
27*c318f730Sdrh
28*c318f730Sdrh /*
29*c318f730Sdrh * This code implements the MD5 message-digest algorithm.
30*c318f730Sdrh * The algorithm is due to Ron Rivest. This code was
31*c318f730Sdrh * written by Colin Plumb in 1993, no copyright is claimed.
32*c318f730Sdrh * This code is in the public domain; do with it what you wish.
33*c318f730Sdrh *
34*c318f730Sdrh * Equivalent code is available from RSA Data Security, Inc.
35*c318f730Sdrh * This code has been tested against that, and is equivalent,
36*c318f730Sdrh * except that you don't need to include two pages of legalese
37*c318f730Sdrh * with every copy.
38*c318f730Sdrh *
39*c318f730Sdrh * To compute the message digest of a chunk of bytes, declare an
40*c318f730Sdrh * MD5Context structure, pass it to MD5Init, call MD5Update as
41*c318f730Sdrh * needed on buffers full of bytes, and then call MD5Final, which
42*c318f730Sdrh * will fill a supplied 16-byte array with the digest.
43*c318f730Sdrh */
44*c318f730Sdrh
45*c318f730Sdrh /*
46*c318f730Sdrh * If compiled on a machine that doesn't have a 32-bit integer,
47*c318f730Sdrh * you just set "uint32" to the appropriate datatype for an
48*c318f730Sdrh * unsigned 32-bit integer. For example:
49*c318f730Sdrh *
50*c318f730Sdrh * cc -Duint32='unsigned long' md5.c
51*c318f730Sdrh *
52*c318f730Sdrh */
53*c318f730Sdrh #ifndef uint32
54*c318f730Sdrh # define uint32 unsigned int
55*c318f730Sdrh #endif
56*c318f730Sdrh
57*c318f730Sdrh struct MD5Context {
58*c318f730Sdrh int isInit;
59*c318f730Sdrh uint32 buf[4];
60*c318f730Sdrh uint32 bits[2];
61*c318f730Sdrh unsigned char in[64];
62*c318f730Sdrh };
63*c318f730Sdrh typedef struct MD5Context MD5Context;
64*c318f730Sdrh
65*c318f730Sdrh /*
66*c318f730Sdrh * Note: this code is harmless on little-endian machines.
67*c318f730Sdrh */
byteReverse(unsigned char * buf,unsigned longs)68*c318f730Sdrh static void byteReverse (unsigned char *buf, unsigned longs){
69*c318f730Sdrh uint32 t;
70*c318f730Sdrh do {
71*c318f730Sdrh t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 |
72*c318f730Sdrh ((unsigned)buf[1]<<8 | buf[0]);
73*c318f730Sdrh *(uint32 *)buf = t;
74*c318f730Sdrh buf += 4;
75*c318f730Sdrh } while (--longs);
76*c318f730Sdrh }
77*c318f730Sdrh /* The four core functions - F1 is optimized somewhat */
78*c318f730Sdrh
79*c318f730Sdrh /* #define F1(x, y, z) (x & y | ~x & z) */
80*c318f730Sdrh #define F1(x, y, z) (z ^ (x & (y ^ z)))
81*c318f730Sdrh #define F2(x, y, z) F1(z, x, y)
82*c318f730Sdrh #define F3(x, y, z) (x ^ y ^ z)
83*c318f730Sdrh #define F4(x, y, z) (y ^ (x | ~z))
84*c318f730Sdrh
85*c318f730Sdrh /* This is the central step in the MD5 algorithm. */
86*c318f730Sdrh #define MD5STEP(f, w, x, y, z, data, s) \
87*c318f730Sdrh ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
88*c318f730Sdrh
89*c318f730Sdrh /*
90*c318f730Sdrh * The core of the MD5 algorithm, this alters an existing MD5 hash to
91*c318f730Sdrh * reflect the addition of 16 longwords of new data. MD5Update blocks
92*c318f730Sdrh * the data and converts bytes into longwords for this routine.
93*c318f730Sdrh */
MD5Transform(uint32 buf[4],const uint32 in[16])94*c318f730Sdrh static void MD5Transform(uint32 buf[4], const uint32 in[16]){
95*c318f730Sdrh register uint32 a, b, c, d;
96*c318f730Sdrh
97*c318f730Sdrh a = buf[0];
98*c318f730Sdrh b = buf[1];
99*c318f730Sdrh c = buf[2];
100*c318f730Sdrh d = buf[3];
101*c318f730Sdrh
102*c318f730Sdrh MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7);
103*c318f730Sdrh MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
104*c318f730Sdrh MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
105*c318f730Sdrh MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
106*c318f730Sdrh MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7);
107*c318f730Sdrh MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
108*c318f730Sdrh MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
109*c318f730Sdrh MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
110*c318f730Sdrh MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7);
111*c318f730Sdrh MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
112*c318f730Sdrh MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
113*c318f730Sdrh MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
114*c318f730Sdrh MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7);
115*c318f730Sdrh MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
116*c318f730Sdrh MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
117*c318f730Sdrh MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
118*c318f730Sdrh
119*c318f730Sdrh MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5);
120*c318f730Sdrh MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9);
121*c318f730Sdrh MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
122*c318f730Sdrh MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
123*c318f730Sdrh MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5);
124*c318f730Sdrh MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9);
125*c318f730Sdrh MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
126*c318f730Sdrh MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
127*c318f730Sdrh MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5);
128*c318f730Sdrh MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9);
129*c318f730Sdrh MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
130*c318f730Sdrh MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
131*c318f730Sdrh MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5);
132*c318f730Sdrh MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9);
133*c318f730Sdrh MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
134*c318f730Sdrh MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
135*c318f730Sdrh
136*c318f730Sdrh MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4);
137*c318f730Sdrh MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
138*c318f730Sdrh MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
139*c318f730Sdrh MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
140*c318f730Sdrh MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4);
141*c318f730Sdrh MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
142*c318f730Sdrh MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
143*c318f730Sdrh MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
144*c318f730Sdrh MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4);
145*c318f730Sdrh MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
146*c318f730Sdrh MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
147*c318f730Sdrh MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
148*c318f730Sdrh MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4);
149*c318f730Sdrh MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
150*c318f730Sdrh MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
151*c318f730Sdrh MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
152*c318f730Sdrh
153*c318f730Sdrh MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6);
154*c318f730Sdrh MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
155*c318f730Sdrh MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
156*c318f730Sdrh MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
157*c318f730Sdrh MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6);
158*c318f730Sdrh MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
159*c318f730Sdrh MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
160*c318f730Sdrh MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
161*c318f730Sdrh MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6);
162*c318f730Sdrh MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
163*c318f730Sdrh MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
164*c318f730Sdrh MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
165*c318f730Sdrh MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6);
166*c318f730Sdrh MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
167*c318f730Sdrh MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
168*c318f730Sdrh MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
169*c318f730Sdrh
170*c318f730Sdrh buf[0] += a;
171*c318f730Sdrh buf[1] += b;
172*c318f730Sdrh buf[2] += c;
173*c318f730Sdrh buf[3] += d;
174*c318f730Sdrh }
175*c318f730Sdrh
176*c318f730Sdrh /*
177*c318f730Sdrh * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
178*c318f730Sdrh * initialization constants.
179*c318f730Sdrh */
MD5Init(MD5Context * ctx)180*c318f730Sdrh static void MD5Init(MD5Context *ctx){
181*c318f730Sdrh ctx->isInit = 1;
182*c318f730Sdrh ctx->buf[0] = 0x67452301;
183*c318f730Sdrh ctx->buf[1] = 0xefcdab89;
184*c318f730Sdrh ctx->buf[2] = 0x98badcfe;
185*c318f730Sdrh ctx->buf[3] = 0x10325476;
186*c318f730Sdrh ctx->bits[0] = 0;
187*c318f730Sdrh ctx->bits[1] = 0;
188*c318f730Sdrh }
189*c318f730Sdrh
190*c318f730Sdrh /*
191*c318f730Sdrh * Update context to reflect the concatenation of another buffer full
192*c318f730Sdrh * of bytes.
193*c318f730Sdrh */
194*c318f730Sdrh static
MD5Update(MD5Context * ctx,const unsigned char * buf,unsigned int len)195*c318f730Sdrh void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){
196*c318f730Sdrh uint32 t;
197*c318f730Sdrh
198*c318f730Sdrh /* Update bitcount */
199*c318f730Sdrh
200*c318f730Sdrh t = ctx->bits[0];
201*c318f730Sdrh if ((ctx->bits[0] = t + ((uint32)len << 3)) < t)
202*c318f730Sdrh ctx->bits[1]++; /* Carry from low to high */
203*c318f730Sdrh ctx->bits[1] += len >> 29;
204*c318f730Sdrh
205*c318f730Sdrh t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
206*c318f730Sdrh
207*c318f730Sdrh /* Handle any leading odd-sized chunks */
208*c318f730Sdrh
209*c318f730Sdrh if ( t ) {
210*c318f730Sdrh unsigned char *p = (unsigned char *)ctx->in + t;
211*c318f730Sdrh
212*c318f730Sdrh t = 64-t;
213*c318f730Sdrh if (len < t) {
214*c318f730Sdrh memcpy(p, buf, len);
215*c318f730Sdrh return;
216*c318f730Sdrh }
217*c318f730Sdrh memcpy(p, buf, t);
218*c318f730Sdrh byteReverse(ctx->in, 16);
219*c318f730Sdrh MD5Transform(ctx->buf, (uint32 *)ctx->in);
220*c318f730Sdrh buf += t;
221*c318f730Sdrh len -= t;
222*c318f730Sdrh }
223*c318f730Sdrh
224*c318f730Sdrh /* Process data in 64-byte chunks */
225*c318f730Sdrh
226*c318f730Sdrh while (len >= 64) {
227*c318f730Sdrh memcpy(ctx->in, buf, 64);
228*c318f730Sdrh byteReverse(ctx->in, 16);
229*c318f730Sdrh MD5Transform(ctx->buf, (uint32 *)ctx->in);
230*c318f730Sdrh buf += 64;
231*c318f730Sdrh len -= 64;
232*c318f730Sdrh }
233*c318f730Sdrh
234*c318f730Sdrh /* Handle any remaining bytes of data. */
235*c318f730Sdrh
236*c318f730Sdrh memcpy(ctx->in, buf, len);
237*c318f730Sdrh }
238*c318f730Sdrh
239*c318f730Sdrh /*
240*c318f730Sdrh * Final wrapup - pad to 64-byte boundary with the bit pattern
241*c318f730Sdrh * 1 0* (64-bit count of bits processed, MSB-first)
242*c318f730Sdrh */
MD5Final(unsigned char digest[16],MD5Context * ctx)243*c318f730Sdrh static void MD5Final(unsigned char digest[16], MD5Context *ctx){
244*c318f730Sdrh unsigned count;
245*c318f730Sdrh unsigned char *p;
246*c318f730Sdrh
247*c318f730Sdrh /* Compute number of bytes mod 64 */
248*c318f730Sdrh count = (ctx->bits[0] >> 3) & 0x3F;
249*c318f730Sdrh
250*c318f730Sdrh /* Set the first char of padding to 0x80. This is safe since there is
251*c318f730Sdrh always at least one byte free */
252*c318f730Sdrh p = ctx->in + count;
253*c318f730Sdrh *p++ = 0x80;
254*c318f730Sdrh
255*c318f730Sdrh /* Bytes of padding needed to make 64 bytes */
256*c318f730Sdrh count = 64 - 1 - count;
257*c318f730Sdrh
258*c318f730Sdrh /* Pad out to 56 mod 64 */
259*c318f730Sdrh if (count < 8) {
260*c318f730Sdrh /* Two lots of padding: Pad the first block to 64 bytes */
261*c318f730Sdrh memset(p, 0, count);
262*c318f730Sdrh byteReverse(ctx->in, 16);
263*c318f730Sdrh MD5Transform(ctx->buf, (uint32 *)ctx->in);
264*c318f730Sdrh
265*c318f730Sdrh /* Now fill the next block with 56 bytes */
266*c318f730Sdrh memset(ctx->in, 0, 56);
267*c318f730Sdrh } else {
268*c318f730Sdrh /* Pad block to 56 bytes */
269*c318f730Sdrh memset(p, 0, count-8);
270*c318f730Sdrh }
271*c318f730Sdrh byteReverse(ctx->in, 14);
272*c318f730Sdrh
273*c318f730Sdrh /* Append length in bits and transform */
274*c318f730Sdrh memcpy(ctx->in + 14*4, ctx->bits, 8);
275*c318f730Sdrh
276*c318f730Sdrh MD5Transform(ctx->buf, (uint32 *)ctx->in);
277*c318f730Sdrh byteReverse((unsigned char *)ctx->buf, 4);
278*c318f730Sdrh memcpy(digest, ctx->buf, 16);
279*c318f730Sdrh }
280*c318f730Sdrh
281*c318f730Sdrh /*
282*c318f730Sdrh ** Convert a 128-bit MD5 digest into a 32-digit base-16 number.
283*c318f730Sdrh */
MD5DigestToBase16(unsigned char * digest,char * zBuf)284*c318f730Sdrh static void MD5DigestToBase16(unsigned char *digest, char *zBuf){
285*c318f730Sdrh static char const zEncode[] = "0123456789abcdef";
286*c318f730Sdrh int i, j;
287*c318f730Sdrh
288*c318f730Sdrh for(j=i=0; i<16; i++){
289*c318f730Sdrh int a = digest[i];
290*c318f730Sdrh zBuf[j++] = zEncode[(a>>4)&0xf];
291*c318f730Sdrh zBuf[j++] = zEncode[a & 0xf];
292*c318f730Sdrh }
293*c318f730Sdrh zBuf[j] = 0;
294*c318f730Sdrh }
295*c318f730Sdrh
296*c318f730Sdrh
297*c318f730Sdrh /*
298*c318f730Sdrh ** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers
299*c318f730Sdrh ** each representing 16 bits of the digest and separated from each
300*c318f730Sdrh ** other by a "-" character.
301*c318f730Sdrh */
MD5DigestToBase10x8(unsigned char digest[16],char zDigest[50])302*c318f730Sdrh static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){
303*c318f730Sdrh int i, j;
304*c318f730Sdrh unsigned int x;
305*c318f730Sdrh for(i=j=0; i<16; i+=2){
306*c318f730Sdrh x = digest[i]*256 + digest[i+1];
307*c318f730Sdrh if( i>0 ) zDigest[j++] = '-';
308*c318f730Sdrh sqlite3_snprintf(50-j, &zDigest[j], "%05u", x);
309*c318f730Sdrh j += 5;
310*c318f730Sdrh }
311*c318f730Sdrh zDigest[j] = 0;
312*c318f730Sdrh }
313*c318f730Sdrh
314*c318f730Sdrh /*
315*c318f730Sdrh ** A TCL command for md5. The argument is the text to be hashed. The
316*c318f730Sdrh ** Result is the hash in base64.
317*c318f730Sdrh */
md5_cmd(void * cd,Tcl_Interp * interp,int argc,const char ** argv)318*c318f730Sdrh static int SQLITE_TCLAPI md5_cmd(
319*c318f730Sdrh void*cd,
320*c318f730Sdrh Tcl_Interp *interp,
321*c318f730Sdrh int argc,
322*c318f730Sdrh const char **argv
323*c318f730Sdrh ){
324*c318f730Sdrh MD5Context ctx;
325*c318f730Sdrh unsigned char digest[16];
326*c318f730Sdrh char zBuf[50];
327*c318f730Sdrh void (*converter)(unsigned char*, char*);
328*c318f730Sdrh
329*c318f730Sdrh if( argc!=2 ){
330*c318f730Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
331*c318f730Sdrh " TEXT\"", (char*)0);
332*c318f730Sdrh return TCL_ERROR;
333*c318f730Sdrh }
334*c318f730Sdrh MD5Init(&ctx);
335*c318f730Sdrh MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1]));
336*c318f730Sdrh MD5Final(digest, &ctx);
337*c318f730Sdrh converter = (void(*)(unsigned char*,char*))cd;
338*c318f730Sdrh converter(digest, zBuf);
339*c318f730Sdrh Tcl_AppendResult(interp, zBuf, (char*)0);
340*c318f730Sdrh return TCL_OK;
341*c318f730Sdrh }
342*c318f730Sdrh
343*c318f730Sdrh /*
344*c318f730Sdrh ** A TCL command to take the md5 hash of a file. The argument is the
345*c318f730Sdrh ** name of the file.
346*c318f730Sdrh */
md5file_cmd(void * cd,Tcl_Interp * interp,int argc,const char ** argv)347*c318f730Sdrh static int SQLITE_TCLAPI md5file_cmd(
348*c318f730Sdrh void*cd,
349*c318f730Sdrh Tcl_Interp *interp,
350*c318f730Sdrh int argc,
351*c318f730Sdrh const char **argv
352*c318f730Sdrh ){
353*c318f730Sdrh FILE *in;
354*c318f730Sdrh int ofst;
355*c318f730Sdrh int amt;
356*c318f730Sdrh MD5Context ctx;
357*c318f730Sdrh void (*converter)(unsigned char*, char*);
358*c318f730Sdrh unsigned char digest[16];
359*c318f730Sdrh char zBuf[10240];
360*c318f730Sdrh
361*c318f730Sdrh if( argc!=2 && argc!=4 ){
362*c318f730Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
363*c318f730Sdrh " FILENAME [OFFSET AMT]\"", (char*)0);
364*c318f730Sdrh return TCL_ERROR;
365*c318f730Sdrh }
366*c318f730Sdrh if( argc==4 ){
367*c318f730Sdrh ofst = atoi(argv[2]);
368*c318f730Sdrh amt = atoi(argv[3]);
369*c318f730Sdrh }else{
370*c318f730Sdrh ofst = 0;
371*c318f730Sdrh amt = 2147483647;
372*c318f730Sdrh }
373*c318f730Sdrh in = fopen(argv[1],"rb");
374*c318f730Sdrh if( in==0 ){
375*c318f730Sdrh Tcl_AppendResult(interp,"unable to open file \"", argv[1],
376*c318f730Sdrh "\" for reading", (char*)0);
377*c318f730Sdrh return TCL_ERROR;
378*c318f730Sdrh }
379*c318f730Sdrh fseek(in, ofst, SEEK_SET);
380*c318f730Sdrh MD5Init(&ctx);
381*c318f730Sdrh while( amt>0 ){
382*c318f730Sdrh int n;
383*c318f730Sdrh n = (int)fread(zBuf, 1, sizeof(zBuf)<=amt ? sizeof(zBuf) : amt, in);
384*c318f730Sdrh if( n<=0 ) break;
385*c318f730Sdrh MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
386*c318f730Sdrh amt -= n;
387*c318f730Sdrh }
388*c318f730Sdrh fclose(in);
389*c318f730Sdrh MD5Final(digest, &ctx);
390*c318f730Sdrh converter = (void(*)(unsigned char*,char*))cd;
391*c318f730Sdrh converter(digest, zBuf);
392*c318f730Sdrh Tcl_AppendResult(interp, zBuf, (char*)0);
393*c318f730Sdrh return TCL_OK;
394*c318f730Sdrh }
395*c318f730Sdrh
396*c318f730Sdrh /*
397*c318f730Sdrh ** Register the four new TCL commands for generating MD5 checksums
398*c318f730Sdrh ** with the TCL interpreter.
399*c318f730Sdrh */
Md5_Init(Tcl_Interp * interp)400*c318f730Sdrh int Md5_Init(Tcl_Interp *interp){
401*c318f730Sdrh Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd,
402*c318f730Sdrh MD5DigestToBase16, 0);
403*c318f730Sdrh Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd,
404*c318f730Sdrh MD5DigestToBase10x8, 0);
405*c318f730Sdrh Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd,
406*c318f730Sdrh MD5DigestToBase16, 0);
407*c318f730Sdrh Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd,
408*c318f730Sdrh MD5DigestToBase10x8, 0);
409*c318f730Sdrh return TCL_OK;
410*c318f730Sdrh }
411*c318f730Sdrh
412*c318f730Sdrh /*
413*c318f730Sdrh ** During testing, the special md5sum() aggregate function is available.
414*c318f730Sdrh ** inside SQLite. The following routines implement that function.
415*c318f730Sdrh */
md5step(sqlite3_context * context,int argc,sqlite3_value ** argv)416*c318f730Sdrh static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){
417*c318f730Sdrh MD5Context *p;
418*c318f730Sdrh int i;
419*c318f730Sdrh if( argc<1 ) return;
420*c318f730Sdrh p = sqlite3_aggregate_context(context, sizeof(*p));
421*c318f730Sdrh if( p==0 ) return;
422*c318f730Sdrh if( !p->isInit ){
423*c318f730Sdrh MD5Init(p);
424*c318f730Sdrh }
425*c318f730Sdrh for(i=0; i<argc; i++){
426*c318f730Sdrh const char *zData = (char*)sqlite3_value_text(argv[i]);
427*c318f730Sdrh if( zData ){
428*c318f730Sdrh MD5Update(p, (unsigned char*)zData, (int)strlen(zData));
429*c318f730Sdrh }
430*c318f730Sdrh }
431*c318f730Sdrh }
md5finalize(sqlite3_context * context)432*c318f730Sdrh static void md5finalize(sqlite3_context *context){
433*c318f730Sdrh MD5Context *p;
434*c318f730Sdrh unsigned char digest[16];
435*c318f730Sdrh char zBuf[33];
436*c318f730Sdrh p = sqlite3_aggregate_context(context, sizeof(*p));
437*c318f730Sdrh MD5Final(digest,p);
438*c318f730Sdrh MD5DigestToBase16(digest, zBuf);
439*c318f730Sdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
440*c318f730Sdrh }
Md5_Register(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pThunk)441*c318f730Sdrh int Md5_Register(
442*c318f730Sdrh sqlite3 *db,
443*c318f730Sdrh char **pzErrMsg,
444*c318f730Sdrh const sqlite3_api_routines *pThunk
445*c318f730Sdrh ){
446*c318f730Sdrh int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0,
447*c318f730Sdrh md5step, md5finalize);
448*c318f730Sdrh sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */
449*c318f730Sdrh return rc;
450*c318f730Sdrh }
451