Coverage Report

Created: 2018-08-29 13:53

/src/openssl/ssl/pqueue.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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_locl.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
0
{
20
0
    pitem *item = OPENSSL_malloc(sizeof(*item));
21
0
22
0
    if (item == NULL) {
23
0
        SSLerr(SSL_F_PITEM_NEW, ERR_R_MALLOC_FAILURE);
24
0
        return NULL;
25
0
    }
26
0
27
0
    memcpy(item->priority, prio64be, sizeof(item->priority));
28
0
    item->data = data;
29
0
    item->next = NULL;
30
0
    return item;
31
0
}
32
33
void pitem_free(pitem *item)
34
0
{
35
0
    OPENSSL_free(item);
36
0
}
37
38
pqueue *pqueue_new(void)
39
0
{
40
0
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
41
0
42
0
    if (pq == NULL)
43
0
        SSLerr(SSL_F_PQUEUE_NEW, ERR_R_MALLOC_FAILURE);
44
0
45
0
    return pq;
46
0
}
47
48
void pqueue_free(pqueue *pq)
49
0
{
50
0
    OPENSSL_free(pq);
51
0
}
52
53
pitem *pqueue_insert(pqueue *pq, pitem *item)
54
0
{
55
0
    pitem *curr, *next;
56
0
57
0
    if (pq->items == NULL) {
58
0
        pq->items = item;
59
0
        return item;
60
0
    }
61
0
62
0
    for (curr = NULL, next = pq->items;
63
0
         next != NULL; curr = next, next = next->next) {
64
0
        /*
65
0
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
66
0
         */
67
0
        int cmp = memcmp(next->priority, item->priority, 8);
68
0
        if (cmp > 0) {          /* next > item */
69
0
            item->next = next;
70
0
71
0
            if (curr == NULL)
72
0
                pq->items = item;
73
0
            else
74
0
                curr->next = item;
75
0
76
0
            return item;
77
0
        }
78
0
79
0
        else if (cmp == 0)      /* duplicates not allowed */
80
0
            return NULL;
81
0
    }
82
0
83
0
    item->next = NULL;
84
0
    curr->next = item;
85
0
86
0
    return item;
87
0
}
88
89
pitem *pqueue_peek(pqueue *pq)
90
0
{
91
0
    return pq->items;
92
0
}
93
94
pitem *pqueue_pop(pqueue *pq)
95
0
{
96
0
    pitem *item = pq->items;
97
0
98
0
    if (pq->items != NULL)
99
0
        pq->items = pq->items->next;
100
0
101
0
    return item;
102
0
}
103
104
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
105
0
{
106
0
    pitem *next;
107
0
    pitem *found = NULL;
108
0
109
0
    if (pq->items == NULL)
110
0
        return NULL;
111
0
112
0
    for (next = pq->items; next->next != NULL; next = next->next) {
113
0
        if (memcmp(next->priority, prio64be, 8) == 0) {
114
0
            found = next;
115
0
            break;
116
0
        }
117
0
    }
118
0
119
0
    /* check the one last node */
120
0
    if (memcmp(next->priority, prio64be, 8) == 0)
121
0
        found = next;
122
0
123
0
    if (!found)
124
0
        return NULL;
125
0
126
0
    return found;
127
0
}
128
129
pitem *pqueue_iterator(pqueue *pq)
130
0
{
131
0
    return pqueue_peek(pq);
132
0
}
133
134
pitem *pqueue_next(piterator *item)
135
0
{
136
0
    pitem *ret;
137
0
138
0
    if (item == NULL || *item == NULL)
139
0
        return NULL;
140
0
141
0
    /* *item != NULL */
142
0
    ret = *item;
143
0
    *item = (*item)->next;
144
0
145
0
    return ret;
146
0
}
147
148
size_t pqueue_size(pqueue *pq)
149
0
{
150
0
    pitem *item = pq->items;
151
0
    size_t count = 0;
152
0
153
0
    while (item != NULL) {
154
0
        count++;
155
0
        item = item->next;
156
0
    }
157
0
    return count;
158
0
}