DECODER: Some cleanups, and aligning with OSSL_ENCODER
[openssl.git] / crypto / encode_decode / decoder_lib.c
index 9eeff20f7ca720ddcf3171e808a0db4269f1ec69..0bc772e43bbec5ad719c4758566e0a0cc75b0cbc 100644 (file)
@@ -159,8 +159,7 @@ int ossl_decoder_ctx_add_decoder_inst(OSSL_DECODER_CTX *ctx,
     return (sk_OSSL_DECODER_INSTANCE_push(ctx->decoder_insts, di) > 0);
 }
 
-int OSSL_DECODER_CTX_add_decoder(OSSL_DECODER_CTX *ctx,
-                                           OSSL_DECODER *decoder)
+int OSSL_DECODER_CTX_add_decoder(OSSL_DECODER_CTX *ctx, OSSL_DECODER *decoder)
 {
     OSSL_DECODER_INSTANCE *decoder_inst = NULL;
     const OSSL_PROVIDER *prov = NULL;
@@ -246,7 +245,8 @@ int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
         for (i = w_prev_start; i < w_prev_end; i++) {
             OSSL_DECODER_INSTANCE *decoder_inst =
                 sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
-            const char *name = decoder_inst->input_type;
+            const char *input_type =
+                OSSL_DECODER_INSTANCE_get_input_type(decoder_inst);
             OSSL_DECODER *decoder = NULL;
 
             /*
@@ -256,12 +256,11 @@ int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
              * on top of this one, so we don't.
              */
             if (ctx->start_input_type != NULL
-                && strcasecmp(ctx->start_input_type,
-                              decoder_inst->input_type) != 0)
+                && strcasecmp(ctx->start_input_type, input_type) != 0)
                 continue;
 
             ERR_set_mark();
-            decoder = OSSL_DECODER_fetch(libctx, name, propq);
+            decoder = OSSL_DECODER_fetch(libctx, input_type, propq);
             ERR_pop_to_mark();
 
             if (decoder != NULL) {
@@ -308,7 +307,7 @@ int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
     return 1;
 }
 
-int OSSL_DECODER_CTX_num_decoders(OSSL_DECODER_CTX *ctx)
+int OSSL_DECODER_CTX_get_num_decoders(OSSL_DECODER_CTX *ctx)
 {
     if (ctx == NULL || ctx->decoder_insts == NULL)
         return 0;
@@ -375,6 +374,9 @@ int OSSL_DECODER_export(OSSL_DECODER_INSTANCE *decoder_inst,
                         void *reference, size_t reference_sz,
                         OSSL_CALLBACK *export_cb, void *export_cbarg)
 {
+    OSSL_DECODER *decoder = NULL;
+    void *decoderctx = NULL;
+
     if (!(ossl_assert(decoder_inst != NULL)
           && ossl_assert(reference != NULL)
           && ossl_assert(export_cb != NULL)
@@ -383,25 +385,36 @@ int OSSL_DECODER_export(OSSL_DECODER_INSTANCE *decoder_inst,
         return 0;
     }
 
-    return decoder_inst->decoder->export_object(decoder_inst->decoderctx,
-                                                reference, reference_sz,
-                                                export_cb, export_cbarg);
+    decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
+    decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
+    return decoder->export_object(decoderctx, reference, reference_sz,
+                                  export_cb, export_cbarg);
 }
 
-OSSL_DECODER *OSSL_DECODER_INSTANCE_decoder(OSSL_DECODER_INSTANCE *decoder_inst)
+OSSL_DECODER *
+OSSL_DECODER_INSTANCE_get_decoder(OSSL_DECODER_INSTANCE *decoder_inst)
 {
     if (decoder_inst == NULL)
         return NULL;
     return decoder_inst->decoder;
 }
 
-void *OSSL_DECODER_INSTANCE_decoder_ctx(OSSL_DECODER_INSTANCE *decoder_inst)
+void *
+OSSL_DECODER_INSTANCE_get_decoder_ctx(OSSL_DECODER_INSTANCE *decoder_inst)
 {
     if (decoder_inst == NULL)
         return NULL;
     return decoder_inst->decoderctx;
 }
 
+const char *
+OSSL_DECODER_INSTANCE_get_input_type(OSSL_DECODER_INSTANCE *decoder_inst)
+{
+    if (decoder_inst == NULL)
+        return NULL;
+    return decoder_inst->input_type;
+}
+
 static int decoder_process(const OSSL_PARAM params[], void *arg)
 {
     struct decoder_process_data_st *data = arg;
@@ -422,7 +435,7 @@ static int decoder_process(const OSSL_PARAM params[], void *arg)
         /* First iteration, where we prepare for what is to come */
 
         data->current_decoder_inst_index =
-            OSSL_DECODER_CTX_num_decoders(ctx);
+            OSSL_DECODER_CTX_get_num_decoders(ctx);
 
         bio = data->bio;
     } else {
@@ -431,7 +444,7 @@ static int decoder_process(const OSSL_PARAM params[], void *arg)
         decoder_inst =
             sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts,
                                            data->current_decoder_inst_index);
-        decoder = OSSL_DECODER_INSTANCE_decoder(decoder_inst);
+        decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
 
         if (ctx->construct != NULL
             && ctx->construct(decoder_inst, params, ctx->construct_data)) {
@@ -473,7 +486,11 @@ static int decoder_process(const OSSL_PARAM params[], void *arg)
         OSSL_DECODER_INSTANCE *new_decoder_inst =
             sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
         OSSL_DECODER *new_decoder =
-            OSSL_DECODER_INSTANCE_decoder(new_decoder_inst);
+            OSSL_DECODER_INSTANCE_get_decoder(new_decoder_inst);
+        void *new_decoderctx =
+            OSSL_DECODER_INSTANCE_get_decoder_ctx(new_decoder_inst);
+        const char *new_input_type =
+            OSSL_DECODER_INSTANCE_get_input_type(new_decoder_inst);
 
         /*
          * If |decoder| is NULL, it means we've just started, and the caller
@@ -481,18 +498,16 @@ static int decoder_process(const OSSL_PARAM params[], void *arg)
          * that's the case, we do this extra check.
          */
         if (decoder == NULL && ctx->start_input_type != NULL
-            && strcasecmp(ctx->start_input_type,
-                          new_decoder_inst->input_type) != 0)
+            && strcasecmp(ctx->start_input_type, new_input_type) != 0)
             continue;
 
         /*
          * If we have a previous decoder, we check that the input type
          * of the next to be used matches the type of this previous one.
-         * decoder_inst->input_type is a cache of the parameter "input-type"
-         * value for that decoder.
+         * input_type is a cache of the parameter "input-type" value for
+         * that decoder.
          */
-        if (decoder != NULL
-            && !OSSL_DECODER_is_a(decoder, new_decoder_inst->input_type))
+        if (decoder != NULL && !OSSL_DECODER_is_a(decoder, new_input_type))
             continue;
 
         /*
@@ -511,8 +526,7 @@ static int decoder_process(const OSSL_PARAM params[], void *arg)
 
         /* Recurse */
         new_data.current_decoder_inst_index = i;
-        ok = new_decoder->decode(new_decoder_inst->decoderctx,
-                                 (OSSL_CORE_BIO *)bio,
+        ok = new_decoder->decode(new_decoderctx, (OSSL_CORE_BIO *)bio,
                                  decoder_process, &new_data,
                                  ossl_pw_passphrase_callback_dec,
                                  &new_data.ctx->pwdata);