xref: /freebsd/crypto/openssl/include/internal/uint_set.h (revision e7be843b4a162e68651d3911f0357ed464915629)
1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery  *
4*e7be843bSPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e7be843bSPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*e7be843bSPierre Pronchery  * in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery  * https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery  */
9*e7be843bSPierre Pronchery #ifndef OSSL_UINT_SET_H
10*e7be843bSPierre Pronchery # define OSSL_UINT_SET_H
11*e7be843bSPierre Pronchery 
12*e7be843bSPierre Pronchery #include "openssl/params.h"
13*e7be843bSPierre Pronchery #include "internal/list.h"
14*e7be843bSPierre Pronchery 
15*e7be843bSPierre Pronchery /*
16*e7be843bSPierre Pronchery  * uint64_t Integer Sets
17*e7be843bSPierre Pronchery  * =====================
18*e7be843bSPierre Pronchery  *
19*e7be843bSPierre Pronchery  * Utilities for managing a logical set of unsigned 64-bit integers. The
20*e7be843bSPierre Pronchery  * structure tracks each contiguous range of integers using one allocation and
21*e7be843bSPierre Pronchery  * is thus optimised for cases where integers tend to appear consecutively.
22*e7be843bSPierre Pronchery  * Queries are optimised under the assumption that they will generally be made
23*e7be843bSPierre Pronchery  * on integers near the end of the set.
24*e7be843bSPierre Pronchery  *
25*e7be843bSPierre Pronchery  * Discussion of implementation details can be found in uint_set.c.
26*e7be843bSPierre Pronchery  */
27*e7be843bSPierre Pronchery typedef struct uint_range_st {
28*e7be843bSPierre Pronchery     uint64_t    start, end;
29*e7be843bSPierre Pronchery } UINT_RANGE;
30*e7be843bSPierre Pronchery 
31*e7be843bSPierre Pronchery typedef struct uint_set_item_st UINT_SET_ITEM;
32*e7be843bSPierre Pronchery struct uint_set_item_st {
33*e7be843bSPierre Pronchery     OSSL_LIST_MEMBER(uint_set, UINT_SET_ITEM);
34*e7be843bSPierre Pronchery     UINT_RANGE                  range;
35*e7be843bSPierre Pronchery };
36*e7be843bSPierre Pronchery 
37*e7be843bSPierre Pronchery DEFINE_LIST_OF(uint_set, UINT_SET_ITEM);
38*e7be843bSPierre Pronchery 
39*e7be843bSPierre Pronchery typedef OSSL_LIST(uint_set) UINT_SET;
40*e7be843bSPierre Pronchery 
41*e7be843bSPierre Pronchery void ossl_uint_set_init(UINT_SET *s);
42*e7be843bSPierre Pronchery void ossl_uint_set_destroy(UINT_SET *s);
43*e7be843bSPierre Pronchery 
44*e7be843bSPierre Pronchery /*
45*e7be843bSPierre Pronchery  * Insert a range into a integer set. Returns 0 on allocation failure, in which
46*e7be843bSPierre Pronchery  * case the integer set is in a valid but undefined state. Otherwise, returns 1.
47*e7be843bSPierre Pronchery  * Ranges can overlap existing ranges without limitation. If a range is a subset
48*e7be843bSPierre Pronchery  * of an existing range in the set, this is a no-op and returns 1.
49*e7be843bSPierre Pronchery  */
50*e7be843bSPierre Pronchery int ossl_uint_set_insert(UINT_SET *s, const UINT_RANGE *range);
51*e7be843bSPierre Pronchery 
52*e7be843bSPierre Pronchery /*
53*e7be843bSPierre Pronchery  * Remove a range from the set. Returns 0 on allocation failure, in which case
54*e7be843bSPierre Pronchery  * the integer set is unchanged. Otherwise, returns 1. Ranges which are not
55*e7be843bSPierre Pronchery  * already in the set can be removed without issue. If a passed range is not in
56*e7be843bSPierre Pronchery  * the integer set at all, this is a no-op and returns 1.
57*e7be843bSPierre Pronchery  */
58*e7be843bSPierre Pronchery int ossl_uint_set_remove(UINT_SET *s, const UINT_RANGE *range);
59*e7be843bSPierre Pronchery 
60*e7be843bSPierre Pronchery /* Returns 1 iff the given integer is in the integer set. */
61*e7be843bSPierre Pronchery int ossl_uint_set_query(const UINT_SET *s, uint64_t v);
62*e7be843bSPierre Pronchery 
63*e7be843bSPierre Pronchery #endif
64