X-Git-Url: http://the.earth.li/gitweb/?a=blobdiff_plain;f=keyarray.c;h=8bade8ca193913330146c3dbe8d1332652692e9d;hb=f51ec034c08df2d771b9ac6d670ee106ebe46537;hp=389eb0d86d65a867e67a319c0931c76edb5c26be;hpb=be024aa68a513a2a85a7cddb28de4664b0b96144;p=onak.git diff --git a/keyarray.c b/keyarray.c index 389eb0d..8bade8c 100644 --- a/keyarray.c +++ b/keyarray.c @@ -1,9 +1,20 @@ /* * keyarray.c - routines to maintain a sorted array of keyids. * - * Jonathan McDowell + * Copyright 2004 Jonathan McDowell * - * Copyright 2004 Project Purple + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include @@ -13,8 +24,21 @@ #include #include "keyarray.h" +#include "keystructs.h" + +int fingerprint_cmp(struct openpgp_fingerprint *a, + struct openpgp_fingerprint *b) +{ + if (a->length < b->length) { + return -1; + } else if (a->length > b->length) { + return 1; + } else { + return memcmp(a->fp, b->fp, a->length); + } +} -bool array_find(struct keyarray *array, uint64_t key) +bool array_find(struct keyarray *array, struct openpgp_fingerprint *fp) { bool found; int top = 0; @@ -27,19 +51,19 @@ bool array_find(struct keyarray *array, uint64_t key) top = array->count - 1; while ((top - bottom) > 1) { curpos = (top + bottom) / 2; - if (key > array->keys[curpos]) { + if (fingerprint_cmp(fp, &array->keys[curpos]) > 0) { bottom = curpos; } else { top = curpos; } } - found = (array->keys[top] == key); + found = (fingerprint_cmp(fp, &array->keys[top]) == 0); } return found; } -bool array_add(struct keyarray *array, uint64_t key) +bool array_add(struct keyarray *array, struct openpgp_fingerprint *fp) { bool found; int top = 0; @@ -52,15 +76,15 @@ bool array_add(struct keyarray *array, uint64_t key) top = array->count - 1; while ((top - bottom) > 1) { curpos = (top + bottom) / 2; - if (key > array->keys[curpos]) { + if (fingerprint_cmp(fp, &array->keys[curpos]) > 0) { bottom = curpos; } else { top = curpos; } } - found = (array->keys[top] == key); + found = (fingerprint_cmp(fp, &array->keys[top]) == 0); - if (key > array->keys[top]) { + if (fingerprint_cmp(fp, &array->keys[top]) > 0) { curpos = top + 1; } else { curpos = top; @@ -69,23 +93,25 @@ bool array_add(struct keyarray *array, uint64_t key) if (!found) { if (array->size == 0) { - array->keys = malloc(16 * sizeof(uint64_t)); + array->keys = malloc(16 * + sizeof(struct openpgp_fingerprint)); array->size = 16; array->count = 1; - array->keys[0] = key; + array->keys[0] = *fp; } else { if (array->count >= array->size) { array->size *= 2; array->keys = realloc(array->keys, - array->size * sizeof(uint64_t)); + array->size * + sizeof(struct openpgp_fingerprint)); } if (curpos < array->count) { memmove(&array->keys[curpos+1], &array->keys[curpos], - sizeof(uint64_t) * + sizeof(struct openpgp_fingerprint) * (array->count - curpos)); } - array->keys[curpos] = key; + array->keys[curpos] = *fp; array->count++; } }