diff --git a/src/dbus_fast/_private/unmarshaller.pxd b/src/dbus_fast/_private/unmarshaller.pxd index 88d6c5dd..3d68d58e 100644 --- a/src/dbus_fast/_private/unmarshaller.pxd +++ b/src/dbus_fast/_private/unmarshaller.pxd @@ -12,7 +12,7 @@ cdef unsigned int HEADER_SIGNATURE_SIZE cdef class Unmarshaller: cdef object unix_fds - cdef object buf + cdef bytearray buf cdef object view cdef unsigned int pos cdef object stream @@ -30,10 +30,15 @@ cdef class Unmarshaller: @cython.locals( start_len=cython.ulong, - missing_bytes=cython.ulong, + missing_bytes=cython.ulong ) cpdef read_to_pos(self, unsigned long pos) + cpdef read_uint32_cast(self, object type_) + + @cython.locals( + buf_bytes=cython.bytearray, + ) cpdef read_string_cast(self, type_ = *) @cython.locals( @@ -54,6 +59,10 @@ cdef class Unmarshaller: ) cpdef _read_header(self) + cpdef _read_body(self) + + cpdef unmarshall(self) + @cython.locals( beginning_pos=cython.ulong, o=cython.ulong, diff --git a/src/dbus_fast/_private/unmarshaller.py b/src/dbus_fast/_private/unmarshaller.py index 4a38c596..f8e58d11 100644 --- a/src/dbus_fast/_private/unmarshaller.py +++ b/src/dbus_fast/_private/unmarshaller.py @@ -218,6 +218,10 @@ def read_to_pos(self, pos) -> None: if len(data) + start_len != pos: raise MarshallerStreamEndError() + def read_uint32_cast(self, signature: SignatureType) -> Any: + self.pos += UINT32_SIZE + (-self.pos & (UINT32_SIZE - 1)) # align + return self.view[self.pos - UINT32_SIZE : self.pos].cast(UINT32_CAST)[0] + def read_boolean(self, type_=None) -> bool: return bool(self.readers[UINT32_SIGNATURE.token](self, UINT32_SIGNATURE)) @@ -228,8 +232,7 @@ def read_string_cast(self, type_=None) -> str: # read terminating '\0' byte as well (str_length + 1) start_pos = self.pos - UINT32_SIZE self.pos += self.view[start_pos : self.pos].cast(UINT32_CAST)[0] + 1 - end_pos = self.pos - 1 - return self.buf[str_start:end_pos].decode() + return self.buf[str_start : self.pos - 1].decode() def read_string_unpack(self, type_=None) -> str: """Read a string using unpack.""" @@ -397,12 +400,12 @@ def unmarshall(self): to be resumed when more data comes in over the wire. """ try: - if not self.message_type: + if not self.msg_len: self._read_header() self._read_body() except MarshallerStreamEndError: return None - return self.message + return self._message _complex_parsers_unpack: Dict[ str, Callable[["Unmarshaller", SignatureType], Any] @@ -426,6 +429,8 @@ def unmarshall(self): "(": read_struct, "{": read_dict_entry, "v": read_variant, + "h": read_uint32_cast, + UINT32_DBUS_TYPE: read_uint32_cast, } _ctype_by_endian: Dict[