Make all *_impl provide ctx_new and ctx_free functions

This commit is contained in:
Lucas Gabriel Vuotto 2024-06-05 22:05:37 +00:00
parent fa489d2b4b
commit e9bcc64e62
6 changed files with 54 additions and 32 deletions

View file

@ -82,18 +82,16 @@ struct lc_cipher_ctx *
lc_cipher_ctx_new(const struct lc_cipher_impl *impl)
{
struct lc_cipher_ctx *ctx;
void *arg;
ctx = malloc(sizeof(*ctx));
if (ctx == NULL)
return NULL;
if (impl->argsz > 0) {
arg = malloc(impl->argsz);
if (arg == NULL) {
if (impl->ctx_new != NULL) {
ctx->arg = impl->ctx_new(NULL);
if (ctx->arg == NULL) {
free(ctx);
return NULL;
}
ctx->arg = arg;
} else
ctx->arg = NULL;
ctx->impl = impl;
@ -104,7 +102,11 @@ lc_cipher_ctx_new(const struct lc_cipher_impl *impl)
void
lc_cipher_ctx_free(struct lc_cipher_ctx *ctx)
{
if (ctx != NULL)
free(ctx->arg);
free(ctx);
if (ctx != NULL && ctx->impl != NULL && ctx->impl->ctx_free != NULL)
ctx->impl->ctx_free(ctx);
else {
if (ctx != NULL)
free(ctx->arg);
free(ctx);
}
}