...

Source file src/github.com/klauspost/compress/ossfuzz/cmd/setup_dicts.go

Documentation: github.com/klauspost/compress/ossfuzz/cmd

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bytes"
    19  	"flag"
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  	"strings"
    24  	"text/template"
    25  
    26  	"github.com/klauspost/compress/zip"
    27  )
    28  
    29  var (
    30  	dictPath   = flag.String("dict-path", "", "dict path")
    31  	outputFile = flag.String("output-file", "", "output file")
    32  )
    33  
    34  func main() {
    35  	flag.Parse()
    36  	if *dictPath == "" {
    37  		panic("Need a dict path")
    38  	}
    39  	if *outputFile == "" {
    40  		panic("Need an output file")
    41  	}
    42  	dicts := getFuzzDicts(*dictPath)
    43  
    44  	t, err := template.New("todos").Parse(`
    45  package zstd
    46  var fuzzDicts = make([][]byte, 0)
    47  func init() {
    48  {{range $val := .}}
    49      fuzzDicts = append(fuzzDicts, {{$val}})
    50  {{end}}
    51  }
    52  `)
    53  	if err != nil {
    54  		panic(err)
    55  	}
    56  	f, err := os.Create(*outputFile)
    57  	err = t.Execute(f, dicts)
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  	f.Close()
    62  }
    63  
    64  func getFuzzDicts(path string) []string {
    65  	data, err := os.ReadFile(path)
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  	zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
    70  	if err != nil {
    71  		panic(err)
    72  	}
    73  	var dicts [][]byte
    74  	for _, tt := range zr.File {
    75  		if !strings.HasSuffix(tt.Name, ".dict") {
    76  			continue
    77  		}
    78  		func() {
    79  			r, err := tt.Open()
    80  			if err != nil {
    81  				panic(err)
    82  			}
    83  			defer r.Close()
    84  			in, err := io.ReadAll(r)
    85  			if err != nil {
    86  				panic(err)
    87  			}
    88  			dicts = append(dicts, in)
    89  		}()
    90  	}
    91  	stringDicts := make([]string, 0)
    92  	for _, d := range dicts {
    93  		stringedArray := fmt.Sprintf("%v", d)
    94  		withComma := strings.Replace(stringedArray, " ", ", ", -1)
    95  		withClosingBracket := strings.Replace(withComma, "]", "}", -1)
    96  		withOpenBracket := strings.Replace(withClosingBracket, "[", "[]byte{", -1)
    97  		stringDicts = append(stringDicts, withOpenBracket)
    98  	}
    99  	return stringDicts
   100  }
   101  

View as plain text