Coverage Report

Created: 2025-08-11 07:04

/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
71.8k
{
20
71.8k
    pitem *item = OPENSSL_malloc(sizeof(*item));
21
22
71.8k
    if (item == NULL)
23
0
        return NULL;
24
25
71.8k
    memcpy(item->priority, prio64be, sizeof(item->priority));
26
71.8k
    item->data = data;
27
71.8k
    item->next = NULL;
28
71.8k
    return item;
29
71.8k
}
30
31
void pitem_free(pitem *item)
32
71.8k
{
33
71.8k
    OPENSSL_free(item);
34
71.8k
}
35
36
pqueue *pqueue_new(void)
37
415k
{
38
415k
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
39
40
415k
    return pq;
41
415k
}
42
43
void pqueue_free(pqueue *pq)
44
415k
{
45
415k
    OPENSSL_free(pq);
46
415k
}
47
48
pitem *pqueue_insert(pqueue *pq, pitem *item)
49
71.8k
{
50
71.8k
    pitem *curr, *next;
51
52
71.8k
    if (pq->items == NULL) {
53
35.5k
        pq->items = item;
54
35.5k
        return item;
55
35.5k
    }
56
57
36.3k
    for (curr = NULL, next = pq->items;
58
116k
         next != NULL; curr = next, next = next->next) {
59
        /*
60
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
61
         */
62
94.2k
        int cmp = memcmp(next->priority, item->priority, 8);
63
94.2k
        if (cmp > 0) {          /* next > item */
64
4.30k
            item->next = next;
65
66
4.30k
            if (curr == NULL)
67
1.87k
                pq->items = item;
68
2.43k
            else
69
2.43k
                curr->next = item;
70
71
4.30k
            return item;
72
4.30k
        }
73
74
89.9k
        else if (cmp == 0)      /* duplicates not allowed */
75
10.0k
            return NULL;
76
94.2k
    }
77
78
22.0k
    item->next = NULL;
79
22.0k
    curr->next = item;
80
81
22.0k
    return item;
82
36.3k
}
83
84
pitem *pqueue_peek(pqueue *pq)
85
200k
{
86
200k
    return pq->items;
87
200k
}
88
89
pitem *pqueue_pop(pqueue *pq)
90
20.8M
{
91
20.8M
    pitem *item = pq->items;
92
93
20.8M
    if (pq->items != NULL)
94
61.8k
        pq->items = pq->items->next;
95
96
20.8M
    return item;
97
20.8M
}
98
99
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
100
63.0k
{
101
63.0k
    pitem *next;
102
63.0k
    pitem *found = NULL;
103
104
63.0k
    if (pq->items == NULL)
105
25.3k
        return NULL;
106
107
76.2k
    for (next = pq->items; next->next != NULL; next = next->next) {
108
42.9k
        if (memcmp(next->priority, prio64be, 8) == 0) {
109
4.49k
            found = next;
110
4.49k
            break;
111
4.49k
        }
112
42.9k
    }
113
114
    /* check the one last node */
115
37.6k
    if (memcmp(next->priority, prio64be, 8) == 0)
116
24.4k
        found = next;
117
118
37.6k
    if (!found)
119
13.2k
        return NULL;
120
121
24.4k
    return found;
122
37.6k
}
123
124
pitem *pqueue_iterator(pqueue *pq)
125
118k
{
126
118k
    return pqueue_peek(pq);
127
118k
}
128
129
pitem *pqueue_next(piterator *item)
130
118k
{
131
118k
    pitem *ret;
132
133
118k
    if (item == NULL || *item == NULL)
134
70.3k
        return NULL;
135
136
    /* *item != NULL */
137
48.1k
    ret = *item;
138
48.1k
    *item = (*item)->next;
139
140
48.1k
    return ret;
141
118k
}
142
143
size_t pqueue_size(pqueue *pq)
144
14.2k
{
145
14.2k
    pitem *item = pq->items;
146
14.2k
    size_t count = 0;
147
148
147k
    while (item != NULL) {
149
132k
        count++;
150
132k
        item = item->next;
151
132k
    }
152
14.2k
    return count;
153
14.2k
}