Skip to content

Commit

Permalink
chore: Update Keystone engine import paths and constants for ARM32 an…
Browse files Browse the repository at this point in the history
…d ARM64 architectures
  • Loading branch information
suifei committed May 22, 2024
1 parent 0ef9e62 commit 7619765
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Website = "https://github.com/suifei/asm2hex"
Name = "ASM to HEX Converter"
ID = "suifei.asm2hex.app"
Version = "1.0.0"
Build = 30
Build = 34
18 changes: 15 additions & 3 deletions archs/arm32.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package archs

import (
"fmt"
"strings"

"github.com/suifei/asm2hex/bindings/keystone"
"github.com/suifei/asm2hex/bindings/capstone"
"github.com/suifei/asm2hex/bindings/keystone"
)

func Arm32Disasm(encoding []byte, offset uint64, bigEndian bool) (code string, stat_count uint64, ok bool, err error) {
Expand All @@ -19,7 +20,7 @@ func Arm32Disasm(encoding []byte, offset uint64, bigEndian bool) (code string, s

engine, err := capstone.New(capstone.ARCH_ARM, capstone.MODE_ARM)
if err == nil {
engine.Option(capstone.OPT_TYPE_SYNTAX, capstone.OPT_SYNTAX_ATT)
// engine.Option(capstone.OPT_TYPE_SYNTAX, capstone.OPT_SYNTAX_ATT)
defer engine.Close()

if bigEndian {
Expand All @@ -29,7 +30,7 @@ func Arm32Disasm(encoding []byte, offset uint64, bigEndian bool) (code string, s

if err == nil {
for _, insn := range insns {
code += fmt.Sprintf("%08X:\t%-6s\t%-20s\n", insn.Addr(), insn.Mnemonic(), insn.OpStr())
code += fmt.Sprintf("%-6s\t%-20s\t;%08X\n", insn.Mnemonic(), insn.OpStr(), insn.Addr())
}
stat_count = uint64(len(insns))
ok = true
Expand All @@ -50,6 +51,17 @@ func Arm32(code string, offset uint64, bigEndian bool) (encoding []byte, stat_co
}
}()

code = strings.TrimSpace(code)
if code == "" {
return encoding, stat_count, ok, fmt.Errorf("Empty code")
}
if strings.HasPrefix(code, ";") {
return encoding, stat_count, ok, fmt.Errorf("Commented code")
}
if strings.Index(code, ";") > 0 {
code = strings.Split(code, ";")[0]
}

var ks *keystone.Keystone
ks, _ = keystone.New(keystone.ARCH_ARM, keystone.MODE_ARM)
// ks.Option(keystone.OPT_SYNTAX, keystone.OPT_SYNTAX_GAS)
Expand Down
17 changes: 14 additions & 3 deletions archs/arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package archs

import (
"fmt"
"strings"

"github.com/suifei/asm2hex/bindings/keystone"
"github.com/suifei/asm2hex/bindings/capstone"
"github.com/suifei/asm2hex/bindings/keystone"
)

func Arm64Disasm(encoding []byte, offset uint64, bigEndian bool) (code string, stat_count uint64, ok bool, err error) {
Expand All @@ -19,14 +20,14 @@ func Arm64Disasm(encoding []byte, offset uint64, bigEndian bool) (code string, s

engine, err := capstone.New(capstone.ARCH_ARM64, capstone.MODE_LITTLE_ENDIAN)
if err == nil {
engine.Option(capstone.OPT_TYPE_SYNTAX, capstone.OPT_SYNTAX_ATT)
// engine.Option(capstone.OPT_TYPE_SYNTAX, capstone.OPT_SYNTAX_ATT)
defer engine.Close()

insns, err := engine.Dis(encoding, offset, 0)

if err == nil {
for _, insn := range insns {
code += fmt.Sprintf("%08X:\t%-6s\t%-20s\n", insn.Addr(), insn.Mnemonic(), insn.OpStr())
code += fmt.Sprintf("%-6s\t%-20s\t;%08X\n", insn.Mnemonic(), insn.OpStr(), insn.Addr())
}
stat_count = uint64(len(insns))
ok = true
Expand All @@ -45,6 +46,16 @@ func Arm64(code string, offset uint64, bigEndian bool) (encoding []byte, stat_co
return
}
}()
code = strings.TrimSpace(code)
if code == "" {
return encoding, stat_count, ok, fmt.Errorf("Empty code")
}
if strings.HasPrefix(code, ";") {
return encoding, stat_count, ok, fmt.Errorf("Commented code")
}
if strings.Index(code, ";") > 0 {
code = strings.Split(code, ";")[0]
}

var ks *keystone.Keystone
ks, _ = keystone.New(keystone.ARCH_ARM64, keystone.MODE_LITTLE_ENDIAN)
Expand Down
17 changes: 14 additions & 3 deletions archs/thumb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package archs

import (
"fmt"
"strings"

"github.com/suifei/asm2hex/bindings/keystone"
"github.com/suifei/asm2hex/bindings/capstone"
"github.com/suifei/asm2hex/bindings/keystone"
)

func ThumbDisasm(encoding []byte, offset uint64, bigEndian bool) (code string, stat_count uint64, ok bool, err error) {
Expand All @@ -19,7 +20,7 @@ func ThumbDisasm(encoding []byte, offset uint64, bigEndian bool) (code string, s

engine, err := capstone.New(capstone.ARCH_ARM, capstone.MODE_THUMB)
if err == nil {
engine.Option(capstone.OPT_TYPE_SYNTAX, capstone.OPT_SYNTAX_ATT)
// engine.Option(capstone.OPT_TYPE_SYNTAX, capstone.OPT_SYNTAX_ATT)
defer engine.Close()
if bigEndian {
encoding = bigEndian16Bytes(encoding)
Expand All @@ -28,7 +29,7 @@ func ThumbDisasm(encoding []byte, offset uint64, bigEndian bool) (code string, s

if err == nil {
for _, insn := range insns {
code += fmt.Sprintf("%08X:\t%-6s\t%-20s\n", insn.Addr(), insn.Mnemonic(), insn.OpStr())
code += fmt.Sprintf("%-6s\t%-20s\t;%08X\n", insn.Mnemonic(), insn.OpStr(), insn.Addr())
}
stat_count = uint64(len(insns))
ok = true
Expand All @@ -48,6 +49,16 @@ func Thumb(code string, offset uint64, bigEndian bool) (encoding []byte, stat_co
return
}
}()
code = strings.TrimSpace(code)
if code == "" {
return encoding, stat_count, ok, fmt.Errorf("Empty code")
}
if strings.HasPrefix(code, ";") {
return encoding, stat_count, ok, fmt.Errorf("Commented code")
}
if strings.Index(code, ";") > 0 {
code = strings.Split(code, ";")[0]
}

var ks *keystone.Keystone
ks, _ = keystone.New(keystone.ARCH_ARM, keystone.MODE_THUMB)
Expand Down
6 changes: 0 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func hexdump(buf []byte) string {
}
var hexStr string
for _, b := range buf {
// 16进制转换为字符串,保持4位长度,大写,不足补0
hexStr += fmt.Sprintf("%02X", b)
}
if prefix_hex {
Expand All @@ -64,15 +63,10 @@ func hexdump(buf []byte) string {
func hexStringsToBytes(hexStrings []string) ([]byte, error) {
var result []byte
for _, hexStr := range hexStrings {
// 移除字符串中的空格
hexStr = strings.ReplaceAll(hexStr, " ", "")

// 检查字符串长度是否为偶数
if len(hexStr)%2 != 0 {
return nil, fmt.Errorf("invalid hex string length: %s", hexStr)
}

// 解析每个字节
for i := 0; i < len(hexStr); i += 2 {
b, err := strconv.ParseUint(hexStr[i:i+2], 16, 8)
if err != nil {
Expand Down

0 comments on commit 7619765

Please sign in to comment.