PR: 2730
[oweals/openssl.git] / crypto / pqueue / pqueue.c
index f4fa37fe64bb07ac4b53bbe08a36fd935132eff4..eab13a125013bffc7b7e08d0cafb3a182b8728f5 100644 (file)
@@ -68,12 +68,13 @@ typedef struct _pqueue
        } pqueue_s;
 
 pitem *
-pitem_new(BN_ULLONG priority, void *data)
+pitem_new(unsigned char *prio64be, void *data)
        {
        pitem *item = (pitem *) OPENSSL_malloc(sizeof(pitem));
        if (item == NULL) return NULL;
 
-       item->priority = priority;
+       memcpy(item->priority,prio64be,sizeof(item->priority));
+
        item->data = data;
        item->next = NULL;
 
@@ -84,7 +85,7 @@ void
 pitem_free(pitem *item)
        {
        if (item == NULL) return;
-       
+
        OPENSSL_free(item);
        }
 
@@ -121,7 +122,10 @@ pqueue_insert(pqueue_s *pq, pitem *item)
                next != NULL;
                curr = next, next = next->next)
                {
-               if (item->priority < next->priority)
+               /* 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;
 
@@ -132,8 +136,8 @@ pqueue_insert(pqueue_s *pq, pitem *item)
 
                        return item;
                        }
-               /* duplicates not allowed */
-               if (item->priority == next->priority)
+               
+               else if (cmp == 0)      /* duplicates not allowed */
                        return NULL;
                }
 
@@ -161,18 +165,17 @@ pqueue_pop(pqueue_s *pq)
        }
 
 pitem *
-pqueue_find(pqueue_s *pq, BN_ULLONG priority)
+pqueue_find(pqueue_s *pq, unsigned char *prio64be)
        {
-       pitem *next, *prev = NULL;
+       pitem *next;
        pitem *found = NULL;
 
        if ( pq->items == NULL)
                return NULL;
 
-       for ( next = pq->items; next->next != NULL; 
-                 prev = next, next = next->next)
+       for ( next = pq->items; next->next != NULL; next = next->next)
                {
-               if ( next->priority == priority)
+               if ( memcmp(next->priority, prio64be,8) == 0)
                        {
                        found = next;
                        break;
@@ -180,7 +183,7 @@ pqueue_find(pqueue_s *pq, BN_ULLONG priority)
                }
        
        /* check the one last node */
-       if ( next->priority == priority)
+       if ( memcmp(next->priority, prio64be,8) ==0)
                found = next;
 
        if ( ! found)
@@ -203,7 +206,11 @@ pqueue_print(pqueue_s *pq)
 
        while(item != NULL)
                {
-               printf("item\t%lld\n", item->priority);
+               printf("item\t%02x%02x%02x%02x%02x%02x%02x%02x\n",
+                       item->priority[0],item->priority[1],
+                       item->priority[2],item->priority[3],
+                       item->priority[4],item->priority[5],
+                       item->priority[6],item->priority[7]);
                item = item->next;
                }
        }
@@ -229,3 +236,17 @@ pqueue_next(pitem **item)
 
        return ret;
        }
+
+int
+pqueue_size(pqueue_s *pq)
+{
+       pitem *item = pq->items;
+       int count = 0;
+       
+       while(item != NULL)
+       {
+               count++;
+               item = item->next;
+       }
+       return count;
+}