Coverage Report

Created: 2026-03-04 06:14

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
34.1k
{
70
34.1k
  return InitOnceExecuteOnce(&s_winpr_app_details_once, init_app_details, nullptr, nullptr);
71
34.1k
}
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
11.3k
{
98
11.3k
  if (!initializeApplicationDetails())
99
0
    return nullptr;
100
11.3k
  return s_winpr_vendor_string;
101
11.3k
}
102
103
const char* winpr_getApplicationDetailsProduct(void)
104
11.3k
{
105
11.3k
  if (!initializeApplicationDetails())
106
0
    return nullptr;
107
11.3k
  return s_winpr_product_string;
108
11.3k
}
109
110
char* winpr_getApplicatonDetailsRegKey(const char* fmt)
111
0
{
112
0
  char* val = winpr_getApplicatonDetailsCombined('\\');
113
0
  if (!val)
114
0
    return nullptr;
115
116
0
  char* str = nullptr;
117
0
  size_t slen = 0;
118
0
  (void)winpr_asprintf(&str, &slen, fmt, val);
119
0
  free(val);
120
0
  return str;
121
0
}
122
123
char* winpr_getApplicatonDetailsCombined(char separator)
124
0
{
125
0
  const SSIZE_T version = winpr_getApplicationDetailsVersion();
126
0
  const char* vendor = winpr_getApplicationDetailsVendor();
127
0
  const char* product = winpr_getApplicationDetailsProduct();
128
129
0
  size_t slen = 0;
130
0
  char* str = nullptr;
131
0
  if (version < 0)
132
0
  {
133
0
    (void)winpr_asprintf(&str, &slen, "%s%c%s", vendor, separator, product);
134
0
  }
135
0
  else
136
0
  {
137
0
    (void)winpr_asprintf(&str, &slen, "%s%c%s%" PRIdz, vendor, separator, product, version);
138
0
  }
139
140
0
  return str;
141
0
}
142
143
SSIZE_T winpr_getApplicationDetailsVersion(void)
144
11.3k
{
145
11.3k
  if (!initializeApplicationDetails())
146
0
    return -1;
147
11.3k
  return s_winpr_version;
148
11.3k
}
149
150
BOOL winpr_areApplicationDetailsCustomized(void)
151
0
{
152
0
  return s_winpr_app_details_are_custom;
153
0
}
154
155
void winpr_get_version(int* major, int* minor, int* revision)
156
0
{
157
0
  if (major)
158
0
    *major = WINPR_VERSION_MAJOR;
159
0
  if (minor)
160
0
    *minor = WINPR_VERSION_MINOR;
161
0
  if (revision)
162
0
    *revision = WINPR_VERSION_REVISION;
163
0
}
164
165
const char* winpr_get_version_string(void)
166
0
{
167
0
  return WINPR_VERSION_FULL;
168
0
}
169
170
const char* winpr_get_build_revision(void)
171
0
{
172
0
  return WINPR_GIT_REVISION;
173
0
}
174
175
const char* winpr_get_build_config(void)
176
0
{
177
0
  static const char build_config[] =
178
0
      "Build configuration: " WINPR_BUILD_CONFIG "\n"
179
0
      "Build type:          " WINPR_BUILD_TYPE "\n"
180
0
      "CFLAGS:              " WINPR_CFLAGS "\n"
181
0
      "Compiler:            " WINPR_COMPILER_ID ", " WINPR_COMPILER_VERSION "\n"
182
0
      "Target architecture: " WINPR_TARGET_ARCH "\n";
183
184
0
  return build_config;
185
0
}