Exemple #1
0
    def decode_buf(cls, buf, offset=0, end=None):
        if end is None:
            end = len(buf)

        ret = cls()
        for k, v in cls._mfields_.items():
            if v[2]: setattr(ret, v[0], [])

        while offset < end:
            a = buf[offset]
            field_number = a >> 3
            wtype = a & 7

            our = cls._mfields_.get(field_number, None)
            if our is None:
                raise DecodeError(
                    'No field definition for type {0!r} slot {1} wire type {2}'
                    .format(cls.__name__, field_number, wtype))
            name, typehandler, is_array = our

            if wtype == serialize.WTYPE_LEN_DELIM:
                length, offset = serialize.read_varint(buf, offset + 1)
                val = typehandler.decode_buf(buf, offset, end=offset + length)
                offset += length
            elif wtype == serialize.WTYPE_VARINT:
                val, offset = typehandler.decode_buf(buf, offset + 1)
            elif wtype == serialize.WTYPE_FIXED32:
                val, offset = typehandler.decode_buf(buf, offset + 1)
            elif wtype == serialize.WTYPE_FIXED64:
                val, offset = typehandler.decode_buf(buf, offset + 1)
            else:
                raise DecodeError('Unhandled wire type {0}'.format(wtype))

            if is_array:
                if isinstance(val, list):
                    getattr(ret, name).extend(val)
                else:
                    getattr(ret, name).append(val)
            else:
                if hasattr(ret, name):
                    raise DecodeError('Duplicated slot for non-array type')
                setattr(ret, name, val)
        return ret
Exemple #2
0
    def decode_buf(cls, buf, offset=0, end=None):
        if end is None:
            end = len(buf)

        ret = cls()
        for k,v in cls._mfields_.items():
            if v[2]: setattr(ret, v[0], [])

        while offset < end:
            a = buf[offset]
            field_number = a >> 3
            wtype = a & 7

            our = cls._mfields_.get(field_number, None)
            if our is None:
                raise DecodeError('No field definition for type {0!r} slot {1} wire type {2}'.format(cls.__name__, field_number, wtype))
            name, typehandler, is_array = our

            if wtype == serialize.WTYPE_LEN_DELIM:
                length, offset = serialize.read_varint(buf, offset+1)
                val = typehandler.decode_buf(buf, offset, end=offset+length)
                offset += length
            elif wtype == serialize.WTYPE_VARINT:
                val, offset = typehandler.decode_buf(buf, offset+1)
            elif wtype == serialize.WTYPE_FIXED32:
                val, offset = typehandler.decode_buf(buf, offset+1)
            elif wtype == serialize.WTYPE_FIXED64:
                val, offset = typehandler.decode_buf(buf, offset+1)
            else:
                raise DecodeError('Unhandled wire type {0}'.format(wtype))

            if is_array:
                if isinstance(val, list):
                    getattr(ret, name).extend(val)
                else:
                    getattr(ret, name).append(val)
            else:
                if hasattr(ret, name):
                    raise DecodeError('Duplicated slot for non-array type')
                setattr(ret, name, val)
        return ret
Exemple #3
0
 def decode_buf(self, buf, offset=0, end=None):
     # Note: could check/clamp to _nbits here
     if end is None:
         return serialize.read_varint(buf, offset, signed=self._signed)
     else:
         return serialize.read_packed_varint(buf, offset, end, self._signed)
Exemple #4
0
 def decode_buf(self, buf, offset=0, end=None):
     # Note: could check/clamp to _nbits here
     if end is None:
         return serialize.read_varint(buf, offset, signed=self._signed)
     else:
         return serialize.read_packed_varint(buf, offset, end, self._signed)