|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V., and/or licensed to Elasticsearch B.V. |
| 3 | + * under one or more license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + * This file is based on a modification of https://github.com/FasterXML/jackson-dataformats-binary which is licensed under the Apache 2.0 License. |
| 20 | + */ |
| 21 | + |
| 22 | +package org.elasticsearch.xcontent.provider.cbor; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.core.JsonToken; |
| 25 | +import com.fasterxml.jackson.core.ObjectCodec; |
| 26 | +import com.fasterxml.jackson.core.io.IOContext; |
| 27 | +import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer; |
| 28 | +import com.fasterxml.jackson.dataformat.cbor.CBORConstants; |
| 29 | +import com.fasterxml.jackson.dataformat.cbor.CBORParser; |
| 30 | + |
| 31 | +import org.elasticsearch.xcontent.Text; |
| 32 | +import org.elasticsearch.xcontent.XContentString; |
| 33 | +import org.elasticsearch.xcontent.provider.OptimizedTextCapable; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.InputStream; |
| 37 | +import java.util.Locale; |
| 38 | + |
| 39 | +/** |
| 40 | + * Contains code adapted from {@link CBORParser} licensed under the Apache License 2.0. |
| 41 | + */ |
| 42 | +public class ESCborParser extends CBORParser implements OptimizedTextCapable { |
| 43 | + public ESCborParser( |
| 44 | + IOContext ctxt, |
| 45 | + int parserFeatures, |
| 46 | + int cborFeatures, |
| 47 | + ObjectCodec codec, |
| 48 | + ByteQuadsCanonicalizer sym, |
| 49 | + InputStream in, |
| 50 | + byte[] inputBuffer, |
| 51 | + int start, |
| 52 | + int end, |
| 53 | + boolean bufferRecyclable |
| 54 | + ) { |
| 55 | + super(ctxt, parserFeatures, cborFeatures, codec, sym, in, inputBuffer, start, end, bufferRecyclable); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Text getValueAsText() throws IOException { |
| 60 | + JsonToken t = _currToken; |
| 61 | + if (_tokenIncomplete) { |
| 62 | + if (t == JsonToken.VALUE_STRING) { |
| 63 | + return _finishAndReturnText(_typeByte); |
| 64 | + } |
| 65 | + } |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + private Text _finishAndReturnText(int ch) throws IOException { |
| 70 | + final int type = ((ch >> 5) & 0x7); |
| 71 | + ch &= 0x1F; |
| 72 | + |
| 73 | + // sanity check |
| 74 | + if (type != CBORConstants.MAJOR_TYPE_TEXT) { |
| 75 | + // should never happen so |
| 76 | + _throwInternal(); |
| 77 | + } |
| 78 | + int previousPointer = _inputPtr; |
| 79 | + |
| 80 | + // String value, decode |
| 81 | + final int len = _decodeExplicitLength(ch); |
| 82 | + if (len == 0) { |
| 83 | + return new Text(new XContentString.UTF8Bytes(new byte[0], 0, 0), 0); |
| 84 | + } |
| 85 | + if (len < 0) { |
| 86 | + // optimized text is not supported for chunked strings |
| 87 | + return null; |
| 88 | + } |
| 89 | + final int available = _inputEnd - _inputPtr; |
| 90 | + if (available >= len) { |
| 91 | + Text text = new Text(new XContentString.UTF8Bytes(_inputBuffer, _inputPtr, len)); |
| 92 | + _inputPtr = previousPointer; |
| 93 | + return text; |
| 94 | + } |
| 95 | + // this is expected to be used in the context where the input stream is not available |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Method used to decode explicit length of a variable-length value |
| 101 | + * (or, for indefinite/chunked, indicate that one is not known). |
| 102 | + * Note that long (64-bit) length is only allowed if it fits in |
| 103 | + * 32-bit signed int, for now; expectation being that longer values |
| 104 | + * are always encoded as chunks. |
| 105 | + */ |
| 106 | + private int _decodeExplicitLength(int lowBits) throws IOException { |
| 107 | + // common case, indefinite length; relies on marker |
| 108 | + if (lowBits == 31) { |
| 109 | + return -1; |
| 110 | + } |
| 111 | + if (lowBits <= 23) { |
| 112 | + return lowBits; |
| 113 | + } |
| 114 | + switch (lowBits - 24) { |
| 115 | + case 0: |
| 116 | + return _decode8Bits(); |
| 117 | + case 1: |
| 118 | + return _decode16Bits(); |
| 119 | + case 2: |
| 120 | + return _decode32Bits(); |
| 121 | + case 3: |
| 122 | + long l = _decode64Bits(); |
| 123 | + if (l < 0 || l > MAX_INT_L) { |
| 124 | + throw _constructError("Illegal length for " + currentToken() + ": " + l); |
| 125 | + } |
| 126 | + return (int) l; |
| 127 | + } |
| 128 | + throw _constructError( |
| 129 | + String.format( |
| 130 | + Locale.ROOT, |
| 131 | + "Invalid 5-bit length indicator for `JsonToken.%s`: 0x%02X; only 0x00-0x17, 0x1F allowed", |
| 132 | + currentToken(), |
| 133 | + lowBits |
| 134 | + ) |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + private int _decode8Bits() throws IOException { |
| 139 | + if (_inputPtr >= _inputEnd) { |
| 140 | + loadMoreGuaranteed(); |
| 141 | + } |
| 142 | + return _inputBuffer[_inputPtr++] & 0xFF; |
| 143 | + } |
| 144 | + |
| 145 | + private int _decode16Bits() throws IOException { |
| 146 | + int ptr = _inputPtr; |
| 147 | + if ((ptr + 1) >= _inputEnd) { |
| 148 | + return _slow16(); |
| 149 | + } |
| 150 | + final byte[] b = _inputBuffer; |
| 151 | + int v = ((b[ptr] & 0xFF) << 8) + (b[ptr + 1] & 0xFF); |
| 152 | + _inputPtr = ptr + 2; |
| 153 | + return v; |
| 154 | + } |
| 155 | + |
| 156 | + private int _slow16() throws IOException { |
| 157 | + if (_inputPtr >= _inputEnd) { |
| 158 | + loadMoreGuaranteed(); |
| 159 | + } |
| 160 | + int v = (_inputBuffer[_inputPtr++] & 0xFF); |
| 161 | + if (_inputPtr >= _inputEnd) { |
| 162 | + loadMoreGuaranteed(); |
| 163 | + } |
| 164 | + return (v << 8) + (_inputBuffer[_inputPtr++] & 0xFF); |
| 165 | + } |
| 166 | + |
| 167 | + private int _decode32Bits() throws IOException { |
| 168 | + int ptr = _inputPtr; |
| 169 | + if ((ptr + 3) >= _inputEnd) { |
| 170 | + return _slow32(); |
| 171 | + } |
| 172 | + final byte[] b = _inputBuffer; |
| 173 | + int v = (b[ptr++] << 24) + ((b[ptr++] & 0xFF) << 16) + ((b[ptr++] & 0xFF) << 8) + (b[ptr++] & 0xFF); |
| 174 | + _inputPtr = ptr; |
| 175 | + return v; |
| 176 | + } |
| 177 | + |
| 178 | + private int _slow32() throws IOException { |
| 179 | + if (_inputPtr >= _inputEnd) { |
| 180 | + loadMoreGuaranteed(); |
| 181 | + } |
| 182 | + int v = _inputBuffer[_inputPtr++]; // sign will disappear anyway |
| 183 | + if (_inputPtr >= _inputEnd) { |
| 184 | + loadMoreGuaranteed(); |
| 185 | + } |
| 186 | + v = (v << 8) + (_inputBuffer[_inputPtr++] & 0xFF); |
| 187 | + if (_inputPtr >= _inputEnd) { |
| 188 | + loadMoreGuaranteed(); |
| 189 | + } |
| 190 | + v = (v << 8) + (_inputBuffer[_inputPtr++] & 0xFF); |
| 191 | + if (_inputPtr >= _inputEnd) { |
| 192 | + loadMoreGuaranteed(); |
| 193 | + } |
| 194 | + return (v << 8) + (_inputBuffer[_inputPtr++] & 0xFF); |
| 195 | + } |
| 196 | + |
| 197 | + private long _decode64Bits() throws IOException { |
| 198 | + int ptr = _inputPtr; |
| 199 | + if ((ptr + 7) >= _inputEnd) { |
| 200 | + return _slow64(); |
| 201 | + } |
| 202 | + final byte[] b = _inputBuffer; |
| 203 | + int i1 = (b[ptr++] << 24) + ((b[ptr++] & 0xFF) << 16) + ((b[ptr++] & 0xFF) << 8) + (b[ptr++] & 0xFF); |
| 204 | + int i2 = (b[ptr++] << 24) + ((b[ptr++] & 0xFF) << 16) + ((b[ptr++] & 0xFF) << 8) + (b[ptr++] & 0xFF); |
| 205 | + _inputPtr = ptr; |
| 206 | + return _long(i1, i2); |
| 207 | + } |
| 208 | + |
| 209 | + private long _slow64() throws IOException { |
| 210 | + return _long(_decode32Bits(), _decode32Bits()); |
| 211 | + } |
| 212 | + |
| 213 | + private static long _long(int i1, int i2) { |
| 214 | + long l1 = i1; |
| 215 | + long l2 = i2; |
| 216 | + l2 = (l2 << 32) >>> 32; |
| 217 | + return (l1 << 32) + l2; |
| 218 | + } |
| 219 | +} |
0 commit comments