Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 1 | package types |
| 2 | |
| 3 | // Reference: https://www.ietf.org/rfc/rfc4120.txt |
| 4 | // Section: 5.2.8 |
| 5 | |
| 6 | import ( |
| 7 | "github.com/jcmturner/gofork/encoding/asn1" |
| 8 | ) |
| 9 | |
| 10 | // NewKrbFlags returns an ASN1 BitString struct of the right size for KrbFlags. |
| 11 | func NewKrbFlags() asn1.BitString { |
| 12 | f := asn1.BitString{} |
| 13 | f.Bytes = make([]byte, 4) |
| 14 | f.BitLength = len(f.Bytes) * 8 |
| 15 | return f |
| 16 | } |
| 17 | |
| 18 | // SetFlags sets the flags of an ASN1 BitString. |
| 19 | func SetFlags(f *asn1.BitString, j []int) { |
| 20 | for _, i := range j { |
| 21 | SetFlag(f, i) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // SetFlag sets a flag in an ASN1 BitString. |
| 26 | func SetFlag(f *asn1.BitString, i int) { |
| 27 | for l := len(f.Bytes); l < 4; l++ { |
| 28 | (*f).Bytes = append((*f).Bytes, byte(0)) |
| 29 | (*f).BitLength = len((*f).Bytes) * 8 |
| 30 | } |
| 31 | //Which byte? |
| 32 | b := i / 8 |
| 33 | //Which bit in byte |
| 34 | p := uint(7 - (i - 8*b)) |
| 35 | (*f).Bytes[b] = (*f).Bytes[b] | (1 << p) |
| 36 | } |
| 37 | |
| 38 | // UnsetFlags unsets flags in an ASN1 BitString. |
| 39 | func UnsetFlags(f *asn1.BitString, j []int) { |
| 40 | for _, i := range j { |
| 41 | UnsetFlag(f, i) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // UnsetFlag unsets a flag in an ASN1 BitString. |
| 46 | func UnsetFlag(f *asn1.BitString, i int) { |
| 47 | for l := len(f.Bytes); l < 4; l++ { |
| 48 | (*f).Bytes = append((*f).Bytes, byte(0)) |
| 49 | (*f).BitLength = len((*f).Bytes) * 8 |
| 50 | } |
| 51 | //Which byte? |
| 52 | b := i / 8 |
| 53 | //Which bit in byte |
| 54 | p := uint(7 - (i - 8*b)) |
| 55 | (*f).Bytes[b] = (*f).Bytes[b] &^ (1 << p) |
| 56 | } |
| 57 | |
| 58 | // IsFlagSet tests if a flag is set in the ASN1 BitString. |
| 59 | func IsFlagSet(f *asn1.BitString, i int) bool { |
| 60 | //Which byte? |
| 61 | b := i / 8 |
| 62 | //Which bit in byte |
| 63 | p := uint(7 - (i - 8*b)) |
| 64 | if (*f).Bytes[b]&(1<<p) != 0 { |
| 65 | return true |
| 66 | } |
| 67 | return false |
| 68 | } |