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"
"io/ioutil"
"os"
"flag"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr,
"required argument missing\n")
flags := flag.NewFlagSet("sdb", flag.ExitOnError)
flags.SetOutput(os.Stdout)
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)
}
raw, err := ioutil.ReadFile(os.Args[1])
for _, fn := range flags.Args() {
raw, err := ioutil.ReadFile(fn)
if err != nil {
fmt.Fprintf(os.Stderr,
"error reading \"%s\": %s\n",
os.Args[1], err.Error())
fn, err.Error())
continue
}
data, err := sdb.ParseSDB(raw)
if err != nil {
fmt.Fprintf(os.Stderr,
"%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",
data.ProtocolRevision)
fmt.Printf("Message Length: %d\n",
@ -71,3 +94,5 @@ func main() {
}
}
}
}
}