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