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