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