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"
13 #include "base/files/file_path.h"
14 #include "base/i18n/icu_string_conversions.h"
15 #include "base/i18n/string_compare.h"
16 #include "base/logging.h"
17 #include "base/macros.h"
18 #include "base/memory/singleton.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/sys_string_conversions.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "build/build_config.h"
23 #include "third_party/icu/source/common/unicode/uniset.h"
24 #include "third_party/icu/source/i18n/unicode/coll.h"
31 class IllegalCharacters {
33 static IllegalCharacters* GetInstance() {
34 return Singleton<IllegalCharacters>::get();
37 bool DisallowedEverywhere(UChar32 ucs4) {
38 return !!illegal_anywhere_->contains(ucs4);
41 bool DisallowedLeadingOrTrailing(UChar32 ucs4) {
42 return !!illegal_at_ends_->contains(ucs4);
45 bool IsAllowedName(const string16& s) {
46 return s.empty() || (!!illegal_anywhere_->containsNone(
47 icu::UnicodeString(s.c_str(), s.size())) &&
48 !illegal_at_ends_->contains(*s.begin()) &&
49 !illegal_at_ends_->contains(*s.rbegin()));
53 friend class Singleton<IllegalCharacters>;
54 friend struct DefaultSingletonTraits<IllegalCharacters>;
57 ~IllegalCharacters() = default;
59 // set of characters considered invalid anywhere inside a filename.
60 std::unique_ptr<icu::UnicodeSet> illegal_anywhere_;
62 // set of characters considered invalid at either end of a filename.
63 std::unique_ptr<icu::UnicodeSet> illegal_at_ends_;
65 DISALLOW_COPY_AND_ASSIGN(IllegalCharacters);
68 IllegalCharacters::IllegalCharacters() {
69 UErrorCode everywhere_status = U_ZERO_ERROR;
70 UErrorCode ends_status = U_ZERO_ERROR;
71 // Control characters, formatting characters, non-characters, path separators,
72 // and some printable ASCII characters regarded as dangerous ('"*/:<>?\\').
73 // See http://blogs.msdn.com/michkap/archive/2006/11/03/941420.aspx
74 // and http://msdn2.microsoft.com/en-us/library/Aa365247.aspx
75 // Note that code points in the "Other, Format" (Cf) category are ignored on
76 // HFS+ despite the ZERO_WIDTH_JOINER and ZERO_WIDTH_NON-JOINER being
77 // legitimate in Arabic and some S/SE Asian scripts. In addition tilde (~) is
78 // also excluded due to the possibility of interacting poorly with short
79 // filenames on VFAT. (Related to CVE-2014-9390)
80 illegal_anywhere_.reset(new icu::UnicodeSet(
81 UNICODE_STRING_SIMPLE("[[\"~*/:<>?\\\\|][:Cc:][:Cf:]]"),
83 illegal_at_ends_.reset(new icu::UnicodeSet(
84 UNICODE_STRING_SIMPLE("[[:WSpace:][.]]"), ends_status));
85 DCHECK(U_SUCCESS(everywhere_status));
86 DCHECK(U_SUCCESS(ends_status));
88 // Add non-characters. If this becomes a performance bottleneck by
89 // any chance, do not add these to |set| and change IsFilenameLegal()
90 // to check |ucs4 & 0xFFFEu == 0xFFFEu|, in addiition to calling
92 illegal_anywhere_->add(0xFDD0, 0xFDEF);
93 for (int i = 0; i <= 0x10; ++i) {
94 int plane_base = 0x10000 * i;
95 illegal_anywhere_->add(plane_base + 0xFFFE, plane_base + 0xFFFF);
97 illegal_anywhere_->freeze();
98 illegal_at_ends_->freeze();
103 bool IsFilenameLegal(const string16& file_name) {
104 return IllegalCharacters::GetInstance()->IsAllowedName(file_name);
107 void ReplaceIllegalCharactersInPath(FilePath::StringType* file_name,
109 IllegalCharacters* illegal = IllegalCharacters::GetInstance();
111 DCHECK(!(illegal->DisallowedEverywhere(replace_char)));
112 DCHECK(!(illegal->DisallowedLeadingOrTrailing(replace_char)));
114 int cursor = 0; // The ICU macros expect an int.
115 while (cursor < static_cast<int>(file_name->size())) {
116 int char_begin = cursor;
119 // Windows uses UTF-16 encoding for filenames.
120 U16_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()),
123 // Mac and Chrome OS use UTF-8 encoding for filenames.
124 // Linux doesn't actually define file system encoding. Try to parse as
126 U8_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()),
130 if (illegal->DisallowedEverywhere(code_point) ||
131 ((char_begin == 0 || cursor == static_cast<int>(file_name->length())) &&
132 illegal->DisallowedLeadingOrTrailing(code_point))) {
133 file_name->replace(char_begin, cursor - char_begin, 1, replace_char);
134 // We just made the potentially multi-byte/word char into one that only
135 // takes one byte/word, so need to adjust the cursor to point to the next
137 cursor = char_begin + 1;
142 bool LocaleAwareCompareFilenames(const FilePath& a, const FilePath& b) {
143 UErrorCode error_code = U_ZERO_ERROR;
144 // Use the default collator. The default locale should have been properly
145 // set by the time this constructor is called.
146 std::unique_ptr<icu::Collator> collator(
147 icu::Collator::createInstance(error_code));
148 DCHECK(U_SUCCESS(error_code));
149 // Make it case-sensitive.
150 collator->setStrength(icu::Collator::TERTIARY);
153 return CompareString16WithCollator(*collator, WideToUTF16(a.value()),
154 WideToUTF16(b.value())) == UCOL_LESS;
156 #elif defined(OS_POSIX)
157 // On linux, the file system encoding is not defined. We assume
158 // SysNativeMBToWide takes care of it.
159 return CompareString16WithCollator(
160 *collator, WideToUTF16(SysNativeMBToWide(a.value())),
161 WideToUTF16(SysNativeMBToWide(b.value()))) == UCOL_LESS;
163 #error Not implemented on your system
167 void NormalizeFileNameEncoding(FilePath* file_name) {
168 #if defined(OS_CHROMEOS)
169 std::string normalized_str;
170 if (ConvertToUtf8AndNormalize(file_name->BaseName().value(), kCodepageUTF8,
172 !normalized_str.empty()) {
173 *file_name = file_name->DirName().Append(FilePath(normalized_str));