/src/FreeRDP/winpr/libwinpr/utils/config-readers.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * JSON configuration read helpers |
4 | | * |
5 | | * Copyright 2026 Armin Novak <anovak@thincast.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
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 | | |
27 | | BOOL winpr_config_apply_bool(const char* config, WINPR_JSON* json, const char* name, BOOL* dst) |
28 | 0 | { |
29 | 0 | WINPR_ASSERT(dst); |
30 | |
|
31 | 0 | WINPR_JSON* obj = WINPR_JSON_GetObjectItemCaseSensitive(json, name); |
32 | 0 | if (!obj) |
33 | 0 | return FALSE; |
34 | | |
35 | 0 | if (!WINPR_JSON_IsBool(obj)) |
36 | 0 | { |
37 | 0 | WLog_WARN(TAG, "[%s] Invalid setting %s: must be of type bool", config, name); |
38 | 0 | return FALSE; |
39 | 0 | } |
40 | | |
41 | 0 | *dst = WINPR_JSON_IsTrue(obj); |
42 | 0 | return TRUE; |
43 | 0 | } |
44 | | |
45 | | BOOL winpr_config_apply_string(const char* config, WINPR_JSON* json, const char* name, char** dst) |
46 | 0 | { |
47 | 0 | WINPR_ASSERT(dst); |
48 | |
|
49 | 0 | WINPR_JSON* obj = WINPR_JSON_GetObjectItemCaseSensitive(json, name); |
50 | 0 | if (!obj) |
51 | 0 | return FALSE; |
52 | | |
53 | 0 | if (!WINPR_JSON_IsString(obj)) |
54 | 0 | { |
55 | 0 | WLog_WARN(TAG, "[%s] Invalid setting %s: must be of type string", config, name); |
56 | 0 | return FALSE; |
57 | 0 | } |
58 | | |
59 | 0 | const char* val = WINPR_JSON_GetStringValue(obj); |
60 | 0 | free(*dst); |
61 | 0 | *dst = nullptr; |
62 | 0 | if (val) |
63 | 0 | { |
64 | 0 | *dst = _strdup(val); |
65 | 0 | if (!*dst) |
66 | 0 | return FALSE; |
67 | 0 | } |
68 | 0 | return TRUE; |
69 | 0 | } |
70 | | |
71 | | BOOL winpr_config_apply_uint32(const char* config, WINPR_JSON* json, const char* name, UINT32* dst) |
72 | 0 | { |
73 | 0 | WINPR_ASSERT(dst); |
74 | |
|
75 | 0 | WINPR_JSON* obj = WINPR_JSON_GetObjectItemCaseSensitive(json, name); |
76 | 0 | if (!obj) |
77 | 0 | return FALSE; |
78 | | |
79 | 0 | if (!WINPR_JSON_IsNumber(obj)) |
80 | 0 | { |
81 | 0 | WLog_WARN(TAG, "[%s] Invalid setting %s: must be of type bool", config, name); |
82 | 0 | return FALSE; |
83 | 0 | } |
84 | | |
85 | 0 | const double val = WINPR_JSON_GetNumberValue(obj); |
86 | |
|
87 | 0 | double integer = 0.0; |
88 | 0 | const double fractional = modf(val, &integer); |
89 | 0 | if (signbit(val) || isnan(val) || isinf(val) || (val > UINT32_MAX) || |
90 | 0 | (fabs(fractional) > DBL_EPSILON)) |
91 | 0 | { |
92 | 0 | WLog_WARN(TAG, "[%s] Invalid setting %s: value must be 0 <= %f <= %" PRIu32, config, name, |
93 | 0 | val, UINT32_MAX); |
94 | 0 | return FALSE; |
95 | 0 | } |
96 | | |
97 | 0 | *dst = (UINT32)integer; |
98 | 0 | return TRUE; |
99 | 0 | } |