Eskil

Artifact [d79ff40087]
Login

Artifact d79ff400877ae9103cd2db0408914b34735d47d9:


##Eskil Plugin : Compare binary files, in hex

# Example file for a plugin.
# A plugin must start exactly like this one.
# The text after : is the summary you can get at the command line

# This plugin converts files to hex to be able to compare binary files.
# A set of chars can be defined to be used as "newline". Default "0 10 13".
# Example usage:
# eskil -plugin binary -plugininfo "0 10 13 32" f1 f2

# A plugin must define this procedure to do the job.
# side: left or right
# chi:  An input channel for reading the original file.
# cho:  An output channel for writing the processed file.
proc PreProcess {side chi cho} {
    if {[catch {llength $::Info}]} {
        puts $cho "Binary plugin needs -plugininfo parameter to be a list"
        return 1
    }
    if {[llength $::Info] > 0} {
        set delimitL $::Info
    } else {
        set delimitL [list 0 10 13]
    }

    # Build an RE that matches the given chars
    set REm "\["
    set REi "\[^"
    foreach code $delimitL {
        set c [format %c $code]
        if {[string is wordchar $c]} {
            append REm $c
            append REi $c
        } else {
            # Just in case it is a special char for RE
            append REm \\ $c
            append REi \\ $c
        }
    }
    append REm "\]"
    append REi "\]"

    set RE $REi*$REm*

    fconfigure $chi -translation binary
    # Assume small enough for memory.
    # A file too large to read would be virtually impossible to display anyway.
    set data [read $chi]
    foreach line [regexp -all -inline $RE $data] {
        puts $cho [strToHex $line]
    }

    # Signal that the file after processing should be used both
    # for comparison and for displaying.
    return 1
}

# Note: With 8.6 there is "binary encode hex" that might be faster

# Build a string to hex mapper for speed
set ::hexCharMap {}
for {set i 0} {$i < 256} {incr i} {
    lappend ::hexCharMap [format %c $i] [format "%02X " $i]
}
proc strToHex {str} {
    string map $::hexCharMap $str
}