프로그래밍/Io

이진 파일을 읽어서 16진수로 보여주는 HexView 소스 with Io

Scripter 2013. 8. 5. 11:47

 

 

Io 언어 소스:

/*
 * Filename: testHexView_03.io
 *
 *  Purpose:  Show Hexadecimal codes of the given binary file.
 *
 *  Execute: io testHexView_03.io [filename]
 *
 *  Data: 2013. 8. 3.
 */

printUsage := method(
    writeln("Using: io testHexView_02.io [filename]")
)

toHex := method(b,
    s := ""
    x1 := (b & 0xF0) >> 4
    x2 := b & 0x0F
    if (x1 < 10) then (
        s = s .. ((x1 + (("0x" .. "0" asHex) asNumber)) asCharacter)
    ) else (
        s = s .. (((x1 - 10) + (("0x" .. "A" asHex) asNumber)) asCharacter)
    )
    if (x2 < 10) then (
        s = s .. ((x2 + (("0x" .. "0" asHex) asNumber)) asCharacter)
    ) else (
        s = s .. (((x2 - 10) + (("0x" .. "A" asHex) asNumber)) asCharacter)
    )
    // return s
    s
)

toHex8 := method(n,
    s := ""
    x1 := (n & 0xF0000000) >> 28
    x2 := (n & 0xF000000) >> 24
    x3 := (n & 0xF00000) >> 20
    x4 := (n & 0xF0000) >> 16
    x5 := (n & 0xF000) >> 12
    x6 := (n & 0xF0) >> 8
    x7 := (n & 0xF0) >> 4
    x8 := n & 0x0F
    if (x1 < 10) then (
        s = s .. ((x1 + (("0x" .. "0" asHex) asNumber)) asCharacter)
    ) else (
        s = s .. (((x1 - 10) + (("0x" .. "A" asHex) asNumber)) asCharacter)
    )
    if (x2 < 10) then (
        s = s .. ((x2 + (("0x" .. "0" asHex) asNumber)) asCharacter)
    ) else (
        s = s .. (((x2 - 10) + (("0x" .. "A" asHex) asNumber)) asCharacter)
    )
    if (x3 < 10) then (
        s = s .. ((x3 + (("0x" .. "0" asHex) asNumber)) asCharacter)
    ) else (
        s = s .. (((x3 - 10) + (("0x" .. "A" asHex) asNumber)) asCharacter)
    )
    if (x4 < 10) then (
        s = s .. ((x4 + (("0x" .. "0" asHex) asNumber)) asCharacter)
    ) else (
        s = s .. (((x4 - 10) + (("0x" .. "A" asHex) asNumber)) asCharacter)
    )
    s = s .. " "
    if (x5 < 10) then (
        s = s .. ((x5 + (("0x" .. "0" asHex) asNumber)) asCharacter)
    ) else (
        s = s .. (((x5 - 10) + (("0x" .. "A" asHex) asNumber)) asCharacter)
    )
    if (x6 < 10) then (
        s = s .. ((x6 + (("0x" .. "0" asHex) asNumber)) asCharacter)
    ) else (
        s = s .. (((x6 - 10) + (("0x" .. "A" asHex) asNumber)) asCharacter)
    )
    if (x7 < 10) then (
        s = s .. ((x7 + (("0x" .. "0" asHex) asNumber)) asCharacter)
    ) else (
        s = s .. (((x7 - 10) + (("0x" .. "A" asHex) asNumber)) asCharacter)
    )
    if (x8 < 10) then (
        s = s .. ((x8 + (("0x" .. "0" asHex) asNumber)) asCharacter)
    ) else (
        s = s .. (((x8 - 10) + (("0x" .. "A" asHex) asNumber)) asCharacter)
    )
    // return s
    s
)


args := System args

if (args size  < 2) then (
    printUsage()
    exit(1)
) else (
    fname := args at(1)

    if (File exists(fname) == false) then (
        ("The file \"" .. fname .. "\" does not exist.") println
        return 1
    )


    ff := File with(fname)

    if (ff isDirectory) then (
        ("The file \"" .. fname .. "\" is a directory.") println
        return 1
    )

    infile2 := ff openForReading

    fsize := infile2 size

    if (fsize > 0) then (
        ("The size of the file \"" .. fname .. "\" is " .. fsize .. ".") println
        "" println
    ) else (
        exit(1)
    )

    n := 0
    dum := ""

    while ( a := infile2 readBufferOfLength(1),
        if (n % 16 == 0) then (
            write(toHex8(n) .. ": ")
        )
             
        if (n % 16 != 8) then (
            write(" ")
        ) else (
            write("-")
        )

        write( toHex(a at (0)) )

        if ((a at(0) < " " at (0)) or ((a at (0) & 0x80) != 0)) then (
            dum = dum .. "."
        ) else (
            dum = dum .. ((a at(0)) asCharacter)
        )
               
        if (n > 0 and n % 16 == 15) then (
            write("  |" .. dum .. "|\n")
            dum = ""
        )

        n = n + 1
    )

    if (n > 0 and n % 16 > 0) then (
        for (i, 1,  16 - (fsize % 16),
            write("   ")
        )
        write("  |" .. dum)
        for (i, 1,  16 - (fsize % 16),
            write(" ")
        )
        write("|\n")
        dum = ""
    )

    infile2 close
    ("\nRead " .. n .. " bytes.") println
)

 

 

실행하기 예 1> io testHexView_03.io temp_1.bin
The size of the file "temp_1.bin" is 12.

0000 0000:  48 65 6C 6C 6F 20 74 68-65 72 65 0A              |Hello there.    |

Read 12 bytes.

 

실행하기 예 2> io testHexView_03.io myFile.ser
The size of the file "myFile.ser" is 130.

0000 0000:  AC ED 00 05 73 72 00 06-50 65 72 73 6F 6E 07 31  |....sr..Person.1|
0000 0010:  46 DB A5 1D 44 AB 02 00-03 49 00 03 61 67 65 4C  |F...D....I..ageL|
0000 0020:  00 09 66 69 72 73 74 4E-61 6D 65 74 00 12 4C 6A  |..firstNamet..Lj|
0000 0030:  61 76 61 2F 6C 61 6E 67-2F 53 74 72 69 6E 67 3B  |ava/lang/String;|
0000 0040:  4C 00 08 6C 61 73 74 4E-61 6D 65 71 00 7E 00 01  |L..lastNameq.~..|
0000 0050:  78 70 00 00 00 13 74 00-05 4A 61 6D 65 73 74 00  |xp....t..Jamest.|
0000 0060:  04 52 79 61 6E 73 71 00-7E 00 00 00 00 00 1E 74  |.Ryansq.~......t|
0000 0070:  00 07 4F 62 69 2D 77 61-6E 74 00 06 4B 65 6E 6F  |..Obi-want..Keno|
0000 0080:  62 69                                            |bi              |

Read 130 bytes.