FreeRDP
Loading...
Searching...
No Matches
config-readers.c
1
20#include <math.h>
21#include <float.h>
22#include <winpr/config-readers.h>
23
24#include "../log.h"
25#define TAG WINPR_TAG("utils.config-readers")
26
27BOOL winpr_config_apply_bool(const char* config, WINPR_JSON* json, const char* name, BOOL* dst)
28{
29 WINPR_ASSERT(dst);
30
31 WINPR_JSON* obj = WINPR_JSON_GetObjectItemCaseSensitive(json, name);
32 if (!obj)
33 return FALSE;
34
35 if (!WINPR_JSON_IsBool(obj))
36 {
37 WLog_WARN(TAG, "[%s] Invalid setting %s: must be of type bool", config, name);
38 return FALSE;
39 }
40
41 *dst = WINPR_JSON_IsTrue(obj);
42 return TRUE;
43}
44
45BOOL winpr_config_apply_string(const char* config, WINPR_JSON* json, const char* name, char** dst)
46{
47 WINPR_ASSERT(dst);
48
49 WINPR_JSON* obj = WINPR_JSON_GetObjectItemCaseSensitive(json, name);
50 if (!obj)
51 return FALSE;
52
53 if (!WINPR_JSON_IsString(obj))
54 {
55 WLog_WARN(TAG, "[%s] Invalid setting %s: must be of type string", config, name);
56 return FALSE;
57 }
58
59 const char* val = WINPR_JSON_GetStringValue(obj);
60 free(*dst);
61 *dst = nullptr;
62 if (val)
63 {
64 *dst = _strdup(val);
65 if (!*dst)
66 return FALSE;
67 }
68 return TRUE;
69}
70
71BOOL winpr_config_apply_uint32(const char* config, WINPR_JSON* json, const char* name, UINT32* dst)
72{
73 WINPR_ASSERT(dst);
74
75 WINPR_JSON* obj = WINPR_JSON_GetObjectItemCaseSensitive(json, name);
76 if (!obj)
77 return FALSE;
78
79 if (!WINPR_JSON_IsNumber(obj))
80 {
81 WLog_WARN(TAG, "[%s] Invalid setting %s: must be of type bool", config, name);
82 return FALSE;
83 }
84
85 const double val = WINPR_JSON_GetNumberValue(obj);
86
87 double integer = 0.0;
88 const double fractional = modf(val, &integer);
89 if (signbit(val) || isnan(val) || isinf(val) || (val > UINT32_MAX) ||
90 (fabs(fractional) > DBL_EPSILON))
91 {
92 WLog_WARN(TAG, "[%s] Invalid setting %s: value must be 0 <= %f <= %" PRIu32, config, name,
93 val, UINT32_MAX);
94 return FALSE;
95 }
96
97 *dst = (UINT32)integer;
98 return TRUE;
99}
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition c-json.c:167
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition c-json.c:177
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition c-json.c:162
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON *object, const char *string)
Same as WINPR_JSON_GetObjectItem but with case sensitive matching.
Definition c-json.c:127
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition c-json.c:182
WINPR_ATTR_NODISCARD WINPR_API double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
Definition c-json.c:147
WINPR_ATTR_NODISCARD WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition c-json.c:142