@@ -71,24 +71,21 @@ def get_string(dgram: bytes, start_index: int) -> Tuple[str, int]:
7171 raise ParseError ("start_index < 0" )
7272 offset = 0
7373 try :
74- if (
75- len (dgram ) > start_index + _STRING_DGRAM_PAD
76- and dgram [start_index + _STRING_DGRAM_PAD ] == _EMPTY_STR_DGRAM
77- ):
78- return "" , start_index + _STRING_DGRAM_PAD
7974 while dgram [start_index + offset ] != 0 :
8075 offset += 1
81- # Align to a byte word.
82- if (offset ) % _STRING_DGRAM_PAD == 0 :
83- offset += _STRING_DGRAM_PAD
84- else :
85- offset += - offset % _STRING_DGRAM_PAD
86- # Python slices do not raise an IndexError past the last index,
87- # do it ourselves.
88- if offset > len (dgram [start_index :]):
76+
77+ # OSC spec: "followed by a null, followed by 0-3 additional null characters
78+ # to make the total number of bits a multiple of 32"
79+ # This means the total length (including the first null) must be a multiple of 4.
80+ total_len = offset + 1
81+ if total_len % 4 != 0 :
82+ total_len += 4 - (total_len % 4 )
83+
84+ if start_index + total_len > len (dgram ):
8985 raise ParseError ("Datagram is too short" )
86+
9087 data_str = dgram [start_index : start_index + offset ]
91- return data_str .replace ( b" \x00 " , b"" ). decode ("utf-8" ), start_index + offset
88+ return data_str .decode ("utf-8" ), start_index + total_len
9289 except IndexError as ie :
9390 raise ParseError (f"Could not parse datagram { ie } " )
9491 except TypeError as te :
@@ -214,11 +211,8 @@ def get_timetag(dgram: bytes, start_index: int) -> Tuple[Tuple[datetime, int], i
214211 timetag , _ = get_uint64 (dgram , start_index )
215212 seconds , fraction = ntp .parse_timestamp (timetag )
216213
217- hours , seconds = seconds // 3600 , seconds % 3600
218- minutes , seconds = seconds // 60 , seconds % 60
219-
220214 utc = datetime .combine (ntp ._NTP_EPOCH , datetime .min .time ()) + timedelta (
221- hours = hours , minutes = minutes , seconds = seconds
215+ seconds = seconds
222216 )
223217
224218 return (utc , fraction ), start_index + _TIMETAG_DGRAM_LEN
0 commit comments