/src/FreeRDP/winpr/libwinpr/timezone/TimeZoneNameMapUtils.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Time Zone Name Map Utils |
4 | | * |
5 | | * Copyright 2024 Armin Novak <anovak@thincast.com> |
6 | | * Copyright 2024 Thincast Technologies GmbH |
7 | | * |
8 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | | * you may not use this file except in compliance with the License. |
10 | | * You may obtain a copy of the License at |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
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 | | |
48 | | typedef struct |
49 | | { |
50 | | size_t count; |
51 | | TimeZoneNameMapEntry* entries; |
52 | | } TimeZoneNameMapContext; |
53 | | |
54 | | static TimeZoneNameMapContext tz_context = WINPR_C_ARRAY_INIT; |
55 | | |
56 | | static void tz_entry_free(TimeZoneNameMapEntry* entry) |
57 | 0 | { |
58 | 0 | if (!entry) |
59 | 0 | return; |
60 | 0 | free(entry->DaylightName); |
61 | 0 | free(entry->DisplayName); |
62 | 0 | free(entry->Iana); |
63 | 0 | free(entry->Id); |
64 | 0 | free(entry->StandardName); |
65 | |
|
66 | 0 | const TimeZoneNameMapEntry empty = WINPR_C_ARRAY_INIT; |
67 | 0 | *entry = empty; |
68 | 0 | } |
69 | | |
70 | | static TimeZoneNameMapEntry tz_entry_clone(const TimeZoneNameMapEntry* entry) |
71 | 0 | { |
72 | 0 | TimeZoneNameMapEntry clone = WINPR_C_ARRAY_INIT; |
73 | 0 | if (!entry) |
74 | 0 | return clone; |
75 | | |
76 | 0 | if (entry->DaylightName) |
77 | 0 | clone.DaylightName = _strdup(entry->DaylightName); |
78 | 0 | if (entry->DisplayName) |
79 | 0 | clone.DisplayName = _strdup(entry->DisplayName); |
80 | 0 | if (entry->Iana) |
81 | 0 | clone.Iana = _strdup(entry->Iana); |
82 | 0 | if (entry->Id) |
83 | 0 | clone.Id = _strdup(entry->Id); |
84 | 0 | if (entry->StandardName) |
85 | 0 | clone.StandardName = _strdup(entry->StandardName); |
86 | 0 | return clone; |
87 | 0 | } |
88 | | |
89 | | static void tz_context_free(void) |
90 | 0 | { |
91 | 0 | for (size_t x = 0; x < tz_context.count; x++) |
92 | 0 | tz_entry_free(&tz_context.entries[x]); |
93 | 0 | free(tz_context.entries); |
94 | 0 | tz_context.count = 0; |
95 | 0 | tz_context.entries = nullptr; |
96 | 0 | } |
97 | | |
98 | | #if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON) |
99 | | static 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 | | |
129 | | static 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 | | |
152 | | static 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 | | |
161 | | static BOOL reallocate_context(TimeZoneNameMapContext* context, size_t size_to_add) |
162 | 0 | { |
163 | 0 | { |
164 | 0 | TimeZoneNameMapEntry* tmp = realloc(context->entries, (context->count + size_to_add) * |
165 | 0 | sizeof(TimeZoneNameMapEntry)); |
166 | 0 | if (!tmp) |
167 | 0 | { |
168 | 0 | WLog_WARN(TAG, |
169 | 0 | "Failed to reallocate TimeZoneNameMapEntry::entries to %" PRIuz " elements", |
170 | 0 | context->count + size_to_add); |
171 | 0 | return FALSE; |
172 | 0 | } |
173 | 0 | const size_t offset = context->count; |
174 | 0 | context->entries = tmp; |
175 | 0 | context->count += size_to_add; |
176 | |
|
177 | 0 | memset(&context->entries[offset], 0, size_to_add * sizeof(TimeZoneNameMapEntry)); |
178 | 0 | } |
179 | 0 | return TRUE; |
180 | 0 | } |
181 | | |
182 | | static BOOL CALLBACK load_timezones(PINIT_ONCE once, PVOID param, PVOID* pvcontext) |
183 | 0 | { |
184 | 0 | TimeZoneNameMapContext* context = param; |
185 | 0 | WINPR_ASSERT(context); |
186 | 0 | WINPR_UNUSED(pvcontext); |
187 | 0 | WINPR_UNUSED(once); |
188 | |
|
189 | 0 | const TimeZoneNameMapContext empty = WINPR_C_ARRAY_INIT; |
190 | 0 | *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 | 0 | #if defined(WITH_TIMEZONE_COMPILED) |
236 | 0 | { |
237 | 0 | const size_t offset = context->count; |
238 | 0 | if (!reallocate_context(context, TimeZoneNameMapSize)) |
239 | 0 | return FALSE; |
240 | 0 | for (size_t x = 0; x < TimeZoneNameMapSize; x++) |
241 | 0 | context->entries[offset + x] = tz_entry_clone(&TimeZoneNameMap[x]); |
242 | 0 | } |
243 | 0 | #endif |
244 | | |
245 | 0 | (void)winpr_atexit(tz_context_free); |
246 | 0 | return TRUE; |
247 | 0 | } |
248 | | |
249 | | const TimeZoneNameMapEntry* TimeZoneGetAt(size_t index) |
250 | 0 | { |
251 | 0 | static INIT_ONCE init_guard = INIT_ONCE_STATIC_INIT; |
252 | |
|
253 | 0 | if (!InitOnceExecuteOnce(&init_guard, load_timezones, &tz_context, nullptr)) |
254 | 0 | return nullptr; |
255 | 0 | if (index >= tz_context.count) |
256 | 0 | return nullptr; |
257 | 0 | return &tz_context.entries[index]; |
258 | 0 | } |
259 | | |
260 | | static const char* return_type(const TimeZoneNameMapEntry* entry, TimeZoneNameType type) |
261 | 0 | { |
262 | 0 | WINPR_ASSERT(entry); |
263 | 0 | switch (type) |
264 | 0 | { |
265 | 0 | case TIME_ZONE_NAME_IANA: |
266 | 0 | return entry->Iana; |
267 | 0 | case TIME_ZONE_NAME_ID: |
268 | 0 | return entry->Id; |
269 | 0 | case TIME_ZONE_NAME_STANDARD: |
270 | 0 | return entry->StandardName; |
271 | 0 | case TIME_ZONE_NAME_DISPLAY: |
272 | 0 | return entry->DisplayName; |
273 | 0 | case TIME_ZONE_NAME_DAYLIGHT: |
274 | 0 | return entry->DaylightName; |
275 | 0 | default: |
276 | 0 | return nullptr; |
277 | 0 | } |
278 | 0 | } |
279 | | |
280 | | static BOOL iana_cmp(const TimeZoneNameMapEntry* entry, const char* iana) |
281 | 0 | { |
282 | 0 | if (!entry || !iana || !entry->Iana) |
283 | 0 | return FALSE; |
284 | 0 | return strcmp(iana, entry->Iana) == 0; |
285 | 0 | } |
286 | | |
287 | | static BOOL id_cmp(const TimeZoneNameMapEntry* entry, const char* id) |
288 | 0 | { |
289 | 0 | if (!entry || !id || !entry->Id) |
290 | 0 | return FALSE; |
291 | 0 | return strcmp(id, entry->Id) == 0; |
292 | 0 | } |
293 | | |
294 | | static const char* get_for_type(const char* val, TimeZoneNameType type, |
295 | | BOOL (*cmp)(const TimeZoneNameMapEntry*, const char*)) |
296 | 0 | { |
297 | 0 | WINPR_ASSERT(val); |
298 | 0 | WINPR_ASSERT(cmp); |
299 | |
|
300 | 0 | size_t index = 0; |
301 | 0 | while (TRUE) |
302 | 0 | { |
303 | 0 | const TimeZoneNameMapEntry* entry = TimeZoneGetAt(index++); |
304 | 0 | if (!entry) |
305 | 0 | return nullptr; |
306 | 0 | if (cmp(entry, val)) |
307 | 0 | return return_type(entry, type); |
308 | 0 | } |
309 | 0 | } |
310 | | |
311 | | #if defined(WITH_TIMEZONE_ICU) |
312 | | static 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 | | |
336 | | static 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 | | |
348 | | static 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 |
359 | | static const char* map_fallback(const char* iana, WINPR_ATTR_UNUSED TimeZoneNameType type) |
360 | 0 | { |
361 | 0 | if (!iana) |
362 | 0 | return nullptr; |
363 | | |
364 | 0 | for (size_t x = 0; x < WindowsZonesNrElements; x++) |
365 | 0 | { |
366 | 0 | const WINDOWS_TZID_ENTRY* const entry = &WindowsZones[x]; |
367 | 0 | if (strchr(entry->tzid, ' ')) |
368 | 0 | { |
369 | 0 | const char* res = nullptr; |
370 | 0 | char* tzid = _strdup(entry->tzid); |
371 | 0 | char* ctzid = tzid; |
372 | 0 | while (tzid) |
373 | 0 | { |
374 | 0 | char* space = strchr(tzid, ' '); |
375 | 0 | if (space) |
376 | 0 | *space++ = '\0'; |
377 | 0 | if (strcmp(tzid, iana) == 0) |
378 | 0 | { |
379 | 0 | res = entry->windows; |
380 | 0 | break; |
381 | 0 | } |
382 | 0 | tzid = space; |
383 | 0 | } |
384 | 0 | free(ctzid); |
385 | 0 | if (res) |
386 | 0 | return res; |
387 | 0 | } |
388 | 0 | else if (strcmp(entry->tzid, iana) == 0) |
389 | 0 | return entry->windows; |
390 | 0 | } |
391 | | |
392 | 0 | return nullptr; |
393 | 0 | } |
394 | | #endif |
395 | | |
396 | | const char* TimeZoneIanaToWindows(const char* iana, TimeZoneNameType type) |
397 | 0 | { |
398 | 0 | if (!iana) |
399 | 0 | return nullptr; |
400 | | |
401 | 0 | const char* val = get_for_type(iana, type, iana_cmp); |
402 | 0 | if (val) |
403 | 0 | return val; |
404 | | |
405 | 0 | const char* wzid = map_fallback(iana, type); |
406 | 0 | if (!wzid) |
407 | 0 | return nullptr; |
408 | | |
409 | 0 | return get_for_type(wzid, type, id_cmp); |
410 | 0 | } |