Coverage Report

Created: 2025-07-01 06:46

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