}
static int internal_find(OPENSSL_STACK *st, const void *data,
- int ret_val_options, int *pnum)
+ int ret_val_options, int *pnum_matched)
{
const void *r;
- int i;
+ int i, count = 0;
+ int *pnum = pnum_matched;
if (st == NULL || st->num == 0)
return -1;
+ if (pnum == NULL)
+ pnum = &count;
+
if (st->comp == NULL) {
for (i = 0; i < st->num; i++)
if (st->data[i] == data) {
- if (pnum != NULL)
- *pnum = 1;
+ *pnum = 1;
return i;
}
- if (pnum != NULL)
- *pnum = 0;
+ *pnum = 0;
return -1;
}
- if (!st->sorted) {
- if (st->num > 1)
- qsort(st->data, st->num, sizeof(void *), st->comp);
- st->sorted = 1; /* empty or single-element stack is considered sorted */
- }
if (data == NULL)
return -1;
- if (pnum != NULL)
+
+ if (!st->sorted) {
+ int res = -1;
+
+ for (i = 0; i < st->num; i++)
+ if (st->comp(&data, st->data + i) == 0) {
+ if (res == -1)
+ res = i;
+ ++*pnum;
+ /* Check if only one result is wanted and exit if so */
+ if (pnum_matched == NULL)
+ return i;
+ }
+ if (res == -1)
+ *pnum = 0;
+ return res;
+ }
+
+ if (pnum_matched != NULL)
ret_val_options |= OSSL_BSEARCH_FIRST_VALUE_ON_MATCH;
r = ossl_bsearch(&data, st->data, st->num, sizeof(void *), st->comp,
ret_val_options);
- if (pnum != NULL) {
+ if (pnum_matched != NULL) {
*pnum = 0;
if (r != NULL) {
const void **p = (const void **)r;