LCOV - code coverage report
Current view: top level - elfutils/lib - crc32_file.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 12 28 42.9 %
Date: 2013-03-08 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 4 26 15.4 %

           Branch data     Line data    Source code
       1                 :            : /* Compute CRC32 checksum of file contents.
       2                 :            :    Copyright (C) 2006 Red Hat, Inc.
       3                 :            :    This file is part of elfutils.
       4                 :            : 
       5                 :            :    This file is free software; you can redistribute it and/or modify
       6                 :            :    it under the terms of either
       7                 :            : 
       8                 :            :      * the GNU Lesser General Public License as published by the Free
       9                 :            :        Software Foundation; either version 3 of the License, or (at
      10                 :            :        your option) any later version
      11                 :            : 
      12                 :            :    or
      13                 :            : 
      14                 :            :      * the GNU General Public License as published by the Free
      15                 :            :        Software Foundation; either version 2 of the License, or (at
      16                 :            :        your option) any later version
      17                 :            : 
      18                 :            :    or both in parallel, as here.
      19                 :            : 
      20                 :            :    elfutils is distributed in the hope that it will be useful, but
      21                 :            :    WITHOUT ANY WARRANTY; without even the implied warranty of
      22                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      23                 :            :    General Public License for more details.
      24                 :            : 
      25                 :            :    You should have received copies of the GNU General Public License and
      26                 :            :    the GNU Lesser General Public License along with this program.  If
      27                 :            :    not, see <http://www.gnu.org/licenses/>.  */
      28                 :            : 
      29                 :            : #include "system.h"
      30                 :            : #include <errno.h>
      31                 :            : #include <unistd.h>
      32                 :            : #include <sys/stat.h>
      33                 :            : #include <sys/mman.h>
      34                 :            : 
      35                 :            : int
      36                 :         19 : crc32_file (int fd, uint32_t *resp)
      37                 :            : {
      38                 :            :   unsigned char buffer[1024 * 8];
      39                 :         19 :   uint32_t crc = 0;
      40                 :         19 :   off_t off = 0;
      41                 :            :   ssize_t count;
      42                 :            : 
      43                 :            :   struct stat st;
      44         [ +  - ]:         19 :   if (fstat (fd, &st) == 0)
      45                 :            :     {
      46                 :            :       /* Try mapping in the file data.  */
      47                 :         19 :       size_t mapsize = st.st_size;
      48                 :         19 :       void *mapped = mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
      49 [ -  + ][ #  # ]:         19 :       if (mapped == MAP_FAILED && errno == ENOMEM)
      50                 :            :         {
      51                 :          0 :           const size_t pagesize = sysconf (_SC_PAGE_SIZE);
      52                 :          0 :           mapsize = ((mapsize / 2) + pagesize - 1) & -pagesize;
      53         [ #  # ]:          0 :           while (mapsize >= pagesize
      54         [ #  # ]:          0 :                  && (mapped = mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE,
      55         [ #  # ]:          0 :                                     fd, 0)) == MAP_FAILED && errno == ENOMEM)
      56                 :          0 :             mapsize /= 2;
      57                 :            :         }
      58         [ +  - ]:         19 :       if (mapped != MAP_FAILED)
      59                 :            :         {
      60                 :            :           do
      61                 :            :             {
      62         [ +  - ]:         19 :               if (st.st_size <= (off_t) mapsize)
      63                 :            :                 {
      64                 :         19 :                   *resp = crc32 (crc, mapped, st.st_size);
      65                 :         19 :                   munmap (mapped, mapsize);
      66                 :            :                   return 0;
      67                 :            :                 }
      68                 :          0 :               crc = crc32 (crc, mapped, mapsize);
      69                 :          0 :               off += mapsize;
      70                 :          0 :               st.st_size -= mapsize;
      71                 :          0 :             } while (mmap (mapped, mapsize, PROT_READ, MAP_FIXED|MAP_PRIVATE,
      72         [ #  # ]:          0 :                            fd, off) == mapped);
      73                 :          0 :           munmap (mapped, mapsize);
      74                 :            :         }
      75                 :            :     }
      76                 :            : 
      77 [ #  # ][ #  # ]:          0 :   while ((count = TEMP_FAILURE_RETRY (pread (fd, buffer, sizeof buffer,
                 [ #  # ]
      78                 :            :                                              off))) > 0)
      79                 :            :     {
      80                 :          0 :       off += count;
      81                 :          0 :       crc = crc32 (crc, buffer, count);
      82                 :            :     }
      83                 :            : 
      84                 :          0 :   *resp = crc;
      85                 :            : 
      86         [ #  # ]:         19 :   return count == 0 ? 0 : -1;
      87                 :            : }

Generated by: LCOV version 1.9