Coverage Report

Created: 2026-10-15 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/utils/winpr.c
Line
Count
Source
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * Debugging Utils
4
 *
5
 * Copyright 2015 Armin Novak <armin.novak@thincast.com>
6
 * Copyright 2015 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 <winpr/config.h>
22
23
#include <winpr/buildflags.h>
24
25
#include <stdlib.h>
26
#include <stdio.h>
27
#include <winpr/crt.h>
28
#include <winpr/string.h>
29
#include <winpr/winpr.h>
30
#include <winpr/version.h>
31
#include <winpr/wlog.h>
32
#include <winpr/file.h>
33
#include <winpr/build-config.h>
34
35
#include "../utils.h"
36
37
#if !defined(WIN32)
38
#include <pthread.h>
39
#endif
40
41
static INIT_ONCE s_winpr_app_details_once = INIT_ONCE_STATIC_INIT;
42
static char s_winpr_vendor_string[MAX_PATH] = WINPR_C_ARRAY_INIT;
43
static char s_winpr_product_string[MAX_PATH] = WINPR_C_ARRAY_INIT;
44
static SSIZE_T s_winpr_version = -1;
45
static BOOL s_winpr_app_details_are_custom = FALSE;
46
47
static BOOL CALLBACK init_app_details(WINPR_ATTR_UNUSED PINIT_ONCE once,
48
                                      WINPR_ATTR_UNUSED PVOID param,
49
                                      WINPR_ATTR_UNUSED PVOID* context)
50
1
{
51
1
  const size_t vlen = sizeof(WINPR_VENDOR_STRING);
52
1
  const size_t plen = sizeof(WINPR_PRODUCT_STRING);
53
1
  if (!strncpy(s_winpr_vendor_string, WINPR_VENDOR_STRING, vlen))
54
0
    return FALSE;
55
56
1
  if (!strncpy(s_winpr_product_string, WINPR_PRODUCT_STRING, plen))
57
0
    return FALSE;
58
59
#if defined(WITH_RESOURCE_VERSIONING)
60
  s_winpr_version = WINPR_VERSION_MAJOR;
61
#else
62
1
  s_winpr_version = -1;
63
1
#endif
64
1
  return TRUE;
65
1
}
66
67
WINPR_ATTR_NODISCARD
68
static BOOL initializeApplicationDetails(void)
69
39.8k
{
70
39.8k
  return InitOnceExecuteOnce(&s_winpr_app_details_once, init_app_details, nullptr, nullptr);
71
39.8k
}
72
73
BOOL winpr_setApplicationDetails(const char* vendor, const char* product, SSIZE_T version)
74
0
{
75
0
  if (!initializeApplicationDetails())
76
0
    return -1;
77
78
0
  if (!vendor || !product)
79
0
    return FALSE;
80
0
  const size_t vlen = strnlen(vendor, MAX_PATH);
81
0
  const size_t plen = strnlen(product, MAX_PATH);
82
0
  if ((vlen == MAX_PATH) || (plen == MAX_PATH))
83
0
    return FALSE;
84
85
0
  if (!strncpy(s_winpr_vendor_string, vendor, vlen + 1))
86
0
    return FALSE;
87
88
0
  if (!strncpy(s_winpr_product_string, product, plen + 1))
89
0
    return FALSE;
90
91
0
  s_winpr_version = version;
92
0
  s_winpr_app_details_are_custom = TRUE;
93
0
  return TRUE;
94
0
}
95
96
const char* winpr_getApplicationDetailsVendor(void)
97
13.2k
{
98
13.2k
  if (!initializeApplicationDetails())
99
0
    return nullptr;
100
13.2k
  return s_winpr_vendor_string;
101
13.2k
}
102
103
const char* winpr_getApplicationDetailsProduct(void)
104
13.2k
{
105
13.2k
  if (!initializeApplicationDetails())
106
0
    return nullptr;
107
13.2k
  return s_winpr_product_string;
108
13.2k
}
109
110
#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
111
char* winpr_getApplicatonDetailsRegKey(const char* fmt)
112
0
{
113
0
  char* val = winpr_getApplicatonDetailsCombined('\\');
114
0
  if (!val)
115
0
    return nullptr;
116
117
0
  char* str = nullptr;
118
0
  size_t slen = 0;
119
0
  (void)winpr_asprintf(&str, &slen, fmt, val);
120
0
  free(val);
121
0
  return str;
122
0
}
123
#endif
124
125
char* winpr_getApplicatonDetailsCombined(char separator)
126
0
{
127
0
  const SSIZE_T version = winpr_getApplicationDetailsVersion();
128
0
  const char* vendor = winpr_getApplicationDetailsVendor();
129
0
  const char* product = winpr_getApplicationDetailsProduct();
130
131
0
  size_t slen = 0;
132
0
  char* str = nullptr;
133
0
  if (version < 0)
134
0
  {
135
0
    (void)winpr_asprintf(&str, &slen, "%s%c%s", vendor, separator, product);
136
0
  }
137
0
  else
138
0
  {
139
0
    (void)winpr_asprintf(&str, &slen, "%s%c%s%" PRIdz, vendor, separator, product, version);
140
0
  }
141
142
0
  return str;
143
0
}
144
145
SSIZE_T winpr_getApplicationDetailsVersion(void)
146
13.2k
{
147
13.2k
  if (!initializeApplicationDetails())
148
0
    return -1;
149
13.2k
  return s_winpr_version;
150
13.2k
}
151
152
BOOL winpr_areApplicationDetailsCustomized(void)
153
0
{
154
0
  return s_winpr_app_details_are_custom;
155
0
}
156
157
void winpr_get_version(int* major, int* minor, int* revision)
158
0
{
159
0
  if (major)
160
0
    *major = WINPR_VERSION_MAJOR;
161
0
  if (minor)
162
0
    *minor = WINPR_VERSION_MINOR;
163
0
  if (revision)
164
0
    *revision = WINPR_VERSION_REVISION;
165
0
}
166
167
const char* winpr_get_version_string(void)
168
0
{
169
0
  return WINPR_VERSION_FULL;
170
0
}
171
172
const char* winpr_get_build_revision(void)
173
0
{
174
0
  return WINPR_GIT_REVISION;
175
0
}
176
177
const char* winpr_get_build_config(void)
178
0
{
179
0
  static const char build_config[] =
180
0
      "Build configuration: " WINPR_BUILD_CONFIG "\n"
181
0
      "Build type:          " WINPR_BUILD_TYPE "\n"
182
0
      "CFLAGS:              " WINPR_CFLAGS "\n"
183
0
      "Compiler:            " WINPR_COMPILER_ID ", " WINPR_COMPILER_VERSION "\n"
184
0
      "Target architecture: " WINPR_TARGET_ARCH "\n";
185
186
0
  return build_config;
187
0
}
188
189
WCHAR* winpr_wcsndup(const WCHAR* str, size_t charCount)
190
0
{
191
0
  if (!str || (charCount == 0))
192
0
    return nullptr;
193
194
0
  WCHAR* copy = calloc(charCount + 1, sizeof(WCHAR));
195
0
  if (!copy)
196
0
    return nullptr;
197
0
  memcpy(copy, str, charCount * sizeof(WCHAR));
198
0
  return copy;
199
0
}