Blame lib/crc32_file.c

Packit 032894
/* Compute CRC32 checksum of file contents.
Packit 032894
   Copyright (C) 2006 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include "libeu.h"
Packit 032894
#include <errno.h>
Packit 032894
#include <unistd.h>
Packit 032894
#include <sys/stat.h>
Packit 032894
#include <sys/mman.h>
Packit 032894
#include "system.h"
Packit 032894
Packit 032894
int
Packit 032894
crc32_file (int fd, uint32_t *resp)
Packit 032894
{
Packit 032894
  unsigned char buffer[1024 * 8];
Packit 032894
  uint32_t crc = 0;
Packit 032894
  off_t off = 0;
Packit 032894
  ssize_t count;
Packit 032894
Packit 032894
  struct stat st;
Packit 032894
  if (fstat (fd, &st) == 0)
Packit 032894
    {
Packit 032894
      /* Try mapping in the file data.  */
Packit 032894
      size_t mapsize = st.st_size;
Packit 032894
      void *mapped = mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
Packit 032894
      if (mapped == MAP_FAILED && errno == ENOMEM)
Packit 032894
	{
Packit 032894
	  const size_t pagesize = sysconf (_SC_PAGESIZE);
Packit 032894
	  mapsize = ((mapsize / 2) + pagesize - 1) & -pagesize;
Packit 032894
	  while (mapsize >= pagesize
Packit 032894
		 && (mapped = mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE,
Packit 032894
				    fd, 0)) == MAP_FAILED && errno == ENOMEM)
Packit 032894
	    mapsize /= 2;
Packit 032894
	}
Packit 032894
      if (mapped != MAP_FAILED)
Packit 032894
	{
Packit 032894
	  do
Packit 032894
	    {
Packit 032894
	      if (st.st_size <= (off_t) mapsize)
Packit 032894
		{
Packit 032894
		  *resp = crc32 (crc, mapped, st.st_size);
Packit 032894
		  munmap (mapped, mapsize);
Packit 032894
		  return 0;
Packit 032894
		}
Packit 032894
	      crc = crc32 (crc, mapped, mapsize);
Packit 032894
	      off += mapsize;
Packit 032894
	      st.st_size -= mapsize;
Packit 032894
	    } while (mmap (mapped, mapsize, PROT_READ, MAP_FIXED|MAP_PRIVATE,
Packit 032894
			   fd, off) == mapped);
Packit 032894
	  munmap (mapped, mapsize);
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  while ((count = TEMP_FAILURE_RETRY (pread (fd, buffer, sizeof buffer,
Packit 032894
					     off))) > 0)
Packit 032894
    {
Packit 032894
      off += count;
Packit 032894
      crc = crc32 (crc, buffer, count);
Packit 032894
    }
Packit 032894
Packit 032894
  *resp = crc;
Packit 032894
Packit 032894
  return count == 0 ? 0 : -1;
Packit 032894
}