Skip to content

Commit

Permalink
aboutcode-org#2 Add binary analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
CatalinStratu committed Aug 2, 2023
1 parent a3530fb commit 4fc9874
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
53 changes: 53 additions & 0 deletions analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package goInspector

import (
"encoding/json"
"github.com/goretk/gore"
"path/filepath"
)

type Vendor struct {
Name string `json:"name"`
}

// InspectLibraries return the list with vendors
func InspectLibraries(filepath string) ([]*gore.Package, error) {
f, err := gore.Open(filepath)
if err != nil {
return nil, err
}

defer func(f *gore.GoFile) {
err := f.Close()
if err != nil {

}
}(f)

std2, err := f.GetVendors()
if err != nil {
return nil, err
}

return std2, nil
}

func ConvertToJSONWithPosixPaths(path string) ([]byte, error) {
vendors, err := InspectLibraries(path)
if err != nil {
return nil, err
}

// Convert the data to use POSIX paths
for i := range vendors {
vendors[i].Name = filepath.ToSlash(vendors[i].Name)
}

// Marshal the data to JSON
dataJSON, err := json.Marshal(vendors)
if err != nil {
return nil, err
}

return dataJSON, nil
}
24 changes: 24 additions & 0 deletions examples/InspectLibraries/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
goInspector "github.com/nexB/go-inspector"
"os"
)

func main() {
args := os.Args
if len(args) != 2 {
fmt.Printf("Usage: %v \n", args[0])
fmt.Printf(" Load executable file file, and\n")
fmt.Printf(" print portions of its creation info data.\n")
return
}
jsonData, err := goInspector.ConvertToJSONWithPosixPaths(args[1])
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}

fmt.Println(string(jsonData))
}
Binary file added examples/files/testfile.exe
Binary file not shown.
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/goretk/gore v0.11.1 h1:QEn9bI0ty//Dtk1sa2yYtssGzZHJIeROehiJEm953DQ=
github.com/goretk/gore v0.11.1/go.mod h1:wZt6TJIR3gZJplzAApeIXA1BjQGd3PKbc2XincS3TAc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
golang.org/x/arch v0.0.0-20220412001346-fc48f9fe4c15 h1:GVfVkciLYxn5mY5EncwAe0SXUn9Rm81rRkZ0TTmn/cU=
golang.org/x/arch v0.0.0-20220412001346-fc48f9fe4c15/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 h1:dbuHpmKjkDzSOMKAWl10QNlgaZUd3V1q99xc81tt2Kc=

0 comments on commit 4fc9874

Please sign in to comment.