Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1 | /* |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 2 | * Copyright 2005-2016 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)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 21 | if (item == NULL) |
| 22 | return NULL; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 23 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 24 | memcpy(item->priority, prio64be, sizeof(item->priority)); |
Richard Levitte | 188b057 | 2005-05-30 22:34:37 +0000 | [diff] [blame] | 25 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 26 | item->data = data; |
| 27 | item->next = NULL; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 28 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 29 | return item; |
| 30 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 31 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 32 | void pitem_free(pitem *item) |
| 33 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 34 | OPENSSL_free(item); |
| 35 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 36 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 37 | pqueue *pqueue_new() |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 38 | { |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 39 | pqueue *pq = OPENSSL_zalloc(sizeof(*pq)); |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 40 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 41 | return pq; |
| 42 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 43 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 44 | void pqueue_free(pqueue *pq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 45 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 46 | OPENSSL_free(pq); |
| 47 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 48 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 49 | pitem *pqueue_insert(pqueue *pq, pitem *item) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 50 | { |
| 51 | pitem *curr, *next; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 52 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 53 | if (pq->items == NULL) { |
| 54 | pq->items = item; |
| 55 | return item; |
| 56 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 57 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 58 | 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 Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 66 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 67 | if (curr == NULL) |
| 68 | pq->items = item; |
| 69 | else |
| 70 | curr->next = item; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 71 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 72 | return item; |
| 73 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 74 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 75 | else if (cmp == 0) /* duplicates not allowed */ |
| 76 | return NULL; |
| 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 | item->next = NULL; |
| 80 | curr->next = item; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 81 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 82 | return item; |
| 83 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 84 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 85 | pitem *pqueue_peek(pqueue *pq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 86 | { |
| 87 | return pq->items; |
| 88 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 89 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 90 | pitem *pqueue_pop(pqueue *pq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 91 | { |
| 92 | pitem *item = pq->items; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 93 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 94 | if (pq->items != NULL) |
| 95 | pq->items = pq->items->next; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 96 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 97 | return item; |
| 98 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 99 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 100 | pitem *pqueue_find(pqueue *pq, unsigned char *prio64be) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 101 | { |
| 102 | pitem *next; |
| 103 | pitem *found = NULL; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 104 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 105 | if (pq->items == NULL) |
| 106 | return NULL; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 107 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 108 | 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 Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 114 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 115 | /* 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 Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 122 | return found; |
| 123 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 124 | |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 125 | pitem *pqueue_iterator(pqueue *pq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 126 | { |
| 127 | return pqueue_peek(pq); |
| 128 | } |
| 129 | |
| 130 | pitem *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 Caswell | 8b0e934 | 2016-10-06 19:17:54 +0100 | [diff] [blame] | 144 | size_t pqueue_size(pqueue *pq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 145 | { |
| 146 | pitem *item = pq->items; |
Matt Caswell | c42a78c | 2016-11-04 10:26:57 +0000 | [diff] [blame] | 147 | size_t count = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 148 | |
| 149 | while (item != NULL) { |
| 150 | count++; |
| 151 | item = item->next; |
| 152 | } |
| 153 | return count; |
Dr. Stephen Henson | 8d932f6 | 2009-05-16 16:18:19 +0000 | [diff] [blame] | 154 | } |