File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -89,6 +89,11 @@ def from_binary(cls, value: bytes) -> Bit:
8989 if not isinstance (value , bytes ):
9090 raise ValueError ('expected bytes' )
9191
92+ length , = unpack_from ('>i' , value )
93+
94+ if len (value ) != 4 + (length + 7 ) // 8 :
95+ raise ValueError ('invalid length' )
96+
9297 bit = cls .__new__ (cls )
9398 bit ._value = value
9499 return bit
Original file line number Diff line number Diff line change @@ -58,7 +58,14 @@ def from_text(cls, value: str) -> HalfVector:
5858
5959 @classmethod
6060 def from_binary (cls , value : bytes ) -> HalfVector :
61- # TODO check dimensions/length and unused
61+ dim , unused = struct .unpack_from ('>HH' , value )
62+
63+ if len (value ) != 4 + 2 * dim :
64+ raise ValueError ('invalid length' )
65+
66+ if unused != 0 :
67+ raise ValueError ('expected unused to be 0' )
68+
6269 vec = cls .__new__ (cls )
6370 vec ._value = value
6471 return vec
Original file line number Diff line number Diff line change @@ -58,7 +58,14 @@ def from_text(cls, value: str) -> Vector:
5858
5959 @classmethod
6060 def from_binary (cls , value : bytes ) -> Vector :
61- # TODO check dimensions/length and unused
61+ dim , unused = struct .unpack_from ('>HH' , value )
62+
63+ if len (value ) != 4 + 4 * dim :
64+ raise ValueError ('invalid length' )
65+
66+ if unused != 0 :
67+ raise ValueError ('expected unused to be 0' )
68+
6269 vec = cls .__new__ (cls )
6370 vec ._value = value
6471 return vec
You can’t perform that action at this time.
0 commit comments