aboutsummaryrefslogtreecommitdiff
path: root/src/protobuf
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2018-04-02 11:38:35 +0300
committerOskari Timperi <oskari.timperi@iki.fi>2018-04-02 11:39:36 +0300
commite1f5d2e73a1f305ca003b24f6025615abade5d64 (patch)
tree3ee8589b78cdda9cf01e54f3d2248db6dcb5af15 /src/protobuf
parent2427dd3c04c37dd78a54a73da41141b74182590a (diff)
downloadnimpb-e1f5d2e73a1f305ca003b24f6025615abade5d64.tar.gz
nimpb-e1f5d2e73a1f305ca003b24f6025615abade5d64.zip
Fix packed field size calculation
The size was calculated based on wire type. We need to calculate based on field type because for sint32/sint64 we need to zigzag encode the value before calculating the size of varint.
Diffstat (limited to 'src/protobuf')
-rw-r--r--src/protobuf/stream.nim21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/protobuf/stream.nim b/src/protobuf/stream.nim
index 9d843e6..824ad60 100644
--- a/src/protobuf/stream.nim
+++ b/src/protobuf/stream.nim
@@ -274,17 +274,24 @@ proc sizeOfVarint[T](value: T): uint64 =
inc(result)
inc(result)
-proc packedFieldSize*[T](values: seq[T], wiretype: WireType): uint64 =
- case wiretype
- of WireType.Fixed32:
- result = uint64(len(values)) * 4
- of WireType.Fixed64:
+proc packedFieldSize*[T](values: seq[T], fieldtype: FieldType): uint64 =
+ case fieldtype
+ of FieldType.Fixed64, FieldType.SFixed64, FieldType.Double:
result = uint64(len(values)) * 8
- of WireType.Varint:
+ of FieldType.Fixed32, FieldType.SFixed32, FieldType.Float:
+ result = uint64(len(values)) * 4
+ of FieldType.Int64, FieldType.UInt64, FieldType.Int32, FieldType.Bool,
+ FieldType.UInt32, FieldType.Enum:
for value in values:
result += sizeOfVarint(value)
+ of FieldType.SInt32:
+ for value in values:
+ result += sizeOfVarint(zigzagEncode(int32(value)))
+ of FieldType.SInt64:
+ for value in values:
+ result += sizeOfVarint(zigzagEncode(int64(value)))
else:
- raise newException(Exception, "invalid wiretype")
+ raise newException(Exception, "invalid fieldtype")
proc sizeOfString*(s: string): uint64 =
result = sizeOfVarint(len(s).uint64) + len(s).uint64