File: bitfield.go

package info (click to toggle)
golang-github-google-go-tpm 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: makefile: 13
file content (82 lines) | stat: -rw-r--r-- 2,007 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package tpm2

import (
	"fmt"
)

// Bitfield represents a TPM bitfield (i.e., TPMA_*) type.
type Bitfield interface {
	// Length returns the length of the bitfield.
	Length() int
}

// BitGetter represents a TPM bitfield (i.e., TPMA_*) type that can be read.
type BitGetter interface {
	Bitfield
	// GetReservedBit returns the value of the given reserved bit.
	// If the bit is not reserved, returns false.
	GetReservedBit(pos int) bool
}

// BitSetter represents a TPM bitfield (i.e., TPMA_*) type that can be written.
type BitSetter interface {
	Bitfield
	// GetReservedBit sets the value of the given reserved bit.
	SetReservedBit(pos int, val bool)
}

func checkPos(pos int, len int) {
	if pos >= len || pos < 0 {
		panic(fmt.Errorf("bit %d out of range for %d-bit field", pos, len))
	}
}

// bitfield8 represents an 8-bit bitfield which may have reserved bits.
// 8-bit TPMA_* types embed this one, and the reserved bits are stored in it.
type bitfield8 uint8

// Length implements the Bitfield interface.
func (bitfield8) Length() int {
	return 8
}

// GetReservedBit implements the BitGetter interface.
func (r bitfield8) GetReservedBit(pos int) bool {
	checkPos(pos, 8)
	return r&(1<<pos) != 0
}

// SetReservedBit implements the BitSetter interface.
func (r *bitfield8) SetReservedBit(pos int, val bool) {
	checkPos(pos, 8)
	if val {
		*r |= 1 << pos
	} else {
		*r &= ^(1 << pos)
	}
}

// bitfield32 represents a 32-bit bitfield which may have reserved bits.
// 32-bit TPMA_* types embed this one, and the reserved bits are stored in it.
type bitfield32 uint32

// Length implements the Bitfield interface.
func (bitfield32) Length() int {
	return 32
}

// GetReservedBit implements the BitGetter interface.
func (r bitfield32) GetReservedBit(pos int) bool {
	checkPos(pos, 32)
	return r&(1<<pos) != 0
}

// SetReservedBit implements the BitSetter interface.
func (r *bitfield32) SetReservedBit(pos int, val bool) {
	checkPos(pos, 32)
	if val {
		*r |= 1 << pos
	} else {
		*r &= ^(1 << pos)
	}
}