2 c36d43ac 2021-04-25 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 c36d43ac 2021-04-25 op * Permission to use, copy, modify, and distribute this software for any
5 c36d43ac 2021-04-25 op * purpose with or without fee is hereby granted, provided that the above
6 c36d43ac 2021-04-25 op * copyright notice and this permission notice appear in all copies.
8 c36d43ac 2021-04-25 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c36d43ac 2021-04-25 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c36d43ac 2021-04-25 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c36d43ac 2021-04-25 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c36d43ac 2021-04-25 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c36d43ac 2021-04-25 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c36d43ac 2021-04-25 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 c36d43ac 2021-04-25 op #include "telescope.h"
19 c36d43ac 2021-04-25 op #include <stdlib.h>
20 c36d43ac 2021-04-25 op #include <string.h>
22 c36d43ac 2021-04-25 op static void *hash_alloc(size_t, void*);
23 c36d43ac 2021-04-25 op static void *hash_calloc(size_t, size_t, void*);
24 c36d43ac 2021-04-25 op static void hash_free(void*, void*);
27 c36d43ac 2021-04-25 op hash_alloc(size_t len, void *d)
29 c36d43ac 2021-04-25 op if ((d = malloc(len)) == NULL)
35 c36d43ac 2021-04-25 op hash_calloc(size_t nmemb, size_t size, void *d)
37 c36d43ac 2021-04-25 op if ((d = calloc(nmemb, size)) == NULL)
43 c36d43ac 2021-04-25 op hash_free(void *ptr, void *d)
49 c36d43ac 2021-04-25 op tofu_init(struct ohash *h, unsigned int sz, ptrdiff_t ko)
51 c36d43ac 2021-04-25 op struct ohash_info info = {
52 c36d43ac 2021-04-25 op .key_offset = ko,
53 c36d43ac 2021-04-25 op .calloc = hash_calloc,
54 c36d43ac 2021-04-25 op .free = hash_free,
55 c36d43ac 2021-04-25 op .alloc = hash_alloc,
58 c36d43ac 2021-04-25 op ohash_init(h, sz, &info);
61 c36d43ac 2021-04-25 op struct tofu_entry *
62 c36d43ac 2021-04-25 op tofu_lookup(struct ohash *h, const char *domain, const char *port)
64 c36d43ac 2021-04-25 op char buf[GEMINI_URL_LEN];
65 c36d43ac 2021-04-25 op unsigned int slot;
67 c36d43ac 2021-04-25 op strlcpy(buf, domain, sizeof(buf));
68 c36d43ac 2021-04-25 op if (port != NULL && *port != '\0' && strcmp(port, "1965")) {
69 c36d43ac 2021-04-25 op strlcat(buf, ":", sizeof(buf));
70 c36d43ac 2021-04-25 op strlcat(buf, port, sizeof(buf));
73 c36d43ac 2021-04-25 op slot = ohash_qlookup(h, buf);
74 c36d43ac 2021-04-25 op return ohash_find(h, slot);
78 c36d43ac 2021-04-25 op tofu_add(struct ohash *h, struct tofu_entry *e)
80 c36d43ac 2021-04-25 op unsigned int slot;
82 c36d43ac 2021-04-25 op slot = ohash_qlookup(h, e->domain);
83 c36d43ac 2021-04-25 op ohash_insert(h, slot, e);
87 288fd238 2021-04-25 op tofu_update(struct ohash *h, struct tofu_entry *e)
89 288fd238 2021-04-25 op struct tofu_entry *t;
91 288fd238 2021-04-25 op if ((t = tofu_lookup(h, e->domain, NULL)) == NULL)
92 288fd238 2021-04-25 op tofu_add(h, e);
94 288fd238 2021-04-25 op strlcpy(t->hash, e->hash, sizeof(t->hash));
95 288fd238 2021-04-25 op t->verified = e->verified;