| 1 | // Copyright 2021 The Go Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // file2fuzz converts binary files, such as those used by go-fuzz, to the Go |
| 6 | // fuzzing corpus format. |
| 7 | // |
| 8 | // Usage: |
| 9 | // |
| 10 | // file2fuzz [-o output] [input...] |
| 11 | // |
| 12 | // The default behavior is to read input from stdin and write the converted |
| 13 | // output to stdout. If any position arguments are provided stdin is ignored |
| 14 | // and the arguments are assumed to be input files to convert. |
| 15 | // |
| 16 | // The -o flag provides an path to write output files to. If only one positional |
| 17 | // argument is specified it may be a file path or an existing directory, if there are |
| 18 | // multiple inputs specified it must be a directory. If a directory is provided |
| 19 | // the name of the file will be the SHA-256 hash of its contents. |
| 20 | package main |
| 21 | |
| 22 | import ( |
| 23 | "crypto/sha256" |
| 24 | "errors" |
| 25 | "flag" |
| 26 | "fmt" |
| 27 | "io" |
| 28 | "io/ioutil" |
| 29 | "log" |
| 30 | "os" |
| 31 | "path/filepath" |
| 32 | ) |
| 33 | |
| 34 | // encVersion1 is version 1 Go fuzzer corpus encoding. |
| 35 | var encVersion1 = "go test fuzz v1" |
| 36 | |
| 37 | func encodeByteSlice(b []byte) []byte { |
| 38 | return []byte(fmt.Sprintf("%s\n[]byte(%q)", encVersion1, b)) |
| 39 | } |
| 40 | |
| 41 | func usage() { |
| 42 | fmt.Fprintf(os.Stderr, "usage: file2fuzz [-o output] [input...]\nconverts files to Go fuzzer corpus format\n") |
| 43 | fmt.Fprintf(os.Stderr, "\tinput: files to convert\n") |
| 44 | fmt.Fprintf(os.Stderr, "\t-o: where to write converted file(s)\n") |
| 45 | os.Exit(2) |
| 46 | } |
| 47 | func dirWriter(dir string) func([]byte) error { |
| 48 | return func(b []byte) error { |
| 49 | sum := fmt.Sprintf("%x", sha256.Sum256(b)) |
| 50 | name := filepath.Join(dir, sum) |
| 51 | if err := os.MkdirAll(dir, 0777); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | if err := ioutil.WriteFile(name, b, 0666); err != nil { |
| 55 | os.Remove(name) |
| 56 | return err |
| 57 | } |
| 58 | return nil |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func convert(inputArgs []string, outputArg string) error { |
| 63 | var input []io.Reader |
| 64 | if args := inputArgs; len(args) == 0 { |
| 65 | input = []io.Reader{os.Stdin} |
| 66 | } else { |
| 67 | for _, a := range args { |
| 68 | f, err := os.Open(a) |
| 69 | if err != nil { |
| 70 | return fmt.Errorf("unable to open %q: %s", a, err) |
| 71 | } |
| 72 | defer f.Close() |
| 73 | if fi, err := f.Stat(); err != nil { |
| 74 | return fmt.Errorf("unable to open %q: %s", a, err) |
| 75 | } else if fi.IsDir() { |
| 76 | return fmt.Errorf("%q is a directory, not a file", a) |
| 77 | } |
| 78 | input = append(input, f) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | var output func([]byte) error |
| 83 | if outputArg == "" { |
| 84 | if len(inputArgs) > 1 { |
| 85 | return errors.New("-o required with multiple input files") |
| 86 | } |
| 87 | output = func(b []byte) error { |
| 88 | _, err := os.Stdout.Write(b) |
| 89 | return err |
| 90 | } |
| 91 | } else { |
| 92 | if len(inputArgs) > 1 { |
| 93 | output = dirWriter(outputArg) |
| 94 | } else { |
| 95 | if fi, err := os.Stat(outputArg); err != nil && !os.IsNotExist(err) { |
| 96 | return fmt.Errorf("unable to open %q for writing: %s", outputArg, err) |
| 97 | } else if err == nil && fi.IsDir() { |
| 98 | output = dirWriter(outputArg) |
| 99 | } else { |
| 100 | output = func(b []byte) error { |
| 101 | return ioutil.WriteFile(outputArg, b, 0666) |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | for _, f := range input { |
| 108 | b, err := ioutil.ReadAll(f) |
| 109 | if err != nil { |
| 110 | return fmt.Errorf("unable to read input: %s", err) |
| 111 | } |
| 112 | if err := output(encodeByteSlice(b)); err != nil { |
| 113 | return fmt.Errorf("unable to write output: %s", err) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | func main() { |
| 121 | log.SetFlags(0) |
| 122 | log.SetPrefix("file2fuzz: ") |
| 123 | |
| 124 | output := flag.String("o", "", "where to write converted file(s)") |
| 125 | flag.Usage = usage |
| 126 | flag.Parse() |
| 127 | |
| 128 | if err := convert(flag.Args(), *output); err != nil { |
| 129 | log.Fatal(err) |
| 130 | } |
| 131 | } |
| 132 |
Members