-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcommon.go
103 lines (83 loc) · 1.81 KB
/
common.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
package siva
import (
"hash"
"hash/crc32"
"io"
"os"
"time"
)
// Flag carries a bitwise OR of flag values affecting an archive entry.
type Flag uint32
const (
// FlagDeleted is set to indicate when a file is deleted.
FlagDeleted Flag = 1 << iota
)
// Header contains the meta information from a file
type Header struct {
// Name is an arbitrary UTF-8 string identifying a file in the archive. Note
// that this might or might not be a POSIX-compliant path.
//
// Security note: Users should be careful when using name as a file path
// (e.g. to extract an archive) since it can contain relative paths and be
// vulnerable to Zip Slip (https://snyk.io/research/zip-slip-vulnerability)
// or other potentially dangerous values such as absolute paths, network
// drive addresses, etc.
Name string
ModTime time.Time
Mode os.FileMode
Flags Flag
}
type hashedWriter struct {
w io.Writer
h hash.Hash32
c int
}
func newHashedWriter(w io.Writer) *hashedWriter {
crc := crc32.NewIEEE()
return &hashedWriter{
w: io.MultiWriter(w, crc),
h: crc,
}
}
func (w *hashedWriter) Write(p []byte) (n int, err error) {
n, err = w.w.Write(p)
w.c += n
return
}
func (w *hashedWriter) Reset() {
w.h.Reset()
w.c = 0
}
func (w *hashedWriter) Position() int {
return w.c
}
func (w *hashedWriter) Checksum() uint32 {
return w.h.Sum32()
}
type hashedReader struct {
r io.Reader
h hash.Hash32
c int
}
func newHashedReader(r io.Reader) *hashedReader {
crc := crc32.NewIEEE()
return &hashedReader{
r: io.TeeReader(r, crc),
h: crc,
}
}
func (r *hashedReader) Read(p []byte) (n int, err error) {
n, err = r.r.Read(p)
r.c += n
return
}
func (r *hashedReader) Reset() {
r.h.Reset()
r.c = 0
}
func (r *hashedReader) Position() int {
return r.c
}
func (r *hashedReader) Checkshum() uint32 {
return r.h.Sum32()
}