Line data Source code
1 : // Copyright 2013 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/icu_util.h"
6 :
7 : #if defined(_WIN32)
8 : #include <windows.h>
9 : #endif
10 :
11 : #if defined(V8_INTL_SUPPORT)
12 : #include <stdio.h>
13 : #include <stdlib.h>
14 :
15 : #include "unicode/putil.h"
16 : #include "unicode/udata.h"
17 :
18 : #include "src/base/build_config.h"
19 : #include "src/base/file-utils.h"
20 :
21 : #define ICU_UTIL_DATA_FILE 0
22 : #define ICU_UTIL_DATA_SHARED 1
23 : #define ICU_UTIL_DATA_STATIC 2
24 :
25 : #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat"
26 : #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll"
27 : #endif
28 :
29 : namespace v8 {
30 :
31 : namespace internal {
32 :
33 : #if defined(V8_INTL_SUPPORT) && (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
34 : namespace {
35 : char* g_icu_data_ptr = nullptr;
36 :
37 58263 : void free_icu_data_ptr() {
38 58263 : delete[] g_icu_data_ptr;
39 58263 : }
40 :
41 : } // namespace
42 : #endif
43 :
44 58263 : bool InitializeICUDefaultLocation(const char* exec_path,
45 : const char* icu_data_file) {
46 : #if !defined(V8_INTL_SUPPORT)
47 : return true;
48 : #else
49 : #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
50 58263 : if (icu_data_file) {
51 0 : return InitializeICU(icu_data_file);
52 : }
53 : char* icu_data_file_default;
54 : #if defined(V8_TARGET_LITTLE_ENDIAN)
55 58263 : base::RelativePath(&icu_data_file_default, exec_path, "icudtl.dat");
56 : #elif defined(V8_TARGET_BIG_ENDIAN)
57 : base::RelativePath(&icu_data_file_default, exec_path, "icudtb.dat");
58 : #else
59 : #error Unknown byte ordering
60 : #endif
61 58263 : bool result = InitializeICU(icu_data_file_default);
62 58263 : free(icu_data_file_default);
63 58263 : return result;
64 : #else
65 : return InitializeICU(nullptr);
66 : #endif
67 : #endif
68 : }
69 :
70 58263 : bool InitializeICU(const char* icu_data_file) {
71 : #if !defined(V8_INTL_SUPPORT)
72 : return true;
73 : #else
74 : #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED
75 : // We expect to find the ICU data module alongside the current module.
76 : HMODULE module = LoadLibraryA(ICU_UTIL_DATA_SHARED_MODULE_NAME);
77 : if (!module) return false;
78 :
79 : FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL);
80 : if (!addr) return false;
81 :
82 : UErrorCode err = U_ZERO_ERROR;
83 : udata_setCommonData(reinterpret_cast<void*>(addr), &err);
84 : // Never try to load ICU data from files.
85 : udata_setFileAccess(UDATA_ONLY_PACKAGES, &err);
86 : return err == U_ZERO_ERROR;
87 : #elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC
88 : // Mac/Linux bundle the ICU data in.
89 : return true;
90 : #elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
91 58263 : if (!icu_data_file) return false;
92 :
93 58263 : if (g_icu_data_ptr) return true;
94 :
95 58263 : FILE* inf = fopen(icu_data_file, "rb");
96 58263 : if (!inf) return false;
97 :
98 58263 : fseek(inf, 0, SEEK_END);
99 58263 : size_t size = ftell(inf);
100 58263 : rewind(inf);
101 :
102 58263 : g_icu_data_ptr = new char[size];
103 58263 : if (fread(g_icu_data_ptr, 1, size, inf) != size) {
104 0 : delete[] g_icu_data_ptr;
105 0 : g_icu_data_ptr = nullptr;
106 0 : fclose(inf);
107 0 : return false;
108 : }
109 58263 : fclose(inf);
110 :
111 58263 : atexit(free_icu_data_ptr);
112 :
113 58263 : UErrorCode err = U_ZERO_ERROR;
114 58263 : udata_setCommonData(reinterpret_cast<void*>(g_icu_data_ptr), &err);
115 : // Never try to load ICU data from files.
116 58263 : udata_setFileAccess(UDATA_ONLY_PACKAGES, &err);
117 58263 : return err == U_ZERO_ERROR;
118 : #endif
119 : #endif
120 : }
121 :
122 : } // namespace internal
123 : } // namespace v8
|