• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 // File utilities that use the ICU library go in this file.
6 
7 #include "base/i18n/file_util_icu.h"
8 
9 #include <stdint.h>
10 
11 #include <memory>
12 
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"
25 
26 namespace base {
27 namespace i18n {
28 
29 namespace {
30 
31 class IllegalCharacters {
32  public:
GetInstance()33   static IllegalCharacters* GetInstance() {
34     return Singleton<IllegalCharacters>::get();
35   }
36 
DisallowedEverywhere(UChar32 ucs4)37   bool DisallowedEverywhere(UChar32 ucs4) {
38     return !!illegal_anywhere_->contains(ucs4);
39   }
40 
DisallowedLeadingOrTrailing(UChar32 ucs4)41   bool DisallowedLeadingOrTrailing(UChar32 ucs4) {
42     return !!illegal_at_ends_->contains(ucs4);
43   }
44 
IsAllowedName(const string16 & s)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()));
50   }
51 
52  private:
53   friend class Singleton<IllegalCharacters>;
54   friend struct DefaultSingletonTraits<IllegalCharacters>;
55 
56   IllegalCharacters();
57   ~IllegalCharacters() = default;
58 
59   // set of characters considered invalid anywhere inside a filename.
60   std::unique_ptr<icu::UnicodeSet> illegal_anywhere_;
61 
62   // set of characters considered invalid at either end of a filename.
63   std::unique_ptr<icu::UnicodeSet> illegal_at_ends_;
64 
65   DISALLOW_COPY_AND_ASSIGN(IllegalCharacters);
66 };
67 
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:]]"),
82       everywhere_status));
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));
87 
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
91   // IsAllowedName().
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);
96   }
97   illegal_anywhere_->freeze();
98   illegal_at_ends_->freeze();
99 }
100 
101 }  // namespace
102 
IsFilenameLegal(const string16 & file_name)103 bool IsFilenameLegal(const string16& file_name) {
104   return IllegalCharacters::GetInstance()->IsAllowedName(file_name);
105 }
106 
ReplaceIllegalCharactersInPath(FilePath::StringType * file_name,char replace_char)107 void ReplaceIllegalCharactersInPath(FilePath::StringType* file_name,
108                                     char replace_char) {
109   IllegalCharacters* illegal = IllegalCharacters::GetInstance();
110 
111   DCHECK(!(illegal->DisallowedEverywhere(replace_char)));
112   DCHECK(!(illegal->DisallowedLeadingOrTrailing(replace_char)));
113 
114   int cursor = 0;  // The ICU macros expect an int.
115   while (cursor < static_cast<int>(file_name->size())) {
116     int char_begin = cursor;
117     uint32_t code_point;
118 #if defined(OS_WIN)
119     // Windows uses UTF-16 encoding for filenames.
120     U16_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()),
121              code_point);
122 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
123     // Mac and Chrome OS use UTF-8 encoding for filenames.
124     // Linux doesn't actually define file system encoding. Try to parse as
125     // UTF-8.
126     U8_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()),
127             code_point);
128 #else
129 #error Unsupported platform
130 #endif
131 
132     if (illegal->DisallowedEverywhere(code_point) ||
133         ((char_begin == 0 || cursor == static_cast<int>(file_name->length())) &&
134          illegal->DisallowedLeadingOrTrailing(code_point))) {
135       file_name->replace(char_begin, cursor - char_begin, 1, replace_char);
136       // We just made the potentially multi-byte/word char into one that only
137       // takes one byte/word, so need to adjust the cursor to point to the next
138       // character again.
139       cursor = char_begin + 1;
140     }
141   }
142 }
143 
LocaleAwareCompareFilenames(const FilePath & a,const FilePath & b)144 bool LocaleAwareCompareFilenames(const FilePath& a, const FilePath& b) {
145   UErrorCode error_code = U_ZERO_ERROR;
146   // Use the default collator. The default locale should have been properly
147   // set by the time this constructor is called.
148   std::unique_ptr<icu::Collator> collator(
149       icu::Collator::createInstance(error_code));
150   DCHECK(U_SUCCESS(error_code));
151   // Make it case-sensitive.
152   collator->setStrength(icu::Collator::TERTIARY);
153 
154 #if defined(OS_WIN)
155   return CompareString16WithCollator(*collator, WideToUTF16(a.value()),
156                                      WideToUTF16(b.value())) == UCOL_LESS;
157 
158 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
159   // On linux, the file system encoding is not defined. We assume
160   // SysNativeMBToWide takes care of it.
161   return CompareString16WithCollator(
162              *collator, WideToUTF16(SysNativeMBToWide(a.value())),
163              WideToUTF16(SysNativeMBToWide(b.value()))) == UCOL_LESS;
164 #endif
165 }
166 
NormalizeFileNameEncoding(FilePath * file_name)167 void NormalizeFileNameEncoding(FilePath* file_name) {
168 #if defined(OS_CHROMEOS)
169   std::string normalized_str;
170   if (ConvertToUtf8AndNormalize(file_name->BaseName().value(), kCodepageUTF8,
171                                 &normalized_str) &&
172       !normalized_str.empty()) {
173     *file_name = file_name->DirName().Append(FilePath(normalized_str));
174   }
175 #endif
176 }
177 
178 }  // namespace i18n
179 }  // namespace base
180