FreeRDP
Loading...
Searching...
No Matches
TimeZoneNameMapUtils.c
1
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
46typedef struct
47{
48 size_t count;
49 TimeZoneNameMapEntry* entries;
50} TimeZoneNameMapContext;
51
52static TimeZoneNameMapContext tz_context = WINPR_C_ARRAY_INIT;
53
54static void tz_entry_free(TimeZoneNameMapEntry* entry)
55{
56 if (!entry)
57 return;
58 free(entry->DaylightName);
59 free(entry->DisplayName);
60 free(entry->Iana);
61 free(entry->Id);
62 free(entry->StandardName);
63
64 const TimeZoneNameMapEntry empty = WINPR_C_ARRAY_INIT;
65 *entry = empty;
66}
67
68static TimeZoneNameMapEntry tz_entry_clone(const TimeZoneNameMapEntry* entry)
69{
70 TimeZoneNameMapEntry clone = WINPR_C_ARRAY_INIT;
71 if (!entry)
72 return clone;
73
74 if (entry->DaylightName)
75 clone.DaylightName = _strdup(entry->DaylightName);
76 if (entry->DisplayName)
77 clone.DisplayName = _strdup(entry->DisplayName);
78 if (entry->Iana)
79 clone.Iana = _strdup(entry->Iana);
80 if (entry->Id)
81 clone.Id = _strdup(entry->Id);
82 if (entry->StandardName)
83 clone.StandardName = _strdup(entry->StandardName);
84 return clone;
85}
86
87static void tz_context_free(void)
88{
89 for (size_t x = 0; x < tz_context.count; x++)
90 tz_entry_free(&tz_context.entries[x]);
91 free(tz_context.entries);
92 tz_context.count = 0;
93 tz_context.entries = nullptr;
94}
95
96#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
97static 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 nullptr;
105 }
106 WINPR_JSON* obj = WINPR_JSON_GetObjectItemCaseSensitive(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 nullptr;
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': nullptr string",
120 pos, name);
121 return nullptr;
122 }
123
124 return _strdup(str);
125}
126
127static 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
150static 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
159static BOOL reallocate_context(TimeZoneNameMapContext* context, size_t size_to_add)
160{
161 {
162 TimeZoneNameMapEntry* tmp = realloc(context->entries, (context->count + size_to_add) *
163 sizeof(TimeZoneNameMapEntry));
164 if (!tmp)
165 {
166 WLog_WARN(TAG,
167 "Failed to reallocate TimeZoneNameMapEntry::entries to %" PRIuz " elements",
168 context->count + size_to_add);
169 return FALSE;
170 }
171 const size_t offset = context->count;
172 context->entries = tmp;
173 context->count += size_to_add;
174
175 memset(&context->entries[offset], 0, size_to_add * sizeof(TimeZoneNameMapEntry));
176 }
177 return TRUE;
178}
179
180static BOOL CALLBACK load_timezones(PINIT_ONCE once, PVOID param, PVOID* pvcontext)
181{
182 TimeZoneNameMapContext* context = param;
183 WINPR_ASSERT(context);
184 WINPR_UNUSED(pvcontext);
185 WINPR_UNUSED(once);
186
187 const TimeZoneNameMapContext empty = WINPR_C_ARRAY_INIT;
188 *context = empty;
189
190#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
191 {
192 WINPR_JSON* json = nullptr;
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_GetObjectItemCaseSensitive(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#if defined(WITH_TIMEZONE_COMPILED)
234 {
235 const size_t offset = context->count;
236 if (!reallocate_context(context, TimeZoneNameMapSize))
237 return FALSE;
238 for (size_t x = 0; x < TimeZoneNameMapSize; x++)
239 context->entries[offset + x] = tz_entry_clone(&TimeZoneNameMap[x]);
240 }
241#endif
242
243 (void)atexit(tz_context_free);
244 return TRUE;
245}
246
247const TimeZoneNameMapEntry* TimeZoneGetAt(size_t index)
248{
249 static INIT_ONCE init_guard = INIT_ONCE_STATIC_INIT;
250
251 if (!InitOnceExecuteOnce(&init_guard, load_timezones, &tz_context, nullptr))
252 return nullptr;
253 if (index >= tz_context.count)
254 return nullptr;
255 return &tz_context.entries[index];
256}
257
258static const char* return_type(const TimeZoneNameMapEntry* entry, TimeZoneNameType type)
259{
260 WINPR_ASSERT(entry);
261 switch (type)
262 {
263 case TIME_ZONE_NAME_IANA:
264 return entry->Iana;
265 case TIME_ZONE_NAME_ID:
266 return entry->Id;
267 case TIME_ZONE_NAME_STANDARD:
268 return entry->StandardName;
269 case TIME_ZONE_NAME_DISPLAY:
270 return entry->DisplayName;
271 case TIME_ZONE_NAME_DAYLIGHT:
272 return entry->DaylightName;
273 default:
274 return nullptr;
275 }
276}
277
278static BOOL iana_cmp(const TimeZoneNameMapEntry* entry, const char* iana)
279{
280 if (!entry || !iana || !entry->Iana)
281 return FALSE;
282 return strcmp(iana, entry->Iana) == 0;
283}
284
285static BOOL id_cmp(const TimeZoneNameMapEntry* entry, const char* id)
286{
287 if (!entry || !id || !entry->Id)
288 return FALSE;
289 return strcmp(id, entry->Id) == 0;
290}
291
292static const char* get_for_type(const char* val, TimeZoneNameType type,
293 BOOL (*cmp)(const TimeZoneNameMapEntry*, const char*))
294{
295 WINPR_ASSERT(val);
296 WINPR_ASSERT(cmp);
297
298 size_t index = 0;
299 while (TRUE)
300 {
301 const TimeZoneNameMapEntry* entry = TimeZoneGetAt(index++);
302 if (!entry)
303 return nullptr;
304 if (cmp(entry, val))
305 return return_type(entry, type);
306 }
307}
308
309#if defined(WITH_TIMEZONE_ICU)
310static char* get_wzid_icu(const UChar* utzid, size_t utzid_len)
311{
312 char* res = nullptr;
313 UErrorCode error = U_ZERO_ERROR;
314
315 int32_t rc = ucal_getWindowsTimeZoneID(utzid, WINPR_ASSERTING_INT_CAST(int32_t, utzid_len),
316 nullptr, 0, &error);
317 if ((error == U_BUFFER_OVERFLOW_ERROR) && (rc > 0))
318 {
319 rc++; // make space for '\0'
320 UChar* wzid = calloc((size_t)rc + 1, sizeof(UChar));
321 if (wzid)
322 {
323 UErrorCode error2 = U_ZERO_ERROR;
324 int32_t rc2 = ucal_getWindowsTimeZoneID(
325 utzid, WINPR_ASSERTING_INT_CAST(int32_t, utzid_len), wzid, rc, &error2);
326 if (U_SUCCESS(error2) && (rc2 > 0))
327 res = ConvertWCharNToUtf8Alloc(wzid, (size_t)rc, nullptr);
328 free(wzid);
329 }
330 }
331 return res;
332}
333
334static char* get(const char* iana)
335{
336 size_t utzid_len = 0;
337 UChar* utzid = ConvertUtf8ToWCharAlloc(iana, &utzid_len);
338 if (!utzid)
339 return nullptr;
340
341 char* wzid = get_wzid_icu(utzid, utzid_len);
342 free(utzid);
343 return wzid;
344}
345
346static const char* map_fallback(const char* iana, TimeZoneNameType type)
347{
348 char* wzid = get(iana);
349 if (!wzid)
350 return nullptr;
351
352 const char* res = get_for_type(wzid, type, id_cmp);
353 free(wzid);
354 return res;
355}
356#else
357static const char* map_fallback(const char* iana, WINPR_ATTR_UNUSED TimeZoneNameType type)
358{
359 if (!iana)
360 return nullptr;
361
362 for (size_t x = 0; x < WindowsZonesNrElements; x++)
363 {
364 const WINDOWS_TZID_ENTRY* const entry = &WindowsZones[x];
365 if (strchr(entry->tzid, ' '))
366 {
367 const char* res = nullptr;
368 char* tzid = _strdup(entry->tzid);
369 char* ctzid = tzid;
370 while (tzid)
371 {
372 char* space = strchr(tzid, ' ');
373 if (space)
374 *space++ = '\0';
375 if (strcmp(tzid, iana) == 0)
376 {
377 res = entry->windows;
378 break;
379 }
380 tzid = space;
381 }
382 free(ctzid);
383 if (res)
384 return res;
385 }
386 else if (strcmp(entry->tzid, iana) == 0)
387 return entry->windows;
388 }
389
390 return nullptr;
391}
392#endif
393
394const char* TimeZoneIanaToWindows(const char* iana, TimeZoneNameType type)
395{
396 if (!iana)
397 return nullptr;
398
399 const char* val = get_for_type(iana, type, iana_cmp);
400 if (val)
401 return val;
402
403 const char* wzid = map_fallback(iana, type);
404 if (!wzid)
405 return nullptr;
406
407 return get_for_type(wzid, type, id_cmp);
408}
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_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_ParseFromFile(const char *filename)
Parse a JSON string read from a file filename.
Definition json.c:27
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_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_API BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition c-json.c:192
WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition c-json.c:142
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition c-json.c:103
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 BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition c-json.c:187