1 // Copyright (c) 2012 The Chromium 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.
5 // File utilities that use the ICU library go in this file.
7 #include "base/i18n/file_util_icu.h"
9 #include "base/files/file_path.h"
10 #include "base/i18n/icu_string_conversions.h"
11 #include "base/i18n/string_compare.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/singleton.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/sys_string_conversions.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "build/build_config.h"
19 #include "third_party/icu/source/common/unicode/uniset.h"
20 #include "third_party/icu/source/i18n/unicode/coll.h"
26 class IllegalCharacters
{
28 static IllegalCharacters
* GetInstance() {
29 return Singleton
<IllegalCharacters
>::get();
32 bool contains(UChar32 ucs4
) {
33 return !!set
->contains(ucs4
);
36 bool containsNone(const string16
&s
) {
37 return !!set
->containsNone(icu::UnicodeString(s
.c_str(), s
.size()));
41 friend class Singleton
<IllegalCharacters
>;
42 friend struct DefaultSingletonTraits
<IllegalCharacters
>;
45 ~IllegalCharacters() { }
47 scoped_ptr
<icu::UnicodeSet
> set
;
49 DISALLOW_COPY_AND_ASSIGN(IllegalCharacters
);
52 IllegalCharacters::IllegalCharacters() {
53 UErrorCode status
= U_ZERO_ERROR
;
54 // Control characters, formatting characters, non-characters, and
55 // some printable ASCII characters regarded as dangerous ('"*/:<>?\\').
56 // See http://blogs.msdn.com/michkap/archive/2006/11/03/941420.aspx
57 // and http://msdn2.microsoft.com/en-us/library/Aa365247.aspx
58 // TODO(jungshik): Revisit the set. ZWJ and ZWNJ are excluded because they
59 // are legitimate in Arabic and some S/SE Asian scripts. However, when used
60 // elsewhere, they can be confusing/problematic.
61 // Also, consider wrapping the set with our Singleton class to create and
62 // freeze it only once. Note that there's a trade-off between memory and
64 #if defined(WCHAR_T_IS_UTF16)
65 set
.reset(new icu::UnicodeSet(icu::UnicodeString(
66 L
"[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\u200c\u200d]]"), status
));
68 set
.reset(new icu::UnicodeSet(UNICODE_STRING_SIMPLE(
69 "[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\\u200c\\u200d]]").unescape(),
72 DCHECK(U_SUCCESS(status
));
73 // Add non-characters. If this becomes a performance bottleneck by
74 // any chance, do not add these to |set| and change IsFilenameLegal()
75 // to check |ucs4 & 0xFFFEu == 0xFFFEu|, in addiition to calling
77 set
->add(0xFDD0, 0xFDEF);
78 for (int i
= 0; i
<= 0x10; ++i
) {
79 int plane_base
= 0x10000 * i
;
80 set
->add(plane_base
+ 0xFFFE, plane_base
+ 0xFFFF);
89 bool IsFilenameLegal(const string16
& file_name
) {
90 return IllegalCharacters::GetInstance()->containsNone(file_name
);
93 void ReplaceIllegalCharactersInPath(base::FilePath::StringType
* file_name
,
97 DCHECK(!(IllegalCharacters::GetInstance()->contains(replace_char
)));
99 // Remove leading and trailing whitespace.
100 base::TrimWhitespace(*file_name
, base::TRIM_ALL
, file_name
);
102 IllegalCharacters
* illegal
= IllegalCharacters::GetInstance();
103 int cursor
= 0; // The ICU macros expect an int.
104 while (cursor
< static_cast<int>(file_name
->size())) {
105 int char_begin
= cursor
;
107 #if defined(OS_MACOSX)
108 // Mac uses UTF-8 encoding for filenames.
109 U8_NEXT(file_name
->data(), cursor
, static_cast<int>(file_name
->length()),
111 #elif defined(OS_WIN)
112 // Windows uses UTF-16 encoding for filenames.
113 U16_NEXT(file_name
->data(), cursor
, static_cast<int>(file_name
->length()),
115 #elif defined(OS_POSIX)
116 // Linux doesn't actually define an encoding. It basically allows anything
117 // except for a few special ASCII characters.
118 unsigned char cur_char
= static_cast<unsigned char>((*file_name
)[cursor
++]);
119 if (cur_char
>= 0x80)
121 code_point
= cur_char
;
126 if (illegal
->contains(code_point
)) {
127 file_name
->replace(char_begin
, cursor
- char_begin
, 1, replace_char
);
128 // We just made the potentially multi-byte/word char into one that only
129 // takes one byte/word, so need to adjust the cursor to point to the next
131 cursor
= char_begin
+ 1;
136 bool LocaleAwareCompareFilenames(const base::FilePath
& a
,
137 const base::FilePath
& b
) {
138 UErrorCode error_code
= U_ZERO_ERROR
;
139 // Use the default collator. The default locale should have been properly
140 // set by the time this constructor is called.
141 scoped_ptr
<icu::Collator
> collator(icu::Collator::createInstance(error_code
));
142 DCHECK(U_SUCCESS(error_code
));
143 // Make it case-sensitive.
144 collator
->setStrength(icu::Collator::TERTIARY
);
147 return base::i18n::CompareString16WithCollator(collator
.get(),
148 base::WideToUTF16(a
.value()), base::WideToUTF16(b
.value())) == UCOL_LESS
;
150 #elif defined(OS_POSIX)
151 // On linux, the file system encoding is not defined. We assume
152 // SysNativeMBToWide takes care of it.
153 return base::i18n::CompareString16WithCollator(
155 base::WideToUTF16(base::SysNativeMBToWide(a
.value().c_str())),
156 base::WideToUTF16(base::SysNativeMBToWide(b
.value().c_str()))
159 #error Not implemented on your system
163 void NormalizeFileNameEncoding(base::FilePath
* file_name
) {
164 #if defined(OS_CHROMEOS)
165 std::string normalized_str
;
166 if (base::ConvertToUtf8AndNormalize(file_name
->BaseName().value(),
169 *file_name
= file_name
->DirName().Append(base::FilePath(normalized_str
));