20#include <winpr/config.h>
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>
34#define TAG WINPR_TAG("timezone.utils")
36#if defined(WITH_TIMEZONE_ICU)
37#include <unicode/ucal.h>
39#include "WindowsZones.h"
42#include "TimeZoneNameMap.h"
43#include "TimeZoneIanaAliases.h"
45#if defined(WITH_TIMEZONE_COMPILED)
46#include "TimeZoneNameMap_static.h"
53} TimeZoneNameMapContext;
55static TimeZoneNameMapContext tz_context = WINPR_C_ARRAY_INIT;
61 free(entry->DaylightName);
62 free(entry->DisplayName);
65 free(entry->StandardName);
71#if defined(WITH_TIMEZONE_COMPILED)
78 if (entry->DaylightName)
79 clone.DaylightName = _strdup(entry->DaylightName);
80 if (entry->DisplayName)
81 clone.DisplayName = _strdup(entry->DisplayName);
83 clone.Iana = _strdup(entry->Iana);
85 clone.Id = _strdup(entry->Id);
86 if (entry->StandardName)
87 clone.StandardName = _strdup(entry->StandardName);
92static void tz_context_free(
void)
94 for (
size_t x = 0; x < tz_context.count; x++)
95 tz_entry_free(&tz_context.entries[x]);
96 free(tz_context.entries);
98 tz_context.entries =
nullptr;
101#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
102char* TimeZoneJsonGetString(WINPR_JSON* json,
size_t pos,
const char* name)
107 WLog_WARN(TAG,
"Invalid JSON entry at entry %" PRIuz
", missing an Object named '%s'", pos,
116 "Invalid JSON entry at entry %" PRIuz
", Object named '%s': Not of type string",
124 WLog_WARN(TAG,
"Invalid JSON entry at entry %" PRIuz
", Object named '%s': nullptr string",
137 WLog_WARN(TAG,
"Invalid JSON entry at entry %" PRIuz
", expected an array", pos);
141 entry->Id = TimeZoneJsonGetString(json, pos,
"Id");
142 entry->StandardName = TimeZoneJsonGetString(json, pos,
"StandardName");
143 entry->DisplayName = TimeZoneJsonGetString(json, pos,
"DisplayName");
144 entry->DaylightName = TimeZoneJsonGetString(json, pos,
"DaylightName");
145 entry->Iana = TimeZoneJsonGetString(json, pos,
"Iana");
146 if (!entry->Id || !entry->StandardName || !entry->DisplayName || !entry->DaylightName ||
149 tz_entry_free(entry);
155WINPR_JSON* TimeZoneJsonParseFile(
const char* filename)
159 WLog_WARN(TAG,
"Timezone resource file '%s' is not a valid JSON file", filename);
164static BOOL reallocate_context(TimeZoneNameMapContext* context,
size_t size_to_add)
172 "Failed to reallocate TimeZoneNameMapEntry::entries to %" PRIuz
" elements",
173 context->count + size_to_add);
176 const size_t offset = context->count;
177 context->entries = tmp;
178 context->count += size_to_add;
185static BOOL CALLBACK load_timezones(PINIT_ONCE once, PVOID param, PVOID* pvcontext)
187 TimeZoneNameMapContext* context = param;
188 WINPR_ASSERT(context);
189 WINPR_UNUSED(pvcontext);
192 const TimeZoneNameMapContext empty = WINPR_C_ARRAY_INIT;
195#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
197 WINPR_JSON* json =
nullptr;
198 char* filename = GetCombinedPath(WINPR_RESOURCE_ROOT,
"TimeZoneNameMap.json");
201 WLog_WARN(TAG,
"Could not create WinPR timezone resource filename");
205 json = TimeZoneJsonParseFile(filename);
211 WLog_WARN(TAG,
"Invalid top level JSON type in file %s, expected an array", filename);
218 WLog_WARN(TAG,
"Invalid top level JSON type in file %s, expected an array", filename);
222 const size_t offset = context->count;
223 if (!reallocate_context(context, count))
225 for (
size_t x = 0; x < count; x++)
228 if (!tz_parse_json_entry(entry, x, &context->entries[offset + x]))
238#if defined(WITH_TIMEZONE_COMPILED)
240 const size_t offset = context->count;
241 if (!reallocate_context(context, TimeZoneNameMapSize))
243 for (
size_t x = 0; x < TimeZoneNameMapSize; x++)
244 context->entries[offset + x] = tz_entry_clone(&TimeZoneNameMap[x]);
248 (void)winpr_atexit(tz_context_free);
254 static INIT_ONCE init_guard = INIT_ONCE_STATIC_INIT;
256 if (!InitOnceExecuteOnce(&init_guard, load_timezones, &tz_context,
nullptr))
258 if (index >= tz_context.count)
260 return &tz_context.entries[index];
268 case TIME_ZONE_NAME_IANA:
270 case TIME_ZONE_NAME_ID:
272 case TIME_ZONE_NAME_STANDARD:
273 return entry->StandardName;
274 case TIME_ZONE_NAME_DISPLAY:
275 return entry->DisplayName;
276 case TIME_ZONE_NAME_DAYLIGHT:
277 return entry->DaylightName;
285 if (!entry || !iana || !entry->Iana)
287 return strcmp(iana, entry->Iana) == 0;
292 if (!entry || !
id || !entry->Id)
294 return strcmp(
id, entry->Id) == 0;
297static const char* get_for_type(
const char* val, TimeZoneNameType type,
310 return return_type(entry, type);
314#if defined(WITH_TIMEZONE_ICU)
315static char* get_wzid_icu(
const UChar* utzid,
size_t utzid_len)
318 UErrorCode error = U_ZERO_ERROR;
320 int32_t rc = ucal_getWindowsTimeZoneID(utzid, WINPR_ASSERTING_INT_CAST(int32_t, utzid_len),
322 if ((error == U_BUFFER_OVERFLOW_ERROR) && (rc > 0))
325 UChar* wzid = calloc((
size_t)rc + 1,
sizeof(UChar));
328 UErrorCode error2 = U_ZERO_ERROR;
329 int32_t rc2 = ucal_getWindowsTimeZoneID(
330 utzid, WINPR_ASSERTING_INT_CAST(int32_t, utzid_len), wzid, rc, &error2);
331 if (U_SUCCESS(error2) && (rc2 > 0) && (rc2 < rc))
334 const WCHAR* wzid_le = ByteSwapUnicode(wzid, (
size_t)rc2);
336 const WCHAR* wzid_le = wzid;
338 res = ConvertWCharNToUtf8Alloc(wzid_le, (
size_t)rc2 + 1,
nullptr);
346static char* get(
const char* iana)
348 size_t utzid_len = 0;
349 UChar* utzid = ConvertUtf8ToWCharAlloc(iana, &utzid_len);
354 const UChar* utzid_native = ByteSwapUnicode(utzid, utzid_len);
356 const UChar* utzid_native = utzid;
358 char* wzid = get_wzid_icu(utzid_native, utzid_len);
363static const char* map_fallback(
const char* iana, TimeZoneNameType type)
365 char* wzid = get(iana);
369 const char* res = get_for_type(wzid, type, id_cmp);
374static const char* map_fallback(
const char* iana, WINPR_ATTR_UNUSED TimeZoneNameType type)
379 for (
size_t x = 0; x < WindowsZonesNrElements; x++)
382 if (strchr(entry->tzid,
' '))
384 const char* res =
nullptr;
385 char* tzid = _strdup(entry->tzid);
389 char* space = strchr(tzid,
' ');
392 if (strcmp(tzid, iana) == 0)
394 res = entry->windows;
403 else if (strcmp(entry->tzid, iana) == 0)
404 return entry->windows;
412static const char* iana_to_windows(
const char* iana, TimeZoneNameType type)
414 const char* val = get_for_type(iana, type, iana_cmp);
418 const char* wzid = map_fallback(iana, type);
422 return get_for_type(wzid, type, id_cmp);
425const char* TimeZoneIanaToWindows(
const char* iana, TimeZoneNameType type)
430 const char* val = iana_to_windows(iana, type);
436 const char* alias = TimeZoneIanaAlias(iana);
440 return iana_to_windows(alias, type);
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.
WINPR_ATTR_NODISCARD WINPR_API size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFile(const char *filename)
Parse a JSON string read from a file filename.
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.
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
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.
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
WINPR_ATTR_NODISCARD WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.