Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import bz2
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
if file
|
| 6 |
return "", "", ""
|
| 7 |
|
| 8 |
try:
|
| 9 |
with open(file.name, "rb") as f:
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
hex_original = f"Оригинал (первые 256 байт):\n{hex_dump_original}"
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
if magic != b'OPS1':
|
| 19 |
-
return hex_original, "[Ошибка] Неверная сигнатура файла.", ""
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
except Exception as e:
|
| 28 |
-
return hex_original, f"[Ошибка bzip2]: {e}", ""
|
| 29 |
|
| 30 |
-
# HEX дамп
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
try:
|
| 36 |
-
text_output = decompressed.decode("utf-8", errors="replace")
|
| 37 |
-
except Exception as e:
|
| 38 |
-
text_output = f"[Ошибка декодирования]: {e}"
|
| 39 |
-
|
| 40 |
-
return hex_original, hex_decomp_view, text_output
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
-
return f"[Ошибка
|
| 44 |
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
-
gr.Markdown("## Распаковка и
|
| 47 |
|
| 48 |
file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
hex_decomp = gr.Textbox(label="HEX дамп распакованных данных", lines=10)
|
| 52 |
-
text_output = gr.Textbox(label="Текст / частично читаемые данные", lines=15)
|
| 53 |
|
| 54 |
file_input.change(
|
| 55 |
-
fn=
|
| 56 |
inputs=file_input,
|
| 57 |
-
outputs=[
|
| 58 |
)
|
| 59 |
|
| 60 |
file_input.clear(
|
| 61 |
-
fn=lambda: ("", ""
|
| 62 |
-
outputs=[
|
| 63 |
)
|
| 64 |
|
| 65 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import bz2
|
| 3 |
+
from bson import BSON
|
| 4 |
|
| 5 |
+
def parse_cps_bson(file):
|
| 6 |
+
if not file:
|
| 7 |
return "", "", ""
|
| 8 |
|
| 9 |
try:
|
| 10 |
with open(file.name, "rb") as f:
|
| 11 |
+
raw = f.read()
|
| 12 |
|
| 13 |
+
if raw[:4] != b'OPS1':
|
| 14 |
+
return "Ошибка: неверная сигнатура файла", "", ""
|
|
|
|
| 15 |
|
| 16 |
+
compressed = raw[12:]
|
| 17 |
+
decompressed = bz2.decompress(compressed)
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Парсим BSON
|
| 20 |
+
bson_data = BSON(decompressed)
|
| 21 |
+
decoded = bson_data.decode()
|
| 22 |
|
| 23 |
+
# Выводим красиво
|
| 24 |
+
import json
|
| 25 |
+
json_str = json.dumps(decoded, indent=2, ensure_ascii=False)
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# HEX дамп
|
| 28 |
+
hex_dump = ' '.join(f'{b:02x}' for b in decompressed[:512])
|
| 29 |
+
hex_view = f"Распакованные данные (первые 512 байт):\n{hex_dump}"
|
| 30 |
|
| 31 |
+
return hex_view, json_str, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
+
return "", f"[Ошибка]: {e}", ""
|
| 35 |
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("## Распаковка и парсинг .cps (BSON)")
|
| 38 |
|
| 39 |
file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
|
| 40 |
+
hex_output = gr.Textbox(label="HEX дамп распакованных данных", lines=8)
|
| 41 |
+
json_output = gr.Textbox(label="Структура BSON (JSON)", lines=20)
|
|
|
|
|
|
|
| 42 |
|
| 43 |
file_input.change(
|
| 44 |
+
fn=parse_cps_bson,
|
| 45 |
inputs=file_input,
|
| 46 |
+
outputs=[hex_output, json_output]
|
| 47 |
)
|
| 48 |
|
| 49 |
file_input.clear(
|
| 50 |
+
fn=lambda: ("", ""),
|
| 51 |
+
outputs=[hex_output, json_output]
|
| 52 |
)
|
| 53 |
|
| 54 |
demo.launch()
|