forked from jcmturner/gokrb5
/
KerberosFlags.go
68 lines (60 loc) · 1.5 KB
/
KerberosFlags.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
package types
// Reference: https://www.ietf.org/rfc/rfc4120.txt
// Section: 5.2.8
import (
"github.com/jcmturner/gofork/encoding/asn1"
)
// NewKrbFlags returns an ASN1 BitString struct of the right size for KrbFlags.
func NewKrbFlags() asn1.BitString {
f := asn1.BitString{}
f.Bytes = make([]byte, 4)
f.BitLength = len(f.Bytes) * 8
return f
}
// SetFlags sets the flags of an ASN1 BitString.
func SetFlags(f *asn1.BitString, j []int) {
for _, i := range j {
SetFlag(f, i)
}
}
// SetFlag sets a flag in an ASN1 BitString.
func SetFlag(f *asn1.BitString, i int) {
for l := len(f.Bytes); l < 4; l++ {
(*f).Bytes = append((*f).Bytes, byte(0))
(*f).BitLength = len((*f).Bytes) * 8
}
//Which byte?
b := i / 8
//Which bit in byte
p := uint(7 - (i - 8*b))
(*f).Bytes[b] = (*f).Bytes[b] | (1 << p)
}
// UnsetFlags unsets flags in an ASN1 BitString.
func UnsetFlags(f *asn1.BitString, j []int) {
for _, i := range j {
UnsetFlag(f, i)
}
}
// UnsetFlag unsets a flag in an ASN1 BitString.
func UnsetFlag(f *asn1.BitString, i int) {
for l := len(f.Bytes); l < 4; l++ {
(*f).Bytes = append((*f).Bytes, byte(0))
(*f).BitLength = len((*f).Bytes) * 8
}
//Which byte?
b := i / 8
//Which bit in byte
p := uint(7 - (i - 8*b))
(*f).Bytes[b] = (*f).Bytes[b] &^ (1 << p)
}
// IsFlagSet tests if a flag is set in the ASN1 BitString.
func IsFlagSet(f *asn1.BitString, i int) bool {
//Which byte?
b := i / 8
//Which bit in byte
p := uint(7 - (i - 8*b))
if (*f).Bytes[b]&(1<<p) != 0 {
return true
}
return false
}