annotate src/md5.c @ 606:c9c1c4ce8f0f

[xemacs-hg @ 2001-06-02 06:55:26 by ben] Makefile.in.in: eliminate that pesky "defaulting to 00401000" warning. nt.h: eliminate warnings.
author ben
date Sat, 02 Jun 2001 06:55:28 +0000
parents 183866b06e0b
children b39c14581166
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* md5.c - Functions to compute MD5 message digest of files or memory blocks
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 according to the definition of MD5 in RFC 1321 from April 1992.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 NOTE: The canonical source of this file is maintained with the GNU C
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 This program is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 This program is distributed in the hope that it will be useful,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 GNU General Public License for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 along with this program; if not, write to the Free Software Foundation,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 /* XEmacs frontend written by Ben Wing, Jareth Hein and Hrvoje Niksic. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 # include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 #include <sys/types.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 #include <string.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 #include <stdio.h>
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 438
diff changeset
32 #include <limits.h>
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 /* The following contortions are an attempt to use the C preprocessor
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 to determine an unsigned integral type that is 32 bits wide. An
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 doing that would require that the configure script compile and *run*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 the resulting executable. Locally running cross-compiled executables
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 is usually not possible. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 #ifdef _LIBC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 # include <sys/types.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 typedef u_int32_t md5_uint32;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 # if defined __STDC__ && __STDC__
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 # define UINT_MAX_32_BITS 4294967295U
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 # else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 # define UINT_MAX_32_BITS 0xFFFFFFFF
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 # endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 /* If UINT_MAX isn't defined, assume it's a 32-bit type.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 This should be valid for all systems GNU cares about because
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 that doesn't include 16-bit systems, and only modern systems
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 (that certainly have <limits.h>) have 64+-bit integral types. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 # ifndef UINT_MAX
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 # define UINT_MAX UINT_MAX_32_BITS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 # endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 # if UINT_MAX == UINT_MAX_32_BITS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 typedef unsigned int md5_uint32;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 # else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 # if USHRT_MAX == UINT_MAX_32_BITS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 typedef unsigned short md5_uint32;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 # else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 # if ULONG_MAX == UINT_MAX_32_BITS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 typedef unsigned long md5_uint32;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 # else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 /* The following line is intended to evoke an error.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 Using #error is not portable enough. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 "Cannot determine unsigned 32-bit data type."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 # endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 # endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 # endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 #include "lisp.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 #include "buffer.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 #include "lstream.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 #ifdef FILE_CODING
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 # include "file-coding.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 /* Structure to save state of computation between the single steps. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 struct md5_ctx
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 md5_uint32 A;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 md5_uint32 B;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 md5_uint32 C;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 md5_uint32 D;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 md5_uint32 total[2];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 md5_uint32 buflen;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 char buffer[128];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 #ifdef WORDS_BIGENDIAN
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 # define SWAP(n) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 # define SWAP(n) (n)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 /* This array contains the bytes used to pad the buffer to the next
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 64-byte boundary. (RFC 1321, 3.1: Step 1) */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 438
diff changeset
110 static void md5_process_block (const void *, size_t, struct md5_ctx *);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 /* Initialize structure containing state of computation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 (RFC 1321, 3.3: Step 3) */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 md5_init_ctx (struct md5_ctx *ctx)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 ctx->A = 0x67452301;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 ctx->B = 0xefcdab89;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 ctx->C = 0x98badcfe;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 ctx->D = 0x10325476;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 ctx->total[0] = ctx->total[1] = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 ctx->buflen = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 /* Put result from CTX in first 16 bytes following RESBUF. The result
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 must be in little endian byte order.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 IMPORTANT: On some systems it is required that RESBUF is correctly
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 aligned for a 32 bits value. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 static void *
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 438
diff changeset
133 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 return resbuf;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 /* Process the remaining bytes in the internal buffer and the usual
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 prolog according to the standard and write the result to RESBUF.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 IMPORTANT: On some systems it is required that RESBUF is correctly
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 aligned for a 32 bits value. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 static void *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 /* Take yet unprocessed bytes into account. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 md5_uint32 bytes = ctx->buflen;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 size_t pad;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 /* Now count remaining bytes. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 ctx->total[0] += bytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 if (ctx->total[0] < bytes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 ++ctx->total[1];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 memcpy (&ctx->buffer[bytes], fillbuf, pad);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 /* Put the 64-bit file length in *bits* at the end of the buffer. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 (ctx->total[0] >> 29));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 /* Process last bytes. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 return md5_read_ctx (ctx, resbuf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 #ifndef emacs /* unused in Emacs */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 /* Compute MD5 message digest for bytes read from STREAM. The
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 resulting message digest number will be written into the 16 bytes
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 beginning at RESBLOCK. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 md5_stream (FILE *stream, void *resblock)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 /* Important: BLOCKSIZE must be a multiple of 64. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 #define BLOCKSIZE 4096
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 struct md5_ctx ctx;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 char buffer[BLOCKSIZE + 72];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 size_t sum;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 /* Initialize the computation context. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 md5_init_ctx (&ctx);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 /* Iterate over full file contents. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 while (1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 computation function processes the whole buffer so that with the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 next round of the loop another block can be read. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 size_t n;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 sum = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 /* Read block. Take care for partial reads. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 do
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 sum += n;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 while (sum < BLOCKSIZE && n != 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 if (n == 0 && ferror (stream))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 return 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 /* If end of file is reached, end the loop. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 if (n == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 /* Process buffer with BLOCKSIZE bytes. Note that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 BLOCKSIZE % 64 == 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 md5_process_block (buffer, BLOCKSIZE, &ctx);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 /* Add the last bytes if necessary. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 if (sum > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 md5_process_bytes (buffer, sum, &ctx);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 /* Construct result in desired memory. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 md5_finish_ctx (&ctx, resblock);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 result is always in little endian byte order, so that a byte-wise
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 output yields to the wanted ASCII representation of the message
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 digest. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 void *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 md5_buffer (const char *buffer, size_t len, void *resblock)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 struct md5_ctx ctx;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 /* Initialize the computation context. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 md5_init_ctx (&ctx);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 /* Process whole buffer but last len % 64 bytes. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 md5_process_bytes (buffer, len, &ctx);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 /* Put result in desired memory area. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 return md5_finish_ctx (&ctx, resblock);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 #endif /* not emacs */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 static void
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 438
diff changeset
251 md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 /* When we already have some bits in our internal buffer concatenate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 both inputs first. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 if (ctx->buflen != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 size_t left_over = ctx->buflen;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 size_t add = 128 - left_over > len ? len : 128 - left_over;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 memcpy (&ctx->buffer[left_over], buffer, add);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 ctx->buflen += add;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 if (left_over + add > 64)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 /* The regions in the following copy operation cannot overlap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 (left_over + add) & 63);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 ctx->buflen = (left_over + add) & 63;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 buffer = (const char *) buffer + add;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 len -= add;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 /* Process available complete blocks. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 if (len > 64)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 md5_process_block (buffer, len & ~63, ctx);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 buffer = (const char *) buffer + (len & ~63);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 len &= 63;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 /* Move remaining bytes in internal buffer. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 if (len > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 memcpy (ctx->buffer, buffer, len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 ctx->buflen = len;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 /* These are the four functions used in the four steps of the MD5 algorithm
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 and defined in the RFC 1321. The first function is a little bit optimized
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 (as found in Colin Plumbs public domain implementation). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 #define FF(b, c, d) (d ^ (b & (c ^ d)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 #define FG(b, c, d) FF (d, b, c)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 #define FH(b, c, d) (b ^ c ^ d)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 #define FI(b, c, d) (c ^ (b | ~d))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 /* Process LEN bytes of BUFFER, accumulating context into CTX.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 It is assumed that LEN % 64 == 0. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 static void
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 438
diff changeset
306 md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 md5_uint32 correct_words[16];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 const md5_uint32 *words = (const md5_uint32 *) buffer;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 size_t nwords = len / sizeof (md5_uint32);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 const md5_uint32 *endp = words + nwords;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 md5_uint32 A = ctx->A;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 md5_uint32 B = ctx->B;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 md5_uint32 C = ctx->C;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 md5_uint32 D = ctx->D;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 /* First increment the byte count. RFC 1321 specifies the possible
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 length of the file up to 2^64 bits. Here we only compute the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 number of bytes. Do a double word increment. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 ctx->total[0] += len;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 if (ctx->total[0] < len)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 ++ctx->total[1];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 /* Process all bytes in the buffer with 64 bytes in each round of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 the loop. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 while (words < endp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 md5_uint32 *cwp = correct_words;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 md5_uint32 A_save = A;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 md5_uint32 B_save = B;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 md5_uint32 C_save = C;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 md5_uint32 D_save = D;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 /* First round: using the given function, the context and a constant
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 the next context is computed. Because the algorithms processing
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 unit is a 32-bit word and it is determined to work on words in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 little endian byte order we perhaps have to change the byte order
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 before the computation. To reduce the work for the next steps
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 we store the swapped words in the array CORRECT_WORDS. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 #define OP(a, b, c, d, s, T) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 do \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 { \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 ++words; \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 CYCLIC (a, s); \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 a += b; \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 } \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 while (0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 /* It is unfortunate that C does not provide an operator for
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 cyclic rotation. Hope the C compiler is smart enough. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 /* Before we start, one word to the strange constants.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 They are defined in RFC 1321 as
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 /* Round 1. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 OP (A, B, C, D, 7, 0xd76aa478);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 OP (D, A, B, C, 12, 0xe8c7b756);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 OP (C, D, A, B, 17, 0x242070db);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 OP (B, C, D, A, 22, 0xc1bdceee);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 OP (A, B, C, D, 7, 0xf57c0faf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 OP (D, A, B, C, 12, 0x4787c62a);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 OP (C, D, A, B, 17, 0xa8304613);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 OP (B, C, D, A, 22, 0xfd469501);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 OP (A, B, C, D, 7, 0x698098d8);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 OP (D, A, B, C, 12, 0x8b44f7af);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 OP (C, D, A, B, 17, 0xffff5bb1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 OP (B, C, D, A, 22, 0x895cd7be);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 OP (A, B, C, D, 7, 0x6b901122);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 OP (D, A, B, C, 12, 0xfd987193);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 OP (C, D, A, B, 17, 0xa679438e);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 OP (B, C, D, A, 22, 0x49b40821);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 /* For the second to fourth round we have the possibly swapped words
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 in CORRECT_WORDS. Redefine the macro to take an additional first
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 argument specifying the function to use. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 #undef OP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 #define OP(f, a, b, c, d, k, s, T) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 do \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 { \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 a += f (b, c, d) + correct_words[k] + T; \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 CYCLIC (a, s); \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 a += b; \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 } \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 while (0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 /* Round 2. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 OP (FG, D, A, B, C, 6, 9, 0xc040b340);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 OP (FG, D, A, B, C, 10, 9, 0x02441453);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 /* Round 3. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 OP (FH, D, A, B, C, 8, 11, 0x8771f681);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 OP (FH, B, C, D, A, 6, 23, 0x04881d05);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 /* Round 4. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 OP (FI, A, B, C, D, 0, 6, 0xf4292244);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 OP (FI, D, A, B, C, 7, 10, 0x432aff97);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 OP (FI, C, D, A, B, 6, 15, 0xa3014314);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 /* Add the starting values of the context. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 A += A_save;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 B += B_save;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 C += C_save;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 D += D_save;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 /* Put checksum in context given as argument. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 ctx->A = A;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 ctx->B = B;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 ctx->C = C;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 ctx->D = D;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 #ifdef emacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 #ifdef FILE_CODING
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 /* Find out what format the buffer will be saved in, so we can make
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 the digest based on what it will look like on disk. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 static Lisp_Object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 md5_coding_system (Lisp_Object object, Lisp_Object coding, Lisp_Object istream,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 int error_me_not)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 Lisp_Object coding_system;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 if (NILP (coding))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 if (BUFFERP (object))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 /* Use the file coding for this buffer by default. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 coding_system = XBUFFER (object)->buffer_file_coding_system;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 /* Attempt to autodetect the coding of the string. This is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 VERY hit-and-miss. */
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
482 eol_type_t eol = EOL_AUTODETECT;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 coding_system = Fget_coding_system (Qundecided);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 determine_real_coding_system (XLSTREAM (istream),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 &coding_system, &eol);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 if (NILP (coding_system))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 coding_system = Fget_coding_system (Qbinary);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 coding_system = Ffind_coding_system (coding_system);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 if (NILP (coding_system))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 coding_system = Fget_coding_system (Qbinary);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 coding_system = Ffind_coding_system (coding);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 if (NILP (coding_system))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 if (error_me_not)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 /* Default to binary. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 coding_system = Fget_coding_system (Qbinary);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 else
563
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 462
diff changeset
505 invalid_argument ("No such coding system", coding);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 return coding_system;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 #endif /* FILE_CODING */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 DEFUN ("md5", Fmd5, 1, 5, 0, /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 Return the MD5 message digest of OBJECT, a buffer or string.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 Optional arguments START and END denote positions for computing the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 digest of a portion of OBJECT.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 The optional CODING argument specifies the coding system the text is to be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 represented in while computing the digest. If unspecified, it defaults
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 to the current format of the data, or is guessed.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 If NOERROR is non-nil, silently assume binary coding if the guesswork
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 fails. Normally, an error is signaled in such case.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526 CODING and NOERROR arguments are meaningful only in XEmacsen with
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 file-coding or Mule support. Otherwise, they are ignored.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 (object, start, end, coding, noerror))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 /* This function can GC */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 /* Can this really GC? How? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 struct md5_ctx ctx;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 unsigned char digest[16];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 unsigned char thehash[33];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 int i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 Lisp_Object instream;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 struct gcpro gcpro1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 #ifdef FILE_CODING
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 Lisp_Object raw_instream;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 struct gcpro ngcpro1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 /* Set up the input stream. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 if (BUFFERP (object))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 struct buffer *b;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 Bufpos begv, endv;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 CHECK_LIVE_BUFFER (object);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 b = XBUFFER (object);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 /* Figure out where we need to get info from */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 get_buffer_range_char (b, start, end, &begv, &endv, GB_ALLOW_NIL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 instream = make_lisp_buffer_input_stream (b, begv, endv, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 Bytecount bstart, bend;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 CHECK_STRING (object);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 get_string_range_byte (object, start, end, &bstart, &bend,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562 GB_HISTORICAL_STRING_BEHAVIOR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 instream = make_lisp_string_input_stream (object, bstart, bend - bstart);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 GCPRO1 (instream);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 #ifdef FILE_CODING
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 /* Determine the coding and set up the conversion stream. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 coding = md5_coding_system (object, coding, instream, !NILP (noerror));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 raw_instream = instream;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 instream = make_encoding_input_stream (XLSTREAM (instream), coding);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 NGCPRO1 (raw_instream);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 /* Initialize MD5 context. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 md5_init_ctx (&ctx);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578 /* Get the data while doing the conversion. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 while (1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 Bufbyte tempbuf[1024]; /* some random amount */
462
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 442
diff changeset
582 Lstream_data_count size_in_bytes =
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 Lstream_read (XLSTREAM (instream), tempbuf, sizeof (tempbuf));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 if (!size_in_bytes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 /* Process the bytes. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 md5_process_bytes (tempbuf, size_in_bytes, &ctx);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 Lstream_delete (XLSTREAM (instream));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 #ifdef FILE_CODING
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 Lstream_delete (XLSTREAM (raw_instream));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 NUNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 UNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 md5_finish_ctx (&ctx, digest);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598 for (i = 0; i < 16; i++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 sprintf ((char *) (thehash + (i * 2)), "%02x", digest[i]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 return make_string (thehash, 32);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 syms_of_md5 (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 DEFSUBR (Fmd5);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 vars_of_md5 (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 Fprovide (intern ("md5"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 #endif /* emacs */