forked from facelessuser/HexViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex_writer.py
executable file
·109 lines (94 loc) · 3.66 KB
/
hex_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'''
Hex Viewer
Licensed under MIT
Copyright (c) 2011 Isaac Muse <[email protected]>
'''
import sublime
import sublime_plugin
from os.path import dirname, exists
import re
from hex_common import *
from hex_checksum import checksum
USE_CHECKSUM_ON_SAVE = True
class HexWriterCommand(sublime_plugin.WindowCommand):
export_path = ""
handshake = -1
def is_enabled(self):
return is_enabled()
def export_panel(self):
self.window.show_input_panel(
"Export To:",
self.export_path,
self.prepare_export,
None,
self.reset
)
def overwrite(self, value):
if value.strip().lower() == "yes":
self.export()
else:
self.export_path = self.view.settings().get("hex_viewer_file_name")
self.export_panel()
def prepare_export(self, file_path):
self.export_path = file_path
if exists(dirname(file_path)):
if exists(file_path):
self.window.show_input_panel(
"Overwrite File? (yes | no):",
"no",
self.overwrite,
None,
self.reset
)
else:
self.export()
else:
sublime.error_message("Directory does not exist!")
self.export_path = self.view.settings().get("hex_viewer_file_name")
self.export_panel()
def export(self):
self.view = self.window.active_view()
if self.handshake != -1 and self.handshake == self.view.id():
hex_hash = None
# Get checksum if required
if hv_settings.get("checksum_on_save", USE_CHECKSUM_ON_SAVE):
hex_hash = checksum()
try:
with open(self.export_path, "wb") as bin:
r_buffer = self.view.split_by_newlines(sublime.Region(0, self.view.size()))
h_buffer = []
for line in r_buffer:
hex_data = re.sub(r'[\da-z]{8}:[\s]{2}((?:[\da-z]+[\s]{1})*)\s*\:[\w\W]*', r'\1', self.view.substr(line)).replace(" ", "").decode("hex")
bin.write(hex_data)
if hex_hash != None:
h_buffer.append(hex_data)
if hex_hash != None:
# Checksum will be threaded and will show the result when done
sublime.set_timeout(lambda: sublime.status_message("Checksumming..."), 0)
hex_hash.threaded_update(h_buffer)
except:
sublime.error_message("Failed to export to " + self.export_path)
self.reset()
return
# Update the tab name
self.view.set_name(basename(self.export_path) + ".hex")
# Update the internal path
self.view.settings().set("hex_viewer_file_name", self.export_path)
# Clear the marked edits
clear_edits(self.view)
# Reset class
self.reset()
else:
sublime.error_message("Hex view is no longer in focus! File not saved.")
self.reset()
def reset(self):
self.export_path = ""
self.handshake = -1
def run(self):
self.view = self.window.active_view()
# Identify view
if self.handshake != -1 and self.handshake == self.view.id():
self.reset()
self.handshake = self.view.id()
self.export_path = self.view.settings().get("hex_viewer_file_name")
self.export_panel()