0
|
1 /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
|
|
2 */
|
|
3
|
|
4 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
|
|
5 rights reserved.
|
|
6
|
|
7 License to copy and use this software is granted provided that it
|
|
8 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
|
|
9 Algorithm" in all material mentioning or referencing this software
|
|
10 or this function.
|
|
11
|
|
12 License is also granted to make and use derivative works provided
|
|
13 that such works are identified as "derived from the RSA Data
|
|
14 Security, Inc. MD5 Message-Digest Algorithm" in all material
|
|
15 mentioning or referencing the derived work.
|
|
16
|
|
17 RSA Data Security, Inc. makes no representations concerning either
|
|
18 the merchantability of this software or the suitability of this
|
|
19 software for any particular purpose. It is provided "as is"
|
|
20 without express or implied warranty of any kind.
|
|
21
|
|
22 These notices must be retained in any copies of any part of this
|
|
23 documentation and/or software.
|
|
24 */
|
|
25
|
|
26 /* Synched up with: Not in FSF. */
|
249
|
27 /* This file has been Mule-ized. */
|
0
|
28
|
|
29 #include <config.h>
|
|
30 #include "lisp.h"
|
|
31
|
|
32 #include "buffer.h"
|
|
33 #include "insdel.h"
|
249
|
34 #include "lstream.h"
|
259
|
35 #ifdef FILE_CODING
|
|
36 #include "file-coding.h"
|
249
|
37 #endif
|
0
|
38
|
|
39 typedef unsigned char *POINTER;/* POINTER defines a generic pointer type */
|
|
40 typedef unsigned short int UINT2;/* UINT2 defines a two byte word */
|
118
|
41 typedef unsigned int UINT4;/* UINT4 defines a four byte word */
|
0
|
42
|
|
43 #define PROTO_LIST(list) list
|
|
44 #define MD_CTX MD5_CTX
|
|
45 #define MDInit MD5Init
|
|
46 #define MDUpdate MD5Update
|
|
47 #define MDFinal MD5Final
|
|
48
|
|
49 /* MD5 context. */
|
|
50 typedef struct {
|
|
51 UINT4 state[4]; /* state (ABCD) */
|
|
52 UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
|
|
53 unsigned char buffer[64]; /* input buffer */
|
|
54 } MD5_CTX;
|
|
55
|
|
56 void MD5Init PROTO_LIST ((MD5_CTX *));
|
|
57 void MD5Update PROTO_LIST
|
|
58 ((MD5_CTX *, CONST unsigned char *, unsigned int));
|
|
59 void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
|
|
60
|
|
61 /* Constants for MD5Transform routine.
|
|
62 */
|
|
63 #define S11 7
|
|
64 #define S12 12
|
|
65 #define S13 17
|
|
66 #define S14 22
|
|
67 #define S21 5
|
|
68 #define S22 9
|
|
69 #define S23 14
|
|
70 #define S24 20
|
|
71 #define S31 4
|
|
72 #define S32 11
|
|
73 #define S33 16
|
|
74 #define S34 23
|
|
75 #define S41 6
|
|
76 #define S42 10
|
|
77 #define S43 15
|
|
78 #define S44 21
|
|
79
|
|
80 static void MD5Transform PROTO_LIST ((UINT4 [4], CONST unsigned char [64]));
|
|
81 static void Encode PROTO_LIST
|
|
82 ((unsigned char *, UINT4 *, unsigned int));
|
|
83 static void Decode PROTO_LIST
|
|
84 ((UINT4 *, CONST unsigned char *, unsigned int));
|
|
85 static void MD5_memcpy PROTO_LIST ((POINTER, CONST POINTER, unsigned int));
|
|
86 static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
|
|
87
|
|
88 static unsigned char PADDING[64] = {
|
|
89 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
92 };
|
|
93
|
|
94 /* F, G, H and I are basic MD5 functions.
|
|
95 */
|
|
96 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
|
97 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
|
98 #define H(x, y, z) ((x) ^ (y) ^ (z))
|
|
99 #define I(x, y, z) ((y) ^ ((x) | (~z)))
|
|
100
|
|
101 /* ROTATE_LEFT rotates x left n bits.
|
|
102 */
|
|
103 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
|
104
|
|
105 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
|
|
106 Rotation is separate from addition to prevent recomputation.
|
|
107 */
|
|
108 #define FF(a, b, c, d, x, s, ac) { \
|
|
109 (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
110 (a) = ROTATE_LEFT ((a), (s)); \
|
|
111 (a) += (b); \
|
|
112 }
|
|
113 #define GG(a, b, c, d, x, s, ac) { \
|
|
114 (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
115 (a) = ROTATE_LEFT ((a), (s)); \
|
|
116 (a) += (b); \
|
|
117 }
|
|
118 #define HH(a, b, c, d, x, s, ac) { \
|
|
119 (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
120 (a) = ROTATE_LEFT ((a), (s)); \
|
|
121 (a) += (b); \
|
|
122 }
|
|
123 #define II(a, b, c, d, x, s, ac) { \
|
|
124 (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
125 (a) = ROTATE_LEFT ((a), (s)); \
|
|
126 (a) += (b); \
|
|
127 }
|
|
128
|
|
129 /* MD5 initialization. Begins an MD5 operation, writing a new context.
|
|
130 */
|
|
131 void
|
|
132 MD5Init (MD5_CTX *context)
|
|
133 {
|
|
134 context->count[0] = context->count[1] = 0;
|
|
135
|
|
136 /* Load magic initialization constants. */
|
|
137 context->state[0] = 0x67452301;
|
|
138 context->state[1] = 0xefcdab89;
|
|
139 context->state[2] = 0x98badcfe;
|
|
140 context->state[3] = 0x10325476;
|
|
141 }
|
|
142
|
|
143 /* MD5 block update operation. Continues an MD5 message-digest
|
|
144 operation, processing another message block, and updating the
|
|
145 context.
|
|
146 */
|
|
147 void
|
|
148 MD5Update (MD5_CTX *context, CONST unsigned char *input, unsigned int inputLen)
|
|
149 {
|
173
|
150 unsigned int i, indice, partLen;
|
0
|
151
|
|
152 /* Compute number of bytes mod 64 */
|
173
|
153 indice = (unsigned int)((context->count[0] >> 3) & 0x3F);
|
0
|
154
|
|
155 /* Update number of bits */
|
|
156 if ((context->count[0] += ((UINT4)inputLen << 3))
|
|
157 < ((UINT4)inputLen << 3))
|
|
158 context->count[1]++;
|
|
159 context->count[1] += ((UINT4)inputLen >> 29);
|
|
160
|
173
|
161 partLen = 64 - indice;
|
0
|
162
|
|
163 /* Transform as many times as possible. */
|
|
164 if (inputLen >= partLen)
|
|
165 {
|
173
|
166 MD5_memcpy ((POINTER)&context->buffer[indice], (CONST POINTER)input,
|
0
|
167 partLen);
|
|
168 MD5Transform (context->state, context->buffer);
|
|
169
|
|
170 for (i = partLen; i + 63 < inputLen; i += 64)
|
|
171 MD5Transform (context->state, &input[i]);
|
|
172
|
173
|
173 indice = 0;
|
0
|
174 }
|
|
175 else
|
|
176 i = 0;
|
|
177
|
|
178 /* Buffer remaining input */
|
173
|
179 MD5_memcpy ((POINTER)&context->buffer[indice], (CONST POINTER)&input[i],
|
0
|
180 inputLen-i);
|
|
181 }
|
|
182
|
|
183 /* MD5 finalization. Ends an MD5 message-digest operation, writing the
|
2
|
184 message digest and zeroizing the context. */
|
0
|
185 void
|
|
186 MD5Final (unsigned char digest[16], MD5_CTX *context)
|
|
187 {
|
|
188 unsigned char bits[8];
|
173
|
189 unsigned int indice, padLen;
|
0
|
190
|
|
191 /* Save number of bits */
|
|
192 Encode (bits, context->count, 8);
|
|
193
|
|
194 /* Pad out to 56 mod 64.
|
|
195 */
|
173
|
196 indice = (unsigned int)((context->count[0] >> 3) & 0x3f);
|
|
197 padLen = (indice < 56) ? (56 - indice) : (120 - indice);
|
0
|
198 MD5Update (context, PADDING, padLen);
|
|
199
|
|
200 /* Append length (before padding) */
|
|
201 MD5Update (context, bits, 8);
|
|
202 /* Store state in digest */
|
|
203 Encode (digest, context->state, 16);
|
|
204
|
|
205 /* Zeroize sensitive information.
|
|
206 */
|
|
207 MD5_memset ((POINTER)context, 0, sizeof (*context));
|
|
208 }
|
|
209
|
|
210 /* MD5 basic transformation. Transforms state based on block.
|
|
211 */
|
|
212 static void
|
|
213 MD5Transform (UINT4 state[4], CONST unsigned char block[64])
|
|
214 {
|
|
215 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
|
216
|
|
217 Decode (x, block, 64);
|
|
218
|
|
219 /* Round 1 */
|
|
220 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
|
221 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
|
222 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
|
223 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
|
224 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
|
225 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
|
226 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
|
227 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
|
228 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
|
229 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
|
230 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
|
231 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
|
232 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
|
233 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
|
234 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
|
235 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
|
236
|
|
237 /* Round 2 */
|
|
238 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
|
239 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
|
240 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
|
241 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
|
242 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
|
243 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
|
244 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
|
245 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
|
246 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
|
247 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
|
248 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
|
249 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
|
250 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
|
251 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
|
252 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
|
253 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
|
254
|
|
255 /* Round 3 */
|
|
256 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
|
257 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
|
258 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
|
259 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
|
260 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
|
261 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
|
262 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
|
263 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
|
264 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
|
265 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
|
266 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
|
267 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
|
268 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
|
269 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
|
270 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
|
271 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
|
272
|
|
273 /* Round 4 */
|
|
274 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
|
275 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
|
276 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
|
277 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
|
278 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
|
279 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
|
280 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
|
281 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
|
282 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
|
283 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
|
284 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
|
285 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
|
286 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
|
287 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
|
288 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
|
289 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
|
290
|
|
291 state[0] += a;
|
|
292 state[1] += b;
|
|
293 state[2] += c;
|
|
294 state[3] += d;
|
|
295
|
|
296 /* Zeroize sensitive information.
|
|
297 */
|
|
298 MD5_memset ((POINTER)x, 0, sizeof (x));
|
|
299 }
|
|
300
|
|
301 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
|
|
302 a multiple of 4.
|
|
303 */
|
|
304 static void
|
|
305 Encode (unsigned char *output, UINT4 *input, unsigned int len)
|
|
306 {
|
|
307 unsigned int i, j;
|
|
308
|
|
309 for (i = 0, j = 0; j < len; i++, j += 4)
|
|
310 {
|
|
311 output[j] = (unsigned char)(input[i] & 0xff);
|
|
312 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
|
|
313 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
|
|
314 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
|
|
315 }
|
|
316 }
|
|
317
|
|
318 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
|
|
319 a multiple of 4.
|
|
320 */
|
|
321 static void
|
|
322 Decode (UINT4 *output, CONST unsigned char *input, unsigned int len)
|
|
323 {
|
|
324 unsigned int i, j;
|
|
325
|
|
326 for (i = 0, j = 0; j < len; i++, j += 4)
|
|
327 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
|
|
328 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
|
|
329 }
|
|
330
|
|
331 static void
|
|
332 MD5_memcpy (POINTER output, CONST POINTER input, unsigned int len)
|
|
333 {
|
|
334 memcpy (output, input, len);
|
|
335 }
|
|
336
|
|
337 static void
|
|
338 MD5_memset (POINTER output, int value, unsigned int len)
|
|
339 {
|
|
340 memset (output, value, len);
|
|
341 }
|
|
342
|
|
343 /* unused */
|
|
344 #if 0
|
|
345 static void
|
|
346 LispMDString (char *string)
|
|
347 {
|
|
348 MD_CTX context;
|
|
349 unsigned char digest[16];
|
|
350 unsigned int len = strlen(string);
|
|
351
|
|
352 MDInit (&context);
|
|
353 MDUpdate (&context, string, len);
|
|
354 MDFinal (digest, &context);
|
|
355 }
|
|
356 #endif
|
|
357
|
|
358
|
|
359 /* XEmacs interface code. */
|
|
360 Lisp_Object Qmd5;
|
|
361
|
249
|
362 DEFUN ("md5", Fmd5, 1, 5, 0, /*
|
0
|
363 Return the MD5 (a secure message digest algorithm) of an object.
|
|
364 OBJECT is either a string or a buffer.
|
|
365 Optional arguments START and END denote buffer positions for computing the
|
249
|
366 hash of a portion of OBJECT. The optional CODING argument specifies the coding
|
|
367 system the text is to be represented in while computing the digest. This only
|
|
368 has meaning with MULE, and defaults to the current format of the data.
|
|
369 If ERROR-ME-NOT is nil, report an error if the coding system can't be
|
|
370 determined. Else assume binary coding if all else fails.
|
20
|
371 */
|
249
|
372 (object, start, end, coding, error_me_not))
|
0
|
373 {
|
249
|
374 /* This function can GC */
|
0
|
375 MD_CTX context;
|
|
376 unsigned char digest[16];
|
|
377 unsigned char thehash[32];
|
|
378 int i;
|
173
|
379
|
0
|
380 MDInit (&context);
|
|
381
|
|
382 if (NILP (object))
|
|
383 {
|
|
384 MDUpdate (&context, (CONST unsigned char *) "", 0);
|
|
385 }
|
|
386 else
|
|
387 {
|
249
|
388 Lisp_Object instream, outstream;
|
|
389 Lstream *istr, *ostr;
|
|
390 static Extbyte_dynarr *conversion_out_dynarr;
|
|
391 char tempbuf[1024]; /* some random amount */
|
|
392 struct gcpro gcpro1, gcpro2;
|
259
|
393 #ifdef FILE_CODING
|
249
|
394 Lisp_Object conv_out_stream, coding_system;
|
|
395 Lstream *costr;
|
|
396 struct gcpro gcpro3;
|
|
397 #endif
|
|
398
|
|
399 if (!conversion_out_dynarr)
|
|
400 conversion_out_dynarr = Dynarr_new (Extbyte);
|
|
401 else
|
|
402 Dynarr_reset (conversion_out_dynarr);
|
|
403
|
|
404 /* set up the in stream */
|
|
405 if (BUFFERP (object))
|
|
406 {
|
|
407 struct buffer *b = decode_buffer (object, 1);
|
|
408 Bufpos begv, endv;
|
|
409 /* Figure out where we need to get info from */
|
|
410 get_buffer_range_char (b, start, end, &begv, &endv, GB_ALLOW_NIL);
|
|
411
|
|
412 instream = make_lisp_buffer_input_stream (b, begv, endv, 0);
|
|
413 }
|
|
414 else
|
|
415 {
|
|
416 Bytecount bstart, bend;
|
|
417 CHECK_STRING (object);
|
|
418 get_string_range_byte (object, start, end, &bstart, &bend,
|
|
419 GB_HISTORICAL_STRING_BEHAVIOR);
|
|
420 instream = make_lisp_string_input_stream (object, bstart, bend);
|
|
421 }
|
|
422 istr = XLSTREAM (instream);
|
|
423
|
259
|
424 #ifdef FILE_CODING
|
249
|
425 /* Find out what format the buffer will be saved in, so we can make
|
|
426 the digest based on what it will look like on disk */
|
|
427 if (NILP(coding))
|
|
428 {
|
|
429 if (BUFFERP(object))
|
|
430 {
|
|
431 /* Use the file coding for this buffer by default */
|
|
432 coding_system = XBUFFER(object)->buffer_file_coding_system;
|
|
433 }
|
|
434 else
|
|
435 {
|
|
436 /* attempt to autodetect the coding of the string. Note: this VERY hit-and-miss */
|
|
437 enum eol_type eol = EOL_AUTODETECT;
|
|
438 coding_system = Fget_coding_system(Qundecided);
|
|
439 determine_real_coding_system(istr, &coding_system, &eol);
|
|
440 }
|
|
441 if (NILP(coding_system))
|
|
442 coding_system = Fget_coding_system(Qbinary);
|
|
443 else
|
|
444 {
|
|
445 coding_system = Ffind_coding_system (coding_system);
|
|
446 if (NILP(coding_system))
|
|
447 coding_system = Fget_coding_system(Qbinary);
|
|
448 }
|
|
449 }
|
|
450 else
|
|
451 {
|
|
452 coding_system = Ffind_coding_system (coding);
|
|
453 if (NILP(coding_system))
|
|
454 if (NILP(error_me_not))
|
|
455 signal_simple_error("No such coding system", coding);
|
|
456 else
|
|
457 coding_system = Fget_coding_system(Qbinary); /* default to binary */
|
|
458 }
|
|
459 #endif
|
259
|
460
|
249
|
461 /* setup the out stream */
|
|
462 outstream = make_dynarr_output_stream((unsigned_char_dynarr *)conversion_out_dynarr);
|
|
463 ostr = XLSTREAM (outstream);
|
259
|
464 #ifdef FILE_CODING
|
249
|
465 /* setup the conversion stream */
|
|
466 conv_out_stream = make_encoding_output_stream (ostr, coding_system);
|
|
467 costr = XLSTREAM (conv_out_stream);
|
|
468 GCPRO3 (instream, outstream, conv_out_stream);
|
|
469 #else
|
|
470 GCPRO2 (instream, outstream);
|
|
471 #endif
|
|
472
|
|
473 /* Get the data while doing the conversion */
|
|
474 while (1) {
|
|
475 int size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
|
|
476 if (!size_in_bytes)
|
|
477 break;
|
|
478 /* It does seem the flushes are necessary... */
|
259
|
479 #ifdef FILE_CODING
|
249
|
480 Lstream_write (costr, tempbuf, size_in_bytes);
|
|
481 Lstream_flush (costr);
|
|
482 #else
|
|
483 Lstream_write (ostr, tempbuf, size_in_bytes);
|
|
484 #endif
|
|
485 Lstream_flush (ostr);
|
|
486
|
|
487 /* Update the digest */
|
|
488
|
|
489 MDUpdate (&context, (unsigned char *)Dynarr_atp(conversion_out_dynarr, 0),
|
|
490 Dynarr_length(conversion_out_dynarr));
|
|
491 /* reset the dynarr */
|
|
492 Lstream_rewind(ostr);
|
|
493 }
|
|
494 Lstream_close (istr);
|
259
|
495 #ifdef FILE_CODING
|
249
|
496 Lstream_close (costr);
|
|
497 #endif
|
|
498 Lstream_close (ostr);
|
|
499
|
|
500 UNGCPRO;
|
|
501 Lstream_delete (istr);
|
|
502 Lstream_delete (ostr);
|
259
|
503 #ifdef FILE_CODING
|
249
|
504 Lstream_delete (costr);
|
|
505 #endif
|
0
|
506 }
|
|
507
|
|
508 MDFinal (digest, &context);
|
|
509 for (i = 0; i < 16; i++)
|
|
510 sprintf ((char *) (thehash + (i * 2)), "%02x", digest[i]);
|
|
511
|
|
512 return (make_string (thehash, 32));
|
|
513 }
|
|
514
|
|
515 void
|
|
516 syms_of_md5 (void)
|
|
517 {
|
20
|
518 DEFSUBR (Fmd5);
|
0
|
519 defsymbol (&Qmd5, "md5");
|
|
520 }
|
|
521
|
|
522 void
|
|
523 vars_of_md5 (void)
|
|
524 {
|
|
525 Fprovide (Qmd5);
|
|
526 }
|