Skip to content

Commit

Permalink
adding support to load different types back into corpus (will need to…
Browse files Browse the repository at this point in the history
… add array/enum when supported by codegen

Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed Oct 27, 2021
1 parent b3f998e commit dc6dcfa
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cli/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
// This is just for debugging - it loads and prints the json corpus

import (
"fmt"
"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/vsoch/gosmeagle/corpus"
)
Expand All @@ -30,5 +29,6 @@ func init() {
func RunLoader(r *cmd.Root, c *cmd.Sub) {
args := c.Args.(*LoadArgs)
C := corpus.Load(args.JsonFile[0])
fmt.Println(C)
corp := C.ToCorpus()
corp.ToJson(true)
}
79 changes: 71 additions & 8 deletions corpus/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,48 @@ func Load(filename string) LoadedCorpus {
return corp
}

// loadParameter loads a general parameter
func loadParameter(param interface{}) descriptor.Parameter {

paramClass := param.(map[string]interface{})["class"]

switch paramClass {
case "Pointer":
return loadPointer(param)
case "Struct":
return loadStructure(param)
}

// Integer, Float
return loadFunctionParameter(param)
}

func loadFunctionParameter(param interface{}) descriptor.Parameter {
s := descriptor.FunctionParameter{}
mapstructure.Decode(param, &s)
sizeInt, err := strconv.ParseInt(param.(map[string]interface{})["size"].(string), 10, 64)
if err != nil {
log.Fatalf("Error converting string of size to int64: %x", err)
}
s.Size = sizeInt
return s
}

func loadPointer(param interface{}) descriptor.Parameter {

s := descriptor.PointerParameter{}
mapstructure.Decode(param, &s)
sizeInt, err := strconv.ParseInt(param.(map[string]interface{})["size"].(string), 10, 64)
if err != nil {
log.Fatalf("Error converting string of size to int64: %x", err)
}
indirections, err := strconv.ParseInt(param.(map[string]interface{})["indirections"].(string), 10, 64)
s.UnderlyingType = loadParameter(param.(map[string]interface{})["underlying_type"])
s.Size = sizeInt
s.Indirections = indirections
return s
}

// convertFunctionDescriptor converts to a function descriptor to
func convertFunctionDescriptor(item interface{}) descriptor.FunctionDescription {
desc := descriptor.FunctionDescription{}
Expand All @@ -69,19 +111,40 @@ func convertFunctionDescriptor(item interface{}) descriptor.FunctionDescription
if paramsection != nil {
params := paramsection.([]interface{})
for _, param := range params {
s := descriptor.FunctionParameter{}
mapstructure.Decode(param, &s)
sizeInt, err := strconv.ParseInt(param.(map[string]interface{})["size"].(string), 10, 64)
if err != nil {
log.Fatalf("Error converting string of size to int64: %x", err)
}
s.Size = sizeInt
desc.Parameters = append(desc.Parameters, s)
newParam := loadParameter(param)
desc.Parameters = append(desc.Parameters, newParam)
}
}
return desc
}

// loadStructure will load a structure
func loadStructure(param interface{}) descriptor.Parameter {

s := descriptor.StructureParameter{}
mapstructure.Decode(param, &s)

// If we don't reset, the above will load nils
s.Fields = []descriptor.Parameter{}
sizeInt, err := strconv.ParseInt(param.(map[string]interface{})["size"].(string), 10, 64)
if err != nil {
log.Fatalf("Error converting string of size to int64: %x", err)
}
fieldsraw := param.(map[string]interface{})["fields"]
if fieldsraw != nil {
fields := fieldsraw.([]interface{})
for _, field := range fields {
newField := loadParameter(field)
if newField != nil {
s.Fields = append(s.Fields, newField)
}
}
}
s.Size = sizeInt
return s

}

// convertFunctionDescriptor converts to a function descriptor
func convertVariableDescriptor(item interface{}) descriptor.VariableDescription {
desc := descriptor.VariableDescription{}
Expand Down

0 comments on commit dc6dcfa

Please sign in to comment.