pqueue.c revision 238405
1160814Ssimon/* crypto/pqueue/pqueue.c */ 2160814Ssimon/* 3160814Ssimon * DTLS implementation written by Nagendra Modadugu 4160814Ssimon * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 5160814Ssimon */ 6160814Ssimon/* ==================================================================== 7160814Ssimon * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 8160814Ssimon * 9160814Ssimon * Redistribution and use in source and binary forms, with or without 10160814Ssimon * modification, are permitted provided that the following conditions 11160814Ssimon * are met: 12160814Ssimon * 13160814Ssimon * 1. Redistributions of source code must retain the above copyright 14160814Ssimon * notice, this list of conditions and the following disclaimer. 15160814Ssimon * 16160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright 17160814Ssimon * notice, this list of conditions and the following disclaimer in 18160814Ssimon * the documentation and/or other materials provided with the 19160814Ssimon * distribution. 20160814Ssimon * 21160814Ssimon * 3. All advertising materials mentioning features or use of this 22160814Ssimon * software must display the following acknowledgment: 23160814Ssimon * "This product includes software developed by the OpenSSL Project 24160814Ssimon * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25160814Ssimon * 26160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27160814Ssimon * endorse or promote products derived from this software without 28160814Ssimon * prior written permission. For written permission, please contact 29160814Ssimon * openssl-core@OpenSSL.org. 30160814Ssimon * 31160814Ssimon * 5. Products derived from this software may not be called "OpenSSL" 32160814Ssimon * nor may "OpenSSL" appear in their names without prior written 33160814Ssimon * permission of the OpenSSL Project. 34160814Ssimon * 35160814Ssimon * 6. Redistributions of any form whatsoever must retain the following 36160814Ssimon * acknowledgment: 37160814Ssimon * "This product includes software developed by the OpenSSL Project 38160814Ssimon * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39160814Ssimon * 40160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43160814Ssimon * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE. 52160814Ssimon * ==================================================================== 53160814Ssimon * 54160814Ssimon * This product includes cryptographic software written by Eric Young 55160814Ssimon * (eay@cryptsoft.com). This product includes software written by Tim 56160814Ssimon * Hudson (tjh@cryptsoft.com). 57160814Ssimon * 58160814Ssimon */ 59160814Ssimon 60160814Ssimon#include "cryptlib.h" 61160814Ssimon#include <openssl/bn.h> 62160814Ssimon#include "pqueue.h" 63160814Ssimon 64160814Ssimontypedef struct _pqueue 65160814Ssimon { 66160814Ssimon pitem *items; 67160814Ssimon int count; 68160814Ssimon } pqueue_s; 69160814Ssimon 70160814Ssimonpitem * 71238405Sjkimpitem_new(unsigned char *prio64be, void *data) 72160814Ssimon { 73160814Ssimon pitem *item = (pitem *) OPENSSL_malloc(sizeof(pitem)); 74160814Ssimon if (item == NULL) return NULL; 75160814Ssimon 76238405Sjkim memcpy(item->priority,prio64be,sizeof(item->priority)); 77160814Ssimon 78160814Ssimon item->data = data; 79160814Ssimon item->next = NULL; 80160814Ssimon 81160814Ssimon return item; 82160814Ssimon } 83160814Ssimon 84160814Ssimonvoid 85160814Ssimonpitem_free(pitem *item) 86160814Ssimon { 87160814Ssimon if (item == NULL) return; 88160814Ssimon 89160814Ssimon OPENSSL_free(item); 90160814Ssimon } 91160814Ssimon 92160814Ssimonpqueue_s * 93160814Ssimonpqueue_new() 94160814Ssimon { 95160814Ssimon pqueue_s *pq = (pqueue_s *) OPENSSL_malloc(sizeof(pqueue_s)); 96160814Ssimon if (pq == NULL) return NULL; 97160814Ssimon 98160814Ssimon memset(pq, 0x00, sizeof(pqueue_s)); 99160814Ssimon return pq; 100160814Ssimon } 101160814Ssimon 102160814Ssimonvoid 103160814Ssimonpqueue_free(pqueue_s *pq) 104160814Ssimon { 105160814Ssimon if (pq == NULL) return; 106160814Ssimon 107160814Ssimon OPENSSL_free(pq); 108160814Ssimon } 109160814Ssimon 110160814Ssimonpitem * 111160814Ssimonpqueue_insert(pqueue_s *pq, pitem *item) 112160814Ssimon { 113160814Ssimon pitem *curr, *next; 114160814Ssimon 115160814Ssimon if (pq->items == NULL) 116160814Ssimon { 117160814Ssimon pq->items = item; 118160814Ssimon return item; 119160814Ssimon } 120160814Ssimon 121160814Ssimon for(curr = NULL, next = pq->items; 122160814Ssimon next != NULL; 123160814Ssimon curr = next, next = next->next) 124160814Ssimon { 125238405Sjkim /* we can compare 64-bit value in big-endian encoding 126238405Sjkim * with memcmp:-) */ 127238405Sjkim int cmp = memcmp(next->priority, item->priority,8); 128238405Sjkim if (cmp > 0) /* next > item */ 129160814Ssimon { 130160814Ssimon item->next = next; 131160814Ssimon 132160814Ssimon if (curr == NULL) 133160814Ssimon pq->items = item; 134160814Ssimon else 135160814Ssimon curr->next = item; 136160814Ssimon 137160814Ssimon return item; 138160814Ssimon } 139238405Sjkim 140238405Sjkim else if (cmp == 0) /* duplicates not allowed */ 141160814Ssimon return NULL; 142160814Ssimon } 143160814Ssimon 144160814Ssimon item->next = NULL; 145160814Ssimon curr->next = item; 146160814Ssimon 147160814Ssimon return item; 148160814Ssimon } 149160814Ssimon 150160814Ssimonpitem * 151160814Ssimonpqueue_peek(pqueue_s *pq) 152160814Ssimon { 153160814Ssimon return pq->items; 154160814Ssimon } 155160814Ssimon 156160814Ssimonpitem * 157160814Ssimonpqueue_pop(pqueue_s *pq) 158160814Ssimon { 159160814Ssimon pitem *item = pq->items; 160160814Ssimon 161160814Ssimon if (pq->items != NULL) 162160814Ssimon pq->items = pq->items->next; 163160814Ssimon 164160814Ssimon return item; 165160814Ssimon } 166160814Ssimon 167160814Ssimonpitem * 168238405Sjkimpqueue_find(pqueue_s *pq, unsigned char *prio64be) 169160814Ssimon { 170215697Ssimon pitem *next; 171160814Ssimon pitem *found = NULL; 172160814Ssimon 173160814Ssimon if ( pq->items == NULL) 174160814Ssimon return NULL; 175160814Ssimon 176215697Ssimon for ( next = pq->items; next->next != NULL; next = next->next) 177160814Ssimon { 178238405Sjkim if ( memcmp(next->priority, prio64be,8) == 0) 179160814Ssimon { 180160814Ssimon found = next; 181160814Ssimon break; 182160814Ssimon } 183160814Ssimon } 184160814Ssimon 185160814Ssimon /* check the one last node */ 186238405Sjkim if ( memcmp(next->priority, prio64be,8) ==0) 187160814Ssimon found = next; 188160814Ssimon 189160814Ssimon if ( ! found) 190160814Ssimon return NULL; 191160814Ssimon 192238405Sjkim#if 0 /* find works in peek mode */ 193238405Sjkim if ( prev == NULL) 194238405Sjkim pq->items = next->next; 195238405Sjkim else 196238405Sjkim prev->next = next->next; 197238405Sjkim#endif 198238405Sjkim 199160814Ssimon return found; 200160814Ssimon } 201160814Ssimon 202160814Ssimonvoid 203160814Ssimonpqueue_print(pqueue_s *pq) 204160814Ssimon { 205160814Ssimon pitem *item = pq->items; 206160814Ssimon 207160814Ssimon while(item != NULL) 208160814Ssimon { 209238405Sjkim printf("item\t%02x%02x%02x%02x%02x%02x%02x%02x\n", 210238405Sjkim item->priority[0],item->priority[1], 211238405Sjkim item->priority[2],item->priority[3], 212238405Sjkim item->priority[4],item->priority[5], 213238405Sjkim item->priority[6],item->priority[7]); 214160814Ssimon item = item->next; 215160814Ssimon } 216160814Ssimon } 217160814Ssimon 218160814Ssimonpitem * 219160814Ssimonpqueue_iterator(pqueue_s *pq) 220160814Ssimon { 221160814Ssimon return pqueue_peek(pq); 222160814Ssimon } 223160814Ssimon 224160814Ssimonpitem * 225160814Ssimonpqueue_next(pitem **item) 226160814Ssimon { 227160814Ssimon pitem *ret; 228160814Ssimon 229160814Ssimon if ( item == NULL || *item == NULL) 230160814Ssimon return NULL; 231160814Ssimon 232160814Ssimon 233160814Ssimon /* *item != NULL */ 234160814Ssimon ret = *item; 235160814Ssimon *item = (*item)->next; 236160814Ssimon 237160814Ssimon return ret; 238160814Ssimon } 239196474Ssimon 240196474Ssimonint 241196474Ssimonpqueue_size(pqueue_s *pq) 242196474Ssimon{ 243196474Ssimon pitem *item = pq->items; 244196474Ssimon int count = 0; 245196474Ssimon 246196474Ssimon while(item != NULL) 247196474Ssimon { 248196474Ssimon count++; 249196474Ssimon item = item->next; 250196474Ssimon } 251196474Ssimon return count; 252196474Ssimon} 253