2 * keyarray.c - routines to maintain a sorted array of keyids.
4 * Copyright 2004 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include "keystructs.h"
29 int fingerprint_cmp(struct openpgp_fingerprint *a,
30 struct openpgp_fingerprint *b)
32 if (a->length < b->length) {
34 } else if (a->length > b->length) {
37 return memcmp(a->fp, b->fp, a->length);
41 bool array_find(struct keyarray *array, struct openpgp_fingerprint *fp)
49 if (array->keys != NULL && array->count > 0) {
51 top = array->count - 1;
52 while ((top - bottom) > 1) {
53 curpos = (top + bottom) / 2;
54 if (fingerprint_cmp(fp, &array->keys[curpos]) > 0) {
60 found = (fingerprint_cmp(fp, &array->keys[top]) == 0);
66 bool array_add(struct keyarray *array, struct openpgp_fingerprint *fp)
74 if (array->size != 0 && array->count > 0) {
76 top = array->count - 1;
77 while ((top - bottom) > 1) {
78 curpos = (top + bottom) / 2;
79 if (fingerprint_cmp(fp, &array->keys[curpos]) > 0) {
85 found = (fingerprint_cmp(fp, &array->keys[top]) == 0);
87 if (fingerprint_cmp(fp, &array->keys[top]) > 0) {
95 if (array->size == 0) {
96 array->keys = malloc(16 *
97 sizeof(struct openpgp_fingerprint));
100 array->keys[0] = *fp;
102 if (array->count >= array->size) {
104 array->keys = realloc(array->keys,
106 sizeof(struct openpgp_fingerprint));
108 if (curpos < array->count) {
109 memmove(&array->keys[curpos+1],
110 &array->keys[curpos],
111 sizeof(struct openpgp_fingerprint) *
112 (array->count - curpos));
114 array->keys[curpos] = *fp;
122 void array_free(struct keyarray *array)
128 array->count = array->size = 0;