xref: /openbsd-src/sbin/unwind/libunbound/util/rfc_1982.c (revision 911a1a621ef1d3569d27fafbb7ae85d0ed654468)
1d500c338Sflorian /*
2d500c338Sflorian  * util/rfc_1982.c - RFC 1982 Serial Number Arithmetic
3d500c338Sflorian  *
4d500c338Sflorian  * Copyright (c) 2023, NLnet Labs. All rights reserved.
5d500c338Sflorian  *
6d500c338Sflorian  * This software is open source.
7d500c338Sflorian  *
8d500c338Sflorian  * Redistribution and use in source and binary forms, with or without
9d500c338Sflorian  * modification, are permitted provided that the following conditions
10d500c338Sflorian  * are met:
11d500c338Sflorian  *
12d500c338Sflorian  * Redistributions of source code must retain the above copyright notice,
13d500c338Sflorian  * this list of conditions and the following disclaimer.
14d500c338Sflorian  *
15d500c338Sflorian  * Redistributions in binary form must reproduce the above copyright notice,
16d500c338Sflorian  * this list of conditions and the following disclaimer in the documentation
17d500c338Sflorian  * and/or other materials provided with the distribution.
18d500c338Sflorian  *
19d500c338Sflorian  * Neither the name of the NLNET LABS nor the names of its contributors may
20d500c338Sflorian  * be used to endorse or promote products derived from this software without
21d500c338Sflorian  * specific prior written permission.
22d500c338Sflorian  *
23d500c338Sflorian  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24d500c338Sflorian  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25d500c338Sflorian  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26d500c338Sflorian  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27d500c338Sflorian  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28d500c338Sflorian  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29d500c338Sflorian  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30d500c338Sflorian  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31d500c338Sflorian  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32d500c338Sflorian  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33d500c338Sflorian  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34d500c338Sflorian  */
35d500c338Sflorian 
36d500c338Sflorian /**
37d500c338Sflorian  * \file
38d500c338Sflorian  *
39d500c338Sflorian  * This file contains functions for RFC 1982 serial number arithmetic.
40d500c338Sflorian  */
41d500c338Sflorian #include "config.h"
42*911a1a62Sflorian #include "util/rfc_1982.h"
43d500c338Sflorian 
44d500c338Sflorian int
compare_1982(uint32_t a,uint32_t b)45d500c338Sflorian compare_1982(uint32_t a, uint32_t b)
46d500c338Sflorian {
47d500c338Sflorian 	/* for 32 bit values */
48d500c338Sflorian 	const uint32_t cutoff = ((uint32_t) 1 << (32 - 1));
49d500c338Sflorian 
50d500c338Sflorian 	if (a == b) {
51d500c338Sflorian 		return 0;
52d500c338Sflorian 	} else if ((a < b && b - a < cutoff) || (a > b && a - b > cutoff)) {
53d500c338Sflorian 		return -1;
54d500c338Sflorian 	} else {
55d500c338Sflorian 		return 1;
56d500c338Sflorian 	}
57d500c338Sflorian }
58d500c338Sflorian 
59d500c338Sflorian uint32_t
subtract_1982(uint32_t a,uint32_t b)60d500c338Sflorian subtract_1982(uint32_t a, uint32_t b)
61d500c338Sflorian {
62d500c338Sflorian 	/* for 32 bit values */
63d500c338Sflorian 	const uint32_t cutoff = ((uint32_t) 1 << (32 - 1));
64d500c338Sflorian 
65d500c338Sflorian 	if(a == b)
66d500c338Sflorian 		return 0;
67d500c338Sflorian 	if(a < b && b - a < cutoff) {
68d500c338Sflorian 		return b-a;
69d500c338Sflorian 	}
70d500c338Sflorian 	if(a > b && a - b > cutoff) {
71d500c338Sflorian 		return ((uint32_t)0xffffffff) - (a-b-1);
72d500c338Sflorian 	}
73d500c338Sflorian 	/* wrong case, b smaller than a */
74d500c338Sflorian 	return 0;
75d500c338Sflorian }
76