Use build.info, not ifdef for crypto modules
[openssl.git] / crypto / sparse_array.c
index 8b56b257cf62c92ba4fe817a161fcc764ff08fce..9d444739f53ab8c4098a14fab58ee03c4007b37a 100644 (file)
@@ -9,10 +9,11 @@
  */
 
 #include <openssl/crypto.h>
-#include "internal/sparse_array.h"
+#include <openssl/bn.h>
+#include "crypto/sparse_array.h"
 
 /*
- * How many bits are used to index each level in the tree structre?
+ * How many bits are used to index each level in the tree structure?
  * This setting determines the number of pointers stored in each node of the
  * tree used to represent the sparse array.  Having more pointers reduces the
  * depth of the tree but potentially wastes more memory.  That is, this is a
@@ -28,7 +29,7 @@
  * at a cost in time.
  *
  * The library builder is also permitted to define other sizes in the closed
- * interval [2, sizeof(size_t) * 8].
+ * interval [2, sizeof(ossl_uintmax_t) * 8].
  */
 #ifndef OPENSSL_SA_BLOCK_BITS
 # ifdef OPENSSL_SMALL_FOOTPRINT
 # else
 #  define OPENSSL_SA_BLOCK_BITS           12
 # endif
-#elif OPENSSL_SA_BLOCK_BITS < 2 || OPENSSL_SA_BLOCK_BITS > BN_BITS2
+#elif OPENSSL_SA_BLOCK_BITS < 2 || OPENSSL_SA_BLOCK_BITS > (BN_BITS2 - 1)
 # error OPENSSL_SA_BLOCK_BITS is out of range
 #endif
 
 /*
  * From the number of bits, work out:
  *    the number of pointers in a tree node;
- *    a bit mask to quickly extra an index and
+ *    a bit mask to quickly extract an index and
  *    the maximum depth of the tree structure.
   */
 #define SA_BLOCK_MAX            (1 << OPENSSL_SA_BLOCK_BITS)
 #define SA_BLOCK_MASK           (SA_BLOCK_MAX - 1)
-#define SA_BLOCK_MAX_LEVELS     (((int)sizeof(size_t) * 8 \
+#define SA_BLOCK_MAX_LEVELS     (((int)sizeof(ossl_uintmax_t) * 8 \
                                   + OPENSSL_SA_BLOCK_BITS - 1) \
                                  / OPENSSL_SA_BLOCK_BITS)
 
 struct sparse_array_st {
     int levels;
-    size_t top;
+    ossl_uintmax_t top;
     size_t nelem;
     void **nodes;
 };
@@ -67,10 +68,11 @@ OPENSSL_SA *OPENSSL_SA_new(void)
 }
 
 static void sa_doall(const OPENSSL_SA *sa, void (*node)(void **),
-                     void (*leaf)(void *, void *), void *arg)
+                     void (*leaf)(ossl_uintmax_t, void *, void *), void *arg)
 {
     int i[SA_BLOCK_MAX_LEVELS];
     void *nodes[SA_BLOCK_MAX_LEVELS];
+    ossl_uintmax_t idx = 0;
     int l = 0;
 
     i[0] = 0;
@@ -83,14 +85,17 @@ static void sa_doall(const OPENSSL_SA *sa, void (*node)(void **),
             if (p != NULL && node != NULL)
                 (*node)(p);
             l--;
+            idx >>= OPENSSL_SA_BLOCK_BITS;
         } else {
             i[l] = n + 1;
             if (p != NULL && p[n] != NULL) {
+                idx = (idx & ~SA_BLOCK_MASK) | n;
                 if (l < sa->levels - 1) {
                     i[++l] = 0;
                     nodes[l] = p[n];
+                    idx <<= OPENSSL_SA_BLOCK_BITS;
                 } else if (leaf != NULL) {
-                    (*leaf)(p[n], arg);
+                    (*leaf)(idx, p[n], arg);
                 }
             }
         }
@@ -102,7 +107,7 @@ static void sa_free_node(void **p)
     OPENSSL_free(p);
 }
 
-static void sa_free_leaf(void *p, void *arg)
+static void sa_free_leaf(ossl_uintmax_t n, void *p, void *arg)
 {
     OPENSSL_free(p);
 }
@@ -121,15 +126,16 @@ void OPENSSL_SA_free_leaves(OPENSSL_SA *sa)
 
 /* Wrap this in a structure to avoid compiler warnings */
 struct trampoline_st {
-    void (*func)(void *);
+    void (*func)(ossl_uintmax_t, void *);
 };
 
-static void trampoline(void *l, void *arg)
+static void trampoline(ossl_uintmax_t n, void *l, void *arg)
 {
-    ((const struct trampoline_st *)arg)->func(l);
+    ((const struct trampoline_st *)arg)->func(n, l);
 }
 
-void OPENSSL_SA_doall(const OPENSSL_SA *sa, void (*leaf)(void *))
+void OPENSSL_SA_doall(const OPENSSL_SA *sa, void (*leaf)(ossl_uintmax_t,
+                                                         void *))
 {
     struct trampoline_st tramp;
 
@@ -138,7 +144,8 @@ void OPENSSL_SA_doall(const OPENSSL_SA *sa, void (*leaf)(void *))
         sa_doall(sa, NULL, &trampoline, &tramp);
 }
 
-void OPENSSL_SA_doall_arg(const OPENSSL_SA *sa, void (*leaf)(void *, void *),
+void OPENSSL_SA_doall_arg(const OPENSSL_SA *sa,
+                          void (*leaf)(ossl_uintmax_t, void *, void *),
                           void *arg)
 {
     if (sa != NULL)
@@ -150,7 +157,7 @@ size_t OPENSSL_SA_num(const OPENSSL_SA *sa)
     return sa == NULL ? 0 : sa->nelem;
 }
 
-void *OPENSSL_SA_get(const OPENSSL_SA *sa, size_t n)
+void *OPENSSL_SA_get(const OPENSSL_SA *sa, ossl_uintmax_t n)
 {
     int level;
     void **p, *r = NULL;
@@ -173,16 +180,16 @@ static ossl_inline void **alloc_node(void)
     return OPENSSL_zalloc(SA_BLOCK_MAX * sizeof(void *));
 }
 
-int OPENSSL_SA_set(OPENSSL_SA *sa, size_t posn, void *val)
+int OPENSSL_SA_set(OPENSSL_SA *sa, ossl_uintmax_t posn, void *val)
 {
     int i, level = 1;
-    size_t n = posn;
+    ossl_uintmax_t n = posn;
     void **p;
 
     if (sa == NULL)
         return 0;
 
-    for (level = 1; level <= SA_BLOCK_MAX_LEVELS; level++)
+    for (level = 1; level < SA_BLOCK_MAX_LEVELS; level++)
         if ((n >>= OPENSSL_SA_BLOCK_BITS) == 0)
             break;