annotate src/md5.c @ 338:d486d167960c

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