Added command line argument to extract payload

This commit is contained in:
Christopher Ramey 2019-10-06 10:51:41 -08:00 committed by Christopher Ramey
parent 0cb656e659
commit fe886ef655

View File

@ -5,29 +5,52 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"flag"
) )
func main() { func main() {
if len(os.Args) < 2 { flags := flag.NewFlagSet("sdb", flag.ExitOnError)
fmt.Fprintf(os.Stderr, flags.SetOutput(os.Stdout)
"required argument missing\n") flags.Usage = func() {
fmt.Fprintf(flags.Output(),
"Usage: %s [OPTION] <files> ...\nOptions:\n",
os.Args[0])
flags.PrintDefaults()
}
plonly := flags.Bool("payload", false, "only return the payload")
flags.Parse(os.Args[1:])
if flags.NArg() < 1 {
fmt.Fprintf(os.Stderr, "files parameter cannot be empty\n")
os.Exit(1) os.Exit(1)
} }
raw, err := ioutil.ReadFile(os.Args[1]) for _, fn := range flags.Args() {
raw, err := ioutil.ReadFile(fn)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, fmt.Fprintf(os.Stderr,
"error reading \"%s\": %s\n", "error reading \"%s\": %s\n",
os.Args[1], err.Error()) fn, err.Error())
continue
} }
data, err := sdb.ParseSDB(raw) data, err := sdb.ParseSDB(raw)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, fmt.Fprintf(os.Stderr,
"%s\n", err.Error()) "%s\n", err.Error())
os.Exit(1) continue
} }
if *plonly {
for _, i := range data.Elements {
switch v := i.(type) {
case sdb.MOPayload:
os.Stdout.Write(v.Payload)
}
}
} else {
fmt.Printf("Protocol Revision: %d\n", fmt.Printf("Protocol Revision: %d\n",
data.ProtocolRevision) data.ProtocolRevision)
fmt.Printf("Message Length: %d\n", fmt.Printf("Message Length: %d\n",
@ -71,3 +94,5 @@ func main() {
} }
} }
} }
}
}