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/>.
28 #include "keystructs.h"
30 int fingerprint_cmp(struct openpgp_fingerprint *a,
31 struct openpgp_fingerprint *b)
33 if (a->length < b->length) {
35 } else if (a->length > b->length) {
38 return memcmp(a->fp, b->fp, a->length);
42 bool array_find(struct keyarray *array, struct openpgp_fingerprint *fp)
50 if (array->keys != NULL && array->count > 0) {
52 top = array->count - 1;
53 while ((top - bottom) > 1) {
54 curpos = (top + bottom) / 2;
55 if (fingerprint_cmp(fp, &array->keys[curpos]) > 0) {
61 found = (fingerprint_cmp(fp, &array->keys[top]) == 0);
67 bool array_add(struct keyarray *array, struct openpgp_fingerprint *fp)
75 if (array->size != 0 && array->count > 0) {
77 top = array->count - 1;
78 while ((top - bottom) > 1) {
79 curpos = (top + bottom) / 2;
80 if (fingerprint_cmp(fp, &array->keys[curpos]) > 0) {
86 found = (fingerprint_cmp(fp, &array->keys[top]) == 0);
88 if (fingerprint_cmp(fp, &array->keys[top]) > 0) {
96 if (array->size == 0) {
97 array->keys = malloc(16 *
98 sizeof(struct openpgp_fingerprint));
101 array->keys[0] = *fp;
103 if (array->count >= array->size) {
105 array->keys = realloc(array->keys,
107 sizeof(struct openpgp_fingerprint));
109 if (curpos < array->count) {
110 memmove(&array->keys[curpos+1],
111 &array->keys[curpos],
112 sizeof(struct openpgp_fingerprint) *
113 (array->count - curpos));
115 array->keys[curpos] = *fp;
123 void array_free(struct keyarray *array)
129 array->count = array->size = 0;
134 static uint8_t hex2bin(char c)
136 if (c >= '0' && c <= '9') {
138 } else if (c >= 'a' && c <= 'f') {
139 return (c - 'a' + 10);
140 } else if (c >= 'A' && c <= 'F') {
141 return (c - 'A' + 10);
147 bool array_load(struct keyarray *array, const char *file)
149 struct openpgp_fingerprint fp;
154 fpfile = fopen(file, "r");
156 if (fpfile != NULL) {
157 if (!fgets(curline, sizeof(curline) - 1, fpfile)) {
162 while (!feof(fpfile)) {
163 /* Strip any trailing white space */
164 for (i = strlen(curline) - 1;
165 i >= 0 && isspace(curline[i]); i--) {
169 //if ((i % 2) != 0) {
173 if (curline[0] == '#') {
174 // Comment line, ignore
175 } else if (i == FINGERPRINT_V3_LEN ||
176 i == FINGERPRINT_V4_LEN ||
177 i == FINGERPRINT_V5_LEN) {
179 for (i = 0; i < fp.length; i++) {
180 fp.fp[i] = hex2bin(curline[i * 2]);
183 hex2bin(curline[i * 2 + 1]);
185 array_add(array, &fp);
187 printf("Bad line.\n");
190 if (!fgets(curline, sizeof(curline) - 1, fpfile)) {
198 return (array->count != 0);