-
Notifications
You must be signed in to change notification settings - Fork 25
/
brotli.go
59 lines (51 loc) · 1.33 KB
/
brotli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2017, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.
// +build gofuzz
package bzip2
import (
"bytes"
"io/ioutil"
gbrotli "github.com/dsnet/compress/brotli"
cbrotli "github.com/dsnet/compress/internal/cgo/brotli"
)
func Fuzz(data []byte) int {
// Decompress using the Go decoder.
gr, err := gbrotli.NewReader(bytes.NewReader(data), nil)
if err != nil {
panic(err)
}
gb, gerr := ioutil.ReadAll(gr)
if err := gr.Close(); gerr == nil {
gerr = err
} else if gerr != nil && err == nil {
panic("nil on Close after non-nil error")
}
// Decompress using the C decoder.
cr := cbrotli.NewReader(bytes.NewReader(data))
cb, cerr := ioutil.ReadAll(cr)
if err := cr.Close(); cerr == nil {
cerr = err
} else if cerr != nil && err == nil {
panic("nil on Close after non-nil error")
}
switch {
case gerr == nil && cerr == nil:
if !bytes.Equal(gb, cb) {
panic("mismatching bytes")
}
case gerr != nil && cerr == nil:
panic(gerr)
case gerr == nil && cerr != nil:
panic(cerr)
default:
// Ensure that both gb and cb have the same common prefix.
if !bytes.HasPrefix(gb, cb) && !bytes.HasPrefix(cb, gb) {
panic("mismatching leading bytes")
}
}
if cerr == nil || gerr == nil {
return 1 // Favor valid inputs
}
return 0
}