Debian Codegraph @

net/sf/saxon/functions/EncodeForUri.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.functions;
9
10 import net.sf.saxon.expr.XPathContext;
11 import net.sf.saxon.om.Item;
12 import net.sf.saxon.om.ZeroOrOne;
13 import net.sf.saxon.serialize.charcode.UTF8CharacterSet;
14 import net.sf.saxon.trans.Err;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.util.FastStringBuffer;
17 import net.sf.saxon.value.AtomicValue;
18 import net.sf.saxon.value.StringValue;
19
20 /**
21 * This class supports the function fn:encode-for-uri()
22 */
23
24 public class EncodeForUri extends ScalarSystemFunction {
25
26 @Override
27 public AtomicValue evaluate(Item arg, XPathContext context) throws XPathException {
28 final CharSequence s = arg.getStringValueCS();
29 return StringValue.makeStringValue(escape(s, "-_.~"));
30 }
31
32 @Override
33 public ZeroOrOne<? extends AtomicValue> resultWhenEmpty() {
34 return ZERO_LENGTH_STRING;
35 }
36
37 /**
38 * Escape special characters in a URI. The characters that are %HH-encoded are
39 * all non-ASCII characters, plus all ASCII characters except (a) letter A-Z
40 * and a-z, (b) digits 0-9, and (c) characters listed in the allowedPunctuation
41 * argument
42 *
43 * @param s the URI to be escaped
44 * @param allowedPunctuation ASCII characters other than letters and digits that
45 * should NOT be %HH-encoded
46 * @return the %HH-encoded string
47 */
48
49 public static CharSequence escape(CharSequence s, String allowedPunctuation) {
50 FastStringBuffer sb = new FastStringBuffer(s.length());
51 for (int i = 0; i < s.length(); i++) {
52 char c = s.charAt(i);
53 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
54 sb.append(c);
55 } else if (c <= 0x20 || c >= 0x7f) {
56 escapeChar(c, (i + 1) < s.length() ? s.charAt(i + 1) : ' ', sb);
57 } else if (allowedPunctuation.indexOf(c) >= 0) {
58 sb.append(c);
59 } else {
60 escapeChar(c, ' ', sb);
61 }
62
63 }
64 return sb;
65 }
66
67 private static final String hex = "0123456789ABCDEF";
68
69 /**
70 * Escape a single character in %HH representation, or a pair of two chars representing
71 * a surrogate pair
72 *
73 * @param c the character to be escaped, or the first character of a surrogate pair
74 * @param c2 the second character of a surrogate pair
75 * @param sb the buffer to contain the escaped result
76 */
77
78 public static void escapeChar(char c, char c2, FastStringBuffer sb) {
79 byte[] array = new byte[4];
80 int used = UTF8CharacterSet.getUTF8Encoding(c, c2, array);
81 for (int b = 0; b < used; b++) {
82 int v = (int) array[b] & 0xff;
83 sb.append('%');
84 sb.append(hex.charAt(v / 16));
85 sb.append(hex.charAt(v % 16));
86 }
87 }
88
89 /**
90 * Check that any percent-encoding within a URI is well-formed. The method assumes that a percent
91 * sign followed by two hex digits represents an octet of the UTF-8 representation of a character;
92 * any other percent sign is assumed to represent itself.
93 *
94 * @param uri the string to be checked for validity
95 * @throws XPathException if the string is not validly percent-encoded
96 */
97
98 public static void checkPercentEncoding(String uri) throws XPathException {
99 final String hexDigits = "0123456789abcdefABCDEF";
100 for (int i = 0; i < uri.length(); ) {
101 char c = uri.charAt(i);
102 @SuppressWarnings("MismatchedReadAndWriteOfArray") byte[] bytes;
103 // Note: we're translating the UTF-8 byte sequence but then not using the value
104 int expectedOctets;
105 if (c == '%') {
106 if (i + 2 >= uri.length()) {
107 throw new XPathException("% sign in URI must be followed by two hex digits" +
108 Err.wrap(uri));
109 }
110 int h1 = hexDigits.indexOf(uri.charAt(i + 1));
111 if (h1 > 15) {
112 h1 -= 6;
113 }
114
115 int h2 = hexDigits.indexOf(uri.charAt(i + 2));
116 if (h2 > 15) {
117 h2 -= 6;
118 }
119 if (h1 >= 0 && h2 >= 0) {
120 int b = h1 << 4 | h2;
121 expectedOctets = UTF8RepresentationLength[h1];
122 if (expectedOctets == -1) {
123 throw new XPathException("First %-encoded octet in URI is not valid as the start of a UTF-8 " +
124 "character: first two bits must not be '10'" +
125 Err.wrap(uri));
126 }
127 bytes = new byte[expectedOctets];
128 bytes[0] = (byte) b;
129 i += 3;
130 for (int q = 1; q < expectedOctets; q++) {
131 if (i + 2 > uri.length() || uri.charAt(i) != '%') {
132 throw new XPathException("Incomplete %-encoded UTF-8 octet sequence in URI " +
133 Err.wrap(uri));
134 }
135 h1 = hexDigits.indexOf(uri.charAt(i + 1));
136 if (h1 > 15) {
137 h1 -= 6;
138 }
139
140 h2 = hexDigits.indexOf(uri.charAt(i + 2));
141 if (h2 > 15) {
142 h2 -= 6;
143 }
144 if (h1 < 0 || h2 < 0) {
145 throw new XPathException("Invalid %-encoded UTF-8 octet sequence in URI" +
146 Err.wrap(uri));
147 }
148 if (UTF8RepresentationLength[h1] != -1) {
149 throw new XPathException("In a URI, a %-encoded UTF-8 octet after the first " +
150 "must have '10' as the first two bits" +
151 Err.wrap(uri));
152 }
153 b = h1 << 4 | h2;
154 bytes[q] = (byte) b;
155 i += 3;
156 }
157 } else {
158 throw new XPathException("% sign in URI must be followed by two hex digits" +
159 Err.wrap(uri));
160 }
161 } else {
162 i++;
163 }
164
165 }
166
167 }
168
169 // Length of a UTF8 byte sequence, as a function of the first nibble
170 private static int[] UTF8RepresentationLength = {1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 2, 2, 3, 4};
171 }
172