File: KerberosFlags.go

package info (click to toggle)
golang-gopkg-jcmturner-gokrb5.v5 5.3.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, bullseye-backports, sid
  • size: 1,168 kB
  • sloc: makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,555 bytes parent folder | download | duplicates (2)
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 := int(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 := int(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 := int(i / 8)
	//Which bit in byte
	p := uint(7 - (i - 8*b))
	if (*f).Bytes[b]&(1<<p) != 0 {
		return true
	}
	return false
}