Esempio n. 1
0
def _varuint64bytesizenotag(uint64):
    """returns the number of bytes required to serialize a single varint
  using boundary value comparisons. (unrolled loop optimization -wpierce)
  uint64 must be unsigned.
  """
    if uint64 <= 0x7F:
        return 1
    if uint64 <= 0x3FFF:
        return 2
    if uint64 <= 0x1FFFFF:
        return 3
    if uint64 <= 0xFFFFFFF:
        return 4
    if uint64 <= 0x7FFFFFFFF:
        return 5
    if uint64 <= 0x3FFFFFFFFFF:
        return 6
    if uint64 <= 0x1FFFFFFFFFFFF:
        return 7
    if uint64 <= 0xFFFFFFFFFFFFFF:
        return 8
    if uint64 <= 0x7FFFFFFFFFFFFFFF:
        return 9
    if uint64 > uint64_max:
        raise message.encodeerror("value out of range: %d" % uint64)
    return 10
 def serializetostring(self):
   # check if the message has all of its required fields set.
   errors = []
   if not self.isinitialized():
     raise message_mod.encodeerror(
         'message %s is missing required fields: %s' % (
         self.descriptor.full_name, ','.join(self.findinitializationerrors())))
   return self.serializepartialtostring()
Esempio n. 3
0
def packtag(field_number, wire_type):
    """returns an unsigned 32-bit integer that encodes the field number and
  wire type information in standard protocol message wire format.

  args:
    field_number: expected to be an integer in the range [1, 1 << 29)
    wire_type: one of the wiretype_* constants.
  """
    if not 0 <= wire_type <= _wiretype_max:
        raise message.encodeerror("unknown wire type: %d" % wire_type)
    return (field_number << tag_type_bits) | wire_type
def _bytesfornonrepeatedelement(value, field_number, field_type):
  """returns the number of bytes needed to serialize a non-repeated element.
  the returned byte count includes space for tag information and any
  other additional space associated with serializing value.

  args:
    value: value we're serializing.
    field_number: field number of this value.  (since the field number
      is stored as part of a varint-encoded tag, this has an impact
      on the total bytes required to serialize the value).
    field_type: the type of the field.  one of the type_* constants
      within fielddescriptor.
  """
  try:
    fn = type_checkers.type_to_byte_size_fn[field_type]
    return fn(field_number, value)
  except keyerror:
    raise message_mod.encodeerror('unrecognized field type: %d' % field_type)
Esempio n. 5
0
 def serializetostring(self):
   if not self.isinitialized():
     raise message.encodeerror(
         'message %s is missing required fields: %s' % (
         self._cmsg.full_name, ','.join(self.findinitializationerrors())))
   return self._cmsg.serializetostring()