1210284Sjmallett/*
2210284Sjmallett * util/rfc_1982.c - RFC 1982 Serial Number Arithmetic
3210284Sjmallett *
4210284Sjmallett * Copyright (c) 2023, NLnet Labs. All rights reserved.
5210284Sjmallett *
6210284Sjmallett * This software is open source.
7210284Sjmallett *
8210284Sjmallett * Redistribution and use in source and binary forms, with or without
9210284Sjmallett * modification, are permitted provided that the following conditions
10210284Sjmallett * are met:
11210284Sjmallett *
12210284Sjmallett * Redistributions of source code must retain the above copyright notice,
13210284Sjmallett * this list of conditions and the following disclaimer.
14210284Sjmallett *
15210284Sjmallett * Redistributions in binary form must reproduce the above copyright notice,
16210284Sjmallett * this list of conditions and the following disclaimer in the documentation
17210284Sjmallett * and/or other materials provided with the distribution.
18210284Sjmallett *
19210284Sjmallett * Neither the name of the NLNET LABS nor the names of its contributors may
20210284Sjmallett * be used to endorse or promote products derived from this software without
21210284Sjmallett * specific prior written permission.
22210284Sjmallett *
23210284Sjmallett * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24210284Sjmallett * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25210284Sjmallett * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26210284Sjmallett * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27210284Sjmallett * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28210284Sjmallett * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29210284Sjmallett * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30210284Sjmallett * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31210284Sjmallett * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32210284Sjmallett * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33210284Sjmallett * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34210284Sjmallett */
35210284Sjmallett
36210284Sjmallett/**
37210284Sjmallett * \file
38210284Sjmallett *
39210284Sjmallett * This file contains functions for RFC 1982 serial number arithmetic.
40210284Sjmallett */
41210284Sjmallett#include "config.h"
42210284Sjmallett#include "util/rfc_1982.h"
43210284Sjmallett
44210284Sjmallettint
45210284Sjmallettcompare_1982(uint32_t a, uint32_t b)
46210284Sjmallett{
47210284Sjmallett	/* for 32 bit values */
48210284Sjmallett	const uint32_t cutoff = ((uint32_t) 1 << (32 - 1));
49210284Sjmallett
50210284Sjmallett	if (a == b) {
51210284Sjmallett		return 0;
52210284Sjmallett	} else if ((a < b && b - a < cutoff) || (a > b && a - b > cutoff)) {
53210284Sjmallett		return -1;
54210284Sjmallett	} else {
55210284Sjmallett		return 1;
56210284Sjmallett	}
57210284Sjmallett}
58210284Sjmallett
59210284Sjmallettuint32_t
60210284Sjmallettsubtract_1982(uint32_t a, uint32_t b)
61210284Sjmallett{
62210284Sjmallett	/* for 32 bit values */
63210284Sjmallett	const uint32_t cutoff = ((uint32_t) 1 << (32 - 1));
64210284Sjmallett
65210284Sjmallett	if(a == b)
66210284Sjmallett		return 0;
67210284Sjmallett	if(a < b && b - a < cutoff) {
68210284Sjmallett		return b-a;
69210284Sjmallett	}
70210284Sjmallett	if(a > b && a - b > cutoff) {
71210284Sjmallett		return ((uint32_t)0xffffffff) - (a-b-1);
72210284Sjmallett	}
73210284Sjmallett	/* wrong case, b smaller than a */
74210284Sjmallett	return 0;
75210284Sjmallett}
76210284Sjmallett