Hal Canary | 6102192 | 2019-02-06 12:29:11 -0500 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
| 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 | |
| 4 | #include "SkLoadICU.h" |
| 5 | |
| 6 | #if defined(_WIN32) && defined(SK_USING_THIRD_PARTY_ICU) |
| 7 | |
| 8 | #ifndef WIN32_LEAN_AND_MEAN |
| 9 | #define WIN32_LEAN_AND_MEAN |
| 10 | #endif |
| 11 | #include <windows.h> |
| 12 | |
| 13 | #include <cstdio> |
| 14 | #include <mutex> |
| 15 | |
| 16 | #include "unicode/udata.h" |
| 17 | |
| 18 | #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" |
| 19 | #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" |
| 20 | |
| 21 | bool SkLoadICU() { |
| 22 | static bool good = false; |
| 23 | static std::once_flag flag; |
| 24 | std::call_once(flag, []() { |
| 25 | HMODULE module = LoadLibraryA(ICU_UTIL_DATA_SHARED_MODULE_NAME); |
| 26 | if (!module) { |
| 27 | fprintf(stderr, "Failed to load " ICU_UTIL_DATA_SHARED_MODULE_NAME "\n"); |
| 28 | return; |
| 29 | } |
| 30 | FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL); |
| 31 | if (!addr) { |
| 32 | fprintf(stderr, "Symbol " ICU_UTIL_DATA_SYMBOL " missing in " |
| 33 | ICU_UTIL_DATA_SHARED_MODULE_NAME ".\n"); |
| 34 | return; |
| 35 | } |
| 36 | UErrorCode err = U_ZERO_ERROR; |
| 37 | udata_setCommonData(reinterpret_cast<void*>(addr), &err); |
| 38 | if (err != U_ZERO_ERROR) { |
| 39 | fprintf(stderr, "udata_setCommonData() returned %d.\n", (int)err); |
| 40 | return; |
| 41 | } |
| 42 | udata_setFileAccess(UDATA_ONLY_PACKAGES, &err); |
| 43 | if (err != U_ZERO_ERROR) { |
| 44 | fprintf(stderr, "udata_setFileAccess() returned %d.\n", (int)err); |
| 45 | return; |
| 46 | } |
| 47 | good = true; |
| 48 | }); |
| 49 | return good; |
| 50 | } |
| 51 | |
| 52 | #undef ICU_UTIL_DATA_SHARED_MODULE_NAME |
| 53 | #undef ICU_UTIL_DATA_SYMBOL |
| 54 | |
| 55 | #endif // defined(_WIN32) && defined(SK_USING_THIRD_PARTY_ICU) |