-
Notifications
You must be signed in to change notification settings - Fork 613
/
ProtobufReader.kt
204 lines (171 loc) · 6.34 KB
/
ProtobufReader.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:OptIn(ExperimentalSerializationApi::class)
package kotlinx.serialization.protobuf.internal
import kotlinx.serialization.*
import kotlinx.serialization.protobuf.*
import kotlin.jvm.*
internal class ProtobufReader(private val input: ByteArrayInput) {
@JvmField
public var currentId = -1
@JvmField
public var currentType = -1
private var pushBack = false
private var pushBackHeader = 0
public val eof
get() = !pushBack && input.availableBytes == 0
public fun readTag(): Int {
if (pushBack) {
pushBack = false
val previousHeader = (currentId shl 3) or currentType
return updateIdAndType(pushBackHeader).also {
pushBackHeader = previousHeader
}
}
// Header to use when pushed back is the old id/type
pushBackHeader = (currentId shl 3) or currentType
val header = input.readVarint64(true).toInt()
return updateIdAndType(header)
}
private fun updateIdAndType(header: Int): Int {
return if (header == -1) {
currentId = -1
currentType = -1
-1
} else {
currentId = header ushr 3
currentType = header and 0b111
currentId
}
}
public fun pushBackTag() {
pushBack = true
val nextHeader = (currentId shl 3) or currentType
updateIdAndType(pushBackHeader)
pushBackHeader = nextHeader
}
fun skipElement() {
when (currentType) {
VARINT -> readInt(ProtoIntegerType.DEFAULT)
i64 -> readLong(ProtoIntegerType.FIXED)
SIZE_DELIMITED -> readByteArray()
i32 -> readInt(ProtoIntegerType.FIXED)
else -> throw ProtobufDecodingException("Unsupported start group or end group wire type: $currentType")
}
}
@Suppress("NOTHING_TO_INLINE")
private inline fun assertWireType(expected: Int) {
if (currentType != expected) throw ProtobufDecodingException("Expected wire type $expected, but found $currentType")
}
fun readByteArray(): ByteArray {
assertWireType(SIZE_DELIMITED)
return readByteArrayNoTag()
}
fun readByteArrayNoTag(): ByteArray {
val length = decode32()
checkLength(length)
return input.readExactNBytes(length)
}
fun objectInput(): ByteArrayInput {
assertWireType(SIZE_DELIMITED)
return objectTaglessInput()
}
fun objectTaglessInput(): ByteArrayInput {
val length = decode32()
checkLength(length)
return input.slice(length)
}
fun readInt(format: ProtoIntegerType): Int {
val wireType = if (format == ProtoIntegerType.FIXED) i32 else VARINT
assertWireType(wireType)
return decode32(format)
}
fun readInt32NoTag(): Int = decode32()
fun readLong(format: ProtoIntegerType): Long {
val wireType = if (format == ProtoIntegerType.FIXED) i64 else VARINT
assertWireType(wireType)
return decode64(format)
}
fun readLongNoTag(): Long = decode64(ProtoIntegerType.DEFAULT)
fun readFloat(): Float {
assertWireType(i32)
return Float.fromBits(readIntLittleEndian())
}
fun readFloatNoTag(): Float = Float.fromBits(readIntLittleEndian())
private fun readIntLittleEndian(): Int {
// TODO this could be optimized by extracting method to the IS
var result = 0
for (i in 0..3) {
val byte = input.read() and 0x000000FF
result = result or (byte shl (i * 8))
}
return result
}
private fun readLongLittleEndian(): Long {
// TODO this could be optimized by extracting method to the IS
var result = 0L
for (i in 0..7) {
val byte = (input.read() and 0x000000FF).toLong()
result = result or (byte shl (i * 8))
}
return result
}
fun readDouble(): Double {
assertWireType(i64)
return Double.fromBits(readLongLittleEndian())
}
fun readDoubleNoTag(): Double {
return Double.fromBits(readLongLittleEndian())
}
fun readString(): String {
assertWireType(SIZE_DELIMITED)
val length = decode32()
checkLength(length)
return input.readString(length)
}
fun readStringNoTag(): String {
val length = decode32()
checkLength(length)
return input.readString(length)
}
private fun checkLength(length: Int) {
if (length < 0) {
throw ProtobufDecodingException("Unexpected negative length: $length")
}
}
private fun decode32(format: ProtoIntegerType = ProtoIntegerType.DEFAULT): Int = when (format) {
ProtoIntegerType.DEFAULT -> input.readVarint64(false).toInt()
ProtoIntegerType.SIGNED -> decodeSignedVarintInt(
input
)
ProtoIntegerType.FIXED -> readIntLittleEndian()
}
private fun decode64(format: ProtoIntegerType = ProtoIntegerType.DEFAULT): Long = when (format) {
ProtoIntegerType.DEFAULT -> input.readVarint64(false)
ProtoIntegerType.SIGNED -> decodeSignedVarintLong(
input
)
ProtoIntegerType.FIXED -> readLongLittleEndian()
}
/**
* Source for all varint operations:
* https://github.com/addthis/stream-lib/blob/master/src/main/java/com/clearspring/analytics/util/Varint.java
*/
private fun decodeSignedVarintInt(input: ByteArrayInput): Int {
val raw = input.readVarint32()
val temp = raw shl 31 shr 31 xor raw shr 1
// This extra step lets us deal with the largest signed values by treating
// negative results from read unsigned methods as like unsigned values.
// Must re-flip the top bit if the original read value had it set.
return temp xor (raw and (1 shl 31))
}
private fun decodeSignedVarintLong(input: ByteArrayInput): Long {
val raw = input.readVarint64(false)
val temp = raw shl 63 shr 63 xor raw shr 1
// This extra step lets us deal with the largest signed values by treating
// negative results from read unsigned methods as like unsigned values
// Must re-flip the top bit if the original read value had it set.
return temp xor (raw and (1L shl 63))
}
}