ipkg: remove auto-generated files
[oweals/opkg-lede.git] / md5.c
1 /* md5.c - Compute MD5 checksum of files or strings according to the
2  *         definition of MD5 in RFC 1321 from April 1992.
3  * Copyright (C) 1995-1999 Free Software Foundation, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu> */
21 /* Hacked to work with BusyBox by Alfred M. Szmidt <ams@trillian.itslinux.org> */
22
23 /* Sucked directly into ipkg since the md5sum functions aren't in libbb
24    Dropped a few functions since ipkg only needs md5_stream.
25    Got rid of evil, twisted defines of FALSE=1 and TRUE=0
26    6 March 2002 Carl Worth <cworth@east.isi.edu>
27 */
28
29 /*
30  * June 29, 2001        Manuel Novoa III
31  *
32  * Added MD5SUM_SIZE_VS_SPEED configuration option.
33  *
34  * Current valid values, with data from my system for comparison, are:
35  *   (using uClibc and running on linux-2.4.4.tar.bz2)
36  *                     user times (sec)  text size (386)
37  *     0 (fastest)         1.1                6144
38  *     1                   1.4                5392
39  *     2                   3.0                5088
40  *     3 (smallest)        5.1                4912
41  */
42
43 #define MD5SUM_SIZE_VS_SPEED 3
44
45 /**********************************************************************/
46
47 #include <stdio.h>
48 #include <errno.h>
49 #include <ctype.h>
50 #include <getopt.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <endian.h>
54 #include <sys/types.h>
55 #if defined HAVE_LIMITS_H
56 # include <limits.h>
57 #endif
58
59 #include "md5.h"
60
61 //----------------------------------------------------------------------------
62 //--------md5.c
63 //----------------------------------------------------------------------------
64
65 /* md5.c - Functions to compute MD5 message digest of files or memory blocks
66  *         according to the definition of MD5 in RFC 1321 from April 1992.
67  */
68
69 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
70
71 //----------------------------------------------------------------------------
72 //--------md5.h
73 //----------------------------------------------------------------------------
74
75 /* md5.h - Declaration of functions and data types used for MD5 sum
76    computing library functions. */
77
78 typedef u_int32_t md5_uint32;
79
80 /* Structure to save state of computation between the single steps.  */
81 struct md5_ctx
82 {
83   md5_uint32 A;
84   md5_uint32 B;
85   md5_uint32 C;
86   md5_uint32 D;
87
88   md5_uint32 total[2];
89   md5_uint32 buflen;
90   char buffer[128];
91 };
92
93 /*
94  * The following three functions are build up the low level used in
95  * the functions `md5_stream' and `md5_buffer'.
96  */
97
98 /* Initialize structure containing state of computation.
99    (RFC 1321, 3.3: Step 3)  */
100 static void md5_init_ctx __P ((struct md5_ctx *ctx));
101
102 /* Starting with the result of former calls of this function (or the
103    initialization function update the context for the next LEN bytes
104    starting at BUFFER.
105    It is necessary that LEN is a multiple of 64!!! */
106 static void md5_process_block __P ((const void *buffer, size_t len,
107                                     struct md5_ctx *ctx));
108
109 /* Starting with the result of former calls of this function (or the
110    initialization function update the context for the next LEN bytes
111    starting at BUFFER.
112    It is NOT required that LEN is a multiple of 64.  */
113 static void md5_process_bytes __P ((const void *buffer, size_t len,
114                                     struct md5_ctx *ctx));
115
116 /* Process the remaining bytes in the buffer and put result from CTX
117    in first 16 bytes following RESBUF.  The result is always in little
118    endian byte order, so that a byte-wise output yields to the wanted
119    ASCII representation of the message digest.
120
121    IMPORTANT: On some systems it is required that RESBUF is correctly
122    aligned for a 32 bits value.  */
123 static void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf));
124
125 //----------------------------------------------------------------------------
126 //--------end of md5.h
127 //----------------------------------------------------------------------------
128
129 /* Handle endian-ness */
130 #if __BYTE_ORDER == __LITTLE_ENDIAN
131         #define SWAP(n) (n)
132 #else
133         #define SWAP(n) ((n << 24) | ((n&65280)<<8) | ((n&16711680)>>8) | (n>>24))
134 #endif
135
136
137
138 #if MD5SUM_SIZE_VS_SPEED == 0
139 /* This array contains the bytes used to pad the buffer to the next
140    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
141 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */  };
142 #endif
143
144 /* Initialize structure containing state of computation.
145    (RFC 1321, 3.3: Step 3)  */
146 void md5_init_ctx(struct md5_ctx *ctx)
147 {
148   ctx->A = 0x67452301;
149   ctx->B = 0xefcdab89;
150   ctx->C = 0x98badcfe;
151   ctx->D = 0x10325476;
152
153   ctx->total[0] = ctx->total[1] = 0;
154   ctx->buflen = 0;
155 }
156
157 /* Process the remaining bytes in the internal buffer and the usual
158    prolog according to the standard and write the result to RESBUF.
159
160    IMPORTANT: On some systems it is required that RESBUF is correctly
161    aligned for a 32 bits value.  */
162 static void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf)
163 {
164   /* Take yet unprocessed bytes into account.  */
165   md5_uint32 bytes = ctx->buflen;
166   size_t pad;
167
168   /* Now count remaining bytes.  */
169   ctx->total[0] += bytes;
170   if (ctx->total[0] < bytes)
171     ++ctx->total[1];
172
173   pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
174 #if MD5SUM_SIZE_VS_SPEED > 0
175   memset(&ctx->buffer[bytes], 0, pad);
176   ctx->buffer[bytes] = 0x80;
177 #else
178   memcpy(&ctx->buffer[bytes], fillbuf, pad);
179 #endif
180
181   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
182   *(md5_uint32 *) & ctx->buffer[bytes + pad] = SWAP(ctx->total[0] << 3);
183   *(md5_uint32 *) & ctx->buffer[bytes + pad + 4] =
184     SWAP( ((ctx->total[1] << 3) | (ctx->total[0] >> 29)) );
185
186   /* Process last bytes.  */
187   md5_process_block(ctx->buffer, bytes + pad + 8, ctx);
188
189 /* Put result from CTX in first 16 bytes following RESBUF.  The result is
190    always in little endian byte order, so that a byte-wise output yields
191    to the wanted ASCII representation of the message digest.
192
193    IMPORTANT: On some systems it is required that RESBUF is correctly
194    aligned for a 32 bits value.  */
195   ((md5_uint32 *) resbuf)[0] = SWAP(ctx->A);
196   ((md5_uint32 *) resbuf)[1] = SWAP(ctx->B);
197   ((md5_uint32 *) resbuf)[2] = SWAP(ctx->C);
198   ((md5_uint32 *) resbuf)[3] = SWAP(ctx->D);
199
200   return resbuf;
201 }
202
203 /* Compute MD5 message digest for bytes read from STREAM.  The
204    resulting message digest number will be written into the 16 bytes
205    beginning at RESBLOCK.  */
206 int md5_stream(FILE *stream, void *resblock)
207 {
208   /* Important: BLOCKSIZE must be a multiple of 64.  */
209 static const int BLOCKSIZE = 4096;
210   struct md5_ctx ctx;
211   char buffer[BLOCKSIZE + 72];
212   size_t sum;
213
214   /* Initialize the computation context.  */
215   md5_init_ctx(&ctx);
216
217   /* Iterate over full file contents.  */
218   while (1) {
219     /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
220        computation function processes the whole buffer so that with the
221        next round of the loop another block can be read.  */
222     size_t n;
223     sum = 0;
224
225     /* Read block.  Take care for partial reads.  */
226     do {
227       n = fread(buffer + sum, 1, BLOCKSIZE - sum, stream);
228
229       sum += n;
230     }
231     while (sum < BLOCKSIZE && n != 0);
232     if (n == 0 && ferror(stream))
233       return 1;
234
235     /* If end of file is reached, end the loop.  */
236     if (n == 0)
237       break;
238
239     /* Process buffer with BLOCKSIZE bytes.  Note that
240        BLOCKSIZE % 64 == 0
241     */
242     md5_process_block(buffer, BLOCKSIZE, &ctx);
243   }
244
245   /* Add the last bytes if necessary.  */
246   if (sum > 0)
247     md5_process_bytes(buffer, sum, &ctx);
248
249   /* Construct result in desired memory.  */
250   md5_finish_ctx(&ctx, resblock);
251   return 0;
252 }
253
254 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
255    result is always in little endian byte order, so that a byte-wise
256    output yields to the wanted ASCII representation of the message
257    digest.  */
258 void *md5_buffer(const char *buffer, size_t len, void *resblock)
259 {
260   struct md5_ctx ctx;
261
262   /* Initialize the computation context.  */
263   md5_init_ctx(&ctx);
264
265   /* Process whole buffer but last len % 64 bytes.  */
266   md5_process_bytes(buffer, len, &ctx);
267
268   /* Put result in desired memory area.  */
269   return md5_finish_ctx(&ctx, resblock);
270 }
271
272 static void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx)
273 {
274   /* When we already have some bits in our internal buffer concatenate
275      both inputs first.  */
276   if (ctx->buflen != 0) {
277     size_t left_over = ctx->buflen;
278     size_t add = 128 - left_over > len ? len : 128 - left_over;
279
280     memcpy(&ctx->buffer[left_over], buffer, add);
281     ctx->buflen += add;
282
283     if (left_over + add > 64) {
284       md5_process_block(ctx->buffer, (left_over + add) & ~63, ctx);
285       /* The regions in the following copy operation cannot overlap.  */
286       memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
287              (left_over + add) & 63);
288       ctx->buflen = (left_over + add) & 63;
289     }
290
291     buffer = (const char *) buffer + add;
292     len -= add;
293   }
294
295   /* Process available complete blocks.  */
296   if (len > 64) {
297     md5_process_block(buffer, len & ~63, ctx);
298     buffer = (const char *) buffer + (len & ~63);
299     len &= 63;
300   }
301
302   /* Move remaining bytes in internal buffer.  */
303   if (len > 0) {
304     memcpy(ctx->buffer, buffer, len);
305     ctx->buflen = len;
306   }
307 }
308
309 /* These are the four functions used in the four steps of the MD5 algorithm
310    and defined in the RFC 1321.  The first function is a little bit optimized
311    (as found in Colin Plumbs public domain implementation).  */
312 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
313 #define FF(b, c, d) (d ^ (b & (c ^ d)))
314 #define FG(b, c, d) FF (d, b, c)
315 #define FH(b, c, d) (b ^ c ^ d)
316 #define FI(b, c, d) (c ^ (b | ~d))
317
318 /* Process LEN bytes of BUFFER, accumulating context into CTX.
319    It is assumed that LEN % 64 == 0.  */
320 static void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
321 {
322   md5_uint32 correct_words[16];
323   const md5_uint32 *words = buffer;
324   size_t nwords = len / sizeof(md5_uint32);
325   const md5_uint32 *endp = words + nwords;
326 #if MD5SUM_SIZE_VS_SPEED > 0
327   static const md5_uint32 C_array[] = {
328       /* round 1 */
329       0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
330       0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
331       0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
332       0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
333       /* round 2 */
334       0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
335       0xd62f105d, 0x2441453,  0xd8a1e681, 0xe7d3fbc8,
336       0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
337       0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
338       /* round 3 */
339       0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
340       0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
341       0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
342       0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
343       /* round 4 */
344       0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
345       0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
346       0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
347       0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
348   };
349
350   static const char P_array[] = {
351 #if MD5SUM_SIZE_VS_SPEED > 1
352       0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
353 #endif
354       1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
355       5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
356       0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9  /* 4 */
357   };
358
359 #if MD5SUM_SIZE_VS_SPEED > 1
360   static const char S_array[] = {
361       7, 12, 17, 22,
362       5, 9, 14, 20,
363       4, 11, 16, 23,
364       6, 10, 15, 21
365   };
366 #endif
367 #endif
368
369   md5_uint32 A = ctx->A;
370   md5_uint32 B = ctx->B;
371   md5_uint32 C = ctx->C;
372   md5_uint32 D = ctx->D;
373
374   /* First increment the byte count.  RFC 1321 specifies the possible
375      length of the file up to 2^64 bits.  Here we only compute the
376      number of bytes.  Do a double word increment.  */
377   ctx->total[0] += len;
378   if (ctx->total[0] < len)
379     ++ctx->total[1];
380
381   /* Process all bytes in the buffer with 64 bytes in each round of
382      the loop.  */
383   while (words < endp) {
384     md5_uint32 *cwp = correct_words;
385     md5_uint32 A_save = A;
386     md5_uint32 B_save = B;
387     md5_uint32 C_save = C;
388     md5_uint32 D_save = D;
389
390 #if MD5SUM_SIZE_VS_SPEED > 1
391 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
392
393     const md5_uint32 *pc;
394     const char *pp;
395     const char *ps;
396     int i;
397     md5_uint32 temp;
398
399     for ( i=0 ; i < 16 ; i++ ) {
400         cwp[i] = SWAP(words[i]);
401     }
402     words += 16;
403
404 #if MD5SUM_SIZE_VS_SPEED > 2
405     pc = C_array; pp = P_array; ps = S_array - 4;
406
407     for ( i = 0 ; i < 64 ; i++ ) {
408         if ((i&0x0f) == 0) ps += 4;
409         temp = A;
410         switch (i>>4) {
411             case 0:
412                 temp += FF(B,C,D);
413                 break;
414             case 1:
415                 temp += FG(B,C,D);
416                 break;
417             case 2:
418                 temp += FH(B,C,D);
419                 break;
420             case 3:
421                 temp += FI(B,C,D);
422         }
423         temp += cwp[(int)(*pp++)] + *pc++;
424         temp = CYCLIC (temp, ps[i&3]);
425         temp += B;
426         A = D; D = C; C = B; B = temp;
427     }
428 #else
429     pc = C_array; pp = P_array; ps = S_array;
430
431     for ( i = 0 ; i < 16 ; i++ ) {
432         temp = A + FF(B,C,D) + cwp[(int)(*pp++)] + *pc++;
433         temp = CYCLIC (temp, ps[i&3]);
434         temp += B;
435         A = D; D = C; C = B; B = temp;
436     }
437
438     ps += 4;
439     for ( i = 0 ; i < 16 ; i++ ) {
440         temp = A + FG(B,C,D) + cwp[(int)(*pp++)] + *pc++;
441         temp = CYCLIC (temp, ps[i&3]);
442         temp += B;
443         A = D; D = C; C = B; B = temp;
444     }
445     ps += 4;
446     for ( i = 0 ; i < 16 ; i++ ) {
447         temp = A + FH(B,C,D) + cwp[(int)(*pp++)] + *pc++;
448         temp = CYCLIC (temp, ps[i&3]);
449         temp += B;
450         A = D; D = C; C = B; B = temp;
451     }
452     ps += 4;
453     for ( i = 0 ; i < 16 ; i++ ) {
454         temp = A + FI(B,C,D) + cwp[(int)(*pp++)] + *pc++;
455         temp = CYCLIC (temp, ps[i&3]);
456         temp += B;
457         A = D; D = C; C = B; B = temp;
458     }
459
460 #endif
461 #else
462     /* First round: using the given function, the context and a constant
463        the next context is computed.  Because the algorithms processing
464        unit is a 32-bit word and it is determined to work on words in
465        little endian byte order we perhaps have to change the byte order
466        before the computation.  To reduce the work for the next steps
467        we store the swapped words in the array CORRECT_WORDS.  */
468
469 #define OP(a, b, c, d, s, T)                                            \
470       do                                                                \
471         {                                                               \
472           a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;             \
473           ++words;                                                      \
474           CYCLIC (a, s);                                                \
475           a += b;                                                       \
476         }                                                               \
477       while (0)
478
479     /* It is unfortunate that C does not provide an operator for
480        cyclic rotation.  Hope the C compiler is smart enough.  */
481     /* gcc 2.95.4 seems to be --aaronl */
482 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
483
484     /* Before we start, one word to the strange constants.
485        They are defined in RFC 1321 as
486
487        T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
488     */
489
490 #if MD5SUM_SIZE_VS_SPEED == 1
491     const md5_uint32 *pc;
492     const char *pp;
493     int i;
494 #endif
495
496     /* Round 1.  */
497 #if MD5SUM_SIZE_VS_SPEED == 1
498     pc = C_array;
499     for ( i=0 ; i < 4 ; i++ ) {
500         OP(A, B, C, D, 7, *pc++);
501         OP(D, A, B, C, 12, *pc++);
502         OP(C, D, A, B, 17, *pc++);
503         OP(B, C, D, A, 22, *pc++);
504     }
505 #else
506     OP(A, B, C, D, 7, 0xd76aa478);
507     OP(D, A, B, C, 12, 0xe8c7b756);
508     OP(C, D, A, B, 17, 0x242070db);
509     OP(B, C, D, A, 22, 0xc1bdceee);
510     OP(A, B, C, D, 7, 0xf57c0faf);
511     OP(D, A, B, C, 12, 0x4787c62a);
512     OP(C, D, A, B, 17, 0xa8304613);
513     OP(B, C, D, A, 22, 0xfd469501);
514     OP(A, B, C, D, 7, 0x698098d8);
515     OP(D, A, B, C, 12, 0x8b44f7af);
516     OP(C, D, A, B, 17, 0xffff5bb1);
517     OP(B, C, D, A, 22, 0x895cd7be);
518     OP(A, B, C, D, 7, 0x6b901122);
519     OP(D, A, B, C, 12, 0xfd987193);
520     OP(C, D, A, B, 17, 0xa679438e);
521     OP(B, C, D, A, 22, 0x49b40821);
522 #endif
523
524     /* For the second to fourth round we have the possibly swapped words
525        in CORRECT_WORDS.  Redefine the macro to take an additional first
526        argument specifying the function to use.  */
527 #undef OP
528 #define OP(f, a, b, c, d, k, s, T)                                      \
529       do                                                                \
530         {                                                               \
531           a += f (b, c, d) + correct_words[k] + T;                      \
532           CYCLIC (a, s);                                                \
533           a += b;                                                       \
534         }                                                               \
535       while (0)
536
537     /* Round 2.  */
538 #if MD5SUM_SIZE_VS_SPEED == 1
539     pp = P_array;
540     for ( i=0 ; i < 4 ; i++ ) {
541         OP(FG, A, B, C, D, (int)(*pp++), 5, *pc++);
542         OP(FG, D, A, B, C, (int)(*pp++), 9, *pc++);
543         OP(FG, C, D, A, B, (int)(*pp++), 14, *pc++);
544         OP(FG, B, C, D, A, (int)(*pp++), 20, *pc++);
545     }
546 #else
547     OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
548     OP(FG, D, A, B, C, 6, 9, 0xc040b340);
549     OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
550     OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
551     OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
552     OP(FG, D, A, B, C, 10, 9, 0x02441453);
553     OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
554     OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
555     OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
556     OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
557     OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
558     OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
559     OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
560     OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
561     OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
562     OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
563 #endif
564
565     /* Round 3.  */
566 #if MD5SUM_SIZE_VS_SPEED == 1
567     for ( i=0 ; i < 4 ; i++ ) {
568         OP(FH, A, B, C, D, (int)(*pp++), 4, *pc++);
569         OP(FH, D, A, B, C, (int)(*pp++), 11, *pc++);
570         OP(FH, C, D, A, B, (int)(*pp++), 16, *pc++);
571         OP(FH, B, C, D, A, (int)(*pp++), 23, *pc++);
572     }
573 #else
574     OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
575     OP(FH, D, A, B, C, 8, 11, 0x8771f681);
576     OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
577     OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
578     OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
579     OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
580     OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
581     OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
582     OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
583     OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
584     OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
585     OP(FH, B, C, D, A, 6, 23, 0x04881d05);
586     OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
587     OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
588     OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
589     OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
590 #endif
591
592     /* Round 4.  */
593 #if MD5SUM_SIZE_VS_SPEED == 1
594     for ( i=0 ; i < 4 ; i++ ) {
595         OP(FI, A, B, C, D, (int)(*pp++), 6, *pc++);
596         OP(FI, D, A, B, C, (int)(*pp++), 10, *pc++);
597         OP(FI, C, D, A, B, (int)(*pp++), 15, *pc++);
598         OP(FI, B, C, D, A, (int)(*pp++), 21, *pc++);
599     }
600 #else
601     OP(FI, A, B, C, D, 0, 6, 0xf4292244);
602     OP(FI, D, A, B, C, 7, 10, 0x432aff97);
603     OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
604     OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
605     OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
606     OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
607     OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
608     OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
609     OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
610     OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
611     OP(FI, C, D, A, B, 6, 15, 0xa3014314);
612     OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
613     OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
614     OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
615     OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
616     OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
617 #endif
618 #endif
619
620     /* Add the starting values of the context.  */
621     A += A_save;
622     B += B_save;
623     C += C_save;
624     D += D_save;
625   }
626
627   /* Put checksum in context given as argument.  */
628   ctx->A = A;
629   ctx->B = B;
630   ctx->C = C;
631   ctx->D = D;
632 }
633
634 //----------------------------------------------------------------------------
635 //--------end of md5.c
636 //----------------------------------------------------------------------------
637
638 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
639 #define ISXDIGIT(c) (isxdigit (c))
640
641 /* The minimum length of a valid digest line in a file produced
642    by `md5sum FILE' and read by `md5sum -c'.  This length does
643    not include any newline character at the end of a line.  */
644 static const int MIN_DIGEST_LINE_LENGTH = 35; /* 32 - message digest length
645                                       2 - blank and binary indicator
646                                       1 - minimum filename length */
647
648 static inline int hex_digits(unsigned char const *s)
649 {
650   while (*s) {
651     if (!ISXDIGIT(*s))
652       return 0;
653     ++s;
654   }
655   return 1;
656 }
657
658