156
result = result_type(result)
145
157
return (result, pos)
148
160
raise _DecodeError('Too many bytes when decoding varint.')
149
161
return DecodeVarint
163
# We force 32-bit values to int and 64-bit values to long to make
164
# alternate implementations where the distinction is more significant
165
# (e.g. the C++ implementation) simpler.
152
_DecodeVarint = _VarintDecoder((1 << 64) - 1)
153
_DecodeSignedVarint = _SignedVarintDecoder((1 << 64) - 1)
167
_DecodeVarint = _VarintDecoder((1 << 64) - 1, long)
168
_DecodeSignedVarint = _SignedVarintDecoder((1 << 64) - 1, long)
155
170
# Use these versions for values which must be limited to 32 bits.
156
_DecodeVarint32 = _VarintDecoder((1 << 32) - 1)
157
_DecodeSignedVarint32 = _SignedVarintDecoder((1 << 32) - 1)
171
_DecodeVarint32 = _VarintDecoder((1 << 32) - 1, int)
172
_DecodeSignedVarint32 = _SignedVarintDecoder((1 << 32) - 1, int)
160
175
def ReadTag(buffer, pos):
294
312
# If this value has all its exponent bits set, then it's non-finite.
295
313
# In Python 2.4, struct.unpack will convert it to a finite 64-bit value.
296
314
# To avoid that, we parse it specially.
297
if ((float_bytes[3] in '\x7F\xFF')
298
and (float_bytes[2] >= '\x80')):
315
if ((float_bytes[3:4] in b('\x7F\xFF')) ##PY25
316
##!PY25 if ((float_bytes[3:4] in b'\x7F\xFF')
317
and (float_bytes[2:3] >= b('\x80'))): ##PY25
318
##!PY25 and (float_bytes[2:3] >= b'\x80')):
299
319
# If at least one significand bit is set...
300
if float_bytes[0:3] != '\x00\x00\x80':
320
if float_bytes[0:3] != b('\x00\x00\x80'): ##PY25
321
##!PY25 if float_bytes[0:3] != b'\x00\x00\x80':
301
322
return (_NAN, new_pos)
302
323
# If sign bit is set...
303
if float_bytes[3] == '\xFF':
324
if float_bytes[3:4] == b('\xFF'): ##PY25
325
##!PY25 if float_bytes[3:4] == b'\xFF':
304
326
return (_NEG_INF, new_pos)
305
327
return (_POS_INF, new_pos)
329
352
# If this value has all its exponent bits set and at least one significand
330
353
# bit set, it's not a number. In Python 2.4, struct.unpack will treat it
331
354
# as inf or -inf. To avoid that, we treat it specially.
332
if ((double_bytes[7] in '\x7F\xFF')
333
and (double_bytes[6] >= '\xF0')
334
and (double_bytes[0:7] != '\x00\x00\x00\x00\x00\x00\xF0')):
355
##!PY25 if ((double_bytes[7:8] in b'\x7F\xFF')
356
##!PY25 and (double_bytes[6:7] >= b'\xF0')
357
##!PY25 and (double_bytes[0:7] != b'\x00\x00\x00\x00\x00\x00\xF0')):
358
if ((double_bytes[7:8] in b('\x7F\xFF')) ##PY25
359
and (double_bytes[6:7] >= b('\xF0')) ##PY25
360
and (double_bytes[0:7] != b('\x00\x00\x00\x00\x00\x00\xF0'))): ##PY25
335
361
return (_NAN, new_pos)
337
363
# Note that we expect someone up-stack to catch struct.error and convert
342
368
return _SimpleDecoder(wire_format.WIRETYPE_FIXED64, InnerDecode)
371
def EnumDecoder(field_number, is_repeated, is_packed, key, new_default):
372
enum_type = key.enum_type
374
local_DecodeVarint = _DecodeVarint
375
def DecodePackedField(buffer, pos, end, message, field_dict):
376
value = field_dict.get(key)
378
value = field_dict.setdefault(key, new_default(message))
379
(endpoint, pos) = local_DecodeVarint(buffer, pos)
382
raise _DecodeError('Truncated message.')
383
while pos < endpoint:
384
value_start_pos = pos
385
(element, pos) = _DecodeSignedVarint32(buffer, pos)
386
if element in enum_type.values_by_number:
387
value.append(element)
389
if not message._unknown_fields:
390
message._unknown_fields = []
391
tag_bytes = encoder.TagBytes(field_number,
392
wire_format.WIRETYPE_VARINT)
393
message._unknown_fields.append(
394
(tag_bytes, buffer[value_start_pos:pos]))
396
if element in enum_type.values_by_number:
397
del value[-1] # Discard corrupt value.
399
del message._unknown_fields[-1]
400
raise _DecodeError('Packed element was truncated.')
402
return DecodePackedField
404
tag_bytes = encoder.TagBytes(field_number, wire_format.WIRETYPE_VARINT)
405
tag_len = len(tag_bytes)
406
def DecodeRepeatedField(buffer, pos, end, message, field_dict):
407
value = field_dict.get(key)
409
value = field_dict.setdefault(key, new_default(message))
411
(element, new_pos) = _DecodeSignedVarint32(buffer, pos)
412
if element in enum_type.values_by_number:
413
value.append(element)
415
if not message._unknown_fields:
416
message._unknown_fields = []
417
message._unknown_fields.append(
418
(tag_bytes, buffer[pos:new_pos]))
419
# Predict that the next tag is another copy of the same repeated
421
pos = new_pos + tag_len
422
if buffer[new_pos:pos] != tag_bytes or new_pos >= end:
423
# Prediction failed. Return.
425
raise _DecodeError('Truncated message.')
427
return DecodeRepeatedField
429
def DecodeField(buffer, pos, end, message, field_dict):
430
value_start_pos = pos
431
(enum_value, pos) = _DecodeSignedVarint32(buffer, pos)
433
raise _DecodeError('Truncated message.')
434
if enum_value in enum_type.values_by_number:
435
field_dict[key] = enum_value
437
if not message._unknown_fields:
438
message._unknown_fields = []
439
tag_bytes = encoder.TagBytes(field_number,
440
wire_format.WIRETYPE_VARINT)
441
message._unknown_fields.append(
442
(tag_bytes, buffer[value_start_pos:pos]))
345
447
# --------------------------------------------------------------------
348
Int32Decoder = EnumDecoder = _SimpleDecoder(
450
Int32Decoder = _SimpleDecoder(
349
451
wire_format.WIRETYPE_VARINT, _DecodeSignedVarint32)
351
453
Int64Decoder = _SimpleDecoder(