Coverage Report

Created: 2026-11-10 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/timezone/TimeZoneIanaAliases.c
Line
Count
Source
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * Renamed IANA time zones
4
 *
5
 * Copyright 2026 Island Technology, Inc.
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
#include <winpr/config.h>
20
21
#include <winpr/atexit.h>
22
#include <winpr/assert.h>
23
#include <winpr/string.h>
24
#include <winpr/synch.h>
25
#include <winpr/json.h>
26
#include <winpr/path.h>
27
#include <winpr/file.h>
28
29
#include "../log.h"
30
31
#include <string.h>
32
33
#define TAG WINPR_TAG("timezone.aliases")
34
35
#include "TimeZoneNameMap.h"
36
#include "TimeZoneIanaAliases.h"
37
38
#if defined(WITH_TIMEZONE_COMPILED)
39
#include "TimeZoneIanaAliases_static.h"
40
#endif
41
42
typedef struct
43
{
44
  size_t count;
45
  size_t size;
46
  TimeZoneIanaAliasEntry* entries;
47
} TimeZoneIanaAliasContext;
48
49
static TimeZoneIanaAliasContext alias_context = WINPR_C_ARRAY_INIT;
50
51
static void alias_context_free(void)
52
0
{
53
0
  for (size_t x = 0; x < alias_context.count; x++)
54
0
  {
55
0
    free(alias_context.entries[x].Current);
56
0
    free(alias_context.entries[x].Legacy);
57
0
  }
58
0
  free(alias_context.entries);
59
60
0
  const TimeZoneIanaAliasContext empty = WINPR_C_ARRAY_INIT;
61
0
  alias_context = empty;
62
0
}
63
64
WINPR_ATTR_NODISCARD
65
static BOOL alias_reserve(TimeZoneIanaAliasContext* context, size_t size_to_add)
66
0
{
67
0
  WINPR_ASSERT(context);
68
69
0
  TimeZoneIanaAliasEntry* tmp =
70
0
      realloc(context->entries, (context->size + size_to_add) * sizeof(TimeZoneIanaAliasEntry));
71
0
  if (!tmp)
72
0
  {
73
0
    WLog_WARN(TAG,
74
0
              "Failed to reallocate TimeZoneIanaAliasEntry::entries to %" PRIuz " elements",
75
0
              context->size + size_to_add);
76
0
    return FALSE;
77
0
  }
78
0
  context->entries = tmp;
79
0
  context->size += size_to_add;
80
0
  return TRUE;
81
0
}
82
83
/* takes ownership of current and legacy, source and pos name the entry in log messages */
84
WINPR_ATTR_NODISCARD
85
static BOOL alias_add(TimeZoneIanaAliasContext* context, const char* source, size_t pos,
86
                      char* current, char* legacy)
87
0
{
88
0
  WINPR_ASSERT(context);
89
0
  WINPR_ASSERT(source);
90
0
  WINPR_ASSERT(context->count < context->size);
91
92
0
  if (!current || !legacy)
93
0
  {
94
0
    WLog_WARN(TAG, "Invalid timezone alias at entry %" PRIuz " of %s", pos, source);
95
0
    free(current);
96
0
    free(legacy);
97
0
    return FALSE;
98
0
  }
99
100
0
  context->entries[context->count].Current = current;
101
0
  context->entries[context->count].Legacy = legacy;
102
0
  context->count++;
103
0
  return TRUE;
104
0
}
105
106
#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
107
static void load_aliases_from_file(TimeZoneIanaAliasContext* context)
108
{
109
  WINPR_JSON* json = nullptr;
110
  char* filename = GetCombinedPath(WINPR_RESOURCE_ROOT, "TimeZoneIanaAliases.json");
111
  if (!filename)
112
  {
113
    WLog_WARN(TAG, "Could not create WinPR timezone alias resource filename");
114
    goto end;
115
  }
116
117
  json = TimeZoneJsonParseFile(filename);
118
  if (!json)
119
    goto end;
120
121
  WINPR_JSON* obj = WINPR_JSON_IsObject(json)
122
                        ? WINPR_JSON_GetObjectItemCaseSensitive(json, "TimeZoneIanaAliases")
123
                        : nullptr;
124
  if (!WINPR_JSON_IsArray(obj))
125
  {
126
    WLog_WARN(TAG, "Invalid top level JSON type in file %s, expected an array", filename);
127
    goto end;
128
  }
129
130
  const size_t count = WINPR_JSON_GetArraySize(obj);
131
  if (!alias_reserve(context, count))
132
    goto end;
133
  for (size_t x = 0; x < count; x++)
134
  {
135
    WINPR_JSON* entry = WINPR_JSON_GetArrayItem(obj, x);
136
    if (!alias_add(context, filename, x, TimeZoneJsonGetString(entry, x, "Current"),
137
                   TimeZoneJsonGetString(entry, x, "Legacy")))
138
      goto end;
139
  }
140
141
end:
142
  free(filename);
143
  WINPR_JSON_Delete(json);
144
}
145
#endif
146
147
static BOOL CALLBACK load_aliases(PINIT_ONCE once, PVOID param, PVOID* pvcontext)
148
0
{
149
0
  TimeZoneIanaAliasContext* context = param;
150
0
  WINPR_ASSERT(context);
151
0
  WINPR_UNUSED(pvcontext);
152
0
  WINPR_UNUSED(once);
153
154
#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
155
  load_aliases_from_file(context);
156
#endif
157
158
0
#if defined(WITH_TIMEZONE_COMPILED)
159
0
  if (!alias_reserve(context, TimeZoneIanaAliasesSize))
160
0
  {
161
0
    alias_context_free();
162
0
    return FALSE;
163
0
  }
164
0
  for (size_t x = 0; x < TimeZoneIanaAliasesSize; x++)
165
0
  {
166
0
    if (!alias_add(context, "the compiled in table", x, _strdup(TimeZoneIanaAliases[x].Current),
167
0
                   _strdup(TimeZoneIanaAliases[x].Legacy)))
168
0
    {
169
0
      alias_context_free();
170
0
      return FALSE;
171
0
    }
172
0
  }
173
0
#endif
174
175
0
  (void)winpr_atexit(alias_context_free);
176
0
  return TRUE;
177
0
}
178
179
const char* TimeZoneIanaAlias(const char* iana)
180
0
{
181
0
  static INIT_ONCE init_guard = INIT_ONCE_STATIC_INIT;
182
183
0
  if (!iana)
184
0
    return nullptr;
185
0
  if (!InitOnceExecuteOnce(&init_guard, load_aliases, &alias_context, nullptr))
186
0
    return nullptr;
187
188
0
  for (size_t x = 0; x < alias_context.count; x++)
189
0
  {
190
0
    const TimeZoneIanaAliasEntry* alias = &alias_context.entries[x];
191
0
    if (strcmp(iana, alias->Current) == 0)
192
0
      return alias->Legacy;
193
0
    if (strcmp(iana, alias->Legacy) == 0)
194
0
      return alias->Current;
195
0
  }
196
0
  return nullptr;
197
0
}