Coverage Report

Created: 2024-09-08 06:32

/src/openssl/crypto/bsearch.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stddef.h>
11
#include "internal/cryptlib.h"
12
13
const void *ossl_bsearch(const void *key, const void *base, int num,
14
                         int size, int (*cmp) (const void *, const void *),
15
                         int flags)
16
7.32M
{
17
7.32M
    const char *base_ = base;
18
7.32M
    int l, h, i = 0, c = 0;
19
7.32M
    const char *p = NULL;
20
21
7.32M
    if (num == 0)
22
0
        return NULL;
23
7.32M
    l = 0;
24
7.32M
    h = num;
25
52.7M
    while (l < h) {
26
52.4M
        i = (l + h) / 2;
27
52.4M
        p = &(base_[i * size]);
28
52.4M
        c = (*cmp) (key, p);
29
52.4M
        if (c < 0)
30
29.4M
            h = i;
31
22.9M
        else if (c > 0)
32
15.9M
            l = i + 1;
33
7.01M
        else
34
7.01M
            break;
35
52.4M
    }
36
7.32M
    if (c != 0 && !(flags & OSSL_BSEARCH_VALUE_ON_NOMATCH))
37
313k
        p = NULL;
38
7.01M
    else if (c == 0 && (flags & OSSL_BSEARCH_FIRST_VALUE_ON_MATCH)) {
39
0
        while (i > 0 && (*cmp) (key, &(base_[(i - 1) * size])) == 0)
40
0
            i--;
41
0
        p = &(base_[i * size]);
42
0
    }
43
7.32M
    return p;
44
7.32M
}