Skip to content

Commit

Permalink
type: add type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
joholl committed Jul 1, 2023
1 parent b2d8939 commit 3d12d60
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 29 deletions.
20 changes: 12 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@
<head>
<title>tpmstream</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<link rel="stylesheet" href="./style.css" />
</head>

<body>
<div class="container">
<py-config>
packages = ["tpmstream"]
</py-config>
<py-script src="./main.py"></py-script>

<div class="container-fluid">
<select id="tpm-type" placeholder="Detect..." py-change="on_select()" class="form-select" aria-label="Default select example">
<option value="">Detect...</option>
</select>
<div class="input-area">
<textarea id="in" placeholder="Enter hexadecimal..." py-input="convert()"></textarea>
<textarea id="in" placeholder="Enter hexadecimal..." py-input="on_input()"></textarea>
</div>
<div class="output-area">
<div id="out">
</div>
</div>
</div>

<py-config>
packages = ["tpmstream"]
</py-config>
<py-script src="./main.py"></py-script>
</body>
</html>
84 changes: 66 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
@@ -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'<span class="color-warning">{str(e)}</span>'
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,
)

Expand All @@ -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'<span class="color-name">\1</span>', line)
line = re.sub(r"\[34m([^]*)", r'<span class="color-type">\1</span>', line)
line = re.sub(r"\[33m([^]*)", r'<span class="color-value">\1</span>', line)
line = re.sub(r"\[31m([^]*)", r'<span class="color-warning">\1</span>', line)
line = re.sub(r"\[30m([^]*)", r'<span class="color-indent">\1</span>', line)
line = re.sub(r"\[0m", '', line)
line = re.sub(
r"\[33m([^]*)", r'<span class="color-value">\1</span>', line
)
line = re.sub(
r"\[31m([^]*)", r'<span class="color-warning">\1</span>', line
)
line = re.sub(
r"\[30m([^]*)", r'<span class="color-indent">\1</span>', line
)
line = re.sub(r"\[0m", "", line)
output.innerHTML += line + "\n"
11 changes: 8 additions & 3 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 3d12d60

Please sign in to comment.