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