DECODER: Some cleanups, and aligning with OSSL_ENCODER
[openssl.git] / crypto / encode_decode / decoder_meth.c
index 41406df90f51f14da602e607f7ddcd7552541703..37c6ab2b57540381c581e14afc1568b09690577d 100644 (file)
@@ -484,10 +484,8 @@ OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
 {
     OSSL_DECODER_CTX *ctx;
 
-    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
+    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
-        return NULL;
-    }
 
     return ctx;
 }
@@ -506,42 +504,30 @@ int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
     if (ctx->decoder_insts == NULL)
         return 1;
 
-    l = (size_t)sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
+    l = OSSL_DECODER_CTX_get_num_decoders(ctx);
     for (i = 0; i < l; i++) {
         OSSL_DECODER_INSTANCE *decoder_inst =
             sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
+        OSSL_DECODER *decoder =
+            OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
+        OSSL_DECODER *decoderctx =
+            OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
 
-        if (decoder_inst->decoderctx == NULL
-            || decoder_inst->decoder->set_ctx_params == NULL)
+        if (decoderctx == NULL || decoder->set_ctx_params == NULL)
             continue;
-        if (!decoder_inst->decoder->set_ctx_params(decoder_inst->decoderctx,
-                                                   params))
+        if (!decoder->set_ctx_params(decoderctx, params))
             return 0;
     }
     return 1;
 }
 
-static void
-OSSL_DECODER_INSTANCE_free(OSSL_DECODER_INSTANCE *decoder_inst)
-{
-    if (decoder_inst != NULL) {
-        if (decoder_inst->decoder->freectx != NULL)
-            decoder_inst->decoder->freectx(decoder_inst->decoderctx);
-        decoder_inst->decoderctx = NULL;
-        OSSL_DECODER_free(decoder_inst->decoder);
-        decoder_inst->decoder = NULL;
-        OPENSSL_free(decoder_inst);
-        decoder_inst = NULL;
-    }
-}
-
 void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
 {
     if (ctx != NULL) {
         if (ctx->cleanup != NULL)
             ctx->cleanup(ctx->construct_data);
         sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
-                                          OSSL_DECODER_INSTANCE_free);
+                                          ossl_decoder_instance_free);
         ossl_pw_clear_passphrase_data(&ctx->pwdata);
         OPENSSL_free(ctx);
     }