/*
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <string.h>
#include <openssl/params.h>
#include "internal/thread_once.h"
#include "internal/numbers.h"
OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
{
if (p != NULL && key != NULL)
for (; p->key != NULL; p++)
if (strcmp(key, p->key) == 0)
return p;
return NULL;
}
const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
{
return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
}
static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
void *data, size_t data_size)
{
OSSL_PARAM res;
res.key = key;
res.data_type = data_type;
res.data = data;
res.data_size = data_size;
res.return_size = OSSL_PARAM_UNMODIFIED;
return res;
}
int OSSL_PARAM_modified(const OSSL_PARAM *p)
{
return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
}
void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p)
{
if (p != NULL)
while (p->key != NULL)
p++->return_size = OSSL_PARAM_UNMODIFIED;
}
int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
{
switch (sizeof(int)) {
case sizeof(int32_t):
return OSSL_PARAM_get_int32(p, (int32_t *)val);
case sizeof(int64_t):
return OSSL_PARAM_get_int64(p, (int64_t *)val);
}
return 0;
}
int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
{
switch (sizeof(int)) {
case sizeof(int32_t):
return OSSL_PARAM_set_int32(p, (int32_t)val);
case sizeof(int64_t):
return OSSL_PARAM_set_int64(p, (int64_t)val);
}
return 0;
}
OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
{
return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
}
int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
{
switch (sizeof(unsigned int)) {
case sizeof(uint32_t):
return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
case sizeof(uint64_t):
return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
}
return 0;
}
int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
{
switch (sizeof(unsigned int)) {
case sizeof(uint32_t):
return OSSL_PARAM_set_uint32(p, (uint32_t)val);
case sizeof(uint64_t):
return OSSL_PARAM_set_uint64(p, (uint64_t)val);
}
return 0;
}
OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
{
return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
sizeof(unsigned int));
}
int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
{
switch (sizeof(long int)) {
case sizeof(int32_t):
return OSSL_PARAM_get_int32(p, (int32_t *)val);
case sizeof(int64_t):
return OSSL_PARAM_get_int64(p, (int64_t *)val);
}
return