Protobuf: fixed a bug in serialization of negative generic integer types (int32, int64)

This commit is contained in:
dsavvinov
2016-08-11 18:45:51 +03:00
parent 43f4f2f509
commit 039e59c0e6
3 changed files with 19 additions and 11 deletions
@@ -150,22 +150,22 @@ class CodedInputStream(val buffer: ByteArray) {
// reads varint not larger than 32-bit integer according to protobuf varint-encoding
fun readInt32NoTag(): Int {
var done: Boolean = false
var result: Int = 0
var result: Long = 0
var step: Int = 0
while (!done) {
val byte: Int = inputStream.read().toInt()
result = result or
(
(byte and WireFormat.VARINT_INFO_BITS_MASK)
shl
(WireFormat.VARINT_INFO_BITS_COUNT * step)
)
(byte and WireFormat.VARINT_INFO_BITS_MASK).toLong()
shl
(WireFormat.VARINT_INFO_BITS_COUNT * step)
).toLong()
step++
if ((byte and WireFormat.VARINT_UTIL_BIT_MASK) == 0) {
done = true
}
}
return result
return result.toInt()
}
// 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()
result = result or
(
(byte and WireFormat.VARINT_INFO_BITS_MASK).toLong()
shl
(WireFormat.VARINT_INFO_BITS_COUNT * step)
)
(byte and WireFormat.VARINT_INFO_BITS_MASK).toLong()
shl
(WireFormat.VARINT_INFO_BITS_COUNT * step)
)
step++
if ((byte and WireFormat.VARINT_UTIL_BIT_MASK) == 0 /* || byte == -1 ???? */) {
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.
*/
fun writeInt32NoTag(value: Int) {
fun writeInt32NoTag(value: Int) {
if (value < 0) {
writeInt64NoTag(value.toLong())
return
}
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
@@ -23,6 +23,9 @@ object WireFormat {
}
fun getVarint32Size(value: Int): Int {
if (value < 0) {
return getVarint64Size(value.toLong())
}
var curValue = value
var size = 0
while (curValue != 0) {