Plugin example

import hexedit
import math

BLOCK_SIZE = 256

def run_entropy_map():
    size = hexedit.file_size()
    blocks = size // BLOCK_SIZE

    hexedit.set_status(f"Analyzing entropy ({blocks} blocks)...")

    for i in range(blocks):
        offset = i * BLOCK_SIZE
        data = hexedit.read_bytes(offset, BLOCK_SIZE)

        freq = [0] * 256
        for b in data:
            freq[b] += 1

        entropy = 0.0
        for f in freq:
            if f > 0:
                p = f / BLOCK_SIZE
                entropy -= p * math.log2(p)

        r = int(entropy / 8.0 * 255)
        g = int((1.0 - entropy / 8.0) * 255)
        color = f"{r:02x}{g:02x}44"

        hexedit.add_tag(
            name=f"Block {i} (entropy: {entropy:.2f})",
            offset=offset,
            length=BLOCK_SIZE,
            type="blob",
            color=color,
        )

hexedit.register_command(
    id="plugin.entropy_map",
    name="Plugin: Entropy Map",
    handler=run_entropy_map,
)