| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"math/bits" |
| 6 |
"unsafe" |
| 7 |
|
| 8 |
"github.com/goccy/go-json/internal/errors" |
| 9 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
lsb = 0x0101010101010101 |
| 13 |
msb = 0x8080808080808080 |
| 14 |
// bit5 is the bit which distinguishes '{' from '[' and '}' from ']'. |
| 15 |
bit5 = 0x2020202020202020 |
| 16 |
|
| 17 |
// scanBlockSize is the number of the bytes the masks of a scan cover. |
| 18 |
scanBlockSize = 64 |
| 19 |
// shortScanLength is the number of the bytes a scan looks at one by one before it works by blocks. |
| 20 |
shortScanLength = 16 |
| 21 |
) |
| 22 |
|
| 23 |
// NewInput returns the buffer which the decoders decode: a copy of the input followed by a nul byte, |
| 24 |
// which ends every scan. |
| 25 |
func NewInput(data []byte) []byte { |
| 26 |
buf := make([]byte, len(data)+1) |
| 27 |
copy(buf, data) |
| 28 |
return buf |
| 29 |
} |
| 30 |
|
| 31 |
// load64 returns the eight bytes at pos as a little-endian word, whatever the byte order of the machine: |
| 32 |
// the masks made from a word give the position of a byte by the count of the trailing zeros. |
| 33 |
// It compiles to a single load on a little-endian machine. |
| 34 |
func load64(buf []byte, pos int64) uint64 { |
| 35 |
return binary.LittleEndian.Uint64((*[8]byte)(unsafe.Add((*sliceHeader)(unsafe.Pointer(&buf)).data, pos))[:]) |
| 36 |
} |
| 37 |
|
| 38 |
// byteMask returns the word whose byte is 0x80 where the byte of w is c, and 0 elsewhere. |
| 39 |
func byteMask(w, c uint64) uint64 { |
| 40 |
x := w ^ (c * lsb) |
| 41 |
return ^(((x & ^uint64(msb)) + ^uint64(msb)) | x) & msb |
| 42 |
} |
| 43 |
|
| 44 |
// scanMasks are the masks of a block of scanBlockSize bytes: bit i of a mask is set if the byte i |
| 45 |
// of the block is the character. The brackets are folded: open is '{' or '[' and closing is '}' or ']'. |
| 46 |
type scanMasks struct { |
| 47 |
quote uint64 |
| 48 |
backslash uint64 |
| 49 |
open uint64 |
| 50 |
closing uint64 |
| 51 |
} |
| 52 |
|
| 53 |
// scanBlockWords computes the masks of the block at p by words, eight bytes at a time. |
| 54 |
// It is what the architectures without a SIMD scan use. |
| 55 |
func scanBlockWords(p unsafe.Pointer, m *scanMasks) { |
| 56 |
var quote, backslash, open, closing uint64 |
| 57 |
for i := 0; i < scanBlockSize/8; i++ { |
| 58 |
w := binary.LittleEndian.Uint64((*[8]byte)(unsafe.Add(p, i*8))[:]) |
| 59 |
folded := w &^ bit5 |
| 60 |
shift := uint(i * 8) |
| 61 |
quote |= gatherMask(byteMask(w, '"')) << shift |
| 62 |
backslash |= gatherMask(byteMask(w, '\\')) << shift |
| 63 |
open |= gatherMask(byteMask(folded, '[')) << shift |
| 64 |
closing |= gatherMask(byteMask(folded, ']')) << shift |
| 65 |
} |
| 66 |
m.quote, m.backslash, m.open, m.closing = quote, backslash, open, closing |
| 67 |
} |
| 68 |
|
| 69 |
// gatherMask turns a mask of 0x80 bytes into the byte whose bit i is the byte i of the mask. |
| 70 |
func gatherMask(m uint64) uint64 { |
| 71 |
return ((m >> 7) * 0x0102040810204080) >> 56 |
| 72 |
} |
| 73 |
|
| 74 |
// compoundScanner finds the end of an object or an array without decoding it: the position after |
| 75 |
// the bracket which closes it. It keeps its state between the calls of scan, so that a stream may |
| 76 |
// scan what it has read so far and go on after reading more. |
| 77 |
// |
| 78 |
// The bytes are scanned a block at a time, by their masks ( scanMasks ): the quotes which are not |
| 79 |
// escaped toggle whether the bytes are inside a string, which a prefix xor of the quote mask tells |
| 80 |
// for every byte at once, and the brackets outside strings count the depth. The bytes are looked at |
| 81 |
// one by one only in a block where the value may end or the nesting may get too deep. |
| 82 |
type compoundScanner struct { |
| 83 |
// depth is the number of the brackets opened but not closed. |
| 84 |
depth int64 |
| 85 |
// maxDepth is the depth beyond which the nesting is too deep. |
| 86 |
maxDepth int64 |
| 87 |
inString bool |
| 88 |
// escaped is whether the next byte is escaped by the backslash before it. |
| 89 |
escaped bool |
| 90 |
} |
| 91 |
|
| 92 |
// scan scans buf[pos:lim] and returns the position after the bracket which closes the value, or, |
| 93 |
// when the value doesn't end before lim, the position it stopped at and false. |
| 94 |
// A whole block is read at once where the capacity of the buffer has room for it. |
| 95 |
func (sc *compoundScanner) scan(buf []byte, pos, lim int64) (int64, bool, error) { |
| 96 |
const evenBits = 0x5555555555555555 |
| 97 |
// A short value ends within a few bytes, which are looked at one by one: that costs less |
| 98 |
// than the masks of a whole block. |
| 99 |
pos, found, err := sc.scanBytes(buf, pos, min(pos+shortScanLength, lim)) |
| 100 |
if found || err != nil { |
| 101 |
return pos, found, err |
| 102 |
} |
| 103 |
depth := sc.depth |
| 104 |
var inString, prevEscaped uint64 |
| 105 |
if sc.inString { |
| 106 |
inString = ^uint64(0) |
| 107 |
} |
| 108 |
if sc.escaped { |
| 109 |
prevEscaped = 1 |
| 110 |
} |
| 111 |
base := (*sliceHeader)(unsafe.Pointer(&buf)).data |
| 112 |
readable := int64(cap(buf)) |
| 113 |
for pos < lim { |
| 114 |
if pos+scanBlockSize > readable { |
| 115 |
// The buffer has no room for a block: its last bytes are looked at one by one. |
| 116 |
sc.depth, sc.inString, sc.escaped = depth, inString != 0, prevEscaped != 0 |
| 117 |
return sc.scanBytes(buf, pos, lim) |
| 118 |
} |
| 119 |
var m scanMasks |
| 120 |
scanBlock(unsafe.Add(base, pos), &m) |
| 121 |
n := lim - pos |
| 122 |
if n < scanBlockSize { |
| 123 |
valid := uint64(1)<<uint(n) - 1 |
| 124 |
m.quote &= valid |
| 125 |
m.backslash &= valid |
| 126 |
m.open &= valid |
| 127 |
m.closing &= valid |
| 128 |
} |
| 129 |
// escaped has the bytes which follow an odd number of backslashes. A backslash which is |
| 130 |
// itself escaped doesn't escape; a run of backslashes may go on in the next block ( the carry ). |
| 131 |
backslash := m.backslash &^ prevEscaped |
| 132 |
followsEscape := backslash<<1 | prevEscaped |
| 133 |
oddStarts := backslash &^ evenBits &^ followsEscape |
| 134 |
evenSequences, carry := bits.Add64(oddStarts, backslash, 0) |
| 135 |
escaped := (evenBits ^ (evenSequences << 1)) & followsEscape |
| 136 |
if n < scanBlockSize { |
| 137 |
// whether the byte at lim, which the next call scans first, is escaped |
| 138 |
prevEscaped = (escaped >> uint(n)) & 1 |
| 139 |
} else { |
| 140 |
prevEscaped = carry |
| 141 |
} |
| 142 |
// inside has the bytes which are inside a string after the byte itself is read: |
| 143 |
// the parity of the quotes up to the byte, flipped if the block begins inside a string. |
| 144 |
quote := m.quote &^ escaped |
| 145 |
inside := quote |
| 146 |
inside ^= inside << 1 |
| 147 |
inside ^= inside << 2 |
| 148 |
inside ^= inside << 4 |
| 149 |
inside ^= inside << 8 |
| 150 |
inside ^= inside << 16 |
| 151 |
inside ^= inside << 32 |
| 152 |
inside ^= inString |
| 153 |
inString = uint64(int64(inside) >> 63) // the state after the last byte, in every bit |
| 154 |
// An escaped byte is neither a quote nor a bracket, wherever it is. |
| 155 |
outside := ^(inside | escaped) |
| 156 |
open := m.open & outside |
| 157 |
closing := m.closing & outside |
| 158 |
opens := int64(bits.OnesCount64(open)) |
| 159 |
closes := int64(bits.OnesCount64(closing)) |
| 160 |
if closes < depth && depth+opens <= sc.maxDepth { |
| 161 |
// the value can't end in this block, nor go too deep |
| 162 |
depth += opens - closes |
| 163 |
pos += scanBlockSize |
| 164 |
continue |
| 165 |
} |
| 166 |
for brackets := open | closing; brackets != 0; brackets &= brackets - 1 { |
| 167 |
i := bits.TrailingZeros64(brackets) |
| 168 |
if open&(1<<uint(i)) != 0 { |
| 169 |
depth++ |
| 170 |
if depth > sc.maxDepth { |
| 171 |
return 0, false, errors.ErrExceededMaxDepth(buf[pos+int64(i)], pos+int64(i)) |
| 172 |
} |
| 173 |
} else { |
| 174 |
depth-- |
| 175 |
if depth == 0 { |
| 176 |
return pos + int64(i) + 1, true, nil |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
pos += scanBlockSize |
| 181 |
} |
| 182 |
sc.depth, sc.inString, sc.escaped = depth, inString != 0, prevEscaped != 0 |
| 183 |
return lim, false, nil |
| 184 |
} |
| 185 |
|
| 186 |
// scanBytes scans buf[pos:end] one byte at a time, as scan does by blocks. |
| 187 |
// A byte after a backslash is neither a quote nor a bracket, wherever it is. |
| 188 |
func (sc *compoundScanner) scanBytes(buf []byte, pos, end int64) (int64, bool, error) { |
| 189 |
if pos >= end { |
| 190 |
return pos, false, nil |
| 191 |
} |
| 192 |
depth, inString := sc.depth, sc.inString |
| 193 |
if sc.escaped { |
| 194 |
sc.escaped = false |
| 195 |
pos++ |
| 196 |
} |
| 197 |
for pos < end { |
| 198 |
if inString { |
| 199 |
for pos < end { |
| 200 |
c := buf[pos] |
| 201 |
pos++ |
| 202 |
if c == '"' { |
| 203 |
inString = false |
| 204 |
break |
| 205 |
} |
| 206 |
if c == '\\' { |
| 207 |
pos++ |
| 208 |
} |
| 209 |
} |
| 210 |
continue |
| 211 |
} |
| 212 |
c := buf[pos] |
| 213 |
pos++ |
| 214 |
switch c { |
| 215 |
case '"': |
| 216 |
inString = true |
| 217 |
case '\\': |
| 218 |
pos++ |
| 219 |
case '{', '[': |
| 220 |
depth++ |
| 221 |
if depth > sc.maxDepth { |
| 222 |
return 0, false, errors.ErrExceededMaxDepth(c, pos-1) |
| 223 |
} |
| 224 |
case '}', ']': |
| 225 |
depth-- |
| 226 |
if depth == 0 { |
| 227 |
return pos, true, nil |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
// A backslash as the last byte escapes the byte after end. |
| 232 |
escaped := pos > end |
| 233 |
if escaped { |
| 234 |
pos = end |
| 235 |
} |
| 236 |
sc.depth, sc.inString, sc.escaped = depth, inString, escaped |
| 237 |
return pos, false, nil |
| 238 |
} |