xref: /openbsd-src/usr.sbin/unbound/util/rfc_1982.c (revision 9c7f0a49a49eaaf8a541e2c1c60150e851bc44d2)
1437d2860Ssthen /*
2437d2860Ssthen  * util/rfc_1982.c - RFC 1982 Serial Number Arithmetic
3437d2860Ssthen  *
4437d2860Ssthen  * Copyright (c) 2023, NLnet Labs. All rights reserved.
5437d2860Ssthen  *
6437d2860Ssthen  * This software is open source.
7437d2860Ssthen  *
8437d2860Ssthen  * Redistribution and use in source and binary forms, with or without
9437d2860Ssthen  * modification, are permitted provided that the following conditions
10437d2860Ssthen  * are met:
11437d2860Ssthen  *
12437d2860Ssthen  * Redistributions of source code must retain the above copyright notice,
13437d2860Ssthen  * this list of conditions and the following disclaimer.
14437d2860Ssthen  *
15437d2860Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16437d2860Ssthen  * this list of conditions and the following disclaimer in the documentation
17437d2860Ssthen  * and/or other materials provided with the distribution.
18437d2860Ssthen  *
19437d2860Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20437d2860Ssthen  * be used to endorse or promote products derived from this software without
21437d2860Ssthen  * specific prior written permission.
22437d2860Ssthen  *
23437d2860Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24437d2860Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25437d2860Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26437d2860Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27437d2860Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28437d2860Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29437d2860Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30437d2860Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31437d2860Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32437d2860Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33437d2860Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34437d2860Ssthen  */
35437d2860Ssthen 
36437d2860Ssthen /**
37437d2860Ssthen  * \file
38437d2860Ssthen  *
39437d2860Ssthen  * This file contains functions for RFC 1982 serial number arithmetic.
40437d2860Ssthen  */
41437d2860Ssthen #include "config.h"
42*9c7f0a49Ssthen #include "util/rfc_1982.h"
43437d2860Ssthen 
44437d2860Ssthen int
compare_1982(uint32_t a,uint32_t b)45437d2860Ssthen compare_1982(uint32_t a, uint32_t b)
46437d2860Ssthen {
47437d2860Ssthen 	/* for 32 bit values */
48437d2860Ssthen 	const uint32_t cutoff = ((uint32_t) 1 << (32 - 1));
49437d2860Ssthen 
50437d2860Ssthen 	if (a == b) {
51437d2860Ssthen 		return 0;
52437d2860Ssthen 	} else if ((a < b && b - a < cutoff) || (a > b && a - b > cutoff)) {
53437d2860Ssthen 		return -1;
54437d2860Ssthen 	} else {
55437d2860Ssthen 		return 1;
56437d2860Ssthen 	}
57437d2860Ssthen }
58437d2860Ssthen 
59437d2860Ssthen uint32_t
subtract_1982(uint32_t a,uint32_t b)60437d2860Ssthen subtract_1982(uint32_t a, uint32_t b)
61437d2860Ssthen {
62437d2860Ssthen 	/* for 32 bit values */
63437d2860Ssthen 	const uint32_t cutoff = ((uint32_t) 1 << (32 - 1));
64437d2860Ssthen 
65437d2860Ssthen 	if(a == b)
66437d2860Ssthen 		return 0;
67437d2860Ssthen 	if(a < b && b - a < cutoff) {
68437d2860Ssthen 		return b-a;
69437d2860Ssthen 	}
70437d2860Ssthen 	if(a > b && a - b > cutoff) {
71437d2860Ssthen 		return ((uint32_t)0xffffffff) - (a-b-1);
72437d2860Ssthen 	}
73437d2860Ssthen 	/* wrong case, b smaller than a */
74437d2860Ssthen 	return 0;
75437d2860Ssthen }
76