-
Notifications
You must be signed in to change notification settings - Fork 83
/
verify.go
134 lines (129 loc) · 3.22 KB
/
verify.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"fmt"
"github.com/dropbox/rust-brotli/c/go/brotli"
"io"
insecure_random "math/rand"
"os"
"path/filepath"
)
func makeOptions() brotli.CompressionOptions {
ret := brotli.CompressionOptions{
NumThreads: insecure_random.Intn(15) + 1,
Quality: float32(insecure_random.Intn(9) + 3),
Catable: true,
Appendable: true,
Magic: true,
}
if ret.Quality > 9 {
ret.Quality = 9.5
}
return ret
}
func verifyReader(path string) {
f, err := os.Open(path)
if err != nil {
return
}
defer func() { _ = f.Close() }()
origFile := bytes.NewBuffer(nil)
options := makeOptions()
reader := brotli.NewMultiCompressionReader(
io.TeeReader(f, origFile),
options,
)
compressed := bytes.NewBuffer(nil)
rtReader := brotli.NewDecompressionReader(io.TeeReader(reader, compressed))
rt := bytes.NewBuffer(nil)
io.Copy(rt, rtReader)
if string(rt.Bytes()) != string(origFile.Bytes()) {
fi, err := os.Create("/tmp/IN.orig")
if err != nil {
defer func() { _ = fi.Close() }()
_, _ = fi.Write(origFile.Bytes())
}
fc, err := os.Create("/tmp/IN.br")
if err != nil {
defer func() { _ = fc.Close() }()
_, _ = fc.Write(compressed.Bytes())
}
fr, err := os.Create("/tmp/IN.rt")
if err != nil {
defer func() { _ = fr.Close() }()
_, _ = fr.Write(rt.Bytes())
}
panic(fmt.Sprintf("%v Bytes mismatch %d != %d\n", options, len(rt.Bytes()), len(origFile.Bytes())))
}
}
func verifyWriter(path string) {
f, err := os.Open(path)
if err != nil {
return
}
options := makeOptions()
defer func() { _ = f.Close() }()
compressed := bytes.NewBuffer(nil)
rt := bytes.NewBuffer(nil)
origFile := bytes.NewBuffer(nil)
dwriter := brotli.NewDecompressionWriter(rt)
writer := brotli.NewMultiCompressionWriter(
io.MultiWriter(compressed, dwriter),
options,
)
_, err = io.Copy(io.MultiWriter(writer, origFile), f)
if err != nil {
panic(err)
}
err = writer.Close()
if err != nil {
panic(err)
}
err = dwriter.Close()
if err != nil {
panic(err)
}
if string(rt.Bytes()) != string(origFile.Bytes()) {
fi, err := os.Create("/tmp/INW.orig")
if err != nil {
defer func() { _ = fi.Close() }()
_, _ = fi.Write(origFile.Bytes())
}
fc, err := os.Create("/tmp/INW.br")
if err != nil {
defer func() { _ = fc.Close() }()
_, _ = fc.Write(compressed.Bytes())
}
fr, err := os.Create("/tmp/INW.rt")
if err != nil {
defer func() { _ = fr.Close() }()
_, _ = fr.Write(rt.Bytes())
}
panic(fmt.Sprintf("%v Bytes mismatch %d != %d\n", options, len(rt.Bytes()), len(origFile.Bytes())))
} else {
fmt.Fprintf(os.Stderr, "Processing %s %v/%v = %f\n",
path, len(rt.Bytes()), len(compressed.Bytes()), float32(len(compressed.Bytes()))/float32(len(rt.Bytes())))
}
}
func recursiveVerify(root string) {
filepath.Walk(root, filepath.WalkFunc(func(path string, info os.FileInfo, err error) error {
if info.Size() > 1 && !info.IsDir() {
func() {
f, err := os.Open(path)
if err != nil {
return
}
defer func() { _ = f.Close() }()
var test [64]byte
sz, err := f.Read(test[:])
if sz == 0 || err != nil {
return
}
fmt.Fprintf(os.Stderr, "Processing %v (%d)\n", path, info.Size())
verifyReader(path)
verifyWriter(path)
}()
}
return nil
}))
}