-
Notifications
You must be signed in to change notification settings - Fork 51
/
ct.go
107 lines (83 loc) · 2.5 KB
/
ct.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
package claimsheader
import "strconv"
// CompressionType defines the compression used.
type CompressionType int
const (
// CompressionTypeNone implies no compression
CompressionTypeNone CompressionType = iota
// CompressionTypeV1 is version 1 of compression
CompressionTypeV1
// CompressionTypeV2 is version 2 of compression
CompressionTypeV2
)
const (
// CompressedTagLengthV1 is version 1 length of tags
CompressedTagLengthV1 int = 12
// CompressedTagLengthV2 is version 2 length of tags
CompressedTagLengthV2 int = 4
)
// toMask returns the mask based on the type
func (ct CompressionType) toMask() compressionTypeMask {
switch ct {
case CompressionTypeV1:
return compressionTypeV1Mask
case CompressionTypeV2:
return compressionTypeV2Mask
default:
return compressionTypeNoneMask
}
}
func (ct CompressionType) toString() string {
return strconv.Itoa(int(ct))
}
// compressionTypeMask defines the compression mask.
type compressionTypeMask uint8
const (
// compressionTypeNoneMask mask that identifies compression type none
compressionTypeNoneMask compressionTypeMask = 0x40
// compressionTypeV1Mask mask that identifies compression type v1
compressionTypeV1Mask compressionTypeMask = 0x80
// compressionTypeV2Mask mask that identifies compression type v2
compressionTypeV2Mask compressionTypeMask = 0xC0
// compressionTypeBitMask mask used to check relevant compression types
compressionTypeBitMask compressionTypeMask = 0xC0
)
// toType returns the type based on mask
func (cm compressionTypeMask) toType() CompressionType {
switch cm {
case compressionTypeV1Mask:
return CompressionTypeV1
case compressionTypeV2Mask:
return CompressionTypeV2
default:
return CompressionTypeNone
}
}
// toUint8 returns uint8 from compressiontypemask
func (cm compressionTypeMask) toUint8() uint8 {
return uint8(cm)
}
// toUint8 returns uint8 from compressiontypemask
func (cm compressionTypeMask) toUint32() uint32 {
return uint32(cm)
}
// CompressionTypeToTagLength converts CompressionType to length.
func CompressionTypeToTagLength(t CompressionType) int {
if t == CompressionTypeNone {
return 0
}
if t == CompressionTypeV1 {
return CompressedTagLengthV1
}
return CompressedTagLengthV2
}
// String2CompressionType is a helper to convert string to compression type
func String2CompressionType(s string) CompressionType {
if s == CompressionTypeV1.toString() {
return CompressionTypeV1
}
if s == CompressionTypeV2.toString() {
return CompressionTypeV2
}
return CompressionTypeNone
}