Device: Move the structure under a Structures class

This commit is contained in:
rlaphoenix 2022-07-21 15:40:46 +01:00
parent 02ca1b00c9
commit db80776ac0
1 changed files with 11 additions and 8 deletions

View File

@ -21,11 +21,8 @@ class _Types(Enum):
ANDROID = 2 ANDROID = 2
class Device: class Structures:
# needed so bin_format can enumerate the types v2 = Struct(
Types = _Types
bin_format = Struct(
"signature" / Const(b"WVD"), "signature" / Const(b"WVD"),
"version" / Const(Int8ub, 2), "version" / Const(Int8ub, 2),
"type_" / CEnum( "type_" / CEnum(
@ -43,6 +40,12 @@ class Device:
"client_id" / Bytes(this.client_id_len) "client_id" / Bytes(this.client_id_len)
) )
class Device:
Types = _Types
supported_structure = Structures.v2
# == Bin Format Revisions == # # == Bin Format Revisions == #
# Version 2: Removed vmp and vmp_len as it should already be within the Client ID # Version 2: Removed vmp and vmp_len as it should already be within the Client ID
# Version 1: Removed system_id as it can be retrieved from the Client ID's DRM Certificate # Version 1: Removed system_id as it can be retrieved from the Client ID's DRM Certificate
@ -109,18 +112,18 @@ class Device:
data = base64.b64decode(data) data = base64.b64decode(data)
if not isinstance(data, bytes): if not isinstance(data, bytes):
raise ValueError(f"Expecting Bytes or Base64 input, got {data!r}") raise ValueError(f"Expecting Bytes or Base64 input, got {data!r}")
return cls(**cls.bin_format.parse(data)) return cls(**cls.supported_structure.parse(data))
@classmethod @classmethod
def load(cls, path: Union[Path, str]) -> Device: def load(cls, path: Union[Path, str]) -> Device:
if not isinstance(path, (Path, str)): if not isinstance(path, (Path, str)):
raise ValueError(f"Expecting Path object or path string, got {path!r}") raise ValueError(f"Expecting Path object or path string, got {path!r}")
with Path(path).open(mode="rb") as f: with Path(path).open(mode="rb") as f:
return cls(**cls.bin_format.parse_stream(f)) return cls(**cls.supported_structure.parse_stream(f))
def dumps(self) -> bytes: def dumps(self) -> bytes:
private_key = self.private_key.export_key("DER") if self.private_key else None private_key = self.private_key.export_key("DER") if self.private_key else None
return self.bin_format.build(dict( return self.supported_structure.build(dict(
version=2, version=2,
type_=self.type.value, type_=self.type.value,
security_level=self.security_level, security_level=self.security_level,