comparison ProtocolBuffers.cs @ 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 2e0ff842aa4a
children
comparison
equal deleted inserted replaced
16:fba35b87da32 17:5d9a7186c9f7
65 hasCurrentField = true; 65 hasCurrentField = true;
66 return true; 66 return true;
67 } 67 }
68 68
69 private int ReadVarIntTo(out int v) { 69 private int ReadVarIntTo(out int v) {
70 long vl;
71 int l = ReadVarIntTo(out vl);
72 v = (int)vl;
73 return l;
74 }
75 private int ReadVarIntTo(out long v) {
70 v = 0; 76 v = 0;
71 int h = 0; 77 int h = 0;
72 int b = 0x80; 78 int b = 0x80;
73 int l = 0; 79 int l = 0;
74 while ((b & 0x80) != 0) { 80 while ((b & 0x80) != 0) {
75 b = buffer[offset + index + l]; 81 b = buffer[offset + index + l];
76 v |= (b & 0x7F) << h; 82 v |= ((long)b & 0x7FL) << h;
77 h += 7; 83 h += 7;
78 l++; 84 l++;
79 } 85 }
80 return l; 86 return l;
81 } 87 }
83 public int FieldNumber { get { return (int)((UInt32)currentField >> 3); } } 89 public int FieldNumber { get { return (int)((UInt32)currentField >> 3); } }
84 public int WireType { get { return currentField & 7; } } 90 public int WireType { get { return currentField & 7; } }
85 91
86 public long GetVarint() { 92 public long GetVarint() {
87 if (!hasCurrentField || WireType != 0) throw new InvalidOperationException(); 93 if (!hasCurrentField || WireType != 0) throw new InvalidOperationException();
88 int v; 94 long v;
89 ReadVarIntTo(out v); 95 ReadVarIntTo(out v);
90 return v; 96 return v;
91 } 97 }
92 98
93 public long GetFixed64() { 99 public long GetFixed64() {
143 } 149 }
144 150
145 private void WriteVarint(long value) { 151 private void WriteVarint(long value) {
146 while ((value & ~0x7fL) != 0) { 152 while ((value & ~0x7fL) != 0) {
147 stream.WriteByte((Byte)(0x80 | value)); 153 stream.WriteByte((Byte)(0x80 | value));
148 value >>= 7; 154 value = (long)((ulong)value >> 7);
149 } 155 }
150 stream.WriteByte((Byte)value); 156 stream.WriteByte((Byte)value);
151 } 157 }
152 private void WriteTag(int fieldNumber, int wireType) { 158 private void WriteTag(int fieldNumber, int wireType) {
153 WriteVarint((fieldNumber << 3) | wireType); 159 WriteVarint((fieldNumber << 3) | wireType);