Protobuf: fixed a bug in serialization of negative generic integer types (int32, int64)
This commit is contained in:
@@ -150,22 +150,22 @@ class CodedInputStream(val buffer: ByteArray) {
|
|||||||
// reads varint not larger than 32-bit integer according to protobuf varint-encoding
|
// reads varint not larger than 32-bit integer according to protobuf varint-encoding
|
||||||
fun readInt32NoTag(): Int {
|
fun readInt32NoTag(): Int {
|
||||||
var done: Boolean = false
|
var done: Boolean = false
|
||||||
var result: Int = 0
|
var result: Long = 0
|
||||||
var step: Int = 0
|
var step: Int = 0
|
||||||
while (!done) {
|
while (!done) {
|
||||||
val byte: Int = inputStream.read().toInt()
|
val byte: Int = inputStream.read().toInt()
|
||||||
result = result or
|
result = result or
|
||||||
(
|
(
|
||||||
(byte and WireFormat.VARINT_INFO_BITS_MASK)
|
(byte and WireFormat.VARINT_INFO_BITS_MASK).toLong()
|
||||||
shl
|
shl
|
||||||
(WireFormat.VARINT_INFO_BITS_COUNT * step)
|
(WireFormat.VARINT_INFO_BITS_COUNT * step)
|
||||||
)
|
).toLong()
|
||||||
step++
|
step++
|
||||||
if ((byte and WireFormat.VARINT_UTIL_BIT_MASK) == 0) {
|
if ((byte and WireFormat.VARINT_UTIL_BIT_MASK) == 0) {
|
||||||
done = true
|
done = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result.toInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
// reads varint not larger than 64-bit integer according to protobuf varint-encoding
|
// reads varint not larger than 64-bit integer according to protobuf varint-encoding
|
||||||
@@ -177,10 +177,10 @@ class CodedInputStream(val buffer: ByteArray) {
|
|||||||
val byte: Int = inputStream.read().toInt()
|
val byte: Int = inputStream.read().toInt()
|
||||||
result = result or
|
result = result or
|
||||||
(
|
(
|
||||||
(byte and WireFormat.VARINT_INFO_BITS_MASK).toLong()
|
(byte and WireFormat.VARINT_INFO_BITS_MASK).toLong()
|
||||||
shl
|
shl
|
||||||
(WireFormat.VARINT_INFO_BITS_COUNT * step)
|
(WireFormat.VARINT_INFO_BITS_COUNT * step)
|
||||||
)
|
)
|
||||||
step++
|
step++
|
||||||
if ((byte and WireFormat.VARINT_UTIL_BIT_MASK) == 0 /* || byte == -1 ???? */) {
|
if ((byte and WireFormat.VARINT_UTIL_BIT_MASK) == 0 /* || byte == -1 ???? */) {
|
||||||
done = true
|
done = true
|
||||||
|
|||||||
@@ -99,7 +99,12 @@ class CodedOutputStream(val buffer: ByteArray) {
|
|||||||
* Then she/he can re-use low-level methods for operating with raw values, that are not annotated with Protobuf tags.
|
* Then she/he can re-use low-level methods for operating with raw values, that are not annotated with Protobuf tags.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fun writeInt32NoTag(value: Int) {
|
fun writeInt32NoTag(value: Int) {
|
||||||
|
if (value < 0) {
|
||||||
|
writeInt64NoTag(value.toLong())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var curValue: Int = value
|
var curValue: Int = value
|
||||||
|
|
||||||
// we have at most 32 information bits. With overhead of 1 bit per 7 bits we need at most 5 bytes for encoding
|
// we have at most 32 information bits. With overhead of 1 bit per 7 bits we need at most 5 bytes for encoding
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ object WireFormat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getVarint32Size(value: Int): Int {
|
fun getVarint32Size(value: Int): Int {
|
||||||
|
if (value < 0) {
|
||||||
|
return getVarint64Size(value.toLong())
|
||||||
|
}
|
||||||
var curValue = value
|
var curValue = value
|
||||||
var size = 0
|
var size = 0
|
||||||
while (curValue != 0) {
|
while (curValue != 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user