blob: 159c57f3dc3f88daa9d79237052ae73fdc48d29e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*******************************************************************************
* Copyright (c) 2008, 2010 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.equinox.internal.security.storage;
/**
* Utility class to encode forward slash characters so that strings containing
* forward slashes can be used as node names with secure preferences. It is
* the responsibility of the consumer to manually encode such strings before
* attempting to obtain corresponding nodes from secure preferences.
* <p>
* Internally, the class uses a subset of JIT encoding. The forward slashes
* and backward slashes are encoded.
* </p><p>
* This class is not intended to be instantiated or subclassed by users.
* </p>
*/
final public class SlashEncode {
final private static char SLASH = '/';
final private static char BACK_SLASH = '\\';
final private static String ENCODED_SLASH = "\\2f"; //$NON-NLS-1$
final private static String ENCODED_BACK_SLASH = "\\5c"; //$NON-NLS-1$
static public String decode(String str) {
if (str == null)
return null;
int size = str.length();
if (size == 0)
return str;
StringBuffer processed = new StringBuffer(size);
int processedPos = 0;
for (int i = 0; i < size; i++) {
char c = str.charAt(i);
if (c == BACK_SLASH) {
if (i + 2 >= size)
continue;
String encoded = str.substring(i, i + 3);
char decoded = 0;
if (ENCODED_SLASH.equals(encoded))
decoded = SLASH;
else if (ENCODED_BACK_SLASH.equals(encoded))
decoded = BACK_SLASH;
if (decoded == 0)
continue;
if (i > processedPos)
processed.append(str.substring(processedPos, i));
processed.append(decoded);
processedPos = i + 3;
i += 2; // skip over remaining encoded portion
}
}
if (processedPos == 0)
return str;
if (processedPos < size)
processed.append(str.substring(processedPos));
return new String(processed);
}
static public String encode(String str) {
if (str == null)
return null;
int size = str.length();
if (size == 0)
return str;
StringBuffer processed = new StringBuffer(size);
int processedPos = 0;
for (int i = 0; i < size; i++) {
char c = str.charAt(i);
if (c == SLASH || c == BACK_SLASH) {
if (i > processedPos)
processed.append(str.substring(processedPos, i));
if (c == SLASH)
processed.append(ENCODED_SLASH);
else if (c == BACK_SLASH)
processed.append(ENCODED_BACK_SLASH);
processedPos = i + 1;
}
}
if (processedPos == 0)
return str;
if (processedPos < size)
processed.append(str.substring(processedPos));
return new String(processed);
}
}
|