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/TimeZoneNameMapUtils.c
Line
Count
Source
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * Time Zone Name Map Utils
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
#include <winpr/config.h>
21
22
#include <winpr/atexit.h>
23
#include <winpr/assert.h>
24
#include <winpr/string.h>
25
#include <winpr/synch.h>
26
#include <winpr/json.h>
27
#include <winpr/path.h>
28
#include <winpr/file.h>
29
30
#include "../log.h"
31
32
#include <string.h>
33
34
#define TAG WINPR_TAG("timezone.utils")
35
36
#if defined(WITH_TIMEZONE_ICU)
37
#include <unicode/ucal.h>
38
#else
39
#include "WindowsZones.h"
40
#endif
41
42
#include "TimeZoneNameMap.h"
43
#include "TimeZoneIanaAliases.h"
44
45
#if defined(WITH_TIMEZONE_COMPILED)
46
#include "TimeZoneNameMap_static.h"
47
#endif
48
49
typedef struct
50
{
51
  size_t count;
52
  TimeZoneNameMapEntry* entries;
53
} TimeZoneNameMapContext;
54
55
static TimeZoneNameMapContext tz_context = WINPR_C_ARRAY_INIT;
56
57
static void tz_entry_free(TimeZoneNameMapEntry* entry)
58
141
{
59
141
  if (!entry)
60
0
    return;
61
141
  free(entry->DaylightName);
62
141
  free(entry->DisplayName);
63
141
  free(entry->Iana);
64
141
  free(entry->Id);
65
141
  free(entry->StandardName);
66
67
141
  const TimeZoneNameMapEntry empty = WINPR_C_ARRAY_INIT;
68
141
  *entry = empty;
69
141
}
70
71
static TimeZoneNameMapEntry tz_entry_clone(const TimeZoneNameMapEntry* entry)
72
141
{
73
141
  TimeZoneNameMapEntry clone = WINPR_C_ARRAY_INIT;
74
141
  if (!entry)
75
0
    return clone;
76
77
141
  if (entry->DaylightName)
78
141
    clone.DaylightName = _strdup(entry->DaylightName);
79
141
  if (entry->DisplayName)
80
141
    clone.DisplayName = _strdup(entry->DisplayName);
81
141
  if (entry->Iana)
82
141
    clone.Iana = _strdup(entry->Iana);
83
141
  if (entry->Id)
84
141
    clone.Id = _strdup(entry->Id);
85
141
  if (entry->StandardName)
86
141
    clone.StandardName = _strdup(entry->StandardName);
87
141
  return clone;
88
141
}
89
90
static void tz_context_free(void)
91
1
{
92
142
  for (size_t x = 0; x < tz_context.count; x++)
93
141
    tz_entry_free(&tz_context.entries[x]);
94
1
  free(tz_context.entries);
95
1
  tz_context.count = 0;
96
1
  tz_context.entries = nullptr;
97
1
}
98
99
#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
100
char* TimeZoneJsonGetString(WINPR_JSON* json, size_t pos, const char* name)
101
{
102
  WINPR_ASSERT(json);
103
  if (!WINPR_JSON_IsObject(json) || !WINPR_JSON_HasObjectItem(json, name))
104
  {
105
    WLog_WARN(TAG, "Invalid JSON entry at entry %" PRIuz ", missing an Object named '%s'", pos,
106
              name);
107
    return nullptr;
108
  }
109
  WINPR_JSON* obj = WINPR_JSON_GetObjectItemCaseSensitive(json, name);
110
  WINPR_ASSERT(obj);
111
  if (!WINPR_JSON_IsString(obj))
112
  {
113
    WLog_WARN(TAG,
114
              "Invalid JSON entry at entry %" PRIuz ", Object named '%s': Not of type string",
115
              pos, name);
116
    return nullptr;
117
  }
118
119
  const char* str = WINPR_JSON_GetStringValue(obj);
120
  if (!str)
121
  {
122
    WLog_WARN(TAG, "Invalid JSON entry at entry %" PRIuz ", Object named '%s': nullptr string",
123
              pos, name);
124
    return nullptr;
125
  }
126
127
  return _strdup(str);
128
}
129
130
static BOOL tz_parse_json_entry(WINPR_JSON* json, size_t pos, TimeZoneNameMapEntry* entry)
131
{
132
  WINPR_ASSERT(entry);
133
  if (!json || !WINPR_JSON_IsObject(json))
134
  {
135
    WLog_WARN(TAG, "Invalid JSON entry at entry %" PRIuz ", expected an array", pos);
136
    return FALSE;
137
  }
138
139
  entry->Id = TimeZoneJsonGetString(json, pos, "Id");
140
  entry->StandardName = TimeZoneJsonGetString(json, pos, "StandardName");
141
  entry->DisplayName = TimeZoneJsonGetString(json, pos, "DisplayName");
142
  entry->DaylightName = TimeZoneJsonGetString(json, pos, "DaylightName");
143
  entry->Iana = TimeZoneJsonGetString(json, pos, "Iana");
144
  if (!entry->Id || !entry->StandardName || !entry->DisplayName || !entry->DaylightName ||
145
      !entry->Iana)
146
  {
147
    tz_entry_free(entry);
148
    return FALSE;
149
  }
150
  return TRUE;
151
}
152
153
WINPR_JSON* TimeZoneJsonParseFile(const char* filename)
154
{
155
  WINPR_JSON* json = WINPR_JSON_ParseFromFile(filename);
156
  if (!json)
157
    WLog_WARN(TAG, "Timezone resource file '%s' is not a valid JSON file", filename);
158
  return json;
159
}
160
#endif
161
162
static BOOL reallocate_context(TimeZoneNameMapContext* context, size_t size_to_add)
163
1
{
164
1
  {
165
1
    TimeZoneNameMapEntry* tmp = realloc(context->entries, (context->count + size_to_add) *
166
1
                                                              sizeof(TimeZoneNameMapEntry));
167
1
    if (!tmp)
168
0
    {
169
0
      WLog_WARN(TAG,
170
0
                "Failed to reallocate TimeZoneNameMapEntry::entries to %" PRIuz " elements",
171
0
                context->count + size_to_add);
172
0
      return FALSE;
173
0
    }
174
1
    const size_t offset = context->count;
175
1
    context->entries = tmp;
176
1
    context->count += size_to_add;
177
178
1
    memset(&context->entries[offset], 0, size_to_add * sizeof(TimeZoneNameMapEntry));
179
1
  }
180
1
  return TRUE;
181
1
}
182
183
static BOOL CALLBACK load_timezones(PINIT_ONCE once, PVOID param, PVOID* pvcontext)
184
1
{
185
1
  TimeZoneNameMapContext* context = param;
186
1
  WINPR_ASSERT(context);
187
1
  WINPR_UNUSED(pvcontext);
188
1
  WINPR_UNUSED(once);
189
190
1
  const TimeZoneNameMapContext empty = WINPR_C_ARRAY_INIT;
191
1
  *context = empty;
192
193
#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
194
  {
195
    WINPR_JSON* json = nullptr;
196
    char* filename = GetCombinedPath(WINPR_RESOURCE_ROOT, "TimeZoneNameMap.json");
197
    if (!filename)
198
    {
199
      WLog_WARN(TAG, "Could not create WinPR timezone resource filename");
200
      goto end;
201
    }
202
203
    json = TimeZoneJsonParseFile(filename);
204
    if (!json)
205
      goto end;
206
207
    if (!WINPR_JSON_IsObject(json))
208
    {
209
      WLog_WARN(TAG, "Invalid top level JSON type in file %s, expected an array", filename);
210
      goto end;
211
    }
212
213
    WINPR_JSON* obj = WINPR_JSON_GetObjectItemCaseSensitive(json, "TimeZoneNameMap");
214
    if (!WINPR_JSON_IsArray(obj))
215
    {
216
      WLog_WARN(TAG, "Invalid top level JSON type in file %s, expected an array", filename);
217
      goto end;
218
    }
219
    const size_t count = WINPR_JSON_GetArraySize(obj);
220
    const size_t offset = context->count;
221
    if (!reallocate_context(context, count))
222
      goto end;
223
    for (size_t x = 0; x < count; x++)
224
    {
225
      WINPR_JSON* entry = WINPR_JSON_GetArrayItem(obj, x);
226
      if (!tz_parse_json_entry(entry, x, &context->entries[offset + x]))
227
        goto end;
228
    }
229
230
  end:
231
    free(filename);
232
    WINPR_JSON_Delete(json);
233
  }
234
#endif
235
236
1
#if defined(WITH_TIMEZONE_COMPILED)
237
1
  {
238
1
    const size_t offset = context->count;
239
1
    if (!reallocate_context(context, TimeZoneNameMapSize))
240
0
      return FALSE;
241
142
    for (size_t x = 0; x < TimeZoneNameMapSize; x++)
242
141
      context->entries[offset + x] = tz_entry_clone(&TimeZoneNameMap[x]);
243
1
  }
244
0
#endif
245
246
0
  (void)winpr_atexit(tz_context_free);
247
1
  return TRUE;
248
1
}
249
250
const TimeZoneNameMapEntry* TimeZoneGetAt(size_t index)
251
6.76k
{
252
6.76k
  static INIT_ONCE init_guard = INIT_ONCE_STATIC_INIT;
253
254
6.76k
  if (!InitOnceExecuteOnce(&init_guard, load_timezones, &tz_context, nullptr))
255
0
    return nullptr;
256
6.76k
  if (index >= tz_context.count)
257
0
    return nullptr;
258
6.76k
  return &tz_context.entries[index];
259
6.76k
}
260
261
static const char* return_type(const TimeZoneNameMapEntry* entry, TimeZoneNameType type)
262
144
{
263
144
  WINPR_ASSERT(entry);
264
144
  switch (type)
265
144
  {
266
0
    case TIME_ZONE_NAME_IANA:
267
0
      return entry->Iana;
268
48
    case TIME_ZONE_NAME_ID:
269
48
      return entry->Id;
270
48
    case TIME_ZONE_NAME_STANDARD:
271
48
      return entry->StandardName;
272
0
    case TIME_ZONE_NAME_DISPLAY:
273
0
      return entry->DisplayName;
274
48
    case TIME_ZONE_NAME_DAYLIGHT:
275
48
      return entry->DaylightName;
276
0
    default:
277
0
      return nullptr;
278
144
  }
279
144
}
280
281
static BOOL iana_cmp(const TimeZoneNameMapEntry* entry, const char* iana)
282
6.76k
{
283
6.76k
  if (!entry || !iana || !entry->Iana)
284
0
    return FALSE;
285
6.76k
  return strcmp(iana, entry->Iana) == 0;
286
6.76k
}
287
288
static BOOL id_cmp(const TimeZoneNameMapEntry* entry, const char* id)
289
0
{
290
0
  if (!entry || !id || !entry->Id)
291
0
    return FALSE;
292
0
  return strcmp(id, entry->Id) == 0;
293
0
}
294
295
static const char* get_for_type(const char* val, TimeZoneNameType type,
296
                                BOOL (*cmp)(const TimeZoneNameMapEntry*, const char*))
297
144
{
298
144
  WINPR_ASSERT(val);
299
144
  WINPR_ASSERT(cmp);
300
301
144
  size_t index = 0;
302
6.76k
  while (TRUE)
303
6.76k
  {
304
6.76k
    const TimeZoneNameMapEntry* entry = TimeZoneGetAt(index++);
305
6.76k
    if (!entry)
306
0
      return nullptr;
307
6.76k
    if (cmp(entry, val))
308
144
      return return_type(entry, type);
309
6.76k
  }
310
144
}
311
312
#if defined(WITH_TIMEZONE_ICU)
313
static char* get_wzid_icu(const UChar* utzid, size_t utzid_len)
314
{
315
  char* res = nullptr;
316
  UErrorCode error = U_ZERO_ERROR;
317
318
  int32_t rc = ucal_getWindowsTimeZoneID(utzid, WINPR_ASSERTING_INT_CAST(int32_t, utzid_len),
319
                                         nullptr, 0, &error);
320
  if ((error == U_BUFFER_OVERFLOW_ERROR) && (rc > 0))
321
  {
322
    rc++; // make space for '\0'
323
    UChar* wzid = calloc((size_t)rc + 1, sizeof(UChar));
324
    if (wzid)
325
    {
326
      UErrorCode error2 = U_ZERO_ERROR;
327
      int32_t rc2 = ucal_getWindowsTimeZoneID(
328
          utzid, WINPR_ASSERTING_INT_CAST(int32_t, utzid_len), wzid, rc, &error2);
329
      if (U_SUCCESS(error2) && (rc2 > 0))
330
        res = ConvertWCharNToUtf8Alloc(wzid, (size_t)rc, nullptr);
331
      free(wzid);
332
    }
333
  }
334
  return res;
335
}
336
337
static char* get(const char* iana)
338
{
339
  size_t utzid_len = 0;
340
  UChar* utzid = ConvertUtf8ToWCharAlloc(iana, &utzid_len);
341
  if (!utzid)
342
    return nullptr;
343
344
  char* wzid = get_wzid_icu(utzid, utzid_len);
345
  free(utzid);
346
  return wzid;
347
}
348
349
static const char* map_fallback(const char* iana, TimeZoneNameType type)
350
{
351
  char* wzid = get(iana);
352
  if (!wzid)
353
    return nullptr;
354
355
  const char* res = get_for_type(wzid, type, id_cmp);
356
  free(wzid);
357
  return res;
358
}
359
#else
360
static const char* map_fallback(const char* iana, WINPR_ATTR_UNUSED TimeZoneNameType type)
361
0
{
362
0
  if (!iana)
363
0
    return nullptr;
364
365
0
  for (size_t x = 0; x < WindowsZonesNrElements; x++)
366
0
  {
367
0
    const WINDOWS_TZID_ENTRY* const entry = &WindowsZones[x];
368
0
    if (strchr(entry->tzid, ' '))
369
0
    {
370
0
      const char* res = nullptr;
371
0
      char* tzid = _strdup(entry->tzid);
372
0
      char* ctzid = tzid;
373
0
      while (tzid)
374
0
      {
375
0
        char* space = strchr(tzid, ' ');
376
0
        if (space)
377
0
          *space++ = '\0';
378
0
        if (strcmp(tzid, iana) == 0)
379
0
        {
380
0
          res = entry->windows;
381
0
          break;
382
0
        }
383
0
        tzid = space;
384
0
      }
385
0
      free(ctzid);
386
0
      if (res)
387
0
        return res;
388
0
    }
389
0
    else if (strcmp(entry->tzid, iana) == 0)
390
0
      return entry->windows;
391
0
  }
392
393
0
  return nullptr;
394
0
}
395
#endif
396
397
WINPR_ATTR_NODISCARD
398
static const char* iana_to_windows(const char* iana, TimeZoneNameType type)
399
144
{
400
144
  const char* val = get_for_type(iana, type, iana_cmp);
401
144
  if (val)
402
144
    return val;
403
404
0
  const char* wzid = map_fallback(iana, type);
405
0
  if (!wzid)
406
0
    return nullptr;
407
408
0
  return get_for_type(wzid, type, id_cmp);
409
0
}
410
411
const char* TimeZoneIanaToWindows(const char* iana, TimeZoneNameType type)
412
144
{
413
144
  if (!iana)
414
0
    return nullptr;
415
416
144
  const char* val = iana_to_windows(iana, type);
417
144
  if (val)
418
144
    return val;
419
420
  /* The tables (CLDR) and the system (tzdata, browsers) may each use either the current or the
421
   * legacy name of a renamed zone, so try the other spelling too. */
422
0
  const char* alias = TimeZoneIanaAlias(iana);
423
0
  if (!alias)
424
0
    return nullptr;
425
426
0
  return iana_to_windows(alias, type);
427
0
}