crc32_file.c revision 25b3c049e70834cf33790a28643ab058b507b35c
1/* Compute CRC32 checksum of file contents.
2   Copyright (C) 2006 Red Hat, Inc.
3   This file is part of Red Hat elfutils.
4
5   Red Hat elfutils is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by the
7   Free Software Foundation; version 2 of the License.
8
9   Red Hat elfutils is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   General Public License for more details.
13
14   You should have received a copy of the GNU General Public License along
15   with Red Hat elfutils; if not, write to the Free Software Foundation,
16   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17
18   In addition, as a special exception, Red Hat, Inc. gives You the
19   additional right to link the code of Red Hat elfutils with code licensed
20   under an Open Source Initiative certified open source license
21   (http://www.opensource.org/licenses/index.php) and to distribute linked
22   combinations including the two.  Non-GPL Code permitted under this
23   exception must only link to the code of Red Hat elfutils through those
24   well defined interfaces identified in the file named EXCEPTION found in
25   the source code files (the "Approved Interfaces").  The files of Non-GPL
26   Code may instantiate templates or use macros or inline functions from
27   the Approved Interfaces without causing the resulting work to be covered
28   by the GNU General Public License.  Only Red Hat, Inc. may make changes
29   or additions to the list of Approved Interfaces.  Red Hat's grant of
30   this exception is conditioned upon your not adding any new exceptions.
31   If you wish to add a new Approved Interface or exception, please contact
32   Red Hat.  You must obey the GNU General Public License in all respects
33   for all of the Red Hat elfutils code and other code used in conjunction
34   with Red Hat elfutils except the Non-GPL Code covered by this exception.
35   If you modify this file, you may extend this exception to your version
36   of the file, but you are not obligated to do so.  If you do not wish to
37   provide this exception without modification, you must delete this
38   exception statement from your version and license this file solely under
39   the GPL without exception.
40
41   Red Hat elfutils is an included package of the Open Invention Network.
42   An included package of the Open Invention Network is a package for which
43   Open Invention Network licensees cross-license their patents.  No patent
44   license is granted, either expressly or impliedly, by designation as an
45   included package.  Should you wish to participate in the Open Invention
46   Network licensing program, please visit www.openinventionnetwork.com
47   <http://www.openinventionnetwork.com>.  */
48
49#include "system.h"
50#include <errno.h>
51#include <unistd.h>
52#include <sys/stat.h>
53#include <sys/mman.h>
54
55int
56crc32_file (int fd, uint32_t *resp)
57{
58  unsigned char buffer[1024 * 8];
59  uint32_t crc = 0;
60  off_t off = 0;
61  ssize_t count;
62
63  struct stat st;
64  if (fstat (fd, &st) == 0)
65    {
66      /* Try mapping in the file data.  */
67      size_t mapsize = st.st_size;
68      void *mapped = mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
69      if (mapped == MAP_FAILED && errno == ENOMEM)
70	{
71	  const size_t pagesize = sysconf (_SC_PAGE_SIZE);
72	  mapsize = ((mapsize / 2) + pagesize - 1) & -pagesize;
73	  while (mapsize >= pagesize
74		 && (mapped = mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE,
75				    fd, 0)) == MAP_FAILED && errno == ENOMEM)
76	    mapsize /= 2;
77	}
78      if (mapped != MAP_FAILED)
79	{
80	  do
81	    {
82	      if (st.st_size <= (off_t) mapsize)
83		{
84		  *resp = crc32 (crc, mapped, st.st_size);
85		  munmap (mapped, mapsize);
86		  return 0;
87		}
88	      crc = crc32 (crc, mapped, mapsize);
89	      off += mapsize;
90	      st.st_size -= mapsize;
91	    } while (mmap (mapped, mapsize, PROT_READ, MAP_FIXED|MAP_PRIVATE,
92			   fd, off) == mapped);
93	  munmap (mapped, mapsize);
94	}
95    }
96
97  while ((count = TEMP_FAILURE_RETRY (pread (fd, buffer, sizeof buffer,
98					     off))) > 0)
99    {
100      off += count;
101      crc = crc32 (crc, buffer, count);
102    }
103
104  *resp = crc;
105
106  return count == 0 ? 0 : -1;
107}
108