Commit 1328e176 authored by Tim Rühsen's avatar Tim Rühsen

Simplify hasmap/stringmap/vector API

* include/wget/wget.h: Remove wget_hashmap_get_null() and
  wget_stringmap_get_null(),
  change params and return value of wget_hashmap_get() and
  wget_stringmap_get().
* libwget/hashmap.c: Remove wget_hashmap_get_null(),
  change params and return value of wget_hashmap_get(),
  simplify hashmap_find_entry().
* libwget/stringmap.c: Remove wget_stringmap_get(),
  change params and return value of wget_stringmap_get().
* libwget/dns_cache.c: Amend for new API
* libwget/hpkp.c: Likewise
* libwget/hsts.c: Likewise
* libwget/http.c: Likewise
* libwget/netrc.c: Likewise
* libwget/ocsp.c: Likewise
* libwget/tls_session.c: Likewise
* src/host.c: Likewise
* src/options.c: Likewise
* src/plugin.c: Likewise
* src/stats_site.c: Likewise
* unit-tests/test.c: Likewise
parent 8a96e670
Pipeline #41900469 failed with stages
in 30 minutes and 15 seconds
......@@ -720,10 +720,8 @@ WGETAPI void
wget_hashmap_free(wget_hashmap_t **h);
WGETAPI void
wget_hashmap_clear(wget_hashmap_t *h);
WGETAPI void *
wget_hashmap_get(const wget_hashmap_t *h, const void *key);
WGETAPI int
wget_hashmap_get_null(const wget_hashmap_t *h, const void *key, void **value);
wget_hashmap_get(const wget_hashmap_t *h, const void *key, void **value);
WGETAPI int
wget_hashmap_contains(const wget_hashmap_t *h, const void *key);
WGETAPI int
......@@ -772,10 +770,8 @@ WGETAPI void
wget_stringmap_free(wget_stringmap_t **h);
WGETAPI void
wget_stringmap_clear(wget_stringmap_t *h);
WGETAPI void *
wget_stringmap_get(const wget_stringmap_t *h, const char *key);
WGETAPI int
wget_stringmap_get_null(const wget_stringmap_t *h, const char *key, void **value);
wget_stringmap_get(const wget_stringmap_t *h, const char *key, void **value);
WGETAPI int
wget_stringmap_contains(const wget_stringmap_t *h, const char *key);
WGETAPI int
......
......@@ -134,7 +134,8 @@ struct addrinfo *wget_dns_cache_get(const char *host, uint16_t port)
struct _dns_entry *entryp, entry = { .host = host, .port = port };
wget_thread_mutex_lock(dns_mutex);
entryp = wget_hashmap_get(dns_cache, &entry);
if (!wget_hashmap_get(dns_cache, &entry, &entryp))
entryp = NULL;
wget_thread_mutex_unlock(dns_mutex);
if (entryp) {
......@@ -176,7 +177,7 @@ struct addrinfo *wget_dns_cache_add(const char *host, uint16_t port, struct addr
wget_hashmap_set_value_destructor(dns_cache, (wget_hashmap_value_destructor_t)_free_dns);
}
if (wget_hashmap_get(dns_cache, entryp)) {
if (wget_hashmap_contains(dns_cache, entryp)) {
_free_dns(entryp);
} else {
// key and value are the same to make wget_hashmap_get() return old entry
......
......@@ -109,9 +109,9 @@ wget_hashmap_t *wget_hashmap_create(int max, wget_hashmap_hash_t hash, wget_hash
}
G_GNUC_WGET_NONNULL_ALL
static _entry_t * hashmap_find_entry(const wget_hashmap_t *h, const char *key, unsigned int hash, int pos)
static _entry_t * hashmap_find_entry(const wget_hashmap_t *h, const char *key, unsigned int hash)
{
for (_entry_t * e = h->entry[pos]; e; e = e->next) {
for (_entry_t * e = h->entry[hash % h->max]; e; e = e->next) {
if (hash == e->hash && (key == e->key || !h->cmp(key, e->key))) {
return e;
}
......@@ -203,9 +203,8 @@ int wget_hashmap_put_noalloc(wget_hashmap_t *h, const void *key, const void *val
if (h && key) {
_entry_t *entry;
unsigned int hash = h->hash(key);
int pos = hash % h->max;
if ((entry = hashmap_find_entry(h, key, hash, pos))) {
if ((entry = hashmap_find_entry(h, key, hash))) {
if (entry->key != key && entry->key != value) {
if (h->key_destructor)
h->key_destructor(entry->key);
......@@ -254,9 +253,8 @@ int wget_hashmap_put(wget_hashmap_t *h, const void *key, size_t keysize, const v
if (h && key) {
_entry_t *entry;
unsigned int hash = h->hash(key);
int pos = hash % h->max;
if ((entry = hashmap_find_entry(h, key, hash, pos))) {
if ((entry = hashmap_find_entry(h, key, hash))) {
if (h->value_destructor)
h->value_destructor(entry->value);
......@@ -280,20 +278,16 @@ int wget_hashmap_put(wget_hashmap_t *h, const void *key, size_t keysize, const v
*
* Get the value for a given key.
*
* If there are %NULL values in the hashmap, you should use this function
* to distinguish between 'not found' and 'found %NULL value'.
*
* Neither \p h nor \p key must be %NULL.
*/
int wget_hashmap_get_null(const wget_hashmap_t *h, const void *key, void **value)
int wget_hashmap_get(const wget_hashmap_t *h, const void *key, void **value)
{
if (h && key) {
_entry_t *entry;
unsigned int hash = h->hash(key);
int pos = hash % h->max;
if ((entry = hashmap_find_entry(h, key, hash, pos))) {
if (value) *value = entry->value;
if ((entry = hashmap_find_entry(h, key, h->hash(key)))) {
if (value)
*value = entry->value;
return 1;
}
}
......@@ -301,28 +295,6 @@ int wget_hashmap_get_null(const wget_hashmap_t *h, const void *key, void **value
return 0;
}
/**
* \param[in] h Hashmap
* \param[in] key Key to search for
* \return Found value or %NULL if not found
*
* Get the value for a given key.
*
* If there are %NULL values in the hashmap, you should use wget_hashmap_get_null()
* to distinguish between 'not found' and 'found %NULL value'.
*
* Neither \p h nor \p key must be %NULL.
*/
void *wget_hashmap_get(const wget_hashmap_t *h, const void *key)
{
void *value;
if (wget_hashmap_get_null(h, key, &value))
return value;
return NULL;
}
/**
* \param[in] h Hashmap
* \param[in] key Key to search for
......@@ -332,7 +304,7 @@ void *wget_hashmap_get(const wget_hashmap_t *h, const void *key)
*/
int wget_hashmap_contains(const wget_hashmap_t *h, const void *key)
{
return wget_hashmap_get_null(h, key, NULL);
return wget_hashmap_get(h, key, NULL);
}
G_GNUC_WGET_NONNULL_ALL
......
......@@ -403,9 +403,8 @@ static int impl_hpkp_db_check_pubkey(wget_hpkp_db_t *hpkp_db, const char *host,
domain++;
key.host = domain;
hpkp = wget_hashmap_get(hpkp_db_priv->entries, &key);
if (!hpkp)
if (!wget_hashmap_get(hpkp_db_priv->entries, &key, &hpkp))
subdomain = 1;
}
......@@ -459,9 +458,9 @@ static void impl_hpkp_db_add(wget_hpkp_db_t *hpkp_db, wget_hpkp_t *hpkp)
debug_printf("removed HPKP %s\n", hpkp->host);
wget_hpkp_free(hpkp);
} else {
wget_hpkp_t *old = wget_hashmap_get(hpkp_db_priv->entries, hpkp);
wget_hpkp_t *old;
if (old) {
if (wget_hashmap_get(hpkp_db_priv->entries, hpkp, &old)) {
old->created = hpkp->created;
old->maxage = hpkp->maxage;
old->expires = hpkp->expires;
......
......@@ -179,13 +179,13 @@ static int impl_hsts_db_host_match(const wget_hsts_db_t *hsts_db, const char *ho
// we assume the scheme is HTTP
hsts.port = (port == 80 ? 443 : port);
hsts.host = host;
if ((hstsp = wget_hashmap_get(hsts_db_priv->entries, &hsts)) && hstsp->expires >= now)
if (wget_hashmap_get(hsts_db_priv->entries, &hsts, &hstsp) && hstsp->expires >= now)
return 1;
// now look for a valid subdomain match
for (p = host; (p = strchr(p, '.')); ) {
hsts.host = ++p;
if ((hstsp = wget_hashmap_get(hsts_db_priv->entries, &hsts))
if (wget_hashmap_get(hsts_db_priv->entries, &hsts, &hstsp)
&& hstsp->include_subdomains && hstsp->expires >= now)
return 1;
}
......@@ -253,9 +253,9 @@ static void _hsts_db_add_entry(_hsts_db_impl_t *hsts_db_priv, _hsts_t *hsts)
_free_hsts(hsts);
hsts = NULL;
} else {
_hsts_t *old = wget_hashmap_get(hsts_db_priv->entries, hsts);
_hsts_t *old;
if (old) {
if (wget_hashmap_get(hsts_db_priv->entries, hsts, &old)) {
if (old->created < hsts->created || old->maxage != hsts->maxage || old->include_subdomains != hsts->include_subdomains) {
old->created = hsts->created;
old->expires = hsts->expires;
......
......@@ -296,15 +296,21 @@ void wget_http_add_credentials(wget_http_request_t *req, wget_http_challenge_t *
xfree(encoded);
}
else if (!wget_strcasecmp_ascii(challenge->auth_scheme, "digest")) {
const char
*realm = wget_stringmap_get(challenge->params, "realm"),
*opaque = wget_stringmap_get(challenge->params, "opaque"),
*nonce = wget_stringmap_get(challenge->params, "nonce"),
*qop = wget_stringmap_get(challenge->params, "qop"),
*algorithm = wget_stringmap_get(challenge->params, "algorithm");
const char *realm, *opaque, *nonce, *qop, *algorithm;
wget_buffer_t buf;
int hashtype, hashlen;
if (!wget_stringmap_get(challenge->params, "realm", &realm))
realm = NULL;
if (!wget_stringmap_get(challenge->params, "opaque", &opaque))
opaque = NULL;
if (!wget_stringmap_get(challenge->params, "nonce", &nonce))
nonce = NULL;
if (!wget_stringmap_get(challenge->params, "qop", &qop))
qop = NULL;
if (!wget_stringmap_get(challenge->params, "algorithm", &algorithm))
algorithm = NULL;
if (wget_strcasecmp_ascii(qop, "auth")) {
error_printf(_("Unsupported quality of protection '%s'.\n"), qop);
return;
......
......@@ -101,11 +101,13 @@ wget_netrc_t *wget_netrc_new(const char *machine, const char *login, const char
wget_netrc_t *wget_netrc_get(const wget_netrc_db_t *netrc_db, const char *host)
{
if (netrc_db) {
wget_netrc_t netrc;
wget_netrc_t netrc, *netrcp;
// look for an exact match
netrc.host = host;
return wget_hashmap_get(netrc_db->machines, &netrc);
if (wget_hashmap_get(netrc_db->machines, &netrc, &netrcp))
return netrcp;
}
return NULL;
......
......@@ -156,7 +156,7 @@ static bool impl_ocsp_db_fingerprint_in_cache(const wget_ocsp_db_t *ocsp_db, con
// look for an exact match
ocsp.key = fingerprint;
if ((ocspp = wget_hashmap_get(ocsp_db_priv->fingerprints, &ocsp)) && ocspp->maxage >= (int64_t) time(NULL)) {
if (wget_hashmap_get(ocsp_db_priv->fingerprints, &ocsp, &ocspp) && ocspp->maxage >= (int64_t) time(NULL)) {
if (revoked)
*revoked = !ocspp->valid;
return 1;
......@@ -194,7 +194,7 @@ static bool impl_ocsp_db_hostname_is_valid(const wget_ocsp_db_t *ocsp_db, const
// look for an exact match
ocsp.key = hostname;
if ((ocspp = wget_hashmap_get(ocsp_db_priv->hosts, &ocsp)) && ocspp->maxage >= (int64_t) time(NULL)) {
if (wget_hashmap_get(ocsp_db_priv->hosts, &ocsp, &ocspp) && ocspp->maxage >= (int64_t) time(NULL)) {
return 1;
}
......@@ -271,9 +271,9 @@ static void _ocsp_db_add_fingerprint_entry(_ocsp_db_impl_t *ocsp_db_priv, _ocsp_
debug_printf("removed OCSP cert %s\n", ocsp->key);
_free_ocsp(ocsp);
} else {
_ocsp_t *old = wget_hashmap_get(ocsp_db_priv->fingerprints, ocsp);
_ocsp_t *old;
if (old) {
if (wget_hashmap_get(ocsp_db_priv->fingerprints, ocsp, &old)) {
if (old->mtime < ocsp->mtime) {
old->mtime = ocsp->mtime;
old->maxage = ocsp->maxage;
......@@ -338,9 +338,9 @@ static void _ocsp_db_add_host_entry(_ocsp_db_impl_t *ocsp_db_priv, _ocsp_t *ocsp
debug_printf("removed OCSP host %s\n", ocsp->key);
_free_ocsp(ocsp);
} else {
_ocsp_t *old = wget_hashmap_get(ocsp_db_priv->hosts, ocsp);
_ocsp_t *old;
if (old) {
if (wget_hashmap_get(ocsp_db_priv->hosts, ocsp, &old)) {
if (old->mtime < ocsp->mtime) {
old->mtime = ocsp->mtime;
old->maxage = ocsp->maxage;
......
......@@ -163,31 +163,11 @@ int wget_stringmap_put(wget_stringmap_t *h, const char *key, const void *value,
*
* Get the value for a given key.
*
* If there are %NULL values in the stringmap, you should use this function
* to distinguish between 'not found' and 'found %NULL value'.
*
* Neither \p h nor \p key must be %NULL.
*/
int wget_stringmap_get_null(const wget_stringmap_t *h, const char *key, void **value)
{
return wget_hashmap_get_null(h, key, value);
}
/**
* \param[in] h Stringmap
* \param[in] key Key to search for
* \return Found value or %NULL if not found
*
* Get the value for a given key.
*
* If there are %NULL values in the stringmap, you should use wget_stringmap_get_null()
* to distinguish between 'not found' and 'found %NULL value'.
*
* Neither \p h nor \p key must be %NULL.
*/
void *wget_stringmap_get(const wget_stringmap_t *h, const char *key)
int wget_stringmap_get(const wget_stringmap_t *h, const char *key, void **value)
{
return wget_hashmap_get(h, key);
return wget_hashmap_get(h, key, value);
}
/**
......
......@@ -148,7 +148,7 @@ int wget_tls_session_get(const wget_tls_session_db_t *tls_session_db, const char
int64_t now = time(NULL);
tls_session.host = host;
if ((tls_sessionp = wget_hashmap_get(tls_session_db->entries, &tls_session)) && tls_sessionp->expires >= now) {
if (wget_hashmap_get(tls_session_db->entries, &tls_session, &tls_sessionp) && tls_sessionp->expires >= now) {
if (data)
*data = wget_memdup(tls_sessionp->data, tls_sessionp->data_size);
if (size)
......@@ -206,9 +206,9 @@ void wget_tls_session_db_add(wget_tls_session_db_t *tls_session_db, wget_tls_ses
wget_tls_session_free(tls_session);
tls_session = NULL;
} else {
wget_tls_session_t *old = wget_hashmap_get(tls_session_db->entries, tls_session);
wget_tls_session_t *old;
if (old) {
if (wget_hashmap_get(tls_session_db->entries, tls_session, &old)) {
debug_printf("found TLS session data for %s\n", old->host);
if (wget_hashmap_remove(tls_session_db->entries, old))
debug_printf("removed TLS session data for %s\n", tls_session->host);
......
......@@ -132,9 +132,7 @@ HOST *host_get(wget_iri_t *iri)
wget_thread_mutex_lock(hosts_mutex);
if (hosts)
hostp = wget_hashmap_get(hosts, &host);
else
if (!hosts || !wget_hashmap_get(hosts, &host, &hostp))
hostp = NULL;
wget_thread_mutex_unlock(hosts_mutex);
......
......@@ -3151,7 +3151,7 @@ int init(int argc, const char **argv)
wget_stringmap_t *set = wget_stringmap_create(start_len);
for (int i = 0; i < start_len; i++) {
const char *nxt = wget_vector_get(config.sig_ext, i);
if (!wget_stringmap_get_null(set, nxt, NULL)) {
if (!wget_stringmap_contains(set, nxt)) {
wget_vector_add_str(new_sig_ext, nxt);
wget_stringmap_put_noalloc(set, wget_strdup(nxt), NULL);
}
......
......@@ -526,8 +526,7 @@ int plugin_db_forward_option(const char *plugin_option, dl_error_t *e)
}
// Search for plugin
plugin = (plugin_t *) wget_stringmap_get(plugin_name_index, plugin_name);
if (! plugin) {
if (!wget_stringmap_get(plugin_name_index, plugin_name, &plugin)) {
dl_error_set_printf(e, "Plugin '%s' is not loaded.", plugin_name);
wget_free(plugin_option_copy);
return -1;
......
......@@ -112,7 +112,8 @@ void stats_site_add(wget_http_response_t *resp, wget_gpg_info_t *gpg_info)
if ((p = strrchr(uri, '.')))
*p = 0;
site_stats_t *doc = wget_stringmap_get(docs, uri);
site_stats_t *doc;
wget_stringmap_get(docs, uri, &doc);
xfree(uri);
if (doc) {
......
......@@ -1862,7 +1862,7 @@ static void test_stringmap(void)
for (it = 0; it < 26; it++) {
snprintf(key, sizeof(key), "http://www.example.com/subdir/%d.html", it);
snprintf(value, sizeof(value), "%d.html", it);
if (!(val = wget_stringmap_get(m, key))) {
if (!wget_stringmap_get(m, key, &val)) {
failed++;
info_printf("stringmap_get(%s) didn't find entry\n", key);
} else if (strcmp(val, value)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment