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#include <wtf/text/StringBuilder.h>
31
32namespace WebCore {
33
34// The following lower-ASCII characters need escaping to be used in a filename
35// across all systems, including Windows:
36//     - Unprintable ASCII (00-1F)
37//     - Space             (20)
38//     - Double quote      (22)
39//     - Percent           (25) (escaped because it is our escape character)
40//     - Asterisk          (2A)
41//     - Slash             (2F)
42//     - Colon             (3A)
43//     - Less-than         (3C)
44//     - Greater-than      (3E)
45//     - Question Mark     (3F)
46//     - Backslash         (5C)
47//     - Pipe              (7C)
48//     - Delete            (7F)
49
50static const bool needsEscaping[128] = {
51    /* 00-07 */ true,  true,  true,  true,  true,  true,  true,  true,
52    /* 08-0F */ true,  true,  true,  true,  true,  true,  true,  true,
53
54    /* 10-17 */ true,  true,  true,  true,  true,  true,  true,  true,
55    /* 18-1F */ true,  true,  true,  true,  true,  true,  true,  true,
56
57    /* 20-27 */ true,  false, true,  false, false, true,  false, false,
58    /* 28-2F */ false, false, true,  false, false, false, false, true,
59
60    /* 30-37 */ false, false, false, false, false, false, false, false,
61    /* 38-3F */ false, false, true,  false, true,  false, true,  true,
62
63    /* 40-47 */ false, false, false, false, false, false, false, false,
64    /* 48-4F */ false, false, false, false, false, false, false, false,
65
66    /* 50-57 */ false, false, false, false, false, false, false, false,
67    /* 58-5F */ false, false, false, false, true,  false, false, false,
68
69    /* 60-67 */ false, false, false, false, false, false, false, false,
70    /* 68-6F */ false, false, false, false, false, false, false, false,
71
72    /* 70-77 */ false, false, false, false, false, false, false, false,
73    /* 78-7F */ false, false, false, false, true,  false, false, true,
74};
75
76static inline bool shouldEscapeUChar(UChar c)
77{
78    return c > 127 ? false : needsEscaping[c];
79}
80
81String encodeForFileName(const String& inputString)
82{
83    StringBuilder result;
84    StringImpl* stringImpl = inputString.impl();
85    unsigned length = inputString.length();
86    for (unsigned i = 0; i < length; ++i) {
87        UChar character = (*stringImpl)[i];
88        if (shouldEscapeUChar(character)) {
89            result.append('%');
90            appendByteAsHex(character, result);
91        } else
92            result.append(character);
93    }
94
95    return result.toString();
96}
97
98#if !PLATFORM(MAC)
99
100void setMetadataURL(String&, const String&, const String&)
101{
102}
103
104bool canExcludeFromBackup()
105{
106    return false;
107}
108
109bool excludeFromBackup(const String&)
110{
111    return false;
112}
113
114#endif
115
116} // namespace WebCore
117