annotate src/md5.c @ 259:11cf20601dec r20-5b28

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