Eskil

Artifact [999684303f]
Login

Artifact 999684303f467cf043ceb86169417b49c604145b:


##Eskil Plugin : Compare binary files, in hex
## Option -binsep : A set of chars to be used as "newline"
#
# 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 -binsep "0 10 13 32" f1 f2

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

# A plugin may declare command line options that should be allowed through
# to ::argv

# 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} {
    set delimitL [list 0 10 13]
    if {[llength $::Info] > 0} {
        set delimitL $::Info
    }
    set i [lsearch -exact $::argv -binsep]
    if {$i >= 0} {
        incr i
        set delimitL [lindex $::argv $i]
    }
    if {[catch {llength $delimitL}]} {
        puts $cho "Binary plugin needs parameter to be a list"
        return 1
    }

    # 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
}