blob: 7e0ced9909a247e8f8399bdb44116ea456c35fa2 [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Rich Salz846e33c2016-05-17 14:18:30 -04002 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
Ben Laurie36d16f82005-04-26 16:02:40 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * 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
Ben Laurie36d16f82005-04-26 16:02:40 +00008 */
9
Rich Salzcf2cede2016-01-22 14:54:01 -050010#include "ssl_locl.h"
Dr. Stephen Henson6c617262005-04-27 16:27:14 +000011#include <openssl/bn.h>
Ben Laurie36d16f82005-04-26 16:02:40 +000012
Rich Salzcf2cede2016-01-22 14:54:01 -050013struct pqueue_st {
Matt Caswell0f113f32015-01-22 03:40:55 +000014 pitem *items;
15 int count;
Rich Salzcf2cede2016-01-22 14:54:01 -050016};
Ben Laurie36d16f82005-04-26 16:02:40 +000017
Matt Caswell0f113f32015-01-22 03:40:55 +000018pitem *pitem_new(unsigned char *prio64be, void *data)
19{
Rich Salzb4faea52015-05-01 23:10:31 -040020 pitem *item = OPENSSL_malloc(sizeof(*item));
Matt Caswell0f113f32015-01-22 03:40:55 +000021 if (item == NULL)
22 return NULL;
Ben Laurie36d16f82005-04-26 16:02:40 +000023
Matt Caswell0f113f32015-01-22 03:40:55 +000024 memcpy(item->priority, prio64be, sizeof(item->priority));
Richard Levitte188b0572005-05-30 22:34:37 +000025
Matt Caswell0f113f32015-01-22 03:40:55 +000026 item->data = data;
27 item->next = NULL;
Ben Laurie36d16f82005-04-26 16:02:40 +000028
Matt Caswell0f113f32015-01-22 03:40:55 +000029 return item;
30}
Ben Laurie36d16f82005-04-26 16:02:40 +000031
Matt Caswell0f113f32015-01-22 03:40:55 +000032void pitem_free(pitem *item)
33{
Matt Caswell0f113f32015-01-22 03:40:55 +000034 OPENSSL_free(item);
35}
Ben Laurie36d16f82005-04-26 16:02:40 +000036
Rich Salzcf2cede2016-01-22 14:54:01 -050037pqueue *pqueue_new()
Matt Caswell0f113f32015-01-22 03:40:55 +000038{
Rich Salzcf2cede2016-01-22 14:54:01 -050039 pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
Ben Laurie36d16f82005-04-26 16:02:40 +000040
Matt Caswell0f113f32015-01-22 03:40:55 +000041 return pq;
42}
Ben Laurie36d16f82005-04-26 16:02:40 +000043
Rich Salzcf2cede2016-01-22 14:54:01 -050044void pqueue_free(pqueue *pq)
Matt Caswell0f113f32015-01-22 03:40:55 +000045{
Matt Caswell0f113f32015-01-22 03:40:55 +000046 OPENSSL_free(pq);
47}
Ben Laurie36d16f82005-04-26 16:02:40 +000048
Rich Salzcf2cede2016-01-22 14:54:01 -050049pitem *pqueue_insert(pqueue *pq, pitem *item)
Matt Caswell0f113f32015-01-22 03:40:55 +000050{
51 pitem *curr, *next;
Ben Laurie36d16f82005-04-26 16:02:40 +000052
Matt Caswell0f113f32015-01-22 03:40:55 +000053 if (pq->items == NULL) {
54 pq->items = item;
55 return item;
56 }
Ben Laurie36d16f82005-04-26 16:02:40 +000057
Matt Caswell0f113f32015-01-22 03:40:55 +000058 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;
Ben Laurie36d16f82005-04-26 16:02:40 +000066
Matt Caswell0f113f32015-01-22 03:40:55 +000067 if (curr == NULL)
68 pq->items = item;
69 else
70 curr->next = item;
Ben Laurie36d16f82005-04-26 16:02:40 +000071
Matt Caswell0f113f32015-01-22 03:40:55 +000072 return item;
73 }
Ben Laurie36d16f82005-04-26 16:02:40 +000074
Matt Caswell0f113f32015-01-22 03:40:55 +000075 else if (cmp == 0) /* duplicates not allowed */
76 return NULL;
77 }
Ben Laurie36d16f82005-04-26 16:02:40 +000078
Matt Caswell0f113f32015-01-22 03:40:55 +000079 item->next = NULL;
80 curr->next = item;
Ben Laurie36d16f82005-04-26 16:02:40 +000081
Matt Caswell0f113f32015-01-22 03:40:55 +000082 return item;
83}
Ben Laurie36d16f82005-04-26 16:02:40 +000084
Rich Salzcf2cede2016-01-22 14:54:01 -050085pitem *pqueue_peek(pqueue *pq)
Matt Caswell0f113f32015-01-22 03:40:55 +000086{
87 return pq->items;
88}
Ben Laurie36d16f82005-04-26 16:02:40 +000089
Rich Salzcf2cede2016-01-22 14:54:01 -050090pitem *pqueue_pop(pqueue *pq)
Matt Caswell0f113f32015-01-22 03:40:55 +000091{
92 pitem *item = pq->items;
Ben Laurie36d16f82005-04-26 16:02:40 +000093
Matt Caswell0f113f32015-01-22 03:40:55 +000094 if (pq->items != NULL)
95 pq->items = pq->items->next;
Ben Laurie36d16f82005-04-26 16:02:40 +000096
Matt Caswell0f113f32015-01-22 03:40:55 +000097 return item;
98}
Ben Laurie36d16f82005-04-26 16:02:40 +000099
Rich Salzcf2cede2016-01-22 14:54:01 -0500100pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
Matt Caswell0f113f32015-01-22 03:40:55 +0000101{
102 pitem *next;
103 pitem *found = NULL;
Ben Laurie36d16f82005-04-26 16:02:40 +0000104
Matt Caswell0f113f32015-01-22 03:40:55 +0000105 if (pq->items == NULL)
106 return NULL;
Ben Laurie36d16f82005-04-26 16:02:40 +0000107
Matt Caswell0f113f32015-01-22 03:40:55 +0000108 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 }
Ben Laurie36d16f82005-04-26 16:02:40 +0000114
Matt Caswell0f113f32015-01-22 03:40:55 +0000115 /* check the one last node */
116 if (memcmp(next->priority, prio64be, 8) == 0)
117 found = next;
118
119 if (!found)
120 return NULL;
121
Matt Caswell0f113f32015-01-22 03:40:55 +0000122 return found;
123}
Ben Laurie36d16f82005-04-26 16:02:40 +0000124
Rich Salzcf2cede2016-01-22 14:54:01 -0500125pitem *pqueue_iterator(pqueue *pq)
Matt Caswell0f113f32015-01-22 03:40:55 +0000126{
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
Matt Caswell8b0e9342016-10-06 19:17:54 +0100144size_t pqueue_size(pqueue *pq)
Matt Caswell0f113f32015-01-22 03:40:55 +0000145{
146 pitem *item = pq->items;
Matt Caswellc42a78c2016-11-04 10:26:57 +0000147 size_t count = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000148
149 while (item != NULL) {
150 count++;
151 item = item->next;
152 }
153 return count;
Dr. Stephen Henson8d932f62009-05-16 16:18:19 +0000154}