aboutsummaryrefslogtreecommitdiff
path: root/crypto/encode_decode/decoder_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/encode_decode/decoder_lib.c')
-rw-r--r--crypto/encode_decode/decoder_lib.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/crypto/encode_decode/decoder_lib.c b/crypto/encode_decode/decoder_lib.c
index ffcf3cde1155..dedfb24e569e 100644
--- a/crypto/encode_decode/decoder_lib.c
+++ b/crypto/encode_decode/decoder_lib.c
@@ -537,6 +537,14 @@ static void collect_extra_decoder(OSSL_DECODER *decoder, void *arg)
}
}
+static int decoder_sk_cmp(const OSSL_DECODER_INSTANCE *const *a,
+ const OSSL_DECODER_INSTANCE *const *b)
+{
+ if ((*a)->score == (*b)->score)
+ return (*a)->order - (*b)->order;
+ return (*a)->score - (*b)->score;
+}
+
int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
OSSL_LIB_CTX *libctx, const char *propq)
{
@@ -595,6 +603,26 @@ int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
OSSL_DECODER_do_all_provided(libctx, collect_all_decoders, skdecoders);
numdecoders = sk_OSSL_DECODER_num(skdecoders);
+ /*
+ * If there are provided or default properties, sort the initial decoder list
+ * by property matching score so that the highest scored provider is selected
+ * first.
+ */
+ if (propq != NULL || ossl_ctx_global_properties(libctx, 0) != NULL) {
+ int num_decoder_insts = sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
+ int i;
+ OSSL_DECODER_INSTANCE *di;
+ sk_OSSL_DECODER_INSTANCE_compfunc old_cmp =
+ sk_OSSL_DECODER_INSTANCE_set_cmp_func(ctx->decoder_insts, decoder_sk_cmp);
+
+ for (i = 0; i < num_decoder_insts; i++) {
+ di = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
+ di->order = i;
+ }
+ sk_OSSL_DECODER_INSTANCE_sort(ctx->decoder_insts);
+ sk_OSSL_DECODER_INSTANCE_set_cmp_func(ctx->decoder_insts, old_cmp);
+ }
+
memset(&data, 0, sizeof(data));
data.ctx = ctx;
data.w_prev_start = 0;