Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1 | /* |
Richard Levitte | 2842813 | 2018-04-17 15:18:40 +0200 | [diff] [blame] | 2 | * Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved. |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 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 |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 10 | #include "ssl_locl.h" |
Dr. Stephen Henson | 6c61726 | 2005-04-27 16:27:14 +0000 | [diff] [blame] | 11 | #include <openssl/bn.h> |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 12 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 13 | struct pqueue_st { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 14 | pitem *items; |
| 15 | int count; |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 16 | }; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 17 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 18 | pitem *pitem_new(unsigned char *prio64be, void *data) |
| 19 | { |
Rich Salz | b4faea5 | 2015-05-01 23:10:31 -0400 | [diff] [blame] | 20 | pitem *item = OPENSSL_malloc(sizeof(*item)); |
Rich Salz | cdb10ba | 2018-04-03 11:31:16 -0400 | [diff] [blame] | 21 | |
| 22 | if (item == NULL) { |
| 23 | SSLerr(SSL_F_PITEM_NEW, ERR_R_MALLOC_FAILURE); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 24 | return NULL; |
Rich Salz | cdb10ba | 2018-04-03 11:31:16 -0400 | [diff] [blame] | 25 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 26 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 27 | memcpy(item->priority, prio64be, sizeof(item->priority)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 28 | item->data = data; |
| 29 | item->next = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 30 | return item; |
| 31 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 32 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 33 | void pitem_free(pitem *item) |
| 34 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 35 | OPENSSL_free(item); |
| 36 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 37 | |
Kurt Roeckx | 3cb7c5c | 2018-05-09 17:09:50 +0200 | [diff] [blame] | 38 | pqueue *pqueue_new(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 39 | { |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 40 | pqueue *pq = OPENSSL_zalloc(sizeof(*pq)); |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 41 | |
Rich Salz | cdb10ba | 2018-04-03 11:31:16 -0400 | [diff] [blame] | 42 | if (pq == NULL) |
| 43 | SSLerr(SSL_F_PQUEUE_NEW, ERR_R_MALLOC_FAILURE); |
| 44 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 45 | return pq; |
| 46 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 47 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 48 | void pqueue_free(pqueue *pq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 49 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 50 | OPENSSL_free(pq); |
| 51 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 52 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 53 | pitem *pqueue_insert(pqueue *pq, pitem *item) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 54 | { |
| 55 | pitem *curr, *next; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 56 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 57 | if (pq->items == NULL) { |
| 58 | pq->items = item; |
| 59 | return item; |
| 60 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 61 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 62 | 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 Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 70 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 71 | if (curr == NULL) |
| 72 | pq->items = item; |
| 73 | else |
| 74 | curr->next = item; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 75 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 76 | return item; |
| 77 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 78 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 79 | else if (cmp == 0) /* duplicates not allowed */ |
| 80 | return NULL; |
| 81 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 82 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 83 | item->next = NULL; |
| 84 | curr->next = item; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 85 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 86 | return item; |
| 87 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 88 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 89 | pitem *pqueue_peek(pqueue *pq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 90 | { |
| 91 | return pq->items; |
| 92 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 93 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 94 | pitem *pqueue_pop(pqueue *pq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 95 | { |
| 96 | pitem *item = pq->items; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 97 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 98 | if (pq->items != NULL) |
| 99 | pq->items = pq->items->next; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 100 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 101 | return item; |
| 102 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 103 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 104 | pitem *pqueue_find(pqueue *pq, unsigned char *prio64be) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 105 | { |
| 106 | pitem *next; |
| 107 | pitem *found = NULL; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 108 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 109 | if (pq->items == NULL) |
| 110 | return NULL; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 111 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 112 | 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 Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 118 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 119 | /* 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 Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 126 | return found; |
| 127 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 128 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 129 | pitem *pqueue_iterator(pqueue *pq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 130 | { |
| 131 | return pqueue_peek(pq); |
| 132 | } |
| 133 | |
Xiaoyin Liu | 3c05180 | 2017-07-29 19:20:47 -0400 | [diff] [blame] | 134 | pitem *pqueue_next(piterator *item) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 135 | { |
| 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 Caswell | 8b0e934 | 2016-10-06 19:17:54 +0100 | [diff] [blame] | 148 | size_t pqueue_size(pqueue *pq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 149 | { |
| 150 | pitem *item = pq->items; |
Matt Caswell | c42a78c | 2016-11-04 10:26:57 +0000 | [diff] [blame] | 151 | size_t count = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 152 | |
| 153 | while (item != NULL) { |
| 154 | count++; |
| 155 | item = item->next; |
| 156 | } |
| 157 | return count; |
Dr. Stephen Henson | 8d932f6 | 2009-05-16 16:18:19 +0000 | [diff] [blame] | 158 | } |