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