-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Update build files and dependencies
- Loading branch information
Showing
14 changed files
with
1,004 additions
and
440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package archs | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/suifei/asm2hex/bindings/capstone" | ||
"github.com/suifei/asm2hex/bindings/keystone" | ||
) | ||
|
||
func Disassemble(arch capstone.Architecture, mode capstone.Mode, code []byte, offset uint64, bigEndian bool /*syntaxValue capstone.OptionValue, */, addAddress bool) (string, uint64, bool, error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
return | ||
} | ||
}() | ||
|
||
engine, err := capstone.New(arch, mode) | ||
if err != nil { | ||
return "", 0, false, err | ||
} | ||
defer engine.Close() | ||
|
||
// if syntaxValue != 0 { | ||
// engine.Option(capstone.OPT_SYNTAX, capstone.OptionValue(syntaxValue)) | ||
// } | ||
|
||
if bigEndian { | ||
code, err = capstoneToBigEndian(code, arch, mode) | ||
if err != nil { | ||
return "", 0, false, err | ||
} | ||
} | ||
|
||
insns, err := engine.Disasm(code, offset, 0) | ||
if err != nil { | ||
return "", 0, false, err | ||
} | ||
|
||
var result string | ||
for _, insn := range insns { | ||
if addAddress { | ||
result += fmt.Sprintf("%08X:\t%-6s\t%-20s\n", insn.Address(), insn.Mnemonic(), insn.OpStr()) | ||
} else { | ||
result += fmt.Sprintf("%-6s\t%-20s\n", insn.Mnemonic(), insn.OpStr()) | ||
} | ||
} | ||
|
||
return result, uint64(len(insns)), true, nil | ||
} | ||
func Assemble(arch keystone.Architecture, mode keystone.Mode, code string, offset uint64, bigEndian bool /*, syntaxValue keystone.OptionValue*/) ([]byte, uint64, bool, error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
return | ||
} | ||
}() | ||
|
||
code = strings.TrimSpace(code) | ||
if code == "" { | ||
return nil, 0, false, fmt.Errorf("empty code") | ||
} | ||
if strings.HasPrefix(code, ";") { | ||
return nil, 0, false, fmt.Errorf("commented code") | ||
} | ||
if idx := strings.Index(code, ";"); idx > 0 { | ||
code = code[:idx] | ||
} | ||
|
||
ks, err := keystone.New(keystone.Architecture(arch), keystone.Mode(mode)) | ||
if err != nil { | ||
return nil, 0, false, err | ||
} | ||
defer ks.Close() | ||
|
||
// if syntaxValue != 0 { | ||
// ks.Option(keystone.OPT_SYNTAX, keystone.OptionValue(syntaxValue)) | ||
// } | ||
|
||
encoding, stat_count, ok := ks.Assemble(code, offset) | ||
if err := ks.LastError(); err != nil { | ||
return nil, 0, false, err | ||
} | ||
|
||
if ok && bigEndian { | ||
encoding, err = keystoneToBigEndian(encoding, arch, mode) | ||
if err != nil { | ||
return nil, 0, false, err | ||
} | ||
} | ||
|
||
return encoding, stat_count, ok, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package archs | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/suifei/asm2hex/bindings/capstone" | ||
"github.com/suifei/asm2hex/bindings/keystone" | ||
) | ||
|
||
func TestDisassemble(t *testing.T) { | ||
code := []byte{0x48, 0x83, 0x3d, 0x01, 0x02, 0x03, 0x04, 0x05} | ||
arch := capstone.ARCH_X86 | ||
mode := capstone.MODE_64 | ||
syntaxValue := capstone.OPT_SYNTAX_INTEL | ||
str, count, ok, err := Disassemble(arch, mode, code, 0x100, false, syntaxValue) | ||
require.NoError(t, err) | ||
require.True(t, ok) | ||
require.Equal(t, 1, int(count)) | ||
require.Equal(t, "cmpq\t$0x4030201020304,0x105\t;0x100\n", str) | ||
} | ||
|
||
func TestAssemble_x86_64_code_big_endian(t *testing.T) { | ||
code := "mov rax, 1" | ||
encoding, count, ok, err := Assemble(keystone.ARCH_X86, keystone.MODE_64, code, 0x100, true, keystone.OPT_SYNTAX_ATT) | ||
assert.Nil(t, err) | ||
assert.Equal(t, uint64(1), count) | ||
assert.True(t, ok) | ||
assert.Equal(t, []byte{0x48, 0xc7, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00}, encoding) | ||
} | ||
|
||
func TestAssemble_x86_64_code_little_endian(t *testing.T) { | ||
code := "mov rax, 1" | ||
encoding, count, ok, err := Assemble(keystone.ARCH_X86, keystone.MODE_64, code, 0x100, false, keystone.OPT_SYNTAX_ATT) | ||
assert.Nil(t, err) | ||
assert.Equal(t, uint64(1), count) | ||
assert.True(t, ok) | ||
assert.Equal(t, []byte{0xc7, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x48}, encoding) | ||
} | ||
|
||
func TestAssemble_x86_32_code_big_endian(t *testing.T) { | ||
code := "mov eax, 1" | ||
encoding, count, ok, err := Assemble(keystone.ARCH_X86, keystone.MODE_32, code, 0x100, true, keystone.OPT_SYNTAX_ATT) | ||
assert.Nil(t, err) | ||
assert.Equal(t, uint64(1), count) | ||
assert.True(t, ok) | ||
assert.Equal(t, []byte{0x66, 0x3d, 0x01, 0x00, 0x00, 0x00}, encoding) | ||
} | ||
|
||
func TestAssemble_x86_32_code_little_endian(t *testing.T) { | ||
code := "mov eax, 1" | ||
encoding, count, ok, err := Assemble(keystone.ARCH_X86, keystone.MODE_32, code, 0x100, false, keystone.OPT_SYNTAX_ATT) | ||
assert.Nil(t, err) | ||
assert.Equal(t, uint64(1), count) | ||
assert.True(t, ok) | ||
assert.Equal(t, []byte{0x01, 0x3d, 0x66, 0x00, 0x00, 0x00}, encoding) | ||
} | ||
|
||
func TestAssemble_ARM_code_big_endian(t *testing.T) { | ||
code := "mov r1, 1" | ||
encoding, count, ok, err := Assemble(keystone.ARCH_ARM, keystone.MODE_ARM, code, 0x100, true, keystone.OPT_SYNTAX_ATT) | ||
assert.Nil(t, err) | ||
assert.Equal(t, uint64(4), count) | ||
assert.True(t, ok) | ||
assert.Equal(t, []byte{0x0, 0x0, 0xa0, 0xe3}, encoding) | ||
} | ||
|
||
func TestAssemble_ARM_code_little_endian(t *testing.T) { | ||
code := "mov r1, 1" | ||
encoding, count, ok, err := Assemble(keystone.ARCH_ARM, keystone.MODE_ARM, code, 0x100, false, keystone.OPT_SYNTAX_ATT) | ||
assert.Nil(t, err) | ||
assert.Equal(t, uint64(4), count) | ||
assert.True(t, ok) | ||
assert.Equal(t, []byte{0xe3, 0xa0, 0x0, 0x0}, encoding) | ||
} | ||
|
||
func TestAssemble_ARM64_code_big_endian(t *testing.T) { | ||
code := "mov w1, 1" | ||
encoding, count, ok, err := Assemble(keystone.ARCH_ARM64, keystone.MODE_ARM, code, 0x100, true, keystone.OPT_SYNTAX_ATT) | ||
assert.Nil(t, err) | ||
assert.Equal(t, uint64(4), count) | ||
assert.True(t, ok) | ||
assert.Equal(t, []byte{0x0, 0x0, 0x20, 0x1f}, encoding) | ||
} | ||
|
||
func TestAssemble_ARM64_code_little_endian(t *testing.T) { | ||
code := "mov w1, 1" | ||
encoding, count, ok, err := Assemble(keystone.ARCH_ARM64, keystone.MODE_ARM, code, 0x100, false, keystone.OPT_SYNTAX_ATT) | ||
assert.Nil(t, err) | ||
assert.Equal(t, uint64(4), count) | ||
assert.True(t, ok) | ||
assert.Equal(t, []byte{0x1f, 0x20, 0x0, 0x0}, encoding) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.