X-Git-Url: http://the.earth.li/gitweb/?a=blobdiff_plain;f=keyarray.c;h=f869802b83b80fcf549e8c2d685cee47be6a2acc;hb=d7d1077c759ec370ef776eba1d855837b3d6c597;hp=cb846de5811f5477e12e8b626c4d2036c6668a90;hpb=5e1b22d763640c4d7a09d07920403d8d491b4410;p=onak.git
diff --git a/keyarray.c b/keyarray.c
index cb846de..f869802 100644
--- a/keyarray.c
+++ b/keyarray.c
@@ -13,8 +13,7 @@
* 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.
+ * this program. If not, see .
*/
#include
@@ -24,8 +23,21 @@
#include
#include "keyarray.h"
+#include "keystructs.h"
-bool array_find(struct keyarray *array, uint64_t key)
+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, struct openpgp_fingerprint *fp)
{
bool found;
int top = 0;
@@ -38,19 +50,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;
@@ -58,20 +70,20 @@ bool array_add(struct keyarray *array, uint64_t key)
int curpos = 0;
found = false;
- if (array->keys != NULL && array->count > 0) {
+ if (array->size != 0 && array->count > 0) {
bottom = -1;
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;
@@ -80,23 +92,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++;
}
}