From 3d12d608c04f1d8ac2ddf83fb5aa0dbfeab0e425 Mon Sep 17 00:00:00 2001 From: Johannes Holland Date: Sat, 1 Jul 2023 15:20:53 +0200 Subject: [PATCH] type: add type detection --- index.html | 20 +++++++------ main.py | 84 ++++++++++++++++++++++++++++++++++++++++++------------ style.css | 11 +++++-- 3 files changed, 86 insertions(+), 29 deletions(-) diff --git a/index.html b/index.html index 93ad613..9566cd1 100644 --- a/index.html +++ b/index.html @@ -2,25 +2,29 @@ tpmstream - + + -
+ + packages = ["tpmstream"] + + + +
+
- +
- - - packages = ["tpmstream"] - - diff --git a/main.py b/main.py index 7f4e1bb..7ed1e5d 100644 --- a/main.py +++ b/main.py @@ -1,40 +1,83 @@ import asyncio import re +from js import document +from tpmstream.__main__ import parse_all_types from tpmstream.io.auto import Auto +from tpmstream.io.hex import Hex from tpmstream.io.pretty import Pretty -from tpmstream.spec.commands import CommandResponseStream +from tpmstream.spec import all_types +from tpmstream.spec.commands import CommandResponseStream, Response +from tpmstream.spec.structures.constants import TPM_CC +input = Element("in").element +output = Element("out").element +tpm_types = Element("tpm-type").element -input = Element('in').element -output = Element('out').element +def type_to_str(tpm_type, command_code=None): + if command_code is None: + return tpm_type.__name__ + return f"{tpm_type.__name__} ({command_code})" -def convert(*args): - asyncio.create_task(convert_catch_all(args)) +def str_to_type(s): + types_without_response = { + type_to_str(tpm_type): (tpm_type, None) + for tpm_type in all_types + if tpm_type is not Response + } + types_with_response = { + type_to_str(Response, command_code): (Response, command_code) + for command_code in TPM_CC + } + return (types_without_response | types_with_response)[s] -async def convert_catch_all(*args): + +def on_input(*args): + asyncio.create_task(on_input_catch_all(args)) + + +async def on_input_catch_all(*args): input.classList.remove("invalid") if not input.value: return try: - await convert_unwrapped(*args) + await on_input_unwrapped(*args) except Exception as e: input.classList.add("invalid") output.innerHTML = f'{str(e)}' return -async def convert_unwrapped(*args): - buffer = bytearray.fromhex(input.value) +async def on_input_unwrapped(*args): + canonical_objs = list(parse_all_types(Hex, input.value.encode())) + + tpm_types.innerHTML = "" + for canonical_obj, command_code in canonical_objs: + option = document.createElement("option") + value = type_to_str(type(canonical_obj.object), command_code=command_code) + option.text = value + option.value = value + tpm_types.add(option, None) + + if not canonical_objs: + raise ValueError("Cannot match any TPM type.") - events = Auto.marshal( - tpm_type=CommandResponseStream, - buffer=buffer, - command_code=None, + canonical_obj, command_code = canonical_objs[0] + tpm_types.value = type_to_str(type(canonical_obj.object), command_code=command_code) + on_select() + + +def on_select(): + tpm_type, command_code = str_to_type(tpm_types.value) + + events = Hex.marshal( + tpm_type=tpm_type, + buffer=input.value.encode(), + command_code=command_code, abort_on_error=False, ) @@ -43,11 +86,16 @@ async def convert_unwrapped(*args): if isinstance(line, bytes): output.innerHTML += " " + line.hex().decode() else: - line = re.sub(r"\[92m([^]*)", r'\1', line) line = re.sub(r"\[34m([^]*)", r'\1', line) - line = re.sub(r"\[33m([^]*)", r'\1', line) - line = re.sub(r"\[31m([^]*)", r'\1', line) - line = re.sub(r"\[30m([^]*)", r'\1', line) - line = re.sub(r"\[0m", '', line) + line = re.sub( + r"\[33m([^]*)", r'\1', line + ) + line = re.sub( + r"\[31m([^]*)", r'\1', line + ) + line = re.sub( + r"\[30m([^]*)", r'\1', line + ) + line = re.sub(r"\[0m", "", line) output.innerHTML += line + "\n" diff --git a/style.css b/style.css index 984401d..e3360f8 100644 --- a/style.css +++ b/style.css @@ -6,11 +6,16 @@ body { overflow: hidden; } -.container { +#tpm-type { + grid-column: 1 / 3; +} + +.container-fluid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 3fr); - column-gap: 2.5vh; - padding: 2.5vh; + column-gap: 1.5vh; + row-gap: 1.5vh; + padding: 1.5vh; box-sizing: border-box; }