(root)/
coreutils-9.4/
lib/
md5-stream.c
       1  /* Functions to compute MD5 message digest of files or memory blocks.
       2     according to the definition of MD5 in RFC 1321 from April 1992.
       3     Copyright (C) 1995-1997, 1999-2001, 2005-2006, 2008-2023 Free Software
       4     Foundation, Inc.
       5     This file is part of the GNU C Library.
       6  
       7     This file is free software: you can redistribute it and/or modify
       8     it under the terms of the GNU Lesser General Public License as
       9     published by the Free Software Foundation; either version 2.1 of the
      10     License, or (at your option) any later version.
      11  
      12     This file is distributed in the hope that it will be useful,
      13     but WITHOUT ANY WARRANTY; without even the implied warranty of
      14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15     GNU Lesser General Public License for more details.
      16  
      17     You should have received a copy of the GNU Lesser General Public License
      18     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      19  
      20  /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
      21  
      22  #include <config.h>
      23  
      24  /* Specification.  */
      25  #if HAVE_OPENSSL_MD5
      26  # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
      27  #endif
      28  #include "md5.h"
      29  
      30  #include <stdlib.h>
      31  
      32  #if USE_UNLOCKED_IO
      33  # include "unlocked-io.h"
      34  #endif
      35  
      36  #include "af_alg.h"
      37  
      38  #ifdef _LIBC
      39  # include <endian.h>
      40  # if __BYTE_ORDER == __BIG_ENDIAN
      41  #  define WORDS_BIGENDIAN 1
      42  # endif
      43  /* We need to keep the namespace clean so define the MD5 function
      44     protected using leading __ .  */
      45  # define md5_init_ctx __md5_init_ctx
      46  # define md5_process_block __md5_process_block
      47  # define md5_process_bytes __md5_process_bytes
      48  # define md5_finish_ctx __md5_finish_ctx
      49  # define md5_stream __md5_stream
      50  #endif
      51  
      52  #define BLOCKSIZE 32768
      53  #if BLOCKSIZE % 64 != 0
      54  # error "invalid BLOCKSIZE"
      55  #endif
      56  
      57  /* Compute MD5 message digest for bytes read from STREAM.  The
      58     resulting message digest number will be written into the 16 bytes
      59     beginning at RESBLOCK.  */
      60  int
      61  md5_stream (FILE *stream, void *resblock)
      62  {
      63    switch (afalg_stream (stream, "md5", resblock, MD5_DIGEST_SIZE))
      64      {
      65      case 0: return 0;
      66      case -EIO: return 1;
      67      }
      68  
      69    char *buffer = malloc (BLOCKSIZE + 72);
      70    if (!buffer)
      71      return 1;
      72  
      73    struct md5_ctx ctx;
      74    md5_init_ctx (&ctx);
      75    size_t sum;
      76  
      77    /* Iterate over full file contents.  */
      78    while (1)
      79      {
      80        /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
      81           computation function processes the whole buffer so that with the
      82           next round of the loop another block can be read.  */
      83        size_t n;
      84        sum = 0;
      85  
      86        /* Read block.  Take care for partial reads.  */
      87        while (1)
      88          {
      89            /* Either process a partial fread() from this loop,
      90               or the fread() in afalg_stream may have gotten EOF.
      91               We need to avoid a subsequent fread() as EOF may
      92               not be sticky.  For details of such systems, see:
      93               https://sourceware.org/bugzilla/show_bug.cgi?id=1190  */
      94            if (feof (stream))
      95              goto process_partial_block;
      96  
      97            n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
      98  
      99            sum += n;
     100  
     101            if (sum == BLOCKSIZE)
     102              break;
     103  
     104            if (n == 0)
     105              {
     106                /* Check for the error flag IFF N == 0, so that we don't
     107                   exit the loop after a partial read due to e.g., EAGAIN
     108                   or EWOULDBLOCK.  */
     109                if (ferror (stream))
     110                  {
     111                    free (buffer);
     112                    return 1;
     113                  }
     114                goto process_partial_block;
     115              }
     116          }
     117  
     118        /* Process buffer with BLOCKSIZE bytes.  Note that
     119           BLOCKSIZE % 64 == 0
     120         */
     121        md5_process_block (buffer, BLOCKSIZE, &ctx);
     122      }
     123  
     124  process_partial_block:
     125  
     126    /* Process any remaining bytes.  */
     127    if (sum > 0)
     128      md5_process_bytes (buffer, sum, &ctx);
     129  
     130    /* Construct result in desired memory.  */
     131    md5_finish_ctx (&ctx, resblock);
     132    free (buffer);
     133    return 0;
     134  }
     135  
     136  /*
     137   * Hey Emacs!
     138   * Local Variables:
     139   * coding: utf-8
     140   * End:
     141   */