001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.escape; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018import static java.lang.Character.charCount; 019import static java.lang.System.arraycopy; 020 021import com.google.common.annotations.GwtCompatible; 022import org.jspecify.annotations.Nullable; 023 024/** 025 * An {@link Escaper} that converts literal text into a format safe for inclusion in a particular 026 * context (such as an XML document). Typically (but not always), the inverse process of 027 * "unescaping" the text is performed automatically by the relevant parser. 028 * 029 * <p>For example, an XML escaper would convert the literal string {@code "Foo<Bar>"} into {@code 030 * "Foo<Bar>"} to prevent {@code "<Bar>"} from being confused with an XML tag. When the 031 * resulting XML document is parsed, the parser API will return this text as the original literal 032 * string {@code "Foo<Bar>"}. 033 * 034 * <p><b>Note:</b> This class is similar to {@link CharEscaper} but with one very important 035 * difference. A CharEscaper can only process Java <a 036 * href="http://en.wikipedia.org/wiki/UTF-16">UTF16</a> characters in isolation and may not cope 037 * when it encounters surrogate pairs. This class facilitates the correct escaping of all Unicode 038 * characters. 039 * 040 * <p>As there are important reasons, including potential security issues, to handle Unicode 041 * correctly if you are considering implementing a new escaper you should favor using UnicodeEscaper 042 * wherever possible. 043 * 044 * <p>A {@code UnicodeEscaper} instance is required to be stateless, and safe when used concurrently 045 * by multiple threads. 046 * 047 * <p>Popular escapers are defined as constants in classes like {@link 048 * com.google.common.html.HtmlEscapers} and {@link com.google.common.xml.XmlEscapers}. To create 049 * your own escapers extend this class and implement the {@link #escape(int)} method. 050 * 051 * @author David Beaumont 052 * @since 15.0 053 */ 054@GwtCompatible 055@SuppressWarnings("EscapedEntity") // We do mean for the user to see "<" etc. 056public abstract class UnicodeEscaper extends Escaper { 057 /** The amount of padding (chars) to use when growing the escape buffer. */ 058 private static final int DEST_PAD = 32; 059 060 /** Constructor for use by subclasses. */ 061 protected UnicodeEscaper() {} 062 063 /** 064 * Returns the escaped form of the given Unicode code point, or {@code null} if this code point 065 * does not need to be escaped. When called as part of an escaping operation, the given code point 066 * is guaranteed to be in the range {@code 0 <= cp <= Character#MAX_CODE_POINT}. 067 * 068 * <p>If an empty array is returned, this effectively strips the input character from the 069 * resulting text. 070 * 071 * <p>If the character does not need to be escaped, this method should return {@code null}, rather 072 * than an array containing the character representation of the code point. This enables the 073 * escaping algorithm to perform more efficiently. 074 * 075 * <p>If the implementation of this method cannot correctly handle a particular code point then it 076 * should either throw an appropriate runtime exception or return a suitable replacement 077 * character. It must never silently discard invalid input as this may constitute a security risk. 078 * 079 * @param cp the Unicode code point to escape if necessary 080 * @return the replacement characters, or {@code null} if no escaping was needed 081 */ 082 protected abstract char @Nullable [] escape(int cp); 083 084 final char @Nullable [] escapeInternal(int cp) { 085 return escape(cp); 086 } 087 088 /** 089 * Returns the escaped form of a given literal string. 090 * 091 * <p>If you are escaping input in arbitrary successive chunks, then it is not generally safe to 092 * use this method. If an input string ends with an unmatched high surrogate character, then this 093 * method will throw {@link IllegalArgumentException}. You should ensure your input is valid <a 094 * href="http://en.wikipedia.org/wiki/UTF-16">UTF-16</a> before calling this method. 095 * 096 * <p><b>Note:</b> When implementing an escaper it is a good idea to override this method for 097 * efficiency by inlining the implementation of {@link #nextEscapeIndex(CharSequence, int, int)} 098 * directly. Doing this for {@link com.google.common.net.PercentEscaper} more than doubled the 099 * performance for unescaped strings (as measured by {@code CharEscapersBenchmark}). 100 * 101 * @param string the literal string to be escaped 102 * @return the escaped form of {@code string} 103 * @throws NullPointerException if {@code string} is null 104 * @throws IllegalArgumentException if invalid surrogate characters are encountered 105 */ 106 @Override 107 public String escape(String string) { 108 checkNotNull(string); 109 int end = string.length(); 110 int index = nextEscapeIndex(string, 0, end); 111 return index == end ? string : escapeSlow(string, index); 112 } 113 114 /** 115 * Scans a sub-sequence of characters from a given {@link CharSequence}, returning the index of 116 * the next character that requires escaping. 117 * 118 * <p><b>Note:</b> When implementing an escaper, it is a good idea to override this method for 119 * efficiency. The base class implementation determines successive Unicode code points and invokes 120 * {@link #escape(int)} for each of them. If the semantics of your escaper are such that code 121 * points in the supplementary range are either all escaped or all unescaped, this method can be 122 * implemented more efficiently using {@link CharSequence#charAt(int)}. 123 * 124 * <p>Note however that if your escaper does not escape characters in the supplementary range, you 125 * should either continue to validate the correctness of any surrogate characters encountered or 126 * provide a clear warning to users that your escaper does not validate its input. 127 * 128 * <p>See {@link com.google.common.net.PercentEscaper} for an example. 129 * 130 * @param csq a sequence of characters 131 * @param start the index of the first character to be scanned 132 * @param end the index immediately after the last character to be scanned 133 * @throws IllegalArgumentException if the scanned sub-sequence of {@code csq} contains invalid 134 * surrogate pairs 135 */ 136 protected int nextEscapeIndex(CharSequence csq, int start, int end) { 137 int index = start; 138 while (index < end) { 139 int cp = codePointAt(csq, index, end); 140 if (cp < 0 || escape(cp) != null) { 141 break; 142 } 143 index += charCount(cp); 144 } 145 return index; 146 } 147 148 /** 149 * Returns the escaped form of a given literal string, starting at the given index. This method is 150 * called by the {@link #escape(String)} method when it discovers that escaping is required. It is 151 * protected to allow subclasses to override the fastpath escaping function to inline their 152 * escaping test. See {@link CharEscaperBuilder} for an example usage. 153 * 154 * <p>This method is not reentrant and may only be invoked by the top level {@link 155 * #escape(String)} method. 156 * 157 * @param s the literal string to be escaped 158 * @param index the index to start escaping from 159 * @return the escaped form of {@code string} 160 * @throws NullPointerException if {@code string} is null 161 * @throws IllegalArgumentException if invalid surrogate characters are encountered 162 */ 163 protected final String escapeSlow(String s, int index) { 164 int end = s.length(); 165 166 // Get a destination buffer and setup some loop variables. 167 char[] dest = Platform.charBufferFromThreadLocal(); 168 int destIndex = 0; 169 int unescapedChunkStart = 0; 170 171 while (index < end) { 172 int cp = codePointAt(s, index, end); 173 if (cp < 0) { 174 throw new IllegalArgumentException("Trailing high surrogate at end of input"); 175 } 176 // It is possible for this to return null because nextEscapeIndex() may 177 // (for performance reasons) yield some false positives but it must never 178 // give false negatives. 179 char[] escaped = escape(cp); 180 int nextIndex = index + charCount(cp); 181 if (escaped != null) { 182 int charsSkipped = index - unescapedChunkStart; 183 184 // This is the size needed to add the replacement, not the full 185 // size needed by the string. We only regrow when we absolutely must. 186 int sizeNeeded = destIndex + charsSkipped + escaped.length; 187 if (dest.length < sizeNeeded) { 188 int destLength = sizeNeeded + (end - index) + DEST_PAD; 189 dest = growBuffer(dest, destIndex, destLength); 190 } 191 // If we have skipped any characters, we need to copy them now. 192 if (charsSkipped > 0) { 193 s.getChars(unescapedChunkStart, index, dest, destIndex); 194 destIndex += charsSkipped; 195 } 196 if (escaped.length > 0) { 197 arraycopy(escaped, 0, dest, destIndex, escaped.length); 198 destIndex += escaped.length; 199 } 200 // If we dealt with an escaped character, reset the unescaped range. 201 unescapedChunkStart = nextIndex; 202 } 203 index = nextEscapeIndex(s, nextIndex, end); 204 } 205 206 // Process trailing unescaped characters - no need to account for escaped 207 // length or padding the allocation. 208 int charsSkipped = end - unescapedChunkStart; 209 if (charsSkipped > 0) { 210 int endIndex = destIndex + charsSkipped; 211 if (dest.length < endIndex) { 212 dest = growBuffer(dest, destIndex, endIndex); 213 } 214 s.getChars(unescapedChunkStart, end, dest, destIndex); 215 destIndex = endIndex; 216 } 217 return new String(dest, 0, destIndex); 218 } 219 220 /** 221 * Returns the Unicode code point of the character at the given index. 222 * 223 * <p>Unlike {@link Character#codePointAt(CharSequence, int)} or {@link String#codePointAt(int)} 224 * this method will never fail silently when encountering an invalid surrogate pair. 225 * 226 * <p>The behaviour of this method is as follows: 227 * 228 * <ol> 229 * <li>If {@code index >= end}, {@link IndexOutOfBoundsException} is thrown. 230 * <li><b>If the character at the specified index is not a surrogate, it is returned.</b> 231 * <li>If the first character was a high surrogate value, then an attempt is made to read the 232 * next character. 233 * <ol> 234 * <li><b>If the end of the sequence was reached, the negated value of the trailing high 235 * surrogate is returned.</b> 236 * <li><b>If the next character was a valid low surrogate, the code point value of the 237 * high/low surrogate pair is returned.</b> 238 * <li>If the next character was not a low surrogate value, then {@link 239 * IllegalArgumentException} is thrown. 240 * </ol> 241 * <li>If the first character was a low surrogate value, {@link IllegalArgumentException} is 242 * thrown. 243 * </ol> 244 * 245 * @param seq the sequence of characters from which to decode the code point 246 * @param index the index of the first character to decode 247 * @param end the index beyond the last valid character to decode 248 * @return the Unicode code point for the given index or the negated value of the trailing high 249 * surrogate character at the end of the sequence 250 */ 251 protected static int codePointAt(CharSequence seq, int index, int end) { 252 checkNotNull(seq); 253 if (index < end) { 254 char c1 = seq.charAt(index++); 255 if (c1 < Character.MIN_HIGH_SURROGATE || c1 > Character.MAX_LOW_SURROGATE) { 256 // Fast path (first test is probably all we need to do) 257 return c1; 258 } else if (c1 <= Character.MAX_HIGH_SURROGATE) { 259 // If the high surrogate was the last character, return its inverse 260 if (index == end) { 261 return -c1; 262 } 263 // Otherwise look for the low surrogate following it 264 char c2 = seq.charAt(index); 265 if (Character.isLowSurrogate(c2)) { 266 return Character.toCodePoint(c1, c2); 267 } 268 throw new IllegalArgumentException( 269 "Expected low surrogate but got char '" 270 + c2 271 + "' with value " 272 + (int) c2 273 + " at index " 274 + index 275 + " in '" 276 + seq 277 + "'"); 278 } else { 279 throw new IllegalArgumentException( 280 "Unexpected low surrogate character '" 281 + c1 282 + "' with value " 283 + (int) c1 284 + " at index " 285 + (index - 1) 286 + " in '" 287 + seq 288 + "'"); 289 } 290 } 291 throw new IndexOutOfBoundsException("Index exceeds specified range"); 292 } 293 294 static int codePointAtInternal(CharSequence seq, int index, int end) { 295 return codePointAt(seq, index, end); 296 } 297 298 /** 299 * Helper method to grow the character buffer as needed, this only happens once in a while so it's 300 * ok if it's in a method call. If the index passed in is 0 then no copying will be done. 301 */ 302 private static char[] growBuffer(char[] dest, int index, int size) { 303 if (size < 0) { // overflow - should be OutOfMemoryError but GWT/j2cl don't support it 304 throw new AssertionError("Cannot increase internal buffer any further"); 305 } 306 char[] copy = new char[size]; 307 if (index > 0) { 308 arraycopy(dest, 0, copy, 0, index); 309 } 310 return copy; 311 } 312}