1 /*
2 * Copyright (C) 2007, 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "FileSystem.h"
28
29 #include <wtf/HexNumber.h>
30
31 namespace WebCore {
32
33 // The following lower-ASCII characters need escaping to be used in a filename
34 // across all systems, including Windows:
35 // - Unprintable ASCII (00-1F)
36 // - Space (20)
37 // - Double quote (22)
38 // - Percent (25) (escaped because it is our escape character)
39 // - Asterisk (2A)
40 // - Slash (2F)
41 // - Colon (3A)
42 // - Less-than (3C)
43 // - Greater-than (3E)
44 // - Question Mark (3F)
45 // - Backslash (5C)
46 // - Pipe (7C)
47 // - Delete (7F)
48
49 static const bool needsEscaping[128] = {
50 /* 00-07 */ true, true, true, true, true, true, true, true,
51 /* 08-0F */ true, true, true, true, true, true, true, true,
52
53 /* 10-17 */ true, true, true, true, true, true, true, true,
54 /* 18-1F */ true, true, true, true, true, true, true, true,
55
56 /* 20-27 */ true, false, true, false, false, true, false, false,
57 /* 28-2F */ false, false, true, false, false, false, false, true,
58
59 /* 30-37 */ false, false, false, false, false, false, false, false,
60 /* 38-3F */ false, false, true, false, true, false, true, true,
61
62 /* 40-47 */ false, false, false, false, false, false, false, false,
63 /* 48-4F */ false, false, false, false, false, false, false, false,
64
65 /* 50-57 */ false, false, false, false, false, false, false, false,
66 /* 58-5F */ false, false, false, false, true, false, false, false,
67
68 /* 60-67 */ false, false, false, false, false, false, false, false,
69 /* 68-6F */ false, false, false, false, false, false, false, false,
70
71 /* 70-77 */ false, false, false, false, false, false, false, false,
72 /* 78-7F */ false, false, false, false, true, false, false, true,
73 };
74
shouldEscapeUChar(UChar c)75 static inline bool shouldEscapeUChar(UChar c)
76 {
77 return c > 127 ? false : needsEscaping[c];
78 }
79
encodeForFileName(const String & inputStr)80 String encodeForFileName(const String& inputStr)
81 {
82 unsigned length = inputStr.length();
83 Vector<UChar, 512> buffer(length * 3 + 1);
84 UChar* p = buffer.data();
85
86 const UChar* str = inputStr.characters();
87 const UChar* strEnd = str + length;
88
89 while (str < strEnd) {
90 UChar c = *str++;
91 if (shouldEscapeUChar(c)) {
92 *p++ = '%';
93 placeByteAsHex(c, p);
94 } else
95 *p++ = c;
96 }
97
98 ASSERT(p - buffer.data() <= static_cast<int>(buffer.size()));
99
100 return String(buffer.data(), p - buffer.data());
101 }
102
103 #if !PLATFORM(MAC)
104
canExcludeFromBackup()105 bool canExcludeFromBackup()
106 {
107 return false;
108 }
109
excludeFromBackup(const String &)110 bool excludeFromBackup(const String&)
111 {
112 return false;
113 }
114
115 #endif
116
117 } // namespace WebCore
118