xref: /freebsd/contrib/unbound/util/rfc_1982.c (revision 103ba509e72e3949d22485666949e9705d4af8cd)
18f76bb7dSCy Schubert /*
28f76bb7dSCy Schubert  * util/rfc_1982.c - RFC 1982 Serial Number Arithmetic
38f76bb7dSCy Schubert  *
48f76bb7dSCy Schubert  * Copyright (c) 2023, NLnet Labs. All rights reserved.
58f76bb7dSCy Schubert  *
68f76bb7dSCy Schubert  * This software is open source.
78f76bb7dSCy Schubert  *
88f76bb7dSCy Schubert  * Redistribution and use in source and binary forms, with or without
98f76bb7dSCy Schubert  * modification, are permitted provided that the following conditions
108f76bb7dSCy Schubert  * are met:
118f76bb7dSCy Schubert  *
128f76bb7dSCy Schubert  * Redistributions of source code must retain the above copyright notice,
138f76bb7dSCy Schubert  * this list of conditions and the following disclaimer.
148f76bb7dSCy Schubert  *
158f76bb7dSCy Schubert  * Redistributions in binary form must reproduce the above copyright notice,
168f76bb7dSCy Schubert  * this list of conditions and the following disclaimer in the documentation
178f76bb7dSCy Schubert  * and/or other materials provided with the distribution.
188f76bb7dSCy Schubert  *
198f76bb7dSCy Schubert  * Neither the name of the NLNET LABS nor the names of its contributors may
208f76bb7dSCy Schubert  * be used to endorse or promote products derived from this software without
218f76bb7dSCy Schubert  * specific prior written permission.
228f76bb7dSCy Schubert  *
238f76bb7dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
248f76bb7dSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
258f76bb7dSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
268f76bb7dSCy Schubert  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
278f76bb7dSCy Schubert  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
288f76bb7dSCy Schubert  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
298f76bb7dSCy Schubert  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
308f76bb7dSCy Schubert  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
318f76bb7dSCy Schubert  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
328f76bb7dSCy Schubert  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
338f76bb7dSCy Schubert  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
348f76bb7dSCy Schubert  */
358f76bb7dSCy Schubert 
368f76bb7dSCy Schubert /**
378f76bb7dSCy Schubert  * \file
388f76bb7dSCy Schubert  *
398f76bb7dSCy Schubert  * This file contains functions for RFC 1982 serial number arithmetic.
408f76bb7dSCy Schubert  */
418f76bb7dSCy Schubert #include "config.h"
42*103ba509SCy Schubert #include "util/rfc_1982.h"
438f76bb7dSCy Schubert 
448f76bb7dSCy Schubert int
compare_1982(uint32_t a,uint32_t b)458f76bb7dSCy Schubert compare_1982(uint32_t a, uint32_t b)
468f76bb7dSCy Schubert {
478f76bb7dSCy Schubert 	/* for 32 bit values */
488f76bb7dSCy Schubert 	const uint32_t cutoff = ((uint32_t) 1 << (32 - 1));
498f76bb7dSCy Schubert 
508f76bb7dSCy Schubert 	if (a == b) {
518f76bb7dSCy Schubert 		return 0;
528f76bb7dSCy Schubert 	} else if ((a < b && b - a < cutoff) || (a > b && a - b > cutoff)) {
538f76bb7dSCy Schubert 		return -1;
548f76bb7dSCy Schubert 	} else {
558f76bb7dSCy Schubert 		return 1;
568f76bb7dSCy Schubert 	}
578f76bb7dSCy Schubert }
588f76bb7dSCy Schubert 
598f76bb7dSCy Schubert uint32_t
subtract_1982(uint32_t a,uint32_t b)608f76bb7dSCy Schubert subtract_1982(uint32_t a, uint32_t b)
618f76bb7dSCy Schubert {
628f76bb7dSCy Schubert 	/* for 32 bit values */
638f76bb7dSCy Schubert 	const uint32_t cutoff = ((uint32_t) 1 << (32 - 1));
648f76bb7dSCy Schubert 
658f76bb7dSCy Schubert 	if(a == b)
668f76bb7dSCy Schubert 		return 0;
678f76bb7dSCy Schubert 	if(a < b && b - a < cutoff) {
688f76bb7dSCy Schubert 		return b-a;
698f76bb7dSCy Schubert 	}
708f76bb7dSCy Schubert 	if(a > b && a - b > cutoff) {
718f76bb7dSCy Schubert 		return ((uint32_t)0xffffffff) - (a-b-1);
728f76bb7dSCy Schubert 	}
738f76bb7dSCy Schubert 	/* wrong case, b smaller than a */
748f76bb7dSCy Schubert 	return 0;
758f76bb7dSCy Schubert }
76