OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of protobuf; | 5 part of protobuf; |
6 | 6 |
7 class CodedBufferReader { | 7 class CodedBufferReader { |
8 static const int DEFAULT_RECURSION_LIMIT = 64; | 8 static const int DEFAULT_RECURSION_LIMIT = 64; |
9 static const int DEFAULT_SIZE_LIMIT = 64 << 20; | 9 static const int DEFAULT_SIZE_LIMIT = 64 << 20; |
10 | 10 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 } | 89 } |
90 _withLimit(length, () { | 90 _withLimit(length, () { |
91 ++_recursionDepth; | 91 ++_recursionDepth; |
92 message.mergeFromCodedBufferReader(this); | 92 message.mergeFromCodedBufferReader(this); |
93 checkLastTagWas(0); | 93 checkLastTagWas(0); |
94 --_recursionDepth; | 94 --_recursionDepth; |
95 }); | 95 }); |
96 } | 96 } |
97 | 97 |
98 int readEnum() => readInt32(); | 98 int readEnum() => readInt32(); |
99 int readInt32() => _readRawVarint64().getInt32(0, Endianness.LITTLE_ENDIAN); | 99 int readInt32() => _readRawVarint32(); |
100 ByteData readInt64() => _readRawVarint64(); | 100 Int64 readInt64() => _readRawVarint64(); |
101 int readUint32() => _readRawVarint32(); | 101 int readUint32() => _readRawVarint32(); |
102 ByteData readUint64() => _readRawVarint64(); | 102 Int64 readUint64() => _readRawVarint64(); |
103 int readSint32() => _decodeZigZag32(readUint32()); | 103 int readSint32() => _decodeZigZag32(readUint32()); |
104 ByteData readSint64() => _decodeZigZag64(readUint64()); | 104 Int64 readSint64() => _decodeZigZag64(readUint64()); |
105 int readFixed32() => _readByteData(4).getUint32(0, Endianness.LITTLE_ENDIAN); | 105 int readFixed32() => _readByteData(4).getUint32(0, Endianness.LITTLE_ENDIAN); |
106 ByteData readFixed64() => readSfixed64(); | 106 Int64 readFixed64() => readSfixed64(); |
107 int readSfixed32() => _readByteData(4).getInt32(0, Endianness.LITTLE_ENDIAN); | 107 int readSfixed32() => _readByteData(4).getInt32(0, Endianness.LITTLE_ENDIAN); |
108 ByteData readSfixed64() => _readByteData(8); | 108 Int64 readSfixed64() { |
| 109 var data = _readByteData(8); |
| 110 var view = new Uint8List.view(data.buffer, data.offsetInBytes, 8); |
| 111 return new Int64.fromBytes(view); |
| 112 } |
109 bool readBool() => _readRawVarint32() != 0; | 113 bool readBool() => _readRawVarint32() != 0; |
110 List<int> readBytes() { | 114 List<int> readBytes() { |
111 int length = readInt32(); | 115 int length = readInt32(); |
112 _checkLimit(length); | 116 _checkLimit(length); |
113 return new Uint8List.view(_buffer.buffer, _bufferPos - length, length); | 117 return new Uint8List.view(_buffer.buffer, _bufferPos - length, length); |
114 } | 118 } |
115 String readString() => decodeUtf8(readBytes()); | 119 String readString() => decodeUtf8(readBytes()); |
116 double readFloat() => | 120 double readFloat() => |
117 _readByteData(4).getFloat32(0, Endianness.LITTLE_ENDIAN); | 121 _readByteData(4).getFloat32(0, Endianness.LITTLE_ENDIAN); |
118 double readDouble() => | 122 double readDouble() => |
(...skipping 10 matching lines...) Expand all Loading... |
129 throw new InvalidProtocolBufferException.invalidTag(); | 133 throw new InvalidProtocolBufferException.invalidTag(); |
130 } | 134 } |
131 return _lastTag; | 135 return _lastTag; |
132 } | 136 } |
133 | 137 |
134 static int _decodeZigZag32(int value) { | 138 static int _decodeZigZag32(int value) { |
135 if ((value & 0x1) == 1) value = -value; | 139 if ((value & 0x1) == 1) value = -value; |
136 return value >> 1; | 140 return value >> 1; |
137 } | 141 } |
138 | 142 |
139 static ByteData _decodeZigZag64(ByteData value) { | 143 static Int64 _decodeZigZag64(Int64 value) { |
140 int lo = value.getUint32(0, Endianness.LITTLE_ENDIAN); | 144 if ((value & 0x1) == 1) value = -value; |
141 int hi = value.getUint32(4, Endianness.LITTLE_ENDIAN); | 145 return value >> 1; |
142 int newHi = hi >> 1; | |
143 int newLo = (lo >> 1) | ((hi & 0x1) << 31); | |
144 if ((lo & 0x1) == 1) { | |
145 newHi ^= 0xffffffff; | |
146 newLo ^= 0xffffffff; | |
147 } | |
148 return new ByteData(8) | |
149 ..setUint32(0, newLo, Endianness.LITTLE_ENDIAN) | |
150 ..setUint32(4, newHi, Endianness.LITTLE_ENDIAN); | |
151 } | 146 } |
152 | 147 |
153 int _readRawVarintByte() { | 148 int _readRawVarintByte() { |
154 _checkLimit(1); | 149 _checkLimit(1); |
155 return _buffer[_bufferPos - 1]; | 150 return _buffer[_bufferPos - 1]; |
156 } | 151 } |
157 | 152 |
158 int _readRawVarint32() { | 153 int _readRawVarint32() { |
159 int result = 0; | 154 int result = 0; |
160 for (int i = 0; i < 5; i++) { | 155 for (int i = 0; i < 5; i++) { |
161 int byte = _readRawVarintByte(); | 156 int byte = _readRawVarintByte(); |
162 result |= (byte & 0x7f) << (i * 7); | 157 result |= (byte & 0x7f) << (i * 7); |
163 if ((byte & 0x80) == 0) return result; | 158 if ((byte & 0x80) == 0) return result; |
164 } | 159 } |
165 throw new InvalidProtocolBufferException.malformedVarint(); | 160 throw new InvalidProtocolBufferException.malformedVarint(); |
166 } | 161 } |
167 | 162 |
168 ByteData _readRawVarint64() { | 163 Int64 _readRawVarint64() { |
169 final result = new ByteData(8); | |
170 | |
171 setPart(index, value) => | |
172 result..setUint32(index, value, Endianness.LITTLE_ENDIAN); | |
173 | |
174 int lo = 0; | 164 int lo = 0; |
| 165 int hi = 0; |
175 | 166 |
176 // Read low 28 bits. | 167 // Read low 28 bits. |
177 for (int i = 0; i < 4; i++) { | 168 for (int i = 0; i < 4; i++) { |
178 int byte = _readRawVarintByte(); | 169 int byte = _readRawVarintByte(); |
179 lo |= (byte & 0x7f) << (i * 7); | 170 lo |= (byte & 0x7f) << (i * 7); |
180 if ((byte & 0x80) == 0) return setPart(0, lo); | 171 if ((byte & 0x80) == 0) return new Int64.fromInts(hi, lo); |
181 } | 172 } |
182 | 173 |
183 // Read middle 7 bits: 4 low belong to low part above, | 174 // Read middle 7 bits: 4 low belong to low part above, |
184 // 3 remaining belong to hi. | 175 // 3 remaining belong to hi. |
185 int byte = _readRawVarintByte(); | 176 int byte = _readRawVarintByte(); |
186 setPart(0, lo | (byte & 0xf) << 28); | 177 lo |= (byte & 0xf) << 28; |
187 int hi = (byte >> 4) & 0x7; | 178 hi = (byte >> 4) & 0x7; |
188 if ((byte & 0x80) == 0) return setPart(4, hi); | 179 if ((byte & 0x80) == 0) return new Int64.fromInts(hi, lo); |
189 | 180 |
190 // Read remaining bits of hi. | 181 // Read remaining bits of hi. |
191 for (int i = 0; i < 5; i++) { | 182 for (int i = 0; i < 5; i++) { |
192 int byte = _readRawVarintByte(); | 183 int byte = _readRawVarintByte(); |
193 hi |= (byte & 0x7f) << ((i * 7) + 3); | 184 hi |= (byte & 0x7f) << ((i * 7) + 3); |
194 if ((byte & 0x80) == 0) return setPart(4, hi); | 185 if ((byte & 0x80) == 0) return new Int64.fromInts(hi, lo); |
195 } | 186 } |
196 throw new InvalidProtocolBufferException.malformedVarint(); | 187 throw new InvalidProtocolBufferException.malformedVarint(); |
197 } | 188 } |
198 | 189 |
199 ByteData _readByteData(int sizeInBytes) { | 190 ByteData _readByteData(int sizeInBytes) { |
200 _checkLimit(sizeInBytes); | 191 _checkLimit(sizeInBytes); |
201 return new ByteData.view( | 192 return new ByteData.view( |
202 _buffer.buffer, _bufferPos - sizeInBytes, sizeInBytes); | 193 _buffer.buffer, _bufferPos - sizeInBytes, sizeInBytes); |
203 } | 194 } |
204 } | 195 } |
OLD | NEW |