VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.7/crypto/sparse_array.c@ 97371

Last change on this file since 97371 was 95219, checked in by vboxsync, 3 years ago

libs/openssl: Switched to v3.0.3, bugref:10128

File size: 5.8 KB
Line 
1/*
2 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11#include <openssl/crypto.h>
12#include <openssl/bn.h>
13#include "crypto/sparse_array.h"
14
15/*
16 * How many bits are used to index each level in the tree structure?
17 * This setting determines the number of pointers stored in each node of the
18 * tree used to represent the sparse array. Having more pointers reduces the
19 * depth of the tree but potentially wastes more memory. That is, this is a
20 * direct space versus time tradeoff.
21 *
22 * The default is to use four bits which means that the are 16
23 * pointers in each tree node.
24 *
25 * The library builder is also permitted to define other sizes in the closed
26 * interval [2, sizeof(ossl_uintmax_t) * 8]. Space use generally scales
27 * exponentially with the block size, although the implementation only
28 * creates enough blocks to support the largest used index. The depth is:
29 * ceil(log_2(largest index) / 2^{block size})
30 * E.g. with a block size of 4, and a largest index of 1000, the depth
31 * will be three.
32 */
33#ifndef OPENSSL_SA_BLOCK_BITS
34# define OPENSSL_SA_BLOCK_BITS 4
35#elif OPENSSL_SA_BLOCK_BITS < 2 || OPENSSL_SA_BLOCK_BITS > (BN_BITS2 - 1)
36# error OPENSSL_SA_BLOCK_BITS is out of range
37#endif
38
39/*
40 * From the number of bits, work out:
41 * the number of pointers in a tree node;
42 * a bit mask to quickly extract an index and
43 * the maximum depth of the tree structure.
44 */
45#define SA_BLOCK_MAX (1 << OPENSSL_SA_BLOCK_BITS)
46#define SA_BLOCK_MASK (SA_BLOCK_MAX - 1)
47#define SA_BLOCK_MAX_LEVELS (((int)sizeof(ossl_uintmax_t) * 8 \
48 + OPENSSL_SA_BLOCK_BITS - 1) \
49 / OPENSSL_SA_BLOCK_BITS)
50
51struct sparse_array_st {
52 int levels;
53 ossl_uintmax_t top;
54 size_t nelem;
55 void **nodes;
56};
57
58OPENSSL_SA *ossl_sa_new(void)
59{
60 OPENSSL_SA *res = OPENSSL_zalloc(sizeof(*res));
61
62 return res;
63}
64
65static void sa_doall(const OPENSSL_SA *sa, void (*node)(void **),
66 void (*leaf)(ossl_uintmax_t, void *, void *), void *arg)
67{
68 int i[SA_BLOCK_MAX_LEVELS];
69 void *nodes[SA_BLOCK_MAX_LEVELS];
70 ossl_uintmax_t idx = 0;
71 int l = 0;
72
73 i[0] = 0;
74 nodes[0] = sa->nodes;
75 while (l >= 0) {
76 const int n = i[l];
77 void ** const p = nodes[l];
78
79 if (n >= SA_BLOCK_MAX) {
80 if (p != NULL && node != NULL)
81 (*node)(p);
82 l--;
83 idx >>= OPENSSL_SA_BLOCK_BITS;
84 } else {
85 i[l] = n + 1;
86 if (p != NULL && p[n] != NULL) {
87 idx = (idx & ~SA_BLOCK_MASK) | n;
88 if (l < sa->levels - 1) {
89 i[++l] = 0;
90 nodes[l] = p[n];
91 idx <<= OPENSSL_SA_BLOCK_BITS;
92 } else if (leaf != NULL) {
93 (*leaf)(idx, p[n], arg);
94 }
95 }
96 }
97 }
98}
99
100static void sa_free_node(void **p)
101{
102 OPENSSL_free(p);
103}
104
105static void sa_free_leaf(ossl_uintmax_t n, void *p, void *arg)
106{
107 OPENSSL_free(p);
108}
109
110void ossl_sa_free(OPENSSL_SA *sa)
111{
112 sa_doall(sa, &sa_free_node, NULL, NULL);
113 OPENSSL_free(sa);
114}
115
116void ossl_sa_free_leaves(OPENSSL_SA *sa)
117{
118 sa_doall(sa, &sa_free_node, &sa_free_leaf, NULL);
119 OPENSSL_free(sa);
120}
121
122/* Wrap this in a structure to avoid compiler warnings */
123struct trampoline_st {
124 void (*func)(ossl_uintmax_t, void *);
125};
126
127static void trampoline(ossl_uintmax_t n, void *l, void *arg)
128{
129 ((const struct trampoline_st *)arg)->func(n, l);
130}
131
132void ossl_sa_doall(const OPENSSL_SA *sa, void (*leaf)(ossl_uintmax_t, void *))
133{
134 struct trampoline_st tramp;
135
136 tramp.func = leaf;
137 if (sa != NULL)
138 sa_doall(sa, NULL, &trampoline, &tramp);
139}
140
141void ossl_sa_doall_arg(const OPENSSL_SA *sa,
142 void (*leaf)(ossl_uintmax_t, void *, void *),
143 void *arg)
144{
145 if (sa != NULL)
146 sa_doall(sa, NULL, leaf, arg);
147}
148
149size_t ossl_sa_num(const OPENSSL_SA *sa)
150{
151 return sa == NULL ? 0 : sa->nelem;
152}
153
154void *ossl_sa_get(const OPENSSL_SA *sa, ossl_uintmax_t n)
155{
156 int level;
157 void **p, *r = NULL;
158
159 if (sa == NULL || sa->nelem == 0)
160 return NULL;
161
162 if (n <= sa->top) {
163 p = sa->nodes;
164 for (level = sa->levels - 1; p != NULL && level > 0; level--)
165 p = (void **)p[(n >> (OPENSSL_SA_BLOCK_BITS * level))
166 & SA_BLOCK_MASK];
167 r = p == NULL ? NULL : p[n & SA_BLOCK_MASK];
168 }
169 return r;
170}
171
172static ossl_inline void **alloc_node(void)
173{
174 return OPENSSL_zalloc(SA_BLOCK_MAX * sizeof(void *));
175}
176
177int ossl_sa_set(OPENSSL_SA *sa, ossl_uintmax_t posn, void *val)
178{
179 int i, level = 1;
180 ossl_uintmax_t n = posn;
181 void **p;
182
183 if (sa == NULL)
184 return 0;
185
186 for (level = 1; level < SA_BLOCK_MAX_LEVELS; level++)
187 if ((n >>= OPENSSL_SA_BLOCK_BITS) == 0)
188 break;
189
190 for (;sa->levels < level; sa->levels++) {
191 p = alloc_node();
192 if (p == NULL)
193 return 0;
194 p[0] = sa->nodes;
195 sa->nodes = p;
196 }
197 if (sa->top < posn)
198 sa->top = posn;
199
200 p = sa->nodes;
201 for (level = sa->levels - 1; level > 0; level--) {
202 i = (posn >> (OPENSSL_SA_BLOCK_BITS * level)) & SA_BLOCK_MASK;
203 if (p[i] == NULL && (p[i] = alloc_node()) == NULL)
204 return 0;
205 p = p[i];
206 }
207 p += posn & SA_BLOCK_MASK;
208 if (val == NULL && *p != NULL)
209 sa->nelem--;
210 else if (val != NULL && *p == NULL)
211 sa->nelem++;
212 *p = val;
213 return 1;
214}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette