Coverage Report

Created: 2024-07-27 06:39

/src/openssl32/ssl/pqueue.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2005-2020 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 "ssl_local.h"
11
#include <openssl/bn.h>
12
13
struct pqueue_st {
14
    pitem *items;
15
    int count;
16
};
17
18
pitem *pitem_new(unsigned char *prio64be, void *data)
19
20.4k
{
20
20.4k
    pitem *item = OPENSSL_malloc(sizeof(*item));
21
22
20.4k
    if (item == NULL)
23
0
        return NULL;
24
25
20.4k
    memcpy(item->priority, prio64be, sizeof(item->priority));
26
20.4k
    item->data = data;
27
20.4k
    item->next = NULL;
28
20.4k
    return item;
29
20.4k
}
30
31
void pitem_free(pitem *item)
32
20.4k
{
33
20.4k
    OPENSSL_free(item);
34
20.4k
}
35
36
pqueue *pqueue_new(void)
37
115k
{
38
115k
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
39
40
115k
    return pq;
41
115k
}
42
43
void pqueue_free(pqueue *pq)
44
115k
{
45
115k
    OPENSSL_free(pq);
46
115k
}
47
48
pitem *pqueue_insert(pqueue *pq, pitem *item)
49
20.4k
{
50
20.4k
    pitem *curr, *next;
51
52
20.4k
    if (pq->items == NULL) {
53
7.54k
        pq->items = item;
54
7.54k
        return item;
55
7.54k
    }
56
57
12.8k
    for (curr = NULL, next = pq->items;
58
39.6k
         next != NULL; curr = next, next = next->next) {
59
        /*
60
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
61
         */
62
32.0k
        int cmp = memcmp(next->priority, item->priority, 8);
63
32.0k
        if (cmp > 0) {          /* next > item */
64
1.29k
            item->next = next;
65
66
1.29k
            if (curr == NULL)
67
409
                pq->items = item;
68
890
            else
69
890
                curr->next = item;
70
71
1.29k
            return item;
72
1.29k
        }
73
74
30.7k
        else if (cmp == 0)      /* duplicates not allowed */
75
4.04k
            return NULL;
76
32.0k
    }
77
78
7.55k
    item->next = NULL;
79
7.55k
    curr->next = item;
80
81
7.55k
    return item;
82
12.8k
}
83
84
pitem *pqueue_peek(pqueue *pq)
85
51.1k
{
86
51.1k
    return pq->items;
87
51.1k
}
88
89
pitem *pqueue_pop(pqueue *pq)
90
3.98M
{
91
3.98M
    pitem *item = pq->items;
92
93
3.98M
    if (pq->items != NULL)
94
16.3k
        pq->items = pq->items->next;
95
96
3.98M
    return item;
97
3.98M
}
98
99
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
100
11.7k
{
101
11.7k
    pitem *next;
102
11.7k
    pitem *found = NULL;
103
104
11.7k
    if (pq->items == NULL)
105
5.16k
        return NULL;
106
107
13.7k
    for (next = pq->items; next->next != NULL; next = next->next) {
108
8.86k
        if (memcmp(next->priority, prio64be, 8) == 0) {
109
1.67k
            found = next;
110
1.67k
            break;
111
1.67k
        }
112
8.86k
    }
113
114
    /* check the one last node */
115
6.54k
    if (memcmp(next->priority, prio64be, 8) == 0)
116
3.17k
        found = next;
117
118
6.54k
    if (!found)
119
3.36k
        return NULL;
120
121
3.17k
    return found;
122
6.54k
}
123
124
pitem *pqueue_iterator(pqueue *pq)
125
28.5k
{
126
28.5k
    return pqueue_peek(pq);
127
28.5k
}
128
129
pitem *pqueue_next(piterator *item)
130
28.6k
{
131
28.6k
    pitem *ret;
132
133
28.6k
    if (item == NULL || *item == NULL)
134
20.9k
        return NULL;
135
136
    /* *item != NULL */
137
7.67k
    ret = *item;
138
7.67k
    *item = (*item)->next;
139
140
7.67k
    return ret;
141
28.6k
}
142
143
size_t pqueue_size(pqueue *pq)
144
5.53k
{
145
5.53k
    pitem *item = pq->items;
146
5.53k
    size_t count = 0;
147
148
35.4k
    while (item != NULL) {
149
29.8k
        count++;
150
29.8k
        item = item->next;
151
29.8k
    }
152
5.53k
    return count;
153
5.53k
}