sl@0
|
1 |
/* crypto/pqueue/pqueue.c */
|
sl@0
|
2 |
/*
|
sl@0
|
3 |
* DTLS implementation written by Nagendra Modadugu
|
sl@0
|
4 |
* (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
|
sl@0
|
5 |
*/
|
sl@0
|
6 |
/* ====================================================================
|
sl@0
|
7 |
* Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
10 |
* modification, are permitted provided that the following conditions
|
sl@0
|
11 |
* are met:
|
sl@0
|
12 |
*
|
sl@0
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
sl@0
|
14 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
17 |
* notice, this list of conditions and the following disclaimer in
|
sl@0
|
18 |
* the documentation and/or other materials provided with the
|
sl@0
|
19 |
* distribution.
|
sl@0
|
20 |
*
|
sl@0
|
21 |
* 3. All advertising materials mentioning features or use of this
|
sl@0
|
22 |
* software must display the following acknowledgment:
|
sl@0
|
23 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
24 |
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
sl@0
|
25 |
*
|
sl@0
|
26 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
sl@0
|
27 |
* endorse or promote products derived from this software without
|
sl@0
|
28 |
* prior written permission. For written permission, please contact
|
sl@0
|
29 |
* openssl-core@OpenSSL.org.
|
sl@0
|
30 |
*
|
sl@0
|
31 |
* 5. Products derived from this software may not be called "OpenSSL"
|
sl@0
|
32 |
* nor may "OpenSSL" appear in their names without prior written
|
sl@0
|
33 |
* permission of the OpenSSL Project.
|
sl@0
|
34 |
*
|
sl@0
|
35 |
* 6. Redistributions of any form whatsoever must retain the following
|
sl@0
|
36 |
* acknowledgment:
|
sl@0
|
37 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
38 |
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
sl@0
|
39 |
*
|
sl@0
|
40 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
sl@0
|
41 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
42 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
sl@0
|
43 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
sl@0
|
44 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
sl@0
|
45 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
sl@0
|
46 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
sl@0
|
47 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
sl@0
|
49 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
sl@0
|
50 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
sl@0
|
51 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
sl@0
|
52 |
* ====================================================================
|
sl@0
|
53 |
*
|
sl@0
|
54 |
* This product includes cryptographic software written by Eric Young
|
sl@0
|
55 |
* (eay@cryptsoft.com). This product includes software written by Tim
|
sl@0
|
56 |
* Hudson (tjh@cryptsoft.com).
|
sl@0
|
57 |
*
|
sl@0
|
58 |
*/
|
sl@0
|
59 |
|
sl@0
|
60 |
#include "cryptlib.h"
|
sl@0
|
61 |
#include <openssl/bn.h>
|
sl@0
|
62 |
#include <pqueue.h>
|
sl@0
|
63 |
|
sl@0
|
64 |
typedef struct _pqueue
|
sl@0
|
65 |
{
|
sl@0
|
66 |
pitem *items;
|
sl@0
|
67 |
int count;
|
sl@0
|
68 |
} pqueue_s;
|
sl@0
|
69 |
|
sl@0
|
70 |
EXPORT_C pitem *
|
sl@0
|
71 |
pitem_new(PQ_64BIT priority, void *data)
|
sl@0
|
72 |
{
|
sl@0
|
73 |
pitem *item = (pitem *) OPENSSL_malloc(sizeof(pitem));
|
sl@0
|
74 |
if (item == NULL) return NULL;
|
sl@0
|
75 |
|
sl@0
|
76 |
pq_64bit_init(&(item->priority));
|
sl@0
|
77 |
pq_64bit_assign(&item->priority, &priority);
|
sl@0
|
78 |
|
sl@0
|
79 |
item->data = data;
|
sl@0
|
80 |
item->next = NULL;
|
sl@0
|
81 |
|
sl@0
|
82 |
return item;
|
sl@0
|
83 |
}
|
sl@0
|
84 |
|
sl@0
|
85 |
EXPORT_C void
|
sl@0
|
86 |
pitem_free(pitem *item)
|
sl@0
|
87 |
{
|
sl@0
|
88 |
if (item == NULL) return;
|
sl@0
|
89 |
|
sl@0
|
90 |
pq_64bit_free(&(item->priority));
|
sl@0
|
91 |
OPENSSL_free(item);
|
sl@0
|
92 |
}
|
sl@0
|
93 |
|
sl@0
|
94 |
EXPORT_C pqueue_s *
|
sl@0
|
95 |
pqueue_new()
|
sl@0
|
96 |
{
|
sl@0
|
97 |
pqueue_s *pq = (pqueue_s *) OPENSSL_malloc(sizeof(pqueue_s));
|
sl@0
|
98 |
if (pq == NULL) return NULL;
|
sl@0
|
99 |
|
sl@0
|
100 |
memset(pq, 0x00, sizeof(pqueue_s));
|
sl@0
|
101 |
return pq;
|
sl@0
|
102 |
}
|
sl@0
|
103 |
|
sl@0
|
104 |
EXPORT_C void
|
sl@0
|
105 |
pqueue_free(pqueue_s *pq)
|
sl@0
|
106 |
{
|
sl@0
|
107 |
if (pq == NULL) return;
|
sl@0
|
108 |
|
sl@0
|
109 |
OPENSSL_free(pq);
|
sl@0
|
110 |
}
|
sl@0
|
111 |
|
sl@0
|
112 |
EXPORT_C pitem *
|
sl@0
|
113 |
pqueue_insert(pqueue_s *pq, pitem *item)
|
sl@0
|
114 |
{
|
sl@0
|
115 |
pitem *curr, *next;
|
sl@0
|
116 |
|
sl@0
|
117 |
if (pq->items == NULL)
|
sl@0
|
118 |
{
|
sl@0
|
119 |
pq->items = item;
|
sl@0
|
120 |
return item;
|
sl@0
|
121 |
}
|
sl@0
|
122 |
|
sl@0
|
123 |
for(curr = NULL, next = pq->items;
|
sl@0
|
124 |
next != NULL;
|
sl@0
|
125 |
curr = next, next = next->next)
|
sl@0
|
126 |
{
|
sl@0
|
127 |
if (pq_64bit_gt(&(next->priority), &(item->priority)))
|
sl@0
|
128 |
{
|
sl@0
|
129 |
item->next = next;
|
sl@0
|
130 |
|
sl@0
|
131 |
if (curr == NULL)
|
sl@0
|
132 |
pq->items = item;
|
sl@0
|
133 |
else
|
sl@0
|
134 |
curr->next = item;
|
sl@0
|
135 |
|
sl@0
|
136 |
return item;
|
sl@0
|
137 |
}
|
sl@0
|
138 |
/* duplicates not allowed */
|
sl@0
|
139 |
if (pq_64bit_eq(&(item->priority), &(next->priority)))
|
sl@0
|
140 |
return NULL;
|
sl@0
|
141 |
}
|
sl@0
|
142 |
|
sl@0
|
143 |
item->next = NULL;
|
sl@0
|
144 |
curr->next = item;
|
sl@0
|
145 |
|
sl@0
|
146 |
return item;
|
sl@0
|
147 |
}
|
sl@0
|
148 |
|
sl@0
|
149 |
EXPORT_C pitem *
|
sl@0
|
150 |
pqueue_peek(pqueue_s *pq)
|
sl@0
|
151 |
{
|
sl@0
|
152 |
return pq->items;
|
sl@0
|
153 |
}
|
sl@0
|
154 |
|
sl@0
|
155 |
EXPORT_C pitem *
|
sl@0
|
156 |
pqueue_pop(pqueue_s *pq)
|
sl@0
|
157 |
{
|
sl@0
|
158 |
pitem *item = pq->items;
|
sl@0
|
159 |
|
sl@0
|
160 |
if (pq->items != NULL)
|
sl@0
|
161 |
pq->items = pq->items->next;
|
sl@0
|
162 |
|
sl@0
|
163 |
return item;
|
sl@0
|
164 |
}
|
sl@0
|
165 |
|
sl@0
|
166 |
EXPORT_C pitem *
|
sl@0
|
167 |
pqueue_find(pqueue_s *pq, PQ_64BIT priority)
|
sl@0
|
168 |
{
|
sl@0
|
169 |
pitem *next, *prev = NULL;
|
sl@0
|
170 |
pitem *found = NULL;
|
sl@0
|
171 |
|
sl@0
|
172 |
if ( pq->items == NULL)
|
sl@0
|
173 |
return NULL;
|
sl@0
|
174 |
|
sl@0
|
175 |
for ( next = pq->items; next->next != NULL;
|
sl@0
|
176 |
prev = next, next = next->next)
|
sl@0
|
177 |
{
|
sl@0
|
178 |
if ( pq_64bit_eq(&(next->priority), &priority))
|
sl@0
|
179 |
{
|
sl@0
|
180 |
found = next;
|
sl@0
|
181 |
break;
|
sl@0
|
182 |
}
|
sl@0
|
183 |
}
|
sl@0
|
184 |
|
sl@0
|
185 |
/* check the one last node */
|
sl@0
|
186 |
if ( pq_64bit_eq(&(next->priority), &priority))
|
sl@0
|
187 |
found = next;
|
sl@0
|
188 |
|
sl@0
|
189 |
if ( ! found)
|
sl@0
|
190 |
return NULL;
|
sl@0
|
191 |
|
sl@0
|
192 |
#if 0 /* find works in peek mode */
|
sl@0
|
193 |
if ( prev == NULL)
|
sl@0
|
194 |
pq->items = next->next;
|
sl@0
|
195 |
else
|
sl@0
|
196 |
prev->next = next->next;
|
sl@0
|
197 |
#endif
|
sl@0
|
198 |
|
sl@0
|
199 |
return found;
|
sl@0
|
200 |
}
|
sl@0
|
201 |
|
sl@0
|
202 |
#if PQ_64BIT_IS_INTEGER
|
sl@0
|
203 |
EXPORT_C void
|
sl@0
|
204 |
pqueue_print(pqueue_s *pq)
|
sl@0
|
205 |
{
|
sl@0
|
206 |
pitem *item = pq->items;
|
sl@0
|
207 |
|
sl@0
|
208 |
while(item != NULL)
|
sl@0
|
209 |
{
|
sl@0
|
210 |
printf("item\t" PQ_64BIT_PRINT "\n", item->priority);
|
sl@0
|
211 |
item = item->next;
|
sl@0
|
212 |
}
|
sl@0
|
213 |
}
|
sl@0
|
214 |
#endif
|
sl@0
|
215 |
|
sl@0
|
216 |
EXPORT_C pitem *
|
sl@0
|
217 |
pqueue_iterator(pqueue_s *pq)
|
sl@0
|
218 |
{
|
sl@0
|
219 |
return pqueue_peek(pq);
|
sl@0
|
220 |
}
|
sl@0
|
221 |
|
sl@0
|
222 |
EXPORT_C pitem *
|
sl@0
|
223 |
pqueue_next(pitem **item)
|
sl@0
|
224 |
{
|
sl@0
|
225 |
pitem *ret;
|
sl@0
|
226 |
|
sl@0
|
227 |
if ( item == NULL || *item == NULL)
|
sl@0
|
228 |
return NULL;
|
sl@0
|
229 |
|
sl@0
|
230 |
|
sl@0
|
231 |
/* *item != NULL */
|
sl@0
|
232 |
ret = *item;
|
sl@0
|
233 |
*item = (*item)->next;
|
sl@0
|
234 |
|
sl@0
|
235 |
return ret;
|
sl@0
|
236 |
}
|