forked from mmcloughlin/avo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
133 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package asmdb provides access to the asmjit opcodes database. | ||
package asmdb |
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,117 @@ | ||
package asmdb | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// Extension specifies an architecture extension. | ||
type Extension struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
// Attribute is specifies an instruction attribute. | ||
type Attribute struct { | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
Doc string `json:"doc"` | ||
} | ||
|
||
// SpecialReg specifies a special register or flag. | ||
type SpecialReg struct { | ||
Name string `json:"name"` | ||
Group string `json:"group"` | ||
Doc string `json:"doc"` | ||
} | ||
|
||
// Shortcut specifies a shorthand for a collection of attributes. | ||
type Shortcut struct { | ||
Name string `json:"name"` | ||
Expand string `json:"expand"` | ||
} | ||
|
||
// Register defines a class of registers. | ||
type Register struct { | ||
Kind string `json:"kind"` | ||
Any string `json:"any"` | ||
Names []string `json:"names"` | ||
} | ||
|
||
// Raw is an asmdb instruction database in its unprocessed form. | ||
type Raw struct { | ||
Architectures []string `json:"architectures"` | ||
Extensions []Extension `json:"extensions"` | ||
Attributes []Attribute `json:"attributes"` | ||
SpecialRegs []SpecialReg `json:"specialRegs"` | ||
Shortcuts []Shortcut `json:"shortcuts"` | ||
Registers map[string]Register `json:"registers"` | ||
Instructions [][]string `json:"instructions"` | ||
} | ||
|
||
// ParseRaw parses the asmdb x86data.js file as a raw unprocessed data structure. | ||
func ParseRaw(r io.Reader) (*Raw, error) { | ||
// Extract the JSON blob from the javascript file. | ||
b, err := extractjson(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Parse JSON. | ||
db := &Raw{} | ||
if err := json.Unmarshal(b, db); err != nil { | ||
return nil, err | ||
} | ||
|
||
return db, nil | ||
} | ||
|
||
// ParseRawFile parses an asmdb x86data.js file. | ||
func ParseRawFile(filename string) (*Raw, error) { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
return ParseRaw(f) | ||
} | ||
|
||
// extractjson extracts the pure JSON part of the x86data.js file. | ||
func extractjson(r io.Reader) ([]byte, error) { | ||
dividers := []string{ | ||
`// ${JSON:BEGIN}`, | ||
`// ${JSON:END}`, | ||
`/*`, | ||
`*/`, | ||
} | ||
s := bufio.NewScanner(r) | ||
var b []byte | ||
take := false | ||
for s.Scan() { | ||
line := s.Text() | ||
|
||
if contains(strings.TrimSpace(line), dividers) { | ||
take = !take | ||
continue | ||
} | ||
|
||
if take { | ||
b = append(b, []byte(line)...) | ||
} | ||
} | ||
if err := s.Err(); err != nil { | ||
return nil, err | ||
} | ||
return b, nil | ||
} | ||
|
||
func contains(needle string, haystack []string) bool { | ||
for _, s := range haystack { | ||
if s == needle { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,13 @@ | ||
package asmdb | ||
|
||
import "testing" | ||
|
||
func TestParseRawFile(t *testing.T) { | ||
db, err := ParseRawFile("testdata/x86data.js") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(db.Instructions) == 0 { | ||
t.FailNow() | ||
} | ||
} |
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 @@ | ||
../data |