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