| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2017-07-30 10:10:35 +08:00
										 |  |  |  * Copyright 2005-2017 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2005-04-27 00:02:40 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-05-18 02:18:30 +08:00
										 |  |  |  * Licensed under the OpenSSL license (the "License").  You may not use | 
					
						
							|  |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							| 
									
										
										
										
											2005-04-27 00:02:40 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  | #include "ssl_locl.h"
 | 
					
						
							| 
									
										
										
										
											2005-04-28 00:27:14 +08:00
										 |  |  | #include <openssl/bn.h>
 | 
					
						
							| 
									
										
										
										
											2005-04-27 00:02:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  | struct pqueue_st { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     pitem *items; | 
					
						
							|  |  |  |     int count; | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | pitem *pitem_new(unsigned char *prio64be, void *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-02 11:10:31 +08:00
										 |  |  |     pitem *item = OPENSSL_malloc(sizeof(*item)); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (item == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memcpy(item->priority, prio64be, sizeof(item->priority)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item->data = data; | 
					
						
							|  |  |  |     item->next = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return item; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void pitem_free(pitem *item) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OPENSSL_free(item); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  | pqueue *pqueue_new() | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  |     pqueue *pq = OPENSSL_zalloc(sizeof(*pq)); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return pq; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  | void pqueue_free(pqueue *pq) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     OPENSSL_free(pq); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  | pitem *pqueue_insert(pqueue *pq, pitem *item) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     pitem *curr, *next; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (pq->items == NULL) { | 
					
						
							|  |  |  |         pq->items = item; | 
					
						
							|  |  |  |         return item; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (curr = NULL, next = pq->items; | 
					
						
							|  |  |  |          next != NULL; curr = next, next = next->next) { | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * we can compare 64-bit value in big-endian encoding with memcmp:-) | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         int cmp = memcmp(next->priority, item->priority, 8); | 
					
						
							|  |  |  |         if (cmp > 0) {          /* next > item */ | 
					
						
							|  |  |  |             item->next = next; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (curr == NULL) | 
					
						
							|  |  |  |                 pq->items = item; | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 curr->next = item; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return item; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         else if (cmp == 0)      /* duplicates not allowed */ | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item->next = NULL; | 
					
						
							|  |  |  |     curr->next = item; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return item; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  | pitem *pqueue_peek(pqueue *pq) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     return pq->items; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  | pitem *pqueue_pop(pqueue *pq) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     pitem *item = pq->items; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (pq->items != NULL) | 
					
						
							|  |  |  |         pq->items = pq->items->next; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return item; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  | pitem *pqueue_find(pqueue *pq, unsigned char *prio64be) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     pitem *next; | 
					
						
							|  |  |  |     pitem *found = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (pq->items == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (next = pq->items; next->next != NULL; next = next->next) { | 
					
						
							|  |  |  |         if (memcmp(next->priority, prio64be, 8) == 0) { | 
					
						
							|  |  |  |             found = next; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* check the one last node */ | 
					
						
							|  |  |  |     if (memcmp(next->priority, prio64be, 8) == 0) | 
					
						
							|  |  |  |         found = next; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!found) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return found; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 03:54:01 +08:00
										 |  |  | pitem *pqueue_iterator(pqueue *pq) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     return pqueue_peek(pq); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-30 07:20:47 +08:00
										 |  |  | pitem *pqueue_next(piterator *item) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     pitem *ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (item == NULL || *item == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* *item != NULL */ | 
					
						
							|  |  |  |     ret = *item; | 
					
						
							|  |  |  |     *item = (*item)->next; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 02:17:54 +08:00
										 |  |  | size_t pqueue_size(pqueue *pq) | 
					
						
							| 
									
										
										
										
											2009-05-17 00:18:19 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     pitem *item = pq->items; | 
					
						
							| 
									
										
										
										
											2016-11-04 18:26:57 +08:00
										 |  |  |     size_t count = 0; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     while (item != NULL) { | 
					
						
							|  |  |  |         count++; | 
					
						
							|  |  |  |         item = item->next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return count; | 
					
						
							| 
									
										
										
										
											2009-05-17 00:18:19 +08:00
										 |  |  | } |