There were several memory leaks inside jfsck(), they've probably been there for a...
[libjio.git] / checksum.c
blob60e97588a29957c0c8781afb9c4a47158dfcc0ec
2 /*
3 * libjio - A library for Journaled I/O
4 * Alberto Bertogli (albertogli@telpin.com.ar)
6 * Checksum functions
7 * Based on RFC 1071, "Computing the Internet Checksum"
8 */
10 #include <stddef.h>
11 #include <stdint.h>
12 #include <sys/mman.h>
13 #include "common.h"
16 int checksum(int fd, size_t len, uint32_t *csum)
18 uint8_t *map;
20 map = (uint8_t *) mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
21 if (map == MAP_FAILED)
22 return 0;
24 *csum = checksum_map(map, len);
26 munmap(map, len);
27 return 1;
30 uint32_t checksum_map(uint8_t *map, size_t count)
32 uint32_t sum = 0;
34 while( count > 1 ) {
35 sum += * (uint16_t *) map++;
36 count -= 2;
39 if( count > 0 )
40 sum += * (uint8_t *) map;
42 while (sum >> 16)
43 sum = (sum & 0xffff) + (sum >> 16);
45 return ~sum;