Package org.owasp.encoder
Class JavaScriptEncoder
- java.lang.Object
-
- org.owasp.encoder.Encoder
-
- org.owasp.encoder.JavaScriptEncoder
-
class JavaScriptEncoder extends Encoder
JavaScriptEncoder -- An encoder for JavaScript string contexts.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
JavaScriptEncoder.Mode
Mode of operation constants for the JavaScriptEncoder.
-
Field Summary
Fields Modifier and Type Field Description private boolean
_asciiOnly
True if the output should only include ASCII characters.private boolean
_hexEncodeQuotes
True if quotation characters should be hex encoded.private JavaScriptEncoder.Mode
_mode
The mode of operations--used for toString implementation.private int[]
_validMasks
An array of 4 32-bit integers used as bitmasks to check if a character needs encoding or not.
-
Constructor Summary
Constructors Constructor Description JavaScriptEncoder(JavaScriptEncoder.Mode mode, boolean asciiOnly)
Constructs a new JavaScriptEncoder for the specified contextual mode.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.nio.charset.CoderResult
encodeArrays(java.nio.CharBuffer input, java.nio.CharBuffer output, boolean endOfInput)
The core encoding loop used when both the input and output buffers are array backed.(package private) int
firstEncodedOffset(java.lang.String input, int off, int len)
Scans the input string for the first character index that requires encoding.(package private) int
maxEncodedLength(int n)
Returns the maximum encoded length (in chars) of an input sequence ofn
characters.java.lang.String
toString()
-
Methods inherited from class org.owasp.encoder.Encoder
encode, encodeBuffers, overflow, underflow
-
-
-
-
Field Detail
-
_mode
private final JavaScriptEncoder.Mode _mode
The mode of operations--used for toString implementation.
-
_hexEncodeQuotes
private final boolean _hexEncodeQuotes
True if quotation characters should be hex encoded. Hex encoding quotes allows JavaScript to be included in XML attributes without additional XML-based encoding.
-
_validMasks
private final int[] _validMasks
An array of 4 32-bit integers used as bitmasks to check if a character needs encoding or not. If the bit is set, the character is valid and does not need encoding.
-
_asciiOnly
private final boolean _asciiOnly
True if the output should only include ASCII characters. Valid non-ASCII characters that would normally not be encoded, will be encoded.
-
-
Constructor Detail
-
JavaScriptEncoder
JavaScriptEncoder(JavaScriptEncoder.Mode mode, boolean asciiOnly)
Constructs a new JavaScriptEncoder for the specified contextual mode.- Parameters:
mode
- the mode of operationasciiOnly
- true if only ASCII characters should be included in the output (all code-points outside the ASCII range will be encoded).
-
-
Method Detail
-
maxEncodedLength
int maxEncodedLength(int n)
Description copied from class:Encoder
Returns the maximum encoded length (in chars) of an input sequence ofn
characters.- Specified by:
maxEncodedLength
in classEncoder
- Parameters:
n
- the number of characters of input- Returns:
- the worst-case number of characters required to encode
-
firstEncodedOffset
int firstEncodedOffset(java.lang.String input, int off, int len)
Description copied from class:Encoder
Scans the input string for the first character index that requires encoding. If the entire input does not require encoding then the length is returned. This method is used by the Encode.forXYZ methods to return input strings unchanged when possible.- Specified by:
firstEncodedOffset
in classEncoder
- Parameters:
input
- the input to check for encodingoff
- the offset of the first character to checklen
- the number of characters to check- Returns:
- the index of the first character to encode. The return value
will be
off+len
if no characters in the input require encoding.
-
encodeArrays
protected java.nio.charset.CoderResult encodeArrays(java.nio.CharBuffer input, java.nio.CharBuffer output, boolean endOfInput)
Description copied from class:Encoder
The core encoding loop used when both the input and output buffers are array backed. The loop is expected to fetch the arrays and interact with the arrays directly for performance.- Overrides:
encodeArrays
in classEncoder
- Parameters:
input
- the input buffer.output
- the output buffer.endOfInput
- when true, this is the last input to encode- Returns:
- UNDERFLOW or OVERFLOW
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-