GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ospf6d/iso_cksum.c Lines: 0 17 0.0 %
Date: 2017-11-13 Branches: 0 10 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: iso_cksum.c,v 1.1 2007/10/08 10:44:50 norby Exp $ */
2
3
/*
4
 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/types.h>
20
21
#include "ospf6d.h"
22
23
/* implementation of Fletcher Checksum -- see RFC 1008 for more info */
24
25
/* pos needs to be 0 for verify and 2 <= pos < len for calculation */
26
u_int16_t
27
iso_cksum(void *buf, u_int16_t len, u_int16_t pos)
28
{
29
	u_int8_t	*p = buf;
30
	int		 c0 = 0, c1 = 0, x;	/* counters */
31
	u_int16_t	 sop;
32
33
	sop = len - pos - 1;	/* pos is an offset (pos 2 is 3rd element) */
34
	p += 2;			/* jump over age field */
35
	len -= 2;
36
	while (len--) {
37
		c0 += *p++;
38
		c1 += c0;
39
		if ((len & 0xfff) == 0) {
40
			/* overflow protection */
41
			c0 %= 255;
42
			c1 %= 255;
43
		}
44
	}
45
46
	if (pos) {
47
		x = ((sop * c0 - c1)) % 255;
48
		if (x <= 0)
49
			x += 255;
50
		c1 = 510 - c0 - x;
51
		if (c1 > 255)
52
			c1 -= 255;
53
		c0 = x;
54
	}
55
56
	return (c0 << 8 | c1);
57
}