summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2014-12-28 10:45:30 +0200
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2014-12-28 10:45:33 +0200
commit14668f753c9621c08b44ada6f0541ad02f6a0e25 (patch)
tree6f5a4ec27bc004c9e049d9bd01db2655482c8691 /lib
parent145bfae5cbc2364153cdd2cd3177d2f2a32466db (diff)
downloadgnutls-14668f753c9621c08b44ada6f0541ad02f6a0e25.tar.gz
removed cache support for opencdk's keydb
It's implementation looked buggy.
Diffstat (limited to 'lib')
-rw-r--r--lib/opencdk/keydb.c215
1 files changed, 2 insertions, 213 deletions
diff --git a/lib/opencdk/keydb.c b/lib/opencdk/keydb.c
index 64eebf0344..e0ab08d544 100644
--- a/lib/opencdk/keydb.c
+++ b/lib/opencdk/keydb.c
@@ -37,9 +37,7 @@
#include "keydb.h"
#define KEYID_CMP(a, b) ((a[0]) == (b[0]) && (a[1]) == (b[1]))
-#define KEYDB_CACHE_ENTRIES 8
-static void keydb_cache_free(key_table_t cache);
static int classify_data(const byte * buf, size_t len);
static cdk_kbnode_t find_selfsig_node(cdk_kbnode_t key,
cdk_pkt_pubkey_t pk);
@@ -189,72 +187,6 @@ cdk_keydb_idx_rebuild(cdk_keydb_hd_t db, cdk_keydb_search_t dbs)
}
-static cdk_error_t keydb_idx_parse(cdk_stream_t inp, key_idx_t * r_idx)
-{
- key_idx_t idx;
- byte buf[4];
-
- if (!inp || !r_idx) {
- gnutls_assert();
- return CDK_Inv_Value;
- }
-
- idx = cdk_calloc(1, sizeof *idx);
- if (!idx) {
- gnutls_assert();
- return CDK_Out_Of_Core;
- }
-
- while (!cdk_stream_eof(inp)) {
- if (cdk_stream_read(inp, buf, 4) == CDK_EOF)
- break;
- idx->offset = _cdk_buftou32(buf);
- cdk_stream_read(inp, buf, 4);
- idx->keyid[0] = _cdk_buftou32(buf);
- cdk_stream_read(inp, buf, 4);
- idx->keyid[1] = _cdk_buftou32(buf);
- cdk_stream_read(inp, idx->fpr, KEY_FPR_LEN);
- break;
- }
- *r_idx = idx;
- return cdk_stream_eof(inp) ? CDK_EOF : 0;
-}
-
-
-static cdk_error_t
-keydb_idx_search(cdk_stream_t inp, u32 * keyid, const byte * fpr,
- off_t * r_off)
-{
- key_idx_t idx = 0;
-
- if (!inp || !r_off) {
- gnutls_assert();
- return CDK_Inv_Value;
- }
- if ((keyid && fpr) || (!keyid && !fpr)) {
- gnutls_assert();
- return CDK_Inv_Mode;
- }
-
- /* We need an initialize the offset var with a value
- because it might be possible the returned offset will
- be 0 and then we cannot differ between the begin and an EOF. */
- *r_off = 0xFFFFFFFF;
- cdk_stream_seek(inp, 0);
- while (keydb_idx_parse(inp, &idx) != CDK_EOF) {
- if (keyid && KEYID_CMP(keyid, idx->keyid)) {
- *r_off = idx->offset;
- break;
- } else if (fpr && !memcmp(idx->fpr, fpr, KEY_FPR_LEN)) {
- *r_off = idx->offset;
- break;
- }
- }
- cdk_free(idx);
- return *r_off != 0xFFFFFFFF ? 0 : CDK_EOF;
-}
-
-
/**
* cdk_keydb_new_from_mem:
* @r_hd: The keydb output handle.
@@ -451,78 +383,6 @@ static int find_by_pattern(cdk_kbnode_t knode, cdk_keydb_search_t ks)
return 0;
}
-
-static void keydb_cache_free(key_table_t cache)
-{
- key_table_t c2;
-
- while (cache) {
- c2 = cache->next;
- cache->offset = 0;
- cdk_free(cache);
- cache = c2;
- }
-}
-
-
-static key_table_t keydb_cache_find(cdk_keydb_search_t desc)
-{
- key_table_t cache = desc->cache;
- key_table_t t;
-
- for (t = cache; t; t = t->next) {
- switch (desc->type) {
- case CDK_DBSEARCH_SHORT_KEYID:
- case CDK_DBSEARCH_KEYID:
- if (KEYID_CMP(desc->u.keyid, desc->u.keyid))
- return t;
- break;
-
- case CDK_DBSEARCH_EXACT:
- if (strlen(desc->u.pattern) ==
- strlen(desc->u.pattern)
- && !strcmp(desc->u.pattern, desc->u.pattern))
- return t;
- break;
-
- case CDK_DBSEARCH_SUBSTR:
- if (strstr(desc->u.pattern, desc->u.pattern))
- return t;
- break;
-
- case CDK_DBSEARCH_FPR:
- if (!memcmp(desc->u.fpr, desc->u.fpr, KEY_FPR_LEN))
- return t;
- break;
- }
- }
-
- return NULL;
-}
-
-
-static cdk_error_t keydb_cache_add(cdk_keydb_search_t dbs, off_t offset)
-{
- key_table_t k;
-
- if (dbs->ncache > KEYDB_CACHE_ENTRIES)
- return 0; /* FIXME: we should replace the last entry. */
- k = cdk_calloc(1, sizeof *k);
- if (!k) {
- gnutls_assert();
- return CDK_Out_Of_Core;
- }
-
- k->offset = offset;
-
- k->next = dbs->cache;
- dbs->cache = k;
- dbs->ncache++;
- _cdk_log_debug("cache: add entry off=%d type=%d\n", (int) offset,
- (int) dbs->type);
- return 0;
-}
-
static cdk_error_t idx_init(cdk_keydb_hd_t db, cdk_keydb_search_t dbs)
{
cdk_error_t ec, rc = 0;
@@ -694,66 +554,11 @@ cdk_keydb_search_start(cdk_keydb_search_t * st, cdk_keydb_hd_t db,
return 0;
}
-
-static cdk_error_t
-keydb_pos_from_cache(cdk_keydb_hd_t hd, cdk_keydb_search_t ks,
- int *r_cache_hit, off_t * r_off)
-{
- key_table_t c;
-
- if (!hd || !r_cache_hit || !r_off) {
- gnutls_assert();
- return CDK_Inv_Value;
- }
-
- /* Reset the values. */
- *r_cache_hit = 0;
- *r_off = 0;
-
- c = keydb_cache_find(ks);
- if (c != NULL) {
- _cdk_log_debug("cache: found entry in cache.\n");
- *r_cache_hit = 1;
- *r_off = c->offset;
- return 0;
- }
-
- /* No index cache available so we just return here. */
- if (!ks->idx)
- return 0;
-
- if (ks->idx) {
- if (ks->type == CDK_DBSEARCH_KEYID) {
- if (keydb_idx_search
- (ks->idx, ks->u.keyid, NULL, r_off)) {
- gnutls_assert();
- return CDK_Error_No_Key;
- }
- _cdk_log_debug
- ("cache: found keyid entry in idx table.\n");
- *r_cache_hit = 1;
- } else if (ks->type == CDK_DBSEARCH_FPR) {
- if (keydb_idx_search
- (ks->idx, NULL, ks->u.fpr, r_off)) {
- gnutls_assert();
- return CDK_Error_No_Key;
- }
- _cdk_log_debug
- ("cache: found fpr entry in idx table.\n");
- *r_cache_hit = 1;
- }
- }
-
- return 0;
-}
-
void cdk_keydb_search_release(cdk_keydb_search_t st)
{
if (st == NULL)
return;
- keydb_cache_free(st->cache);
-
if (st->idx)
cdk_stream_close(st->idx);
@@ -780,8 +585,7 @@ cdk_keydb_search(cdk_keydb_search_t st, cdk_keydb_hd_t hd,
cdk_stream_t kr;
cdk_kbnode_t knode;
cdk_error_t rc = 0;
- off_t pos = 0, off = 0;
- int key_found = 0, cache_hit = 0;
+ int key_found = 0;
if (!hd || !ret_key || !st) {
gnutls_assert();
@@ -797,25 +601,12 @@ cdk_keydb_search(cdk_keydb_search_t st, cdk_keydb_hd_t hd,
return rc;
}
- if (!st->no_cache) {
- /* It is possible the index is not up-to-date and thus we do
- not find the requesed key. In this case, we reset cache hit
- and continue our normal search procedure. */
- rc = keydb_pos_from_cache(hd, st, &cache_hit, &off);
- if (rc)
- cache_hit = 0;
- }
-
knode = NULL;
while (!key_found && !rc) {
- if (cache_hit && st->type != CDK_DBSEARCH_NEXT)
- cdk_stream_seek(kr, off);
- else if (st->type == CDK_DBSEARCH_NEXT)
+ if (st->type == CDK_DBSEARCH_NEXT)
cdk_stream_seek(kr, st->off);
- pos = cdk_stream_tell(kr);
-
rc = cdk_keydb_get_keyblock(kr, &knode);
if (rc) {
@@ -849,8 +640,6 @@ cdk_keydb_search(cdk_keydb_search_t st, cdk_keydb_hd_t hd,
}
if (key_found) {
- if (!keydb_cache_find(st))
- keydb_cache_add(st, pos);
break;
}