Protobuf: fixed bugs in getVarint32Size/getVarint64Size. Fixed bug in serialization of packed fields

This commit is contained in:
dsavvinov
2016-08-08 14:24:07 +03:00
parent 0a1787b033
commit c662d03699
8 changed files with 16 additions and 204 deletions
@@ -26,20 +26,11 @@ clean:
rm -rf $(BINDIR)
generate:
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/addressbook.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/nested-msg.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/connect.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/error.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/direction.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/location.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/nested-msg.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/routeDone.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/route_done.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/route.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/cross-branch-access.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/base.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/extended.proto
./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/protoc-artifacts ./test/rc_session.proto
debug:
gdb --args ./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/src ./test/addressbook.proto
gdb --args ./protoc --kotlin_out=$$HOME/Downloads/carkot/proto/compiler/src ./test/nested-msg.proto
.PHONY: clean all generate debug
.PHONY: clean all generate
@@ -292,7 +292,7 @@ void FieldGenerator::generateSerializationForPrimitives(io::Printer * printer, b
}
else {
if (noTag) {
printer->Print(vars, "output.write$suffix$NoTag ()\n");
printer->Print(vars, "output.write$suffix$NoTag ($fieldName$)\n");
}
else {
printer->Print(vars, "output.write$suffix$ ($fieldNumber$, $fieldName$)\n");
@@ -5,7 +5,7 @@ option java_package = "proto.car";
option java_outer_classname = "Connect";
message ConnectionRequest {
int32 ip = 1;
repeated int32 ip = 1;
int32 port = 2;
}
+3 -90
View File
@@ -99,71 +99,6 @@ class CodedInputStream(buffer: ByteArray) {
return readZigZag64NoTag()
}
fun readFixed32(expectedFieldNumber: Int): Int {
val tag = readTag(expectedFieldNumber, WireType.FIX_32)
return readFixed32NoInt()
}
fun readFixed32NoInt(): Int {
return readLittleEndianInt()
}
fun readSFixed32(expectedFieldNumber: Int): Int {
val tag = readTag(expectedFieldNumber, WireType.FIX_32)
return readSFixed32NoTag()
}
fun readSFixed32NoTag(): Int {
return readLittleEndianInt()
}
fun readFixed64(expectedFieldNumber: Int): Long {
val tag = readTag(expectedFieldNumber, WireType.FIX_64)
return readFixed64NoTag()
}
fun readFixed64NoTag(): Long {
return readLittleEndianLong()
}
fun readSFixed64(expectedFieldNumber: Int): Long {
val tag = readTag(expectedFieldNumber, WireType.FIX_64)
return readSFixed64NoTag()
}
fun readSFixed64NoTag(): Long {
return readLittleEndianLong()
}
fun readDouble(expectedFieldNumber: Int): Double {
val tag = readTag(expectedFieldNumber, WireType.FIX_64)
return readDoubleNoTag()
}
fun readDoubleNoTag(): Double {
return readLittleEndianDouble()
}
fun readFloat(expectedFieldNumber: Int): Float {
val tag = readTag(expectedFieldNumber, WireType.FIX_32)
return readFloatNoTag()
}
fun readFloatNoTag(): Float {
return readLittleEndianFloat()
}
fun readString(expectedFieldNumber: Int): String {
val tag = readTag(expectedFieldNumber, WireType.LENGTH_DELIMITED)
return readStringNoTag()
}
fun readStringNoTag(): String {
val length = readInt32NoTag()
val value = String(readRawBytes(length))
return value
}
fun readBytes(expectedFieldNumber: Int): ByteArray {
val tag = readTag(expectedFieldNumber, WireType.LENGTH_DELIMITED)
return readBytesNoTag()
@@ -200,34 +135,12 @@ class CodedInputStream(buffer: ByteArray) {
}
}
fun readLittleEndianDouble(): Double {
val byteBuffer = ByteBuffer.wrap(readRawBytes(8))
byteBuffer.order(ByteOrder.LITTLE_ENDIAN)
return byteBuffer.getDouble(0)
}
fun readLittleEndianFloat(): Float {
val byteBuffer = ByteBuffer.wrap(readRawBytes(4))
byteBuffer.order(ByteOrder.LITTLE_ENDIAN)
return byteBuffer.getFloat(0)
}
fun readLittleEndianInt(): Int {
val byteBuffer = ByteBuffer.wrap(readRawBytes(8))
byteBuffer.order(ByteOrder.LITTLE_ENDIAN)
return byteBuffer.getInt(0)
}
fun readLittleEndianLong(): Long {
val byteBuffer = ByteBuffer.wrap(readRawBytes(4))
byteBuffer.order(ByteOrder.LITTLE_ENDIAN)
return byteBuffer.getLong(0)
}
fun readRawBytes(count: Int): ByteArray {
val ba = ByteArray(count)
for (i in 0..(count - 1)) {
var i = 0
while (i < count) {
ba[i] = inputStream.read().toByte()
i++
}
return ba
}
+2 -96
View File
@@ -87,68 +87,6 @@ class CodedOutputStream(val buffer: ByteArray) {
writeInt64NoTag((value shl 1) xor (value shr 63))
}
fun writeFixed32(fieldNumber: Int, value: Int?) {
value ?: return
writeTag(fieldNumber, WireType.FIX_32)
writeFixed32NoTag(value)
}
fun writeFixed32NoTag(value: Int) {
writeLittleEndian(value)
}
// See notes on unsigned integers implementation above
fun writeSFixed32(fieldNumber: Int, value: Int?) {
value ?: return
writeTag(fieldNumber, WireType.FIX_32)
writeSFixed32NoTag(value)
}
fun writeSFixed32NoTag(value: Int) {
writeLittleEndian(value)
}
fun writeFixed64(fieldNumber: Int, value: Long?) {
value ?: return
writeTag(fieldNumber, WireType.FIX_64)
writeFixed64NoTag(value)
}
fun writeFixed64NoTag(value: Long) {
writeLittleEndian(value)
}
// See notes on unsigned integers implementation above
fun writeSFixed64(fieldNumber: Int, value: Long?) {
value ?: return
writeTag(fieldNumber, WireType.FIX_64)
writeSFixed64NoTag(value)
}
fun writeSFixed64NoTag(value: Long) {
writeLittleEndian(value)
}
fun writeDouble(fieldNumber: Int, value: Double?) {
value ?: return
writeTag(fieldNumber, WireType.FIX_64)
writeDoubleNoTag(value)
}
fun writeDoubleNoTag(value: Double) {
writeLittleEndian(value)
}
fun writeFloat(fieldNumber: Int, value: Float?) {
value ?: return
writeTag(fieldNumber, WireType.FIX_32)
writeFloatNoTag(value)
}
fun writeFloatNoTag(value: Float) {
writeLittleEndian(value)
}
fun writeString(fieldNumber: Int, value: String?) {
value ?: return
writeTag(fieldNumber, WireType.LENGTH_DELIMITED)
@@ -179,39 +117,7 @@ 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 writeLittleEndian(value: Int?) {
value ?: return
val bb = ByteBuffer.allocate(4)
bb.order(ByteOrder.LITTLE_ENDIAN)
bb.putInt(value)
output.write(bb.array())
}
fun writeLittleEndian(value: Long?) {
value ?: return
val bb = ByteBuffer.allocate(8)
bb.order(ByteOrder.LITTLE_ENDIAN)
bb.putLong(value)
output.write(bb.array())
}
fun writeLittleEndian(value: Double?) {
value ?: return
val bb = ByteBuffer.allocate(8)
bb.order(ByteOrder.LITTLE_ENDIAN)
bb.putDouble(value)
output.write(bb.array())
}
fun writeLittleEndian(value: Float?) {
value ?: return
val bb = ByteBuffer.allocate(4)
bb.order(ByteOrder.LITTLE_ENDIAN)
bb.putFloat(value)
output.write(bb.array())
}
fun writeInt32NoTag(value: Int?) {
fun writeInt32NoTag(value: Int?) {
value ?: return
var curValue: Int = value
@@ -233,7 +139,7 @@ class CodedOutputStream(val buffer: ByteArray) {
res[resSize] = curByte.toByte()
resSize++
} while(curValue != 0)
} while (curValue != 0)
output.write(res, 0, resSize)
}
+3 -1
View File
@@ -10,9 +10,11 @@ class KotlinOutputStream(val buffer: ByteArray) {
}
fun write (data: ByteArray, begin: Int, size: Int) {
for (i in begin..(begin + size - 1)) {
var i = begin
while (i < begin + size) {
buffer[pos] = data[i]
pos += 1
i++
}
}
}
+2 -2
View File
@@ -31,7 +31,7 @@ object WireFormat {
var size = 0
while (curValue != 0) {
size += 1
curValue = value ushr VARINT_INFO_BITS_COUNT
curValue = curValue ushr VARINT_INFO_BITS_COUNT
}
return size
}
@@ -41,7 +41,7 @@ object WireFormat {
var size = 0
while (curValue != 0L) {
size += 1
curValue = value ushr VARINT_INFO_BITS_COUNT
curValue = curValue ushr VARINT_INFO_BITS_COUNT
}
return size
}