~ubuntu-branches/ubuntu/maverick/wget/maverick

« back to all changes in this revision

Viewing changes to src/gnu-md5.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2008-05-27 11:49:54 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080527114954-ame070pjhqtofeaf
Tags: 1.11.2-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Add wget-udeb to ship wget.gnu as alternative to busybox wget
    implementation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* md5.c - Functions to compute MD5 message digest of files or memory blocks
2
2
   according to the definition of MD5 in RFC 1321 from April 1992.
3
 
   Copyright (C) 1995, 1996 Free Software Foundation, Inc.
 
3
   Copyright (C) 1995, 1996, 2007, 2008 Free Software Foundation, Inc.
4
4
   This file is part of the GNU C library.
5
5
 
6
6
   The GNU C Library is free software; you can redistribute it and/or
7
7
   modify it under the terms of the GNU Library General Public License as
8
 
   published by the Free Software Foundation; either version 2 of the
 
8
   published by the Free Software Foundation; either version 3 of the
9
9
   License, or (at your option) any later version.
10
10
 
11
11
   The GNU C Library is distributed in the hope that it will be useful,
14
14
   Library General Public License for more details.
15
15
 
16
16
   You should have received a copy of the GNU Library General Public
17
 
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
18
 
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
 
   Boston, MA 02111-1307, USA.  */
 
17
   License along with the GNU C Library.  If not, see
 
18
   <http://www.gnu.org/licenses/>.  */
20
19
 
21
20
/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
22
21
 
24
23
# include <config.h>
25
24
#endif
26
25
 
27
 
/* Wget */
28
 
/*#if STDC_HEADERS || defined _LIBC*/
29
 
# include <stdlib.h>
30
 
#ifdef HAVE_STRING_H
31
 
# include <string.h>
32
 
#else
33
 
# include <strings.h>
34
 
#endif
35
 
/*#else*/
36
 
/*# ifndef HAVE_MEMCPY*/
37
 
/*#  define memcpy(d, s, n) bcopy ((s), (d), (n))*/
38
 
/*# endif*/
39
 
/*#endif*/
 
26
/* modified for Wget: depend on C89 */
 
27
#include <stdlib.h>
 
28
#include <string.h>
40
29
 
41
30
#include "wget.h"
42
31
#include "gnu-md5.h"
49
38
#endif
50
39
 
51
40
#ifdef WORDS_BIGENDIAN
52
 
# define SWAP(n)                                                        \
 
41
# define SWAP(n)                                                        \
53
42
    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
54
43
#else
55
44
# define SWAP(n) (n)
114
103
  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
115
104
  *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
116
105
  *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
117
 
                                                        (ctx->total[0] >> 29));
 
106
                                                        (ctx->total[0] >> 29));
118
107
 
119
108
  /* Process last bytes.  */
120
109
  md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
143
132
  while (1)
144
133
    {
145
134
      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
146
 
         computation function processes the whole buffer so that with the
147
 
         next round of the loop another block can be read.  */
 
135
         computation function processes the whole buffer so that with the
 
136
         next round of the loop another block can be read.  */
148
137
      size_t n;
149
138
      sum = 0;
150
139
 
151
140
      /* Read block.  Take care for partial reads.  */
152
141
      do
153
 
        {
154
 
          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
 
142
        {
 
143
          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
155
144
 
156
 
          sum += n;
157
 
        }
 
145
          sum += n;
 
146
        }
158
147
      while (sum < BLOCKSIZE && n != 0);
159
148
      if (n == 0 && ferror (stream))
160
149
        return 1;
161
150
 
162
151
      /* If end of file is reached, end the loop.  */
163
152
      if (n == 0)
164
 
        break;
 
153
        break;
165
154
 
166
155
      /* Process buffer with BLOCKSIZE bytes.  Note that
167
 
                        BLOCKSIZE % 64 == 0
 
156
                        BLOCKSIZE % 64 == 0
168
157
       */
169
158
      md5_process_block (buffer, BLOCKSIZE, &ctx);
170
159
    }
213
202
      ctx->buflen += add;
214
203
 
215
204
      if (left_over + add > 64)
216
 
        {
217
 
          md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
218
 
          /* The regions in the following copy operation cannot overlap.  */
219
 
          memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
220
 
                  (left_over + add) & 63);
221
 
          ctx->buflen = (left_over + add) & 63;
222
 
        }
 
205
        {
 
206
          md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
 
207
          /* The regions in the following copy operation cannot overlap.  */
 
208
          memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
 
209
                  (left_over + add) & 63);
 
210
          ctx->buflen = (left_over + add) & 63;
 
211
        }
223
212
 
224
213
      buffer = (const char *) buffer + add;
225
214
      len -= add;
284
273
      md5_uint32 D_save = D;
285
274
 
286
275
      /* First round: using the given function, the context and a constant
287
 
         the next context is computed.  Because the algorithms processing
288
 
         unit is a 32-bit word and it is determined to work on words in
289
 
         little endian byte order we perhaps have to change the byte order
290
 
         before the computation.  To reduce the work for the next steps
291
 
         we store the swapped words in the array CORRECT_WORDS.  */
 
276
         the next context is computed.  Because the algorithms processing
 
277
         unit is a 32-bit word and it is determined to work on words in
 
278
         little endian byte order we perhaps have to change the byte order
 
279
         before the computation.  To reduce the work for the next steps
 
280
         we store the swapped words in the array CORRECT_WORDS.  */
292
281
 
293
 
#define OP(a, b, c, d, s, T)                                            \
294
 
      do                                                                \
295
 
        {                                                               \
296
 
          a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;             \
297
 
          ++words;                                                      \
298
 
          CYCLIC (a, s);                                                \
299
 
          a += b;                                                       \
300
 
        }                                                               \
 
282
#define OP(a, b, c, d, s, T)                                            \
 
283
      do                                                                \
 
284
        {                                                               \
 
285
          a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;             \
 
286
          ++words;                                                      \
 
287
          CYCLIC (a, s);                                                \
 
288
          a += b;                                                       \
 
289
        }                                                               \
301
290
      while (0)
302
291
 
303
292
      /* It is unfortunate that C does not provide an operator for
304
 
         cyclic rotation.  Hope the C compiler is smart enough.  */
 
293
         cyclic rotation.  Hope the C compiler is smart enough.  */
305
294
#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
306
295
 
307
296
      /* Before we start, one word to the strange constants.
308
 
         They are defined in RFC 1321 as
 
297
         They are defined in RFC 1321 as
309
298
 
310
 
         T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
 
299
         T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
311
300
       */
312
301
 
313
302
      /* Round 1.  */
329
318
      OP (B, C, D, A, 22, 0x49b40821);
330
319
 
331
320
      /* For the second to fourth round we have the possibly swapped words
332
 
         in CORRECT_WORDS.  Redefine the macro to take an additional first
333
 
         argument specifying the function to use.  */
 
321
         in CORRECT_WORDS.  Redefine the macro to take an additional first
 
322
         argument specifying the function to use.  */
334
323
#undef OP
335
 
#define OP(f, a, b, c, d, k, s, T)                                      \
336
 
      do                                                                \
337
 
        {                                                               \
338
 
          a += f (b, c, d) + correct_words[k] + T;                      \
339
 
          CYCLIC (a, s);                                                \
340
 
          a += b;                                                       \
341
 
        }                                                               \
 
324
#define OP(f, a, b, c, d, k, s, T)                                      \
 
325
      do                                                                \
 
326
        {                                                               \
 
327
          a += f (b, c, d) + correct_words[k] + T;                      \
 
328
          CYCLIC (a, s);                                                \
 
329
          a += b;                                                       \
 
330
        }                                                               \
342
331
      while (0)
343
332
 
344
333
      /* Round 2.  */