GoPLS Viewer

Home|gopls/cmd/file2fuzz/main.go
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.
20package main
21
22import (
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.
35var encVersion1 = "go test fuzz v1"
36
37func encodeByteSlice(b []byte) []byte {
38    return []byte(fmt.Sprintf("%s\n[]byte(%q)"encVersion1b))
39}
40
41func 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}
47func dirWriter(dir string) func([]byteerror {
48    return func(b []byteerror {
49        sum := fmt.Sprintf("%x"sha256.Sum256(b))
50        name := filepath.Join(dirsum)
51        if err := os.MkdirAll(dir0777); err != nil {
52            return err
53        }
54        if err := ioutil.WriteFile(nameb0666); err != nil {
55            os.Remove(name)
56            return err
57        }
58        return nil
59    }
60}
61
62func convert(inputArgs []stringoutputArg stringerror {
63    var input []io.Reader
64    if args := inputArgslen(args) == 0 {
65        input = []io.Reader{os.Stdin}
66    } else {
67        for _a := range args {
68            ferr := os.Open(a)
69            if err != nil {
70                return fmt.Errorf("unable to open %q: %s"aerr)
71            }
72            defer f.Close()
73            if fierr := f.Stat(); err != nil {
74                return fmt.Errorf("unable to open %q: %s"aerr)
75            } else if fi.IsDir() {
76                return fmt.Errorf("%q is a directory, not a file"a)
77            }
78            input = append(inputf)
79        }
80    }
81
82    var output func([]byteerror
83    if outputArg == "" {
84        if len(inputArgs) > 1 {
85            return errors.New("-o required with multiple input files")
86        }
87        output = func(b []byteerror {
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 fierr := os.Stat(outputArg); err != nil && !os.IsNotExist(err) {
96                return fmt.Errorf("unable to open %q for writing: %s"outputArgerr)
97            } else if err == nil && fi.IsDir() {
98                output = dirWriter(outputArg)
99            } else {
100                output = func(b []byteerror {
101                    return ioutil.WriteFile(outputArgb0666)
102                }
103            }
104        }
105    }
106
107    for _f := range input {
108        berr := 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
120func 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
MembersX
log
dirWriter.BlockStmt.err
convert.BlockStmt.BlockStmt.err
convert.RangeStmt_2949.BlockStmt.err
io
ioutil
os
convert.inputArgs
main
fmt
dirWriter.dir
dirWriter.BlockStmt.name
convert.BlockStmt.RangeStmt_1934.BlockStmt.f
encodeByteSlice.b
dirWriter
convert.BlockStmt.RangeStmt_1934.a
convert.BlockStmt.BlockStmt.fi
flag
filepath
encVersion1
encodeByteSlice
convert.BlockStmt.RangeStmt_1934.BlockStmt.err
convert.RangeStmt_2949.f
main.output
sha256
dirWriter.BlockStmt.sum
convert
convert.args
convert.BlockStmt.BlockStmt._
usage
convert.outputArg
convert.input
convert.BlockStmt.RangeStmt_1934.BlockStmt.fi
errors
convert.RangeStmt_2949.BlockStmt.b
main.err
Members
X