FreeRDP
Loading...
Searching...
No Matches
TimeZoneNameMapUtils.c
1
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
49typedef struct
50{
51 size_t count;
52 TimeZoneNameMapEntry* entries;
53} TimeZoneNameMapContext;
54
55static TimeZoneNameMapContext tz_context = WINPR_C_ARRAY_INIT;
56
57static void tz_entry_free(TimeZoneNameMapEntry* entry)
58{
59 if (!entry)
60 return;
61 free(entry->DaylightName);
62 free(entry->DisplayName);
63 free(entry->Iana);
64 free(entry->Id);
65 free(entry->StandardName);
66
67 const TimeZoneNameMapEntry empty = WINPR_C_ARRAY_INIT;
68 *entry = empty;
69}
70
71static TimeZoneNameMapEntry tz_entry_clone(const TimeZoneNameMapEntry* entry)
72{
73 TimeZoneNameMapEntry clone = WINPR_C_ARRAY_INIT;
74 if (!entry)
75 return clone;
76
77 if (entry->DaylightName)
78 clone.DaylightName = _strdup(entry->DaylightName);
79 if (entry->DisplayName)
80 clone.DisplayName = _strdup(entry->DisplayName);
81 if (entry->Iana)
82 clone.Iana = _strdup(entry->Iana);
83 if (entry->Id)
84 clone.Id = _strdup(entry->Id);
85 if (entry->StandardName)
86 clone.StandardName = _strdup(entry->StandardName);
87 return clone;
88}
89
90static void tz_context_free(void)
91{
92 for (size_t x = 0; x < tz_context.count; x++)
93 tz_entry_free(&tz_context.entries[x]);
94 free(tz_context.entries);
95 tz_context.count = 0;
96 tz_context.entries = nullptr;
97}
98
99#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
100char* 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
130static 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
153WINPR_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
162static BOOL reallocate_context(TimeZoneNameMapContext* context, size_t size_to_add)
163{
164 {
165 TimeZoneNameMapEntry* tmp = realloc(context->entries, (context->count + size_to_add) *
166 sizeof(TimeZoneNameMapEntry));
167 if (!tmp)
168 {
169 WLog_WARN(TAG,
170 "Failed to reallocate TimeZoneNameMapEntry::entries to %" PRIuz " elements",
171 context->count + size_to_add);
172 return FALSE;
173 }
174 const size_t offset = context->count;
175 context->entries = tmp;
176 context->count += size_to_add;
177
178 memset(&context->entries[offset], 0, size_to_add * sizeof(TimeZoneNameMapEntry));
179 }
180 return TRUE;
181}
182
183static BOOL CALLBACK load_timezones(PINIT_ONCE once, PVOID param, PVOID* pvcontext)
184{
185 TimeZoneNameMapContext* context = param;
186 WINPR_ASSERT(context);
187 WINPR_UNUSED(pvcontext);
188 WINPR_UNUSED(once);
189
190 const TimeZoneNameMapContext empty = WINPR_C_ARRAY_INIT;
191 *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#if defined(WITH_TIMEZONE_COMPILED)
237 {
238 const size_t offset = context->count;
239 if (!reallocate_context(context, TimeZoneNameMapSize))
240 return FALSE;
241 for (size_t x = 0; x < TimeZoneNameMapSize; x++)
242 context->entries[offset + x] = tz_entry_clone(&TimeZoneNameMap[x]);
243 }
244#endif
245
246 (void)winpr_atexit(tz_context_free);
247 return TRUE;
248}
249
250const TimeZoneNameMapEntry* TimeZoneGetAt(size_t index)
251{
252 static INIT_ONCE init_guard = INIT_ONCE_STATIC_INIT;
253
254 if (!InitOnceExecuteOnce(&init_guard, load_timezones, &tz_context, nullptr))
255 return nullptr;
256 if (index >= tz_context.count)
257 return nullptr;
258 return &tz_context.entries[index];
259}
260
261static const char* return_type(const TimeZoneNameMapEntry* entry, TimeZoneNameType type)
262{
263 WINPR_ASSERT(entry);
264 switch (type)
265 {
266 case TIME_ZONE_NAME_IANA:
267 return entry->Iana;
268 case TIME_ZONE_NAME_ID:
269 return entry->Id;
270 case TIME_ZONE_NAME_STANDARD:
271 return entry->StandardName;
272 case TIME_ZONE_NAME_DISPLAY:
273 return entry->DisplayName;
274 case TIME_ZONE_NAME_DAYLIGHT:
275 return entry->DaylightName;
276 default:
277 return nullptr;
278 }
279}
280
281static BOOL iana_cmp(const TimeZoneNameMapEntry* entry, const char* iana)
282{
283 if (!entry || !iana || !entry->Iana)
284 return FALSE;
285 return strcmp(iana, entry->Iana) == 0;
286}
287
288static BOOL id_cmp(const TimeZoneNameMapEntry* entry, const char* id)
289{
290 if (!entry || !id || !entry->Id)
291 return FALSE;
292 return strcmp(id, entry->Id) == 0;
293}
294
295static const char* get_for_type(const char* val, TimeZoneNameType type,
296 BOOL (*cmp)(const TimeZoneNameMapEntry*, const char*))
297{
298 WINPR_ASSERT(val);
299 WINPR_ASSERT(cmp);
300
301 size_t index = 0;
302 while (TRUE)
303 {
304 const TimeZoneNameMapEntry* entry = TimeZoneGetAt(index++);
305 if (!entry)
306 return nullptr;
307 if (cmp(entry, val))
308 return return_type(entry, type);
309 }
310}
311
312#if defined(WITH_TIMEZONE_ICU)
313static 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
337static 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
349static 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
360static const char* map_fallback(const char* iana, WINPR_ATTR_UNUSED TimeZoneNameType type)
361{
362 if (!iana)
363 return nullptr;
364
365 for (size_t x = 0; x < WindowsZonesNrElements; x++)
366 {
367 const WINDOWS_TZID_ENTRY* const entry = &WindowsZones[x];
368 if (strchr(entry->tzid, ' '))
369 {
370 const char* res = nullptr;
371 char* tzid = _strdup(entry->tzid);
372 char* ctzid = tzid;
373 while (tzid)
374 {
375 char* space = strchr(tzid, ' ');
376 if (space)
377 *space++ = '\0';
378 if (strcmp(tzid, iana) == 0)
379 {
380 res = entry->windows;
381 break;
382 }
383 tzid = space;
384 }
385 free(ctzid);
386 if (res)
387 return res;
388 }
389 else if (strcmp(entry->tzid, iana) == 0)
390 return entry->windows;
391 }
392
393 return nullptr;
394}
395#endif
396
397WINPR_ATTR_NODISCARD
398static const char* iana_to_windows(const char* iana, TimeZoneNameType type)
399{
400 const char* val = get_for_type(iana, type, iana_cmp);
401 if (val)
402 return val;
403
404 const char* wzid = map_fallback(iana, type);
405 if (!wzid)
406 return nullptr;
407
408 return get_for_type(wzid, type, id_cmp);
409}
410
411const char* TimeZoneIanaToWindows(const char* iana, TimeZoneNameType type)
412{
413 if (!iana)
414 return nullptr;
415
416 const char* val = iana_to_windows(iana, type);
417 if (val)
418 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 const char* alias = TimeZoneIanaAlias(iana);
423 if (!alias)
424 return nullptr;
425
426 return iana_to_windows(alias, type);
427}
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
Definition c-json.c:132
WINPR_ATTR_NODISCARD WINPR_API size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition c-json.c:114
WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFile(const char *filename)
Parse a JSON string read from a file filename.
Definition json.c:27
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON *object, const char *string)
Same as WINPR_JSON_GetObjectItem but with case sensitive matching.
Definition c-json.c:127
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition c-json.c:182
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
Definition c-json.c:108
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition c-json.c:187
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition c-json.c:192
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition c-json.c:103
WINPR_ATTR_NODISCARD WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition c-json.c:142