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>
32 #define TAG WINPR_TAG("timezone.utils")
34 #if defined(WITH_TIMEZONE_ICU)
35 #include <unicode/ucal.h>
37 #include "WindowsZones.h"
40 #include "TimeZoneNameMap.h"
42 #if defined(WITH_TIMEZONE_COMPILED)
43 #include "TimeZoneNameMap_static.h"
50 } TimeZoneNameMapContext;
52 static TimeZoneNameMapContext tz_context = { 0 };
58 free(entry->DaylightName);
59 free(entry->DisplayName);
62 free(entry->StandardName);
74 if (entry->DaylightName)
75 clone.DaylightName = _strdup(entry->DaylightName);
76 if (entry->DisplayName)
77 clone.DisplayName = _strdup(entry->DisplayName);
79 clone.Iana = _strdup(entry->Iana);
81 clone.Id = _strdup(entry->Id);
82 if (entry->StandardName)
83 clone.StandardName = _strdup(entry->StandardName);
87 static void tz_context_free(
void)
89 for (
size_t x = 0; x < tz_context.count; x++)
90 tz_entry_free(&tz_context.entries[x]);
91 free(tz_context.entries);
93 tz_context.entries = NULL;
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)
102 WLog_WARN(TAG,
"Invalid JSON entry at entry %" PRIuz
", missing an Object named '%s'", pos,
111 "Invalid JSON entry at entry %" PRIuz
", Object named '%s': Not of type string",
119 WLog_WARN(TAG,
"Invalid JSON entry at entry %" PRIuz
", Object named '%s': NULL string",
132 WLog_WARN(TAG,
"Invalid JSON entry at entry %" PRIuz
", expected an array", pos);
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 ||
144 tz_entry_free(entry);
150 static WINPR_JSON* load_timezones_from_file(
const char* filename)
154 WINPR_JSON* json = NULL;
155 FILE* fp = winpr_fopen(filename,
"r");
158 WLog_WARN(TAG,
"Timezone resource file '%s' does not exist or is not readable", filename);
162 if (_fseeki64(fp, 0, SEEK_END) < 0)
164 WLog_WARN(TAG,
"Timezone resource file '%s' seek failed", filename);
167 jstrlen = _ftelli64(fp);
170 WLog_WARN(TAG,
"Timezone resource file '%s' invalid length %" PRId64, filename, jstrlen);
173 if (_fseeki64(fp, 0, SEEK_SET) < 0)
175 WLog_WARN(TAG,
"Timezone resource file '%s' seek failed", filename);
179 jstr = calloc(jstrlen + 1,
sizeof(
char));
182 WLog_WARN(TAG,
"Timezone resource file '%s' failed to allocate buffer of size %" PRId64,
187 if (fread(jstr, jstrlen,
sizeof(
char), fp) != 1)
189 WLog_WARN(TAG,
"Timezone resource file '%s' failed to read buffer of size %" PRId64,
196 WLog_WARN(TAG,
"Timezone resource file '%s' is not a valid JSON file", filename);
204 static BOOL reallocate_context(TimeZoneNameMapContext* context,
size_t size_to_add)
212 "Failed to reallocate TimeZoneNameMapEntry::entries to %" PRIuz
" elements",
213 context->count + size_to_add);
216 const size_t offset = context->count;
217 context->entries = tmp;
218 context->count += size_to_add;
225 static BOOL CALLBACK load_timezones(
PINIT_ONCE once, PVOID param, PVOID* pvcontext)
227 TimeZoneNameMapContext* context = param;
228 WINPR_ASSERT(context);
229 WINPR_UNUSED(pvcontext);
232 const TimeZoneNameMapContext empty = { 0 };
235 #if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
237 WINPR_JSON* json = NULL;
238 char* filename = GetCombinedPath(WINPR_RESOURCE_ROOT,
"TimeZoneNameMap.json");
241 WLog_WARN(TAG,
"Could not create WinPR timezone resource filename");
245 json = load_timezones_from_file(filename);
251 WLog_WARN(TAG,
"Invalid top level JSON type in file %s, expected an array", filename);
258 WLog_WARN(TAG,
"Invalid top level JSON type in file %s, expected an array", filename);
262 const size_t offset = context->count;
263 if (!reallocate_context(context, count))
265 for (
size_t x = 0; x < count; x++)
268 if (!tz_parse_json_entry(entry, x, &context->entries[offset + x]))
278 #if defined(WITH_TIMEZONE_COMPILED)
280 const size_t offset = context->count;
281 if (!reallocate_context(context, TimeZoneNameMapSize))
283 for (
size_t x = 0; x < TimeZoneNameMapSize; x++)
284 context->entries[offset + x] = tz_entry_clone(&TimeZoneNameMap[x]);
288 (void)atexit(tz_context_free);
294 static INIT_ONCE init_guard = INIT_ONCE_STATIC_INIT;
296 InitOnceExecuteOnce(&init_guard, load_timezones, &tz_context, NULL);
297 if (index >= tz_context.count)
299 return &tz_context.entries[index];
307 case TIME_ZONE_NAME_IANA:
309 case TIME_ZONE_NAME_ID:
311 case TIME_ZONE_NAME_STANDARD:
312 return entry->StandardName;
313 case TIME_ZONE_NAME_DISPLAY:
314 return entry->DisplayName;
315 case TIME_ZONE_NAME_DAYLIGHT:
316 return entry->DaylightName;
324 if (!entry || !iana || !entry->Iana)
326 return strcmp(iana, entry->Iana) == 0;
331 if (!entry || !
id || !entry->Id)
333 return strcmp(
id, entry->Id) == 0;
336 static const char* get_for_type(
const char* val, TimeZoneNameType type,
349 return return_type(entry, type);
353 #if defined(WITH_TIMEZONE_ICU)
354 static char* get_wzid_icu(
const UChar* utzid,
size_t utzid_len)
357 UErrorCode error = U_ZERO_ERROR;
359 int32_t rc = ucal_getWindowsTimeZoneID(utzid, utzid_len, NULL, 0, &error);
360 if ((error == U_BUFFER_OVERFLOW_ERROR) && (rc > 0))
363 UChar* wzid = calloc((
size_t)rc + 1,
sizeof(UChar));
366 UErrorCode error2 = U_ZERO_ERROR;
367 int32_t rc2 = ucal_getWindowsTimeZoneID(utzid, utzid_len, wzid, rc, &error2);
368 if (U_SUCCESS(error2) && (rc2 > 0))
369 res = ConvertWCharNToUtf8Alloc(wzid, (
size_t)rc, NULL);
376 static char* get(
const char* iana)
378 size_t utzid_len = 0;
379 UChar* utzid = ConvertUtf8ToWCharAlloc(iana, &utzid_len);
383 char* wzid = get_wzid_icu(utzid, utzid_len);
388 static const char* map_fallback(
const char* iana, TimeZoneNameType type)
390 char* wzid = get(iana);
394 const char* res = get_for_type(wzid, type, id_cmp);
399 static const char* map_fallback(
const char* iana, TimeZoneNameType type)
404 for (
size_t x = 0; x < WindowsZonesNrElements; x++)
407 if (strchr(entry->tzid,
' '))
409 const char* res = NULL;
410 char* tzid = _strdup(entry->tzid);
414 char* space = strchr(tzid,
' ');
417 if (strcmp(tzid, iana) == 0)
419 res = entry->windows;
428 else if (strcmp(entry->tzid, iana) == 0)
429 return entry->windows;
436 const char* TimeZoneIanaToWindows(
const char* iana, TimeZoneNameType type)
441 const char* val = get_for_type(iana, type, iana_cmp);
445 const char* wzid = map_fallback(iana, type);
449 return get_for_type(wzid, type, id_cmp);
WINPR_API BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
WINPR_API WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
WINPR_API BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
WINPR_API WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
WINPR_API size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.