-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
51 lines (47 loc) · 1.03 KB
/
utils.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
package vaar
import (
"errors"
"strings"
"github.com/klauspost/compress/gzip"
"github.com/pierrec/lz4/v4"
)
func validateRelPath(path string) error {
if len(path) == 0 {
return errors.New("empty path")
}
if path[0] == '/' || strings.Contains(path, "/../") || path == ".." ||
strings.HasPrefix(path, "../") || strings.HasSuffix(path, "/..") {
return errors.New("forbidden path")
}
if strings.ContainsAny(path, "\\\x00") {
return errors.New("invalid character")
}
return nil
}
func getCompressionLevel(algorithm Algorithm, level Level) int64 {
switch algorithm {
case LZ4Algorithm:
switch level {
case FastestLevel, FastLevel, DefaultLevel:
return int64(lz4.Fast)
case GoodLevel:
return int64(lz4.Level5)
case BestLevel:
return int64(lz4.Level9)
}
case GzipAlgorithm:
switch level {
case FastestLevel:
return gzip.BestSpeed
case FastLevel:
return 3
case DefaultLevel:
return gzip.DefaultCompression
case GoodLevel:
return 7
case BestLevel:
return gzip.BestCompression
}
}
return 0
}