| 1 |
package com.fasterxml.jackson.dataformat.smile; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
|
| 5 |
import com.fasterxml.jackson.core.*; |
| 6 |
import com.fasterxml.jackson.core.format.InputAccessor; |
| 7 |
import com.fasterxml.jackson.core.format.MatchStrength; |
| 8 |
import com.fasterxml.jackson.core.io.IOContext; |
| 9 |
import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer; |
| 10 |
|
| 11 |
import static com.fasterxml.jackson.dataformat.smile.SmileConstants.*; |
| 12 |
|
| 13 |
/** |
| 14 |
* Simple bootstrapper version used with Smile format parser. |
| 15 |
*/ |
| 16 |
public class SmileParserBootstrapper |
| 17 |
{ |
| 18 |
/* |
| 19 |
/********************************************************** |
| 20 |
/* Configuration |
| 21 |
/********************************************************** |
| 22 |
*/ |
| 23 |
|
| 24 |
protected final IOContext _context; |
| 25 |
|
| 26 |
protected final InputStream _in; |
| 27 |
|
| 28 |
/* |
| 29 |
/********************************************************** |
| 30 |
/* Input buffering |
| 31 |
/********************************************************** |
| 32 |
*/ |
| 33 |
|
| 34 |
protected final byte[] _inputBuffer; |
| 35 |
|
| 36 |
protected int _inputPtr; |
| 37 |
|
| 38 |
protected int _inputEnd; |
| 39 |
|
| 40 |
/** |
| 41 |
* Flag that indicates whether buffer above is to be recycled |
| 42 |
* after being used or not. |
| 43 |
*/ |
| 44 |
protected final boolean _bufferRecyclable; |
| 45 |
|
| 46 |
/* |
| 47 |
/********************************************************** |
| 48 |
/* Input location |
| 49 |
/********************************************************** |
| 50 |
*/ |
| 51 |
|
| 52 |
/** |
| 53 |
* Current number of input units (bytes or chars) that were processed in |
| 54 |
* previous blocks, |
| 55 |
* before contents of current input buffer. |
| 56 |
*<p> |
| 57 |
* Note: includes possible BOMs, if those were part of the input. |
| 58 |
*/ |
| 59 |
protected int _inputProcessed; |
| 60 |
|
| 61 |
/* |
| 62 |
/********************************************************** |
| 63 |
/* Life-cycle |
| 64 |
/********************************************************** |
| 65 |
*/ |
| 66 |
|
| 67 |
public SmileParserBootstrapper(IOContext ctxt, InputStream in) |
| 68 |
{ |
| 69 |
_context = ctxt; |
| 70 |
_in = in; |
| 71 |
_inputBuffer = ctxt.allocReadIOBuffer(); |
| 72 |
_inputEnd = _inputPtr = 0; |
| 73 |
_inputProcessed = 0; |
| 74 |
_bufferRecyclable = true; |
| 75 |
} |
| 76 |
|
| 77 |
public SmileParserBootstrapper(IOContext ctxt, byte[] inputBuffer, int inputStart, int inputLen) |
| 78 |
{ |
| 79 |
_context = ctxt; |
| 80 |
_in = null; |
| 81 |
_inputBuffer = inputBuffer; |
| 82 |
_inputPtr = inputStart; |
| 83 |
_inputEnd = (inputStart + inputLen); |
| 84 |
// Need to offset this for correct location info |
| 85 |
_inputProcessed = -inputStart; |
| 86 |
_bufferRecyclable = false; |
| 87 |
} |
| 88 |
|
| 89 |
public SmileParser constructParser(int factoryFeatures, |
| 90 |
int generalParserFeatures, int smileFeatures, |
| 91 |
ObjectCodec codec, ByteQuadsCanonicalizer rootByteSymbols) |
| 92 |
throws IOException, JsonParseException |
| 93 |
{ |
| 94 |
ByteQuadsCanonicalizer can = rootByteSymbols.makeChild(factoryFeatures); |
| 95 |
// We just need a single byte, really, to know if it starts with header |
| 96 |
int end = _inputEnd; |
| 97 |
if (_inputPtr < end && _in != null) { |
| 98 |
int count = _in.read(_inputBuffer, end, _inputBuffer.length - end); |
| 99 |
if (count > 0) { |
| 100 |
_inputEnd += count; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
SmileParser p = new SmileParser(_context, generalParserFeatures, smileFeatures, |
| 105 |
codec, can, |
| 106 |
_in, _inputBuffer, _inputPtr, _inputEnd, _bufferRecyclable); |
| 107 |
boolean hadSig = false; |
| 108 |
if (_inputPtr < _inputEnd) { // only false for empty doc |
| 109 |
if (_inputBuffer[_inputPtr] == SmileConstants.HEADER_BYTE_1) { |
| 110 |
// need to ensure it gets properly handled so caller won't see the signature |
| 111 |
hadSig = p.handleSignature(true, true); |
| 112 |
} |
| 113 |
} else { |
| 114 |
/* 11-Oct-2012, tatu: Actually, let's allow empty documents even if |
| 115 |
* header signature would otherwise be needed. This is useful for |
| 116 |
* JAX-RS provider, empty PUT/POST payloads. |
| 117 |
*/ |
| 118 |
return p; |
| 119 |
} |
| 120 |
if (!hadSig && SmileParser.Feature.REQUIRE_HEADER.enabledIn(smileFeatures)) { |
| 121 |
// Ok, first, let's see if it looks like plain JSON... |
| 122 |
String msg; |
| 123 |
|
| 124 |
byte firstByte = (_inputPtr < _inputEnd) ? _inputBuffer[_inputPtr] : 0; |
| 125 |
if (firstByte == '{' || firstByte == '[') { |
| 126 |
msg = "Input does not start with Smile format header (first byte = 0x" |
| 127 |
+Integer.toHexString(firstByte & 0xFF)+") -- rather, it starts with '"+((char) firstByte) |
| 128 |
+"' (plain JSON input?) -- can not parse"; |
| 129 |
} else { |
| 130 |
msg = "Input does not start with Smile format header (first byte = 0x" |
| 131 |
+Integer.toHexString(firstByte & 0xFF)+") and parser has REQUIRE_HEADER enabled: can not parse"; |
| 132 |
} |
| 133 |
throw new JsonParseException(msg, JsonLocation.NA); |
| 134 |
} |
| 135 |
return p; |
| 136 |
} |
| 137 |
|
| 138 |
/* |
| 139 |
/********************************************************** |
| 140 |
/* Encoding detection for data format auto-detection |
| 141 |
/********************************************************** |
| 142 |
*/ |
| 143 |
|
| 144 |
public static MatchStrength hasSmileFormat(InputAccessor acc) throws IOException |
| 145 |
{ |
| 146 |
// Ok: ideally we start with the header -- if so, we are golden |
| 147 |
if (!acc.hasMoreBytes()) { |
| 148 |
return MatchStrength.INCONCLUSIVE; |
| 149 |
} |
| 150 |
// We always need at least two bytes to determine, so |
| 151 |
byte b1 = acc.nextByte(); |
| 152 |
if (!acc.hasMoreBytes()) { |
| 153 |
return MatchStrength.INCONCLUSIVE; |
| 154 |
} |
| 155 |
byte b2 = acc.nextByte(); |
| 156 |
|
| 157 |
// First: do we see 3 "magic bytes"? If so, we are golden |
| 158 |
if (b1 == SmileConstants.HEADER_BYTE_1) { // yeah, looks like marker |
| 159 |
if (b2 != SmileConstants.HEADER_BYTE_2) { |
| 160 |
return MatchStrength.NO_MATCH; |
| 161 |
} |
| 162 |
if (!acc.hasMoreBytes()) { |
| 163 |
return MatchStrength.INCONCLUSIVE; |
| 164 |
} |
| 165 |
return (acc.nextByte() == SmileConstants.HEADER_BYTE_3) ? |
| 166 |
MatchStrength.FULL_MATCH : MatchStrength.NO_MATCH; |
| 167 |
} |
| 168 |
// Otherwise: ideally either Object or Array: |
| 169 |
if (b1 == SmileConstants.TOKEN_LITERAL_START_OBJECT) { |
| 170 |
/* Object is bit easier, because now we need to get new name; i.e. can |
| 171 |
* rule out name back-refs |
| 172 |
*/ |
| 173 |
if (b2 == SmileConstants.TOKEN_KEY_LONG_STRING) { |
| 174 |
return MatchStrength.SOLID_MATCH; |
| 175 |
} |
| 176 |
int ch = (int) b2 & 0xFF; |
| 177 |
if (ch >= 0x80 && ch < 0xF8) { |
| 178 |
return MatchStrength.SOLID_MATCH; |
| 179 |
} |
| 180 |
return MatchStrength.NO_MATCH; |
| 181 |
} |
| 182 |
// Array bit trickier |
| 183 |
if (b1 == SmileConstants.TOKEN_LITERAL_START_ARRAY) { |
| 184 |
if (!acc.hasMoreBytes()) { |
| 185 |
return MatchStrength.INCONCLUSIVE; |
| 186 |
} |
| 187 |
/* For arrays, we will actually accept much wider range of values (including |
| 188 |
* things that could otherwise collide) |
| 189 |
*/ |
| 190 |
if (likelySmileValue(b2) || possibleSmileValue(b2, true)) { |
| 191 |
return MatchStrength.SOLID_MATCH; |
| 192 |
} |
| 193 |
return MatchStrength.NO_MATCH; |
| 194 |
} |
| 195 |
// Scalar values are pretty weak, albeit possible; require more certain match, consider it weak: |
| 196 |
if (likelySmileValue(b1) || possibleSmileValue(b2, false)) { |
| 197 |
return MatchStrength.SOLID_MATCH; |
| 198 |
} |
| 199 |
return MatchStrength.NO_MATCH; |
| 200 |
} |
| 201 |
|
| 202 |
private static boolean likelySmileValue(byte b) |
| 203 |
{ |
| 204 |
if ( (b == TOKEN_MISC_LONG_TEXT_ASCII) // 0xE0 |
| 205 |
|| (b == TOKEN_MISC_LONG_TEXT_UNICODE) // 0xE4 |
| 206 |
|| (b == TOKEN_MISC_BINARY_7BIT) // 0xE8 |
| 207 |
|| (b == TOKEN_LITERAL_START_ARRAY) // 0xF8 |
| 208 |
|| (b == TOKEN_LITERAL_START_OBJECT) // 0xFA |
| 209 |
) { |
| 210 |
return true; |
| 211 |
} |
| 212 |
int ch = b & 0xFF; |
| 213 |
// ASCII ctrl char range is pretty good match too |
| 214 |
if (ch >= 0x80 && ch <= 0x9F) { |
| 215 |
return true; |
| 216 |
} |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* @param lenient Whether to consider more speculative matches or not |
| 222 |
* (typically true when there is context like start-array) |
| 223 |
*/ |
| 224 |
private static boolean possibleSmileValue(byte b, boolean lenient) |
| 225 |
{ |
| 226 |
int ch = (int) b & 0xFF; |
| 227 |
// note: we know that likely matches have been handled already, so... |
| 228 |
if (ch >= 0x80) { |
| 229 |
return (ch <= 0xE0); |
| 230 |
} |
| 231 |
if (lenient) { |
| 232 |
if (ch >= 0x40) { // tiny/short ASCII |
| 233 |
return true; |
| 234 |
} |
| 235 |
if (ch >- 0x20) { // various constants |
| 236 |
return (ch < 0x2C); // many reserved bytes that can't be seen |
| 237 |
} |
| 238 |
} |
| 239 |
return false; |
| 240 |
} |
| 241 |
} |