1// Copyright (c) 2011 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 "base/file_path.h"
10#include "base/logging.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/memory/singleton.h"
13#include "base/string_util.h"
14#include "base/utf_string_conversions.h"
15#include "base/sys_string_conversions.h"
16#include "build/build_config.h"
17#include "unicode/coll.h"
18#include "unicode/uniset.h"
19
20namespace {
21
22class IllegalCharacters {
23 public:
24  static IllegalCharacters* GetInstance() {
25    return Singleton<IllegalCharacters>::get();
26  }
27
28  bool contains(UChar32 ucs4) {
29    return !!set->contains(ucs4);
30  }
31
32  bool containsNone(const string16 &s) {
33    return !!set->containsNone(icu::UnicodeString(s.c_str(), s.size()));
34  }
35
36 private:
37  friend class Singleton<IllegalCharacters>;
38  friend struct DefaultSingletonTraits<IllegalCharacters>;
39
40  IllegalCharacters();
41  ~IllegalCharacters() { }
42
43  scoped_ptr<icu::UnicodeSet> set;
44
45  DISALLOW_COPY_AND_ASSIGN(IllegalCharacters);
46};
47
48IllegalCharacters::IllegalCharacters() {
49  UErrorCode status = U_ZERO_ERROR;
50  // Control characters, formatting characters, non-characters, and
51  // some printable ASCII characters regarded as dangerous ('"*/:<>?\\').
52  // See  http://blogs.msdn.com/michkap/archive/2006/11/03/941420.aspx
53  // and http://msdn2.microsoft.com/en-us/library/Aa365247.aspx
54  // TODO(jungshik): Revisit the set. ZWJ and ZWNJ are excluded because they
55  // are legitimate in Arabic and some S/SE Asian scripts. However, when used
56  // elsewhere, they can be confusing/problematic.
57  // Also, consider wrapping the set with our Singleton class to create and
58  // freeze it only once. Note that there's a trade-off between memory and
59  // speed.
60#if defined(WCHAR_T_IS_UTF16)
61  set.reset(new icu::UnicodeSet(icu::UnicodeString(
62      L"[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\u200c\u200d]]"), status));
63#else
64  set.reset(new icu::UnicodeSet(UNICODE_STRING_SIMPLE(
65      "[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\\u200c\\u200d]]").unescape(),
66      status));
67#endif
68  DCHECK(U_SUCCESS(status));
69  // Add non-characters. If this becomes a performance bottleneck by
70  // any chance, do not add these to |set| and change IsFilenameLegal()
71  // to check |ucs4 & 0xFFFEu == 0xFFFEu|, in addiition to calling
72  // containsNone().
73  set->add(0xFDD0, 0xFDEF);
74  for (int i = 0; i <= 0x10; ++i) {
75    int plane_base = 0x10000 * i;
76    set->add(plane_base + 0xFFFE, plane_base + 0xFFFF);
77  }
78  set->freeze();
79}
80
81class LocaleAwareComparator {
82 public:
83  static LocaleAwareComparator* GetInstance() {
84    return Singleton<LocaleAwareComparator>::get();
85  }
86
87  // Note: A similar function is available in l10n_util.
88  // We cannot use it because base should not depend on l10n_util.
89  // TODO(yuzo): Move some of l10n_util to base.
90  int Compare(const string16& a, const string16& b) {
91    // We are not sure if Collator::compare is thread-safe.
92    // Use an AutoLock just in case.
93    base::AutoLock auto_lock(lock_);
94
95    UErrorCode error_code = U_ZERO_ERROR;
96    UCollationResult result = collator_->compare(
97        static_cast<const UChar*>(a.c_str()),
98        static_cast<int>(a.length()),
99        static_cast<const UChar*>(b.c_str()),
100        static_cast<int>(b.length()),
101        error_code);
102    DCHECK(U_SUCCESS(error_code));
103    return result;
104  }
105
106 private:
107  LocaleAwareComparator() {
108    UErrorCode error_code = U_ZERO_ERROR;
109    // Use the default collator. The default locale should have been properly
110    // set by the time this constructor is called.
111    collator_.reset(icu::Collator::createInstance(error_code));
112    DCHECK(U_SUCCESS(error_code));
113    // Make it case-sensitive.
114    collator_->setStrength(icu::Collator::TERTIARY);
115    // Note: We do not set UCOL_NORMALIZATION_MODE attribute. In other words, we
116    // do not pay performance penalty to guarantee sort order correctness for
117    // non-FCD (http://unicode.org/notes/tn5/#FCD) file names. This should be a
118    // reasonable tradeoff because such file names should be rare and the sort
119    // order doesn't change much anyway.
120  }
121
122  scoped_ptr<icu::Collator> collator_;
123  base::Lock lock_;
124  friend struct DefaultSingletonTraits<LocaleAwareComparator>;
125
126  DISALLOW_COPY_AND_ASSIGN(LocaleAwareComparator);
127};
128
129}  // namespace
130
131namespace file_util {
132
133bool IsFilenameLegal(const string16& file_name) {
134  return IllegalCharacters::GetInstance()->containsNone(file_name);
135}
136
137void ReplaceIllegalCharactersInPath(FilePath::StringType* file_name,
138                                    char replace_char) {
139  DCHECK(file_name);
140
141  DCHECK(!(IllegalCharacters::GetInstance()->contains(replace_char)));
142
143  // Remove leading and trailing whitespace.
144  TrimWhitespace(*file_name, TRIM_ALL, file_name);
145
146  IllegalCharacters* illegal = IllegalCharacters::GetInstance();
147  int cursor = 0;  // The ICU macros expect an int.
148  while (cursor < static_cast<int>(file_name->size())) {
149    int char_begin = cursor;
150    uint32 code_point;
151#if defined(OS_MACOSX)
152    // Mac uses UTF-8 encoding for filenames.
153    U8_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()),
154            code_point);
155#elif defined(OS_WIN)
156    // Windows uses UTF-16 encoding for filenames.
157    U16_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()),
158             code_point);
159#elif defined(OS_POSIX)
160    // Linux doesn't actually define an encoding. It basically allows anything
161    // except for a few special ASCII characters.
162    unsigned char cur_char = static_cast<unsigned char>((*file_name)[cursor++]);
163    if (cur_char >= 0x80)
164      continue;
165    code_point = cur_char;
166#else
167    NOTREACHED();
168#endif
169
170    if (illegal->contains(code_point)) {
171      file_name->replace(char_begin, cursor - char_begin, 1, replace_char);
172      // We just made the potentially multi-byte/word char into one that only
173      // takes one byte/word, so need to adjust the cursor to point to the next
174      // character again.
175      cursor = char_begin + 1;
176    }
177  }
178}
179
180bool LocaleAwareCompareFilenames(const FilePath& a, const FilePath& b) {
181#if defined(OS_WIN)
182  return LocaleAwareComparator::GetInstance()->Compare(a.value().c_str(),
183                                                       b.value().c_str()) < 0;
184
185#elif defined(OS_POSIX)
186  // On linux, the file system encoding is not defined. We assume
187  // SysNativeMBToWide takes care of it.
188  //
189  // ICU's collator can take strings in OS native encoding. But we convert the
190  // strings to UTF-16 ourselves to ensure conversion consistency.
191  // TODO(yuzo): Perhaps we should define SysNativeMBToUTF16?
192  return LocaleAwareComparator::GetInstance()->Compare(
193      WideToUTF16(base::SysNativeMBToWide(a.value().c_str())),
194      WideToUTF16(base::SysNativeMBToWide(b.value().c_str()))) < 0;
195#else
196  #error Not implemented on your system
197#endif
198}
199
200}  // namespace
201