1*c3d6a26aSderaadt /* $OpenBSD: pqueue.c,v 1.5 2014/06/12 15:49:31 deraadt Exp $ */
24c60c45eSmiod /*
34c60c45eSmiod * DTLS implementation written by Nagendra Modadugu
44c60c45eSmiod * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
54c60c45eSmiod */
64c60c45eSmiod /* ====================================================================
74c60c45eSmiod * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
84c60c45eSmiod *
94c60c45eSmiod * Redistribution and use in source and binary forms, with or without
104c60c45eSmiod * modification, are permitted provided that the following conditions
114c60c45eSmiod * are met:
124c60c45eSmiod *
134c60c45eSmiod * 1. Redistributions of source code must retain the above copyright
144c60c45eSmiod * notice, this list of conditions and the following disclaimer.
154c60c45eSmiod *
164c60c45eSmiod * 2. Redistributions in binary form must reproduce the above copyright
174c60c45eSmiod * notice, this list of conditions and the following disclaimer in
184c60c45eSmiod * the documentation and/or other materials provided with the
194c60c45eSmiod * distribution.
204c60c45eSmiod *
214c60c45eSmiod * 3. All advertising materials mentioning features or use of this
224c60c45eSmiod * software must display the following acknowledgment:
234c60c45eSmiod * "This product includes software developed by the OpenSSL Project
244c60c45eSmiod * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
254c60c45eSmiod *
264c60c45eSmiod * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
274c60c45eSmiod * endorse or promote products derived from this software without
284c60c45eSmiod * prior written permission. For written permission, please contact
294c60c45eSmiod * openssl-core@OpenSSL.org.
304c60c45eSmiod *
314c60c45eSmiod * 5. Products derived from this software may not be called "OpenSSL"
324c60c45eSmiod * nor may "OpenSSL" appear in their names without prior written
334c60c45eSmiod * permission of the OpenSSL Project.
344c60c45eSmiod *
354c60c45eSmiod * 6. Redistributions of any form whatsoever must retain the following
364c60c45eSmiod * acknowledgment:
374c60c45eSmiod * "This product includes software developed by the OpenSSL Project
384c60c45eSmiod * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
394c60c45eSmiod *
404c60c45eSmiod * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
414c60c45eSmiod * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
424c60c45eSmiod * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
434c60c45eSmiod * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
444c60c45eSmiod * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
454c60c45eSmiod * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
464c60c45eSmiod * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
474c60c45eSmiod * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
484c60c45eSmiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
494c60c45eSmiod * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
504c60c45eSmiod * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
514c60c45eSmiod * OF THE POSSIBILITY OF SUCH DAMAGE.
524c60c45eSmiod * ====================================================================
534c60c45eSmiod *
544c60c45eSmiod * This product includes cryptographic software written by Eric Young
554c60c45eSmiod * (eay@cryptsoft.com). This product includes software written by Tim
564c60c45eSmiod * Hudson (tjh@cryptsoft.com).
574c60c45eSmiod *
584c60c45eSmiod */
594c60c45eSmiod
6046859c4aSjsing #include <stdlib.h>
614c60c45eSmiod #include <string.h>
6246859c4aSjsing
634c60c45eSmiod #include "pqueue.h"
644c60c45eSmiod
654c60c45eSmiod typedef struct _pqueue {
664c60c45eSmiod pitem *items;
674c60c45eSmiod int count;
684c60c45eSmiod } pqueue_s;
694c60c45eSmiod
704c60c45eSmiod pitem *
pitem_new(unsigned char * prio64be,void * data)714c60c45eSmiod pitem_new(unsigned char *prio64be, void *data)
724c60c45eSmiod {
734163340bSderaadt pitem *item = malloc(sizeof(pitem));
744c60c45eSmiod
754c60c45eSmiod if (item == NULL)
764c60c45eSmiod return NULL;
774c60c45eSmiod
784c60c45eSmiod memcpy(item->priority, prio64be, sizeof(item->priority));
794c60c45eSmiod
804c60c45eSmiod item->data = data;
814c60c45eSmiod item->next = NULL;
824c60c45eSmiod
834c60c45eSmiod return item;
844c60c45eSmiod }
854c60c45eSmiod
864c60c45eSmiod void
pitem_free(pitem * item)874c60c45eSmiod pitem_free(pitem *item)
884c60c45eSmiod {
894c60c45eSmiod free(item);
904c60c45eSmiod }
914c60c45eSmiod
924c60c45eSmiod pqueue_s *
pqueue_new(void)934c60c45eSmiod pqueue_new(void)
944c60c45eSmiod {
954163340bSderaadt return calloc(1, sizeof(pqueue_s));
964c60c45eSmiod }
974c60c45eSmiod
984c60c45eSmiod void
pqueue_free(pqueue_s * pq)994c60c45eSmiod pqueue_free(pqueue_s *pq)
1004c60c45eSmiod {
1014c60c45eSmiod free(pq);
1024c60c45eSmiod }
1034c60c45eSmiod
1044c60c45eSmiod pitem *
pqueue_insert(pqueue_s * pq,pitem * item)1054c60c45eSmiod pqueue_insert(pqueue_s *pq, pitem *item)
1064c60c45eSmiod {
1074c60c45eSmiod pitem *curr, *next;
1084c60c45eSmiod
1094c60c45eSmiod if (pq->items == NULL) {
1104c60c45eSmiod pq->items = item;
1114c60c45eSmiod return item;
1124c60c45eSmiod }
1134c60c45eSmiod
1144c60c45eSmiod for (curr = NULL, next = pq->items; next != NULL;
1154c60c45eSmiod curr = next, next = next->next) {
1164c60c45eSmiod /* we can compare 64-bit value in big-endian encoding
1174c60c45eSmiod * with memcmp:-) */
1184c60c45eSmiod int cmp = memcmp(next->priority, item->priority,
1194c60c45eSmiod sizeof(item->priority));
120b1e5b1d2Smiod if (cmp > 0) { /* next > item */
1214c60c45eSmiod item->next = next;
1224c60c45eSmiod
1234c60c45eSmiod if (curr == NULL)
1244c60c45eSmiod pq->items = item;
1254c60c45eSmiod else
1264c60c45eSmiod curr->next = item;
1274c60c45eSmiod
1284c60c45eSmiod return item;
1294c60c45eSmiod } else if (cmp == 0) /* duplicates not allowed */
1304c60c45eSmiod return NULL;
1314c60c45eSmiod }
1324c60c45eSmiod
1334c60c45eSmiod item->next = NULL;
1344c60c45eSmiod curr->next = item;
1354c60c45eSmiod
1364c60c45eSmiod return item;
1374c60c45eSmiod }
1384c60c45eSmiod
1394c60c45eSmiod pitem *
pqueue_peek(pqueue_s * pq)1404c60c45eSmiod pqueue_peek(pqueue_s *pq)
1414c60c45eSmiod {
1424c60c45eSmiod return pq->items;
1434c60c45eSmiod }
1444c60c45eSmiod
1454c60c45eSmiod pitem *
pqueue_pop(pqueue_s * pq)1464c60c45eSmiod pqueue_pop(pqueue_s *pq)
1474c60c45eSmiod {
1484c60c45eSmiod pitem *item = pq->items;
1494c60c45eSmiod
1504c60c45eSmiod if (pq->items != NULL)
1514c60c45eSmiod pq->items = pq->items->next;
1524c60c45eSmiod
1534c60c45eSmiod return item;
1544c60c45eSmiod }
1554c60c45eSmiod
1564c60c45eSmiod pitem *
pqueue_find(pqueue_s * pq,unsigned char * prio64be)1574c60c45eSmiod pqueue_find(pqueue_s *pq, unsigned char *prio64be)
1584c60c45eSmiod {
1594c60c45eSmiod pitem *next;
1604c60c45eSmiod
161b1e5b1d2Smiod for (next = pq->items; next != NULL; next = next->next)
1624c60c45eSmiod if (memcmp(next->priority, prio64be,
163b1e5b1d2Smiod sizeof(next->priority)) == 0)
164b1e5b1d2Smiod return next;
1654c60c45eSmiod
1664c60c45eSmiod return NULL;
1674c60c45eSmiod }
1684c60c45eSmiod
1694c60c45eSmiod pitem *
pqueue_iterator(pqueue_s * pq)1704c60c45eSmiod pqueue_iterator(pqueue_s *pq)
1714c60c45eSmiod {
1724c60c45eSmiod return pqueue_peek(pq);
1734c60c45eSmiod }
1744c60c45eSmiod
1754c60c45eSmiod pitem *
pqueue_next(pitem ** item)1764c60c45eSmiod pqueue_next(pitem **item)
1774c60c45eSmiod {
1784c60c45eSmiod pitem *ret;
1794c60c45eSmiod
1804c60c45eSmiod if (item == NULL || *item == NULL)
1814c60c45eSmiod return NULL;
1824c60c45eSmiod
1834c60c45eSmiod /* *item != NULL */
1844c60c45eSmiod ret = *item;
1854c60c45eSmiod *item = (*item)->next;
1864c60c45eSmiod
1874c60c45eSmiod return ret;
1884c60c45eSmiod }
1894c60c45eSmiod
1904c60c45eSmiod int
pqueue_size(pqueue_s * pq)1914c60c45eSmiod pqueue_size(pqueue_s *pq)
1924c60c45eSmiod {
1934c60c45eSmiod pitem *item = pq->items;
1944c60c45eSmiod int count = 0;
1954c60c45eSmiod
1964c60c45eSmiod while (item != NULL) {
1974c60c45eSmiod count++;
1984c60c45eSmiod item = item->next;
1994c60c45eSmiod }
2004c60c45eSmiod return count;
2014c60c45eSmiod }
202