Coverage Report

Created: 2026-10-15 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/utils/helpers.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * common helper utilities
4
 *
5
 * Copyright 2024 Armin Novak <anovak@thincast.com>
6
 * Copyright 2024 Thincast Technologies GmbH
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
#include <ctype.h>
22
23
#include <freerdp/utils/helpers.h>
24
25
#include <winpr/path.h>
26
#include <winpr/file.h>
27
#include <winpr/build-config.h>
28
#include <freerdp/version.h>
29
#include <freerdp/build-config.h>
30
31
#include "../core/utils.h"
32
33
static INIT_ONCE s_freerdp_app_details_once = INIT_ONCE_STATIC_INIT;
34
static char s_freerdp_vendor_string[MAX_PATH] = WINPR_C_ARRAY_INIT;
35
static char s_freerdp_product_string[MAX_PATH] = WINPR_C_ARRAY_INIT;
36
static char s_freerdp_details_string[3ull * MAX_PATH] = WINPR_C_ARRAY_INIT;
37
static WCHAR s_freerdp_details_string_w[3ull * MAX_PATH] = WINPR_C_ARRAY_INIT;
38
static SSIZE_T s_freerdp_version = -1;
39
static BOOL s_freerdp_app_details_are_custom = FALSE;
40
41
static void updateDetailsString(void)
42
9
{
43
9
  const char* vendor = s_freerdp_vendor_string;
44
9
  const char* product = s_freerdp_product_string;
45
9
  const SSIZE_T version = s_freerdp_version;
46
47
9
  WINPR_ASSERT(vendor);
48
9
  WINPR_ASSERT(product);
49
9
  if (s_freerdp_app_details_are_custom)
50
0
  {
51
0
    if (version < 0)
52
0
      (void)_snprintf(s_freerdp_details_string, sizeof(s_freerdp_details_string) - 1, "%s-%s",
53
0
                      vendor, product);
54
0
    else
55
0
      (void)_snprintf(s_freerdp_details_string, sizeof(s_freerdp_details_string) - 1,
56
0
                      "%s-%s%" PRIdz, vendor, product, version);
57
0
  }
58
9
  else if (version < 0)
59
9
  {
60
9
    (void)_snprintf(s_freerdp_details_string, sizeof(s_freerdp_details_string) - 1, "%s",
61
9
                    product);
62
9
  }
63
0
  else
64
0
    (void)_snprintf(s_freerdp_details_string, sizeof(s_freerdp_details_string) - 1, "%s%" PRIdz,
65
0
                    product, version);
66
67
9
  (void)ConvertUtf8NToWChar(s_freerdp_details_string, sizeof(s_freerdp_details_string),
68
9
                            s_freerdp_details_string_w, sizeof(s_freerdp_details_string_w) - 1);
69
9
}
70
71
static BOOL CALLBACK init_app_details(WINPR_ATTR_UNUSED PINIT_ONCE once,
72
                                      WINPR_ATTR_UNUSED PVOID param,
73
                                      WINPR_ATTR_UNUSED PVOID* context)
74
9
{
75
9
  const size_t vlen = sizeof(FREERDP_VENDOR_STRING);
76
9
  const size_t plen = sizeof(FREERDP_PRODUCT_STRING);
77
9
  const char* rvlen = strncpy(s_freerdp_vendor_string, FREERDP_VENDOR_STRING, vlen);
78
9
  const char* rplen = strncpy(s_freerdp_product_string, FREERDP_PRODUCT_STRING, plen);
79
9
  if (!rvlen || !rplen)
80
0
    return FALSE;
81
82
#if defined(WITH_RESOURCE_VERSIONING)
83
  s_freerdp_version = FREERDP_VERSION_MAJOR;
84
#else
85
9
  s_freerdp_version = -1;
86
9
#endif
87
9
  updateDetailsString();
88
9
  return TRUE;
89
9
}
90
91
WINPR_ATTR_NODISCARD
92
static BOOL initializeApplicationDetails(void)
93
2.11M
{
94
2.11M
  return InitOnceExecuteOnce(&s_freerdp_app_details_once, init_app_details, nullptr, nullptr);
95
2.11M
}
96
97
BOOL freerdp_setApplicationDetails(const char* vendor, const char* product, SSIZE_T version)
98
0
{
99
0
  if (!initializeApplicationDetails())
100
0
    return -1;
101
102
0
  if (!vendor || !product)
103
0
    return FALSE;
104
0
  const size_t vlen = strnlen(vendor, MAX_PATH);
105
0
  const size_t plen = strnlen(product, MAX_PATH);
106
0
  if ((vlen == MAX_PATH) || (plen == MAX_PATH))
107
0
    return FALSE;
108
109
0
  if (!strncpy(s_freerdp_vendor_string, vendor, vlen + 1))
110
0
    return FALSE;
111
112
0
  if (!strncpy(s_freerdp_product_string, product, plen + 1))
113
0
    return FALSE;
114
115
0
  s_freerdp_version = version;
116
0
  s_freerdp_app_details_are_custom = TRUE;
117
118
0
  const char separator = PathGetSeparatorA(PATH_STYLE_NATIVE);
119
0
  char* str = freerdp_getApplicatonDetailsCombined(separator);
120
0
  if (!str)
121
0
    return FALSE;
122
123
0
  const BOOL rc = winpr_setApplicationDetails(str, "WinPR", -1);
124
0
  free(str);
125
0
  updateDetailsString();
126
0
  return rc;
127
0
}
128
129
const char* freerdp_getApplicationDetailsVendor(void)
130
802k
{
131
802k
  if (!initializeApplicationDetails())
132
0
    return nullptr;
133
802k
  return s_freerdp_vendor_string;
134
802k
}
135
136
const char* freerdp_getApplicationDetailsProduct(void)
137
802k
{
138
802k
  if (!initializeApplicationDetails())
139
0
    return nullptr;
140
802k
  return s_freerdp_product_string;
141
802k
}
142
143
#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
144
char* freerdp_getApplicatonDetailsRegKey(const char* fmt)
145
341k
{
146
341k
  char* val = freerdp_getApplicatonDetailsCombined('\\');
147
341k
  if (!val)
148
0
    return nullptr;
149
150
341k
  char* str = nullptr;
151
341k
  size_t slen = 0;
152
341k
  (void)winpr_asprintf(&str, &slen, fmt, val);
153
341k
  free(val);
154
341k
  return str;
155
341k
}
156
#endif
157
158
char* freerdp_getApplicatonDetailsCombined(char separator)
159
341k
{
160
341k
  const SSIZE_T version = freerdp_getApplicationDetailsVersion();
161
341k
  const char* vendor = freerdp_getApplicationDetailsVendor();
162
341k
  const char* product = freerdp_getApplicationDetailsProduct();
163
164
341k
  size_t slen = 0;
165
341k
  char* str = nullptr;
166
341k
  if (version < 0)
167
341k
  {
168
341k
    (void)winpr_asprintf(&str, &slen, "%s%c%s", vendor, separator, product);
169
341k
  }
170
0
  else
171
0
  {
172
0
    (void)winpr_asprintf(&str, &slen, "%s%c%s%" PRIdz, vendor, separator, product, version);
173
0
  }
174
175
341k
  return str;
176
341k
}
177
178
SSIZE_T freerdp_getApplicationDetailsVersion(void)
179
507k
{
180
507k
  if (!initializeApplicationDetails())
181
0
    return -1;
182
507k
  return s_freerdp_version;
183
507k
}
184
185
const char* freerdp_getApplicationDetailsString(void)
186
196k
{
187
196k
  return s_freerdp_details_string;
188
196k
}
189
190
const WCHAR* freerdp_getApplicationDetailsStringW(void)
191
0
{
192
0
  return s_freerdp_details_string_w;
193
0
}
194
195
BOOL freerdp_areApplicationDetailsCustomized(void)
196
165k
{
197
165k
  return s_freerdp_app_details_are_custom;
198
165k
}
199
200
#if !defined(WITH_FULL_CONFIG_PATH)
201
WINPR_ATTR_MALLOC(free, 1)
202
WINPR_ATTR_NODISCARD
203
static char* freerdp_settings_get_legacy_config_path(const char* filename)
204
76.9k
{
205
76.9k
  char product[sizeof(FREERDP_PRODUCT_STRING)] = WINPR_C_ARRAY_INIT;
206
207
692k
  for (size_t i = 0; i < sizeof(product); i++)
208
615k
    product[i] = (char)tolower(FREERDP_PRODUCT_STRING[i]);
209
210
76.9k
  char* path = GetKnownSubPath(KNOWN_PATH_XDG_CONFIG_HOME, product);
211
212
76.9k
  if (!path)
213
0
    return nullptr;
214
215
76.9k
  char* filepath = GetCombinedPath(path, filename);
216
76.9k
  free(path);
217
76.9k
  return filepath;
218
76.9k
}
219
#endif
220
221
WINPR_ATTR_NODISCARD
222
WINPR_ATTR_MALLOC(free, 1) static char* getCustomConfigPath(BOOL system, const char* filename)
223
0
{
224
0
  eKnownPathTypes id = system ? KNOWN_PATH_SYSTEM_CONFIG_HOME : KNOWN_PATH_XDG_CONFIG_HOME;
225
226
0
  const char* vendor = freerdp_getApplicationDetailsVendor();
227
0
  const char* product = freerdp_getApplicationDetailsProduct();
228
0
  const SSIZE_T version = freerdp_getApplicationDetailsVersion();
229
230
0
  if (!vendor || !product)
231
0
    return nullptr;
232
233
0
  char* config = GetKnownSubPathV(id, "%s", vendor);
234
0
  if (!config)
235
0
    return nullptr;
236
237
0
  char* base = nullptr;
238
0
  if (version < 0)
239
0
    base = GetCombinedPathV(config, "%s", product);
240
0
  else
241
0
    base = GetCombinedPathV(config, "%s%" PRIdz, product, version);
242
0
  free(config);
243
244
0
  if (!base)
245
0
    return nullptr;
246
247
0
  if (!filename)
248
0
    return base;
249
250
0
  char* path = GetCombinedPathV(base, "%s", filename);
251
0
  free(base);
252
0
  return path;
253
0
}
254
255
char* freerdp_GetConfigFilePath(BOOL system, const char* filename)
256
165k
{
257
#if defined(FREERDP_USE_VENDOR_PRODUCT_CONFIG_DIR)
258
  const BOOL customized = TRUE;
259
#else
260
165k
  const BOOL customized = freerdp_areApplicationDetailsCustomized();
261
165k
#endif
262
165k
  if (customized)
263
0
    return getCustomConfigPath(system, filename);
264
265
165k
  eKnownPathTypes id = system ? KNOWN_PATH_SYSTEM_CONFIG_HOME : KNOWN_PATH_XDG_CONFIG_HOME;
266
267
165k
  const char* vendor = freerdp_getApplicationDetailsVendor();
268
165k
  const char* product = freerdp_getApplicationDetailsProduct();
269
165k
  const SSIZE_T version = freerdp_getApplicationDetailsVersion();
270
271
165k
  if (!vendor || !product)
272
0
    return nullptr;
273
274
165k
#if !defined(WITH_FULL_CONFIG_PATH)
275
165k
  if (!system && (_stricmp(vendor, product) == 0))
276
76.9k
    return freerdp_settings_get_legacy_config_path(filename);
277
88.9k
#endif
278
279
88.9k
  char* config = GetKnownPath(id);
280
88.9k
  if (!config)
281
0
    return nullptr;
282
283
88.9k
  char* base = nullptr;
284
88.9k
  if (version < 0)
285
88.9k
    base = GetCombinedPathV(config, "%s", product);
286
0
  else
287
0
    base = GetCombinedPathV(config, "%s%" PRIdz, product, version);
288
88.9k
  free(config);
289
290
88.9k
  if (!base)
291
0
    return nullptr;
292
293
88.9k
  if (!filename)
294
0
    return base;
295
296
88.9k
  char* path = GetCombinedPathV(base, "%s", filename);
297
88.9k
  free(base);
298
88.9k
  return path;
299
88.9k
}
300
301
WINPR_JSON* freerdp_GetJSONConfigFile(BOOL system, const char* filename)
302
88.9k
{
303
88.9k
  char* path = freerdp_GetConfigFilePath(system, filename);
304
88.9k
  if (!path)
305
0
    return nullptr;
306
307
88.9k
  WINPR_JSON* json = WINPR_JSON_ParseFromFile(path);
308
88.9k
  free(path);
309
88.9k
  return json;
310
88.9k
}