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, see <https://www.gnu.org/licenses/>.
26 #include "keystructs.h"
28 int fingerprint_cmp(struct openpgp_fingerprint *a,
29 struct openpgp_fingerprint *b)
31 if (a->length < b->length) {
33 } else if (a->length > b->length) {
36 return memcmp(a->fp, b->fp, a->length);
40 bool array_find(struct keyarray *array, struct openpgp_fingerprint *fp)
48 if (array->keys != NULL && array->count > 0) {
50 top = array->count - 1;
51 while ((top - bottom) > 1) {
52 curpos = (top + bottom) / 2;
53 if (fingerprint_cmp(fp, &array->keys[curpos]) > 0) {
59 found = (fingerprint_cmp(fp, &array->keys[top]) == 0);
65 bool array_add(struct keyarray *array, struct openpgp_fingerprint *fp)
73 if (array->size != 0 && array->count > 0) {
75 top = array->count - 1;
76 while ((top - bottom) > 1) {
77 curpos = (top + bottom) / 2;
78 if (fingerprint_cmp(fp, &array->keys[curpos]) > 0) {
84 found = (fingerprint_cmp(fp, &array->keys[top]) == 0);
86 if (fingerprint_cmp(fp, &array->keys[top]) > 0) {
94 if (array->size == 0) {
95 array->keys = malloc(16 *
96 sizeof(struct openpgp_fingerprint));
101 if (array->count >= array->size) {
103 array->keys = realloc(array->keys,
105 sizeof(struct openpgp_fingerprint));
107 if (curpos < array->count) {
108 memmove(&array->keys[curpos+1],
109 &array->keys[curpos],
110 sizeof(struct openpgp_fingerprint) *
111 (array->count - curpos));
113 array->keys[curpos] = *fp;
121 void array_free(struct keyarray *array)
127 array->count = array->size = 0;