blob: 548a7a443d914545cfaba504a7583f388d8df2f8 [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Richard Levitte28428132018-04-17 15:18:40 +02002 * Copyright 2005-2018 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));
Rich Salzcdb10ba2018-04-03 11:31:16 -040021
22 if (item == NULL) {
23 SSLerr(SSL_F_PITEM_NEW, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +000024 return NULL;
Rich Salzcdb10ba2018-04-03 11:31:16 -040025 }
Ben Laurie36d16f82005-04-26 16:02:40 +000026
Matt Caswell0f113f32015-01-22 03:40:55 +000027 memcpy(item->priority, prio64be, sizeof(item->priority));
Matt Caswell0f113f32015-01-22 03:40:55 +000028 item->data = data;
29 item->next = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000030 return item;
31}
Ben Laurie36d16f82005-04-26 16:02:40 +000032
Matt Caswell0f113f32015-01-22 03:40:55 +000033void pitem_free(pitem *item)
34{
Matt Caswell0f113f32015-01-22 03:40:55 +000035 OPENSSL_free(item);
36}
Ben Laurie36d16f82005-04-26 16:02:40 +000037
Kurt Roeckx3cb7c5c2018-05-09 17:09:50 +020038pqueue *pqueue_new(void)
Matt Caswell0f113f32015-01-22 03:40:55 +000039{
Rich Salzcf2cede2016-01-22 14:54:01 -050040 pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
Ben Laurie36d16f82005-04-26 16:02:40 +000041
Rich Salzcdb10ba2018-04-03 11:31:16 -040042 if (pq == NULL)
43 SSLerr(SSL_F_PQUEUE_NEW, ERR_R_MALLOC_FAILURE);
44
Matt Caswell0f113f32015-01-22 03:40:55 +000045 return pq;
46}
Ben Laurie36d16f82005-04-26 16:02:40 +000047
Rich Salzcf2cede2016-01-22 14:54:01 -050048void pqueue_free(pqueue *pq)
Matt Caswell0f113f32015-01-22 03:40:55 +000049{
Matt Caswell0f113f32015-01-22 03:40:55 +000050 OPENSSL_free(pq);
51}
Ben Laurie36d16f82005-04-26 16:02:40 +000052
Rich Salzcf2cede2016-01-22 14:54:01 -050053pitem *pqueue_insert(pqueue *pq, pitem *item)
Matt Caswell0f113f32015-01-22 03:40:55 +000054{
55 pitem *curr, *next;
Ben Laurie36d16f82005-04-26 16:02:40 +000056
Matt Caswell0f113f32015-01-22 03:40:55 +000057 if (pq->items == NULL) {
58 pq->items = item;
59 return item;
60 }
Ben Laurie36d16f82005-04-26 16:02:40 +000061
Matt Caswell0f113f32015-01-22 03:40:55 +000062 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;
Ben Laurie36d16f82005-04-26 16:02:40 +000070
Matt Caswell0f113f32015-01-22 03:40:55 +000071 if (curr == NULL)
72 pq->items = item;
73 else
74 curr->next = item;
Ben Laurie36d16f82005-04-26 16:02:40 +000075
Matt Caswell0f113f32015-01-22 03:40:55 +000076 return item;
77 }
Ben Laurie36d16f82005-04-26 16:02:40 +000078
Matt Caswell0f113f32015-01-22 03:40:55 +000079 else if (cmp == 0) /* duplicates not allowed */
80 return NULL;
81 }
Ben Laurie36d16f82005-04-26 16:02:40 +000082
Matt Caswell0f113f32015-01-22 03:40:55 +000083 item->next = NULL;
84 curr->next = item;
Ben Laurie36d16f82005-04-26 16:02:40 +000085
Matt Caswell0f113f32015-01-22 03:40:55 +000086 return item;
87}
Ben Laurie36d16f82005-04-26 16:02:40 +000088
Rich Salzcf2cede2016-01-22 14:54:01 -050089pitem *pqueue_peek(pqueue *pq)
Matt Caswell0f113f32015-01-22 03:40:55 +000090{
91 return pq->items;
92}
Ben Laurie36d16f82005-04-26 16:02:40 +000093
Rich Salzcf2cede2016-01-22 14:54:01 -050094pitem *pqueue_pop(pqueue *pq)
Matt Caswell0f113f32015-01-22 03:40:55 +000095{
96 pitem *item = pq->items;
Ben Laurie36d16f82005-04-26 16:02:40 +000097
Matt Caswell0f113f32015-01-22 03:40:55 +000098 if (pq->items != NULL)
99 pq->items = pq->items->next;
Ben Laurie36d16f82005-04-26 16:02:40 +0000100
Matt Caswell0f113f32015-01-22 03:40:55 +0000101 return item;
102}
Ben Laurie36d16f82005-04-26 16:02:40 +0000103
Rich Salzcf2cede2016-01-22 14:54:01 -0500104pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
Matt Caswell0f113f32015-01-22 03:40:55 +0000105{
106 pitem *next;
107 pitem *found = NULL;
Ben Laurie36d16f82005-04-26 16:02:40 +0000108
Matt Caswell0f113f32015-01-22 03:40:55 +0000109 if (pq->items == NULL)
110 return NULL;
Ben Laurie36d16f82005-04-26 16:02:40 +0000111
Matt Caswell0f113f32015-01-22 03:40:55 +0000112 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 }
Ben Laurie36d16f82005-04-26 16:02:40 +0000118
Matt Caswell0f113f32015-01-22 03:40:55 +0000119 /* check the one last node */
120 if (memcmp(next->priority, prio64be, 8) == 0)
121 found = next;
122
123 if (!found)
124 return NULL;
125
Matt Caswell0f113f32015-01-22 03:40:55 +0000126 return found;
127}
Ben Laurie36d16f82005-04-26 16:02:40 +0000128
Rich Salzcf2cede2016-01-22 14:54:01 -0500129pitem *pqueue_iterator(pqueue *pq)
Matt Caswell0f113f32015-01-22 03:40:55 +0000130{
131 return pqueue_peek(pq);
132}
133
Xiaoyin Liu3c051802017-07-29 19:20:47 -0400134pitem *pqueue_next(piterator *item)
Matt Caswell0f113f32015-01-22 03:40:55 +0000135{
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
Matt Caswell8b0e9342016-10-06 19:17:54 +0100148size_t pqueue_size(pqueue *pq)
Matt Caswell0f113f32015-01-22 03:40:55 +0000149{
150 pitem *item = pq->items;
Matt Caswellc42a78c2016-11-04 10:26:57 +0000151 size_t count = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000152
153 while (item != NULL) {
154 count++;
155 item = item->next;
156 }
157 return count;
Dr. Stephen Henson8d932f62009-05-16 16:18:19 +0000158}