SnowFlash383935 commited on
Commit
3faccae
·
verified ·
1 Parent(s): 52e9009

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -37
app.py CHANGED
@@ -1,65 +1,54 @@
1
  import gradio as gr
2
  import bz2
 
3
 
4
- def analyze_cps(file):
5
- if file is None:
6
  return "", "", ""
7
 
8
  try:
9
  with open(file.name, "rb") as f:
10
- raw_data = f.read()
11
 
12
- # HEX дамп оригинального файла
13
- hex_dump_original = ' '.join(f'{byte:02x}' for byte in raw_data[:256])
14
- hex_original = f"Оригинал (первые 256 байт):\n{hex_dump_original}"
15
 
16
- # Проверка сигнатуры
17
- magic = raw_data[:4]
18
- if magic != b'OPS1':
19
- return hex_original, "[Ошибка] Неверная сигнатура файла.", ""
20
 
21
- # Пропускаем заголовок (12 байт)
22
- compressed_data = raw_data[12:]
 
23
 
24
- # Распаковка
25
- try:
26
- decompressed = bz2.decompress(compressed_data)
27
- except Exception as e:
28
- return hex_original, f"[Ошибка bzip2]: {e}", ""
29
 
30
- # HEX дамп распакованных данных
31
- hex_decompressed = ' '.join(f'{byte:02x}' for byte in decompressed[:512])
32
- hex_decomp_view = f"Распакованные данные (первые 512 байт):\n{hex_decompressed}"
33
 
34
- # Попробуем декодировать как UTF-8
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"[Ошибка чтения файла]: {e}", "", ""
44
 
45
  with gr.Blocks() as demo:
46
- gr.Markdown("## Распаковка и анализ .cps файла (The Powder Toy)")
47
 
48
  file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
49
-
50
- hex_original = gr.Textbox(label="HEX дамп оригинального файла", lines=5)
51
- hex_decomp = gr.Textbox(label="HEX дамп распакованных данных", lines=10)
52
- text_output = gr.Textbox(label="Текст / частично читаемые данные", lines=15)
53
 
54
  file_input.change(
55
- fn=analyze_cps,
56
  inputs=file_input,
57
- outputs=[hex_original, hex_decomp, text_output]
58
  )
59
 
60
  file_input.clear(
61
- fn=lambda: ("", "", ""),
62
- outputs=[hex_original, hex_decomp, text_output]
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()