コード例 #1
0
ファイル: hex_highlighter.py プロジェクト: orcinus/HexViewer
    def init(self):
        """Initialize."""

        init_status = False
        self.address_done = False
        self.total_bytes = 0
        self.address = []
        self.selected_bytes = []
        self.hex_lower = common.use_hex_lowercase()

        # Get Seetings from settings file
        group_size = self.view.settings().get("hex_viewer_bits", None)
        self.inspector_enabled = common.hv_settings("inspector", False)
        self.throttle = common.hv_settings("highlight_throttle", THROTTLING)
        self.max_highlight = common.hv_settings("highlight_max_bytes", MAX_HIGHIGHT)
        self.bytes_wide = self.view.settings().get("hex_viewer_actual_bytes", None)
        self.highlight_scope = common.hv_settings("highlight_scope", HIGHLIGHT_SCOPE)
        self.highlight_icon = common.hv_settings("highlight_icon", HIGHLIGHT_ICON)
        self.enable_fake_hex = common.hv_settings("enable_fake_hex_file", True)
        style = common.hv_settings("highlight_style", HIGHLIGHT_STYLE)

        if (group_size is None or self.bytes_wide is None) and self.enable_fake_hex:
            m = re.match(r'([\da-z]{8}):[\s]{2}((?:[\da-z]+[\s]{1})*)\s*\:[\w\W]*', self.view.substr(self.view.line(0)))
            if m is not None:
                starting_address = int(m.group(1), 16)
                hex_chars = m.group(2).split(' ')
                group_size = (len(hex_chars[0]) / 2) * 8
                self.bytes_wide = (len(hex_chars[0]) / 2) * (len(hex_chars) - 1)
                self.view.settings().set("hex_viewer_bits", group_size)
                self.view.settings().set("hex_viewer_actual_bytes", self.bytes_wide)
                self.view.settings().set("hex_viewer_fake", True)
                self.view.settings().set("hex_viewer_starting_address", starting_address)
                self.view.set_read_only(True)
                self.view.set_scratch(True)
                if common.hv_settings("inspector", False) and common.hv_settings("inspector_auto_show", False):
                    self.view.window().run_command("hex_show_inspector")

        # No icon?
        if self.highlight_icon == "none":
            self.highlight_icon = ""

        # Process highlight style
        self.highlight_style = 0
        if style == "outline":
            self.highlight_style = sublime.DRAW_OUTLINED
        elif style == "none":
            self.highlight_style = sublime.HIDDEN
        elif style == "underline":
            self.highlight_style = sublime.DRAW_EMPTY_AS_OVERWRITE

        # Process hex grouping
        if group_size is not None and self.bytes_wide is not None:
            self.group_size = group_size / common.BITS_PER_BYTE
            self.hex_char_range = common.get_hex_char_range(self.group_size, self.bytes_wide)
            init_status = True
        return init_status
コード例 #2
0
    def init(self):
        """Initialize."""

        init_status = False
        self.address_done = False
        self.total_bytes = 0
        self.address = []
        self.selected_bytes = []
        self.hex_lower = common.use_hex_lowercase()

        # Get Seetings from settings file
        group_size = self.view.settings().get("hex_viewer_bits", None)
        self.inspector_enabled = common.hv_settings("inspector", False)
        self.throttle = common.hv_settings("highlight_throttle", THROTTLING)
        self.max_highlight = common.hv_settings("highlight_max_bytes", MAX_HIGHIGHT)
        self.bytes_wide = self.view.settings().get("hex_viewer_actual_bytes", None)
        self.highlight_scope = common.hv_settings("highlight_scope", HIGHLIGHT_SCOPE)
        self.highlight_icon = common.hv_settings("highlight_icon", HIGHLIGHT_ICON)
        self.enable_fake_hex = common.hv_settings("enable_fake_hex_file", True)
        style = common.hv_settings("highlight_style", HIGHLIGHT_STYLE)

        if (group_size is None or self.bytes_wide is None) and self.enable_fake_hex:
            m = re.match(r'([\da-z]{8}):[\s]{2}((?:[\da-z]+[\s]{1})*)\s*\:[\w\W]*', self.view.substr(self.view.line(0)))
            if m is not None:
                starting_address = int(m.group(1), 16)
                hex_chars = m.group(2).split(' ')
                group_size = (len(hex_chars[0]) / 2) * 8
                self.bytes_wide = (len(hex_chars[0]) / 2) * (len(hex_chars) - 1)
                self.view.settings().set("hex_viewer_bits", group_size)
                self.view.settings().set("hex_viewer_actual_bytes", self.bytes_wide)
                self.view.settings().set("hex_viewer_fake", True)
                self.view.settings().set("hex_viewer_starting_address", starting_address)
                self.view.set_read_only(True)
                self.view.set_scratch(True)
                if common.hv_settings("inspector", False) and common.hv_settings("inspector_auto_show", False):
                    self.view.window().run_command("hex_show_inspector")

        # No icon?
        if self.highlight_icon == "none":
            self.highlight_icon = ""

        # Process highlight style
        self.highlight_style = 0
        if style == "outline":
            self.highlight_style = sublime.DRAW_OUTLINED
        elif style == "none":
            self.highlight_style = sublime.HIDDEN
        elif style == "underline":
            self.highlight_style = sublime.DRAW_EMPTY_AS_OVERWRITE

        # Process hex grouping
        if group_size is not None and self.bytes_wide is not None:
            self.group_size = group_size / common.BITS_PER_BYTE
            self.hex_char_range = common.get_hex_char_range(self.group_size, self.bytes_wide)
            init_status = True
        return init_status
コード例 #3
0
    def display(self, window=None):
        """Display hash."""

        if window is None:
            window = sublime.active_window()
        if common.use_hex_lowercase():
            digest = str(self.hash.hexdigest())
        else:
            digest = str(self.hash.hexdigest()).upper()
        window.show_input_panel(self.name + ":", digest, None, None, None)
コード例 #4
0
ファイル: hex_checksum.py プロジェクト: orcinus/HexViewer
    def display(self, window=None):
        """Display hash."""

        if window is None:
            window = sublime.active_window()
        if common.use_hex_lowercase():
            digest = str(self.hash.hexdigest())
        else:
            digest = str(self.hash.hexdigest()).upper()
        window.show_input_panel(self.name + ":", digest, None, None, None)
コード例 #5
0
ファイル: hex_viewer.py プロジェクト: orcinus/HexViewer
    def __init__(self, file_name, bytes_wide, group_size, starting_address=0):
        """Initialize."""

        self.starting_address = starting_address
        self.bytes_wide = int(bytes_wide)
        self.group_size = int(group_size)
        self.file_name = file_name
        self.file_size = get_file_size(file_name)
        self.read_count = 0
        self.abort = False
        self.hex_lower = common.use_hex_lowercase()
        threading.Thread.__init__(self)
コード例 #6
0
ファイル: hex_viewer.py プロジェクト: skeptycal/HexViewer
    def __init__(self, file_name, bytes_wide, group_size, starting_address=0):
        """Initialize."""

        self.starting_address = starting_address
        self.bytes_wide = int(bytes_wide)
        self.group_size = int(group_size)
        self.file_name = file_name
        self.file_size = get_file_size(file_name)
        self.read_count = 0
        self.abort = False
        self.hex_lower = common.use_hex_lowercase()
        threading.Thread.__init__(self)
コード例 #7
0
ファイル: hex_editor.py プロジェクト: vrosnet/HexViewer
    def apply_edit(self, value):
        """Apply edits."""

        edits = ""
        self.view = self.window.active_view()
        # Is this the same view as earlier?
        if self.handshake != -1 and self.handshake == self.view.id():
            total_chars = self.total_bytes * 2
            selection = self.line["selection"].replace(" ", "")

            # Transform string if provided
            if re.match("^s\:", value) is not None:
                edits = hexlify(value[2:len(value)].encode("ascii")).decode("ascii")
            elif common.use_hex_lowercase():
                edits = value.replace(" ", "").lower()
            else:
                edits = value.replace(" ", "").upper()

            # See if change occured and if changes are valid
            if len(edits) != total_chars:
                self.edit_panel(value, "Unexpected # of bytes!")
                return
            elif re.match("[\da-fA-F]{" + str(total_chars) + "}", edits) is None:
                self.edit_panel(value, "Invalid data!")
                return
            elif selection != edits:
                # Get previous dirty markers before modifying buffer
                regions = self.view.get_regions("hex_edit")

                # Construct old and new data for diffs
                edits = self.line["data1"] + edits + self.line["data2"]
                original = self.line["data1"] + selection + self.line["data2"]

                # Initialize
                ascii_str = " :"
                start = 0
                ascii_start_pos = self.ascii_pos
                hex_start_pos = self.line["range"].begin() + common.ADDRESS_OFFSET
                end = len(edits)
                count = 1
                change_start = None

                # Reconstruct line
                l_buffer = self.line["address"]
                while start < end:
                    byte_end = start + 2
                    value = edits[start:byte_end]

                    # Diff data and mark changed bytes
                    if value != original[start:byte_end]:
                        if change_start is None:
                            change_start = [hex_start_pos, ascii_start_pos]
                            # Check if group end
                            if count == self.group_size:
                                regions.append(sublime.Region(change_start[0], hex_start_pos + 2))
                                change_start[0] = None
                        else:
                            # Check if after group end
                            if change_start[0] is None:
                                change_start[0] = hex_start_pos
                            # Check if group end
                            if count == self.group_size:
                                regions.append(sublime.Region(change_start[0], hex_start_pos + 2))
                                change_start[0] = None
                    elif change_start is not None:
                        if self.view.score_selector(hex_start_pos - 1, 'raw.nibble.lower'):
                            if change_start[0] is not None:
                                regions.append(sublime.Region(change_start[0], hex_start_pos))
                        else:
                            if change_start[0] is not None:
                                regions.append(sublime.Region(change_start[0], hex_start_pos - 1))
                        regions.append(sublime.Region(change_start[1], ascii_start_pos))
                        change_start = None

                    # Write bytes and add space and at group region end
                    l_buffer += value
                    if count == self.group_size:
                        l_buffer += " "
                        hex_start_pos += 1
                        count = 0

                    # Copy valid printible ascii chars over or substitute with "."
                    dec = unpack("=B", unhexlify(value))[0]
                    ascii_str += chr(dec) if dec in range(32, 127) else "."
                    start += 2
                    count += 1
                    hex_start_pos += 2
                    ascii_start_pos += 1

                # Check for end of line case for highlight
                if change_start is not None:
                    if change_start[0] is not None:
                        regions.append(sublime.Region(change_start[0], hex_start_pos))
                    regions.append(sublime.Region(change_start[1], ascii_start_pos))
                    change_start = None

                # Append ascii chars to line accounting for missing bytes in line
                delta = int(self.bytes_wide) - len(edits) / 2
                group_space = int(delta / self.group_size) + (1 if delta % self.group_size else 0)
                l_buffer += " " * int(group_space + delta * 2) + ascii_str

                # Apply buffer edit
                self.view.sel().clear()
                self.view.set_read_only(False)
                HexEditGlobal.bfr = l_buffer
                HexEditGlobal.region = self.line["range"]
                self.view.run_command("hex_edit_apply")
                HexEditGlobal.clear()
                self.view.set_read_only(True)
                self.view.sel().add(sublime.Region(self.start_pos, self.end_pos))

                # Underline if required
                if self.highlight_style == sublime.DRAW_EMPTY_AS_OVERWRITE:
                    regions = common.underline(regions)

                # Highlight changed bytes
                self.view.add_regions(
                    "hex_edit",
                    regions,
                    self.highlight_scope,
                    self.highlight_icon,
                    self.highlight_style
                )

                # Update selection
                self.window.run_command('hex_highlighter')
        else:
            error("Hex view is no longer in focus! Edit Failed.")
        # Clean up
        self.reset()