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 = { 0 };
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 = { 0 };
65 *entry = empty;
66}
67
68static TimeZoneNameMapEntry tz_entry_clone(const TimeZoneNameMapEntry* entry)
69{
70 TimeZoneNameMapEntry clone = { 0 };
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 = NULL;
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 NULL;
105 }
106 WINPR_JSON* obj = WINPR_JSON_GetObjectItem(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 NULL;
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': NULL string",
120 pos, name);
121 return NULL;
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 = { 0 };
188 *context = empty;
189
190#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
191 {
192 WINPR_JSON* json = NULL;
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_GetObjectItem(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 InitOnceExecuteOnce(&init_guard, load_timezones, &tz_context, NULL);
252 if (index >= tz_context.count)
253 return NULL;
254 return &tz_context.entries[index];
255}
256
257static const char* return_type(const TimeZoneNameMapEntry* entry, TimeZoneNameType type)
258{
259 WINPR_ASSERT(entry);
260 switch (type)
261 {
262 case TIME_ZONE_NAME_IANA:
263 return entry->Iana;
264 case TIME_ZONE_NAME_ID:
265 return entry->Id;
266 case TIME_ZONE_NAME_STANDARD:
267 return entry->StandardName;
268 case TIME_ZONE_NAME_DISPLAY:
269 return entry->DisplayName;
270 case TIME_ZONE_NAME_DAYLIGHT:
271 return entry->DaylightName;
272 default:
273 return NULL;
274 }
275}
276
277static BOOL iana_cmp(const TimeZoneNameMapEntry* entry, const char* iana)
278{
279 if (!entry || !iana || !entry->Iana)
280 return FALSE;
281 return strcmp(iana, entry->Iana) == 0;
282}
283
284static BOOL id_cmp(const TimeZoneNameMapEntry* entry, const char* id)
285{
286 if (!entry || !id || !entry->Id)
287 return FALSE;
288 return strcmp(id, entry->Id) == 0;
289}
290
291static const char* get_for_type(const char* val, TimeZoneNameType type,
292 BOOL (*cmp)(const TimeZoneNameMapEntry*, const char*))
293{
294 WINPR_ASSERT(val);
295 WINPR_ASSERT(cmp);
296
297 size_t index = 0;
298 while (TRUE)
299 {
300 const TimeZoneNameMapEntry* entry = TimeZoneGetAt(index++);
301 if (!entry)
302 return NULL;
303 if (cmp(entry, val))
304 return return_type(entry, type);
305 }
306}
307
308#if defined(WITH_TIMEZONE_ICU)
309static char* get_wzid_icu(const UChar* utzid, size_t utzid_len)
310{
311 char* res = NULL;
312 UErrorCode error = U_ZERO_ERROR;
313
314 int32_t rc = ucal_getWindowsTimeZoneID(utzid, WINPR_ASSERTING_INT_CAST(int32_t, utzid_len),
315 NULL, 0, &error);
316 if ((error == U_BUFFER_OVERFLOW_ERROR) && (rc > 0))
317 {
318 rc++; // make space for '\0'
319 UChar* wzid = calloc((size_t)rc + 1, sizeof(UChar));
320 if (wzid)
321 {
322 UErrorCode error2 = U_ZERO_ERROR;
323 int32_t rc2 = ucal_getWindowsTimeZoneID(
324 utzid, WINPR_ASSERTING_INT_CAST(int32_t, utzid_len), wzid, rc, &error2);
325 if (U_SUCCESS(error2) && (rc2 > 0))
326 res = ConvertWCharNToUtf8Alloc(wzid, (size_t)rc, NULL);
327 free(wzid);
328 }
329 }
330 return res;
331}
332
333static char* get(const char* iana)
334{
335 size_t utzid_len = 0;
336 UChar* utzid = ConvertUtf8ToWCharAlloc(iana, &utzid_len);
337 if (!utzid)
338 return NULL;
339
340 char* wzid = get_wzid_icu(utzid, utzid_len);
341 free(utzid);
342 return wzid;
343}
344
345static const char* map_fallback(const char* iana, TimeZoneNameType type)
346{
347 char* wzid = get(iana);
348 if (!wzid)
349 return NULL;
350
351 const char* res = get_for_type(wzid, type, id_cmp);
352 free(wzid);
353 return res;
354}
355#else
356static const char* map_fallback(const char* iana, WINPR_ATTR_UNUSED TimeZoneNameType type)
357{
358 if (!iana)
359 return NULL;
360
361 for (size_t x = 0; x < WindowsZonesNrElements; x++)
362 {
363 const WINDOWS_TZID_ENTRY* const entry = &WindowsZones[x];
364 if (strchr(entry->tzid, ' '))
365 {
366 const char* res = NULL;
367 char* tzid = _strdup(entry->tzid);
368 char* ctzid = tzid;
369 while (tzid)
370 {
371 char* space = strchr(tzid, ' ');
372 if (space)
373 *space++ = '\0';
374 if (strcmp(tzid, iana) == 0)
375 {
376 res = entry->windows;
377 break;
378 }
379 tzid = space;
380 }
381 free(ctzid);
382 if (res)
383 return res;
384 }
385 else if (strcmp(entry->tzid, iana) == 0)
386 return entry->windows;
387 }
388
389 return NULL;
390}
391#endif
392
393const char* TimeZoneIanaToWindows(const char* iana, TimeZoneNameType type)
394{
395 if (!iana)
396 return NULL;
397
398 const char* val = get_for_type(iana, type, iana_cmp);
399 if (val)
400 return val;
401
402 const char* wzid = map_fallback(iana, type);
403 if (!wzid)
404 return NULL;
405
406 return get_for_type(wzid, type, id_cmp);
407}
WINPR_API BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
Definition json.c:210
WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
Definition json.c:184
WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition json.c:349
WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFile(const char *filename)
Parse a JSON string read from a file filename.
Definition json.c:684
WINPR_API WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
Definition json.c:155
WINPR_API BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition json.c:373
WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition json.c:234
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition json.c:144
WINPR_API size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition json.c:169
WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition json.c:361