changeset 17:5d9a7186c9f7

ProtocolBuffers: fix handling of 64 bit varint
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 10 Apr 2013 01:33:32 +0200
parents fba35b87da32
children a6faa87767bb
files ProtocolBuffers.cs
diffstat 1 files changed, 9 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ProtocolBuffers.cs	Sat Mar 30 16:41:14 2013 +0100
+++ b/ProtocolBuffers.cs	Wed Apr 10 01:33:32 2013 +0200
@@ -67,13 +67,19 @@
 		}
 
 		private int ReadVarIntTo(out int v) {
+			long vl;
+			int l = ReadVarIntTo(out vl);
+			v = (int)vl;
+			return l;
+		}
+		private int ReadVarIntTo(out long v) {
 			v = 0;
 			int h = 0;
 			int b = 0x80;
 			int l = 0;
 			while ((b & 0x80) != 0) {
 				b = buffer[offset + index + l];
-				v |= (b & 0x7F) << h;
+				v |= ((long)b & 0x7FL) << h;
 				h += 7;
 				l++;
 			}
@@ -85,7 +91,7 @@
 
 		public long GetVarint() {
 			if (!hasCurrentField || WireType != 0) throw new InvalidOperationException();
-			int v;
+			long v;
 			ReadVarIntTo(out v);
 			return v;
 		}
@@ -145,7 +151,7 @@
 		private void WriteVarint(long value) {
 			while ((value & ~0x7fL) != 0) {
 				stream.WriteByte((Byte)(0x80 | value));
-				value >>= 7;
+				value = (long)((ulong)value >> 7);
 			}
 			stream.WriteByte((Byte)value);
 		}