return !!chunk;
}
+METHOD(cred_encoding_t, cache, void,
+ private_cred_encoding_t *this, cred_encoding_type_t type, void *cache,
+ chunk_t *encoding)
+{
+ chunk_t *chunk;
+
+ if (type >= CRED_ENCODING_MAX || (int)type < 0)
+ {
+ free(encoding->ptr);
+ return;
+ }
+
+ this->lock->write_lock(this->lock);
+ chunk = this->cache[type]->get(this->cache[type], cache);
+ if (chunk)
+ {
+ free(encoding->ptr);
+ *encoding = *chunk;
+ }
+ else
+ {
+ chunk = malloc_thing(chunk_t);
+ *chunk = *encoding;
+ this->cache[type]->put(this->cache[type], cache, chunk);
+ }
+ this->lock->unlock(this->lock);
+}
+
/**
* Implementation of cred_encoding_t.encode
*/
if (success && cache)
{
- chunk = malloc_thing(chunk_t);
- *chunk = *encoding;
- this->lock->write_lock(this->lock);
- chunk = this->cache[type]->put(this->cache[type], cache, chunk);
- this->lock->unlock(this->lock);
- if (chunk)
- {
- free(chunk->ptr);
- free(chunk);
- }
+ _cache(this, type, cache, encoding);
}
return success;
}
-METHOD(cred_encoding_t, cache, void,
- private_cred_encoding_t *this, cred_encoding_type_t type, void *cache,
- chunk_t encoding)
-{
- chunk_t *chunk;
-
- if (type >= CRED_ENCODING_MAX || (int)type < 0)
- {
- return free(encoding.ptr);
- }
- chunk = malloc_thing(chunk_t);
- *chunk = encoding;
- this->lock->write_lock(this->lock);
- chunk = this->cache[type]->put(this->cache[type], cache, chunk);
- this->lock->unlock(this->lock);
- /* free an encoding already associated to the cache */
- if (chunk)
- {
- free(chunk->ptr);
- free(chunk);
- }
-}
-
METHOD(cred_encoding_t, clear_cache, void,
private_cred_encoding_t *this, void *cache)
{
* Cache a credential encoding created externally.
*
* After calling cache(), the passed encoding is owned by the cred encoding
- * facility.
+ * facility. Note that if there already is a cached encoding for the same
+ * key, the method will return that and free the passed value.
*
* @param type format of the credential encoding
* @param cache key to use for caching, as given to encode()
- * @param encoding encoding to cache, gets owned by this
+ * @param encoding encoding to cache, gets adopted, might get replaced
+ * with cached value
*/
void (*cache)(cred_encoding_t *this, cred_encoding_type_t type, void *cache,
- chunk_t encoding);
+ chunk_t *encoding);
/**
* Register a credential encoder function.
this->set, type, fp);
if (success)
{
- lib->encoding->cache(lib->encoding, type, this, *fp);
+ lib->encoding->cache(lib->encoding, type, this, fp);
}
return success;
}
this->set, type, fp);
if (success)
{
- lib->encoding->cache(lib->encoding, type, this, *fp);
+ lib->encoding->cache(lib->encoding, type, this, fp);
}
return success;
}
if (cache)
{
- lib->encoding->cache(lib->encoding, type, cache, *fp);
+ lib->encoding->cache(lib->encoding, type, cache, fp);
}
return TRUE;
}
success = curve25519_public_key_fingerprint(this->pubkey, type, fp);
if (success)
{
- lib->encoding->cache(lib->encoding, type, this, *fp);
+ lib->encoding->cache(lib->encoding, type, this, fp);
}
return success;
}
success = curve25519_public_key_fingerprint(this->pubkey, type, fp);
if (success)
{
- lib->encoding->cache(lib->encoding, type, this, *fp);
+ lib->encoding->cache(lib->encoding, type, this, fp);
}
return success;
}
return FALSE;
}
hasher->destroy(hasher);
- lib->encoding->cache(lib->encoding, type, key, *fp);
+ lib->encoding->cache(lib->encoding, type, key, fp);
return TRUE;
}
}
free(enc.ptr);
hasher->destroy(hasher);
- lib->encoding->cache(lib->encoding, type, key, *fp);
+ lib->encoding->cache(lib->encoding, type, key, fp);
return TRUE;
}
}
hasher->destroy(hasher);
chunk_clear(&asn1);
- lib->encoding->cache(lib->encoding, type, this, *fp);
+ lib->encoding->cache(lib->encoding, type, this, fp);
return TRUE;
}
return FALSE;
}
hasher->destroy(hasher);
- lib->encoding->cache(lib->encoding, type, ec, *fp);
+ lib->encoding->cache(lib->encoding, type, ec, fp);
return TRUE;
}
}
else
{
- lib->encoding->cache(lib->encoding, type, key, *fp);
+ lib->encoding->cache(lib->encoding, type, key, fp);
success = TRUE;
}
DESTROY_IF(hasher);
}
else
{
- lib->encoding->cache(lib->encoding, type, rsa, *fp);
+ lib->encoding->cache(lib->encoding, type, rsa, fp);
success = TRUE;
}
DESTROY_IF(hasher);