Coverage Report

Created: 2026-03-04 06:14

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
1
{
43
1
  const char* vendor = s_freerdp_vendor_string;
44
1
  const char* product = s_freerdp_product_string;
45
1
  const SSIZE_T version = s_freerdp_version;
46
47
1
  WINPR_ASSERT(vendor);
48
1
  WINPR_ASSERT(product);
49
1
  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
1
  else if (version < 0)
59
1
  {
60
1
    (void)_snprintf(s_freerdp_details_string, sizeof(s_freerdp_details_string) - 1, "%s",
61
1
                    product);
62
1
  }
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
1
  (void)ConvertUtf8NToWChar(s_freerdp_details_string, sizeof(s_freerdp_details_string),
68
1
                            s_freerdp_details_string_w, sizeof(s_freerdp_details_string_w) - 1);
69
1
}
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
1
{
75
1
  const size_t vlen = sizeof(FREERDP_VENDOR_STRING);
76
1
  const size_t plen = sizeof(FREERDP_PRODUCT_STRING);
77
1
  const char* rvlen = strncpy(s_freerdp_vendor_string, FREERDP_VENDOR_STRING, vlen);
78
1
  const char* rplen = strncpy(s_freerdp_product_string, FREERDP_PRODUCT_STRING, plen);
79
1
  if (!rvlen || !rplen)
80
0
    return FALSE;
81
82
#if defined(WITH_RESOURCE_VERSIONING)
83
  s_freerdp_version = FREERDP_VERSION_MAJOR;
84
#else
85
1
  s_freerdp_version = -1;
86
1
#endif
87
1
  updateDetailsString();
88
1
  return TRUE;
89
1
}
90
91
WINPR_ATTR_NODISCARD
92
static BOOL initializeApplicationDetails(void)
93
34.1k
{
94
34.1k
  return InitOnceExecuteOnce(&s_freerdp_app_details_once, init_app_details, nullptr, nullptr);
95
34.1k
}
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
11.3k
{
131
11.3k
  if (!initializeApplicationDetails())
132
0
    return nullptr;
133
11.3k
  return s_freerdp_vendor_string;
134
11.3k
}
135
136
const char* freerdp_getApplicationDetailsProduct(void)
137
11.3k
{
138
11.3k
  if (!initializeApplicationDetails())
139
0
    return nullptr;
140
11.3k
  return s_freerdp_product_string;
141
11.3k
}
142
143
char* freerdp_getApplicatonDetailsRegKey(const char* fmt)
144
11.3k
{
145
11.3k
  char* val = freerdp_getApplicatonDetailsCombined('\\');
146
11.3k
  if (!val)
147
0
    return nullptr;
148
149
11.3k
  char* str = nullptr;
150
11.3k
  size_t slen = 0;
151
11.3k
  (void)winpr_asprintf(&str, &slen, fmt, val);
152
11.3k
  free(val);
153
11.3k
  return str;
154
11.3k
}
155
156
char* freerdp_getApplicatonDetailsCombined(char separator)
157
11.3k
{
158
11.3k
  const SSIZE_T version = freerdp_getApplicationDetailsVersion();
159
11.3k
  const char* vendor = freerdp_getApplicationDetailsVendor();
160
11.3k
  const char* product = freerdp_getApplicationDetailsProduct();
161
162
11.3k
  size_t slen = 0;
163
11.3k
  char* str = nullptr;
164
11.3k
  if (version < 0)
165
11.3k
  {
166
11.3k
    (void)winpr_asprintf(&str, &slen, "%s%c%s", vendor, separator, product);
167
11.3k
  }
168
0
  else
169
0
  {
170
0
    (void)winpr_asprintf(&str, &slen, "%s%c%s%" PRIdz, vendor, separator, product, version);
171
0
  }
172
173
11.3k
  return str;
174
11.3k
}
175
176
SSIZE_T freerdp_getApplicationDetailsVersion(void)
177
11.3k
{
178
11.3k
  if (!initializeApplicationDetails())
179
0
    return -1;
180
11.3k
  return s_freerdp_version;
181
11.3k
}
182
183
const char* freerdp_getApplicationDetailsString(void)
184
0
{
185
0
  return s_freerdp_details_string;
186
0
}
187
188
const WCHAR* freerdp_getApplicationDetailsStringW(void)
189
0
{
190
0
  return s_freerdp_details_string_w;
191
0
}
192
193
BOOL freerdp_areApplicationDetailsCustomized(void)
194
0
{
195
0
  return s_freerdp_app_details_are_custom;
196
0
}
197
198
#if !defined(WITH_FULL_CONFIG_PATH)
199
WINPR_ATTR_MALLOC(free, 1)
200
WINPR_ATTR_NODISCARD
201
static char* freerdp_settings_get_legacy_config_path(const char* filename)
202
0
{
203
0
  char product[sizeof(FREERDP_PRODUCT_STRING)] = WINPR_C_ARRAY_INIT;
204
205
0
  for (size_t i = 0; i < sizeof(product); i++)
206
0
    product[i] = (char)tolower(FREERDP_PRODUCT_STRING[i]);
207
208
0
  char* path = GetKnownSubPath(KNOWN_PATH_XDG_CONFIG_HOME, product);
209
210
0
  if (!path)
211
0
    return nullptr;
212
213
0
  char* filepath = GetCombinedPath(path, filename);
214
0
  free(path);
215
0
  return filepath;
216
0
}
217
#endif
218
219
WINPR_ATTR_NODISCARD
220
WINPR_ATTR_MALLOC(free, 1) static char* getCustomConfigPath(BOOL system, const char* filename)
221
0
{
222
0
  eKnownPathTypes id = system ? KNOWN_PATH_SYSTEM_CONFIG_HOME : KNOWN_PATH_XDG_CONFIG_HOME;
223
224
0
  const char* vendor = freerdp_getApplicationDetailsVendor();
225
0
  const char* product = freerdp_getApplicationDetailsProduct();
226
0
  const SSIZE_T version = freerdp_getApplicationDetailsVersion();
227
228
0
  if (!vendor || !product)
229
0
    return nullptr;
230
231
0
  char* config = GetKnownSubPathV(id, "%s", vendor);
232
0
  if (!config)
233
0
    return nullptr;
234
235
0
  char* base = nullptr;
236
0
  if (version < 0)
237
0
    base = GetCombinedPathV(config, "%s", product);
238
0
  else
239
0
    base = GetCombinedPathV(config, "%s%" PRIdz, product, version);
240
0
  free(config);
241
242
0
  if (!base)
243
0
    return nullptr;
244
245
0
  if (!filename)
246
0
    return base;
247
248
0
  char* path = GetCombinedPathV(base, "%s", filename);
249
0
  free(base);
250
0
  return path;
251
0
}
252
253
char* freerdp_GetConfigFilePath(BOOL system, const char* filename)
254
0
{
255
#if defined(FREERDP_USE_VENDOR_PRODUCT_CONFIG_DIR)
256
  const BOOL customized = TRUE;
257
#else
258
0
  const BOOL customized = freerdp_areApplicationDetailsCustomized();
259
0
#endif
260
0
  if (customized)
261
0
    return getCustomConfigPath(system, filename);
262
263
0
  eKnownPathTypes id = system ? KNOWN_PATH_SYSTEM_CONFIG_HOME : KNOWN_PATH_XDG_CONFIG_HOME;
264
265
0
  const char* vendor = freerdp_getApplicationDetailsVendor();
266
0
  const char* product = freerdp_getApplicationDetailsProduct();
267
0
  const SSIZE_T version = freerdp_getApplicationDetailsVersion();
268
269
0
  if (!vendor || !product)
270
0
    return nullptr;
271
272
0
#if !defined(WITH_FULL_CONFIG_PATH)
273
0
  if (!system && (_stricmp(vendor, product) == 0))
274
0
    return freerdp_settings_get_legacy_config_path(filename);
275
0
#endif
276
277
0
  char* config = GetKnownPath(id);
278
0
  if (!config)
279
0
    return nullptr;
280
281
0
  char* base = nullptr;
282
0
  if (version < 0)
283
0
    base = GetCombinedPathV(config, "%s", product);
284
0
  else
285
0
    base = GetCombinedPathV(config, "%s%" PRIdz, product, version);
286
0
  free(config);
287
288
0
  if (!base)
289
0
    return nullptr;
290
291
0
  if (!filename)
292
0
    return base;
293
294
0
  char* path = GetCombinedPathV(base, "%s", filename);
295
0
  free(base);
296
0
  return path;
297
0
}
298
299
WINPR_JSON* freerdp_GetJSONConfigFile(BOOL system, const char* filename)
300
0
{
301
0
  char* path = freerdp_GetConfigFilePath(system, filename);
302
0
  if (!path)
303
0
    return nullptr;
304
305
0
  WINPR_JSON* json = WINPR_JSON_ParseFromFile(path);
306
0
  free(path);
307
0
  return json;
308
0
}