-
Notifications
You must be signed in to change notification settings - Fork 51
/
bytes.go
31 lines (23 loc) · 954 Bytes
/
bytes.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
package claimsheader
import (
"encoding/binary"
)
// HeaderBytes is the claimsheader in bytes
type HeaderBytes []byte
// ToClaimsHeader parses the bytes and returns the ClaimsHeader
// WARNING: Caller has to make sure that headerbytes is NOT nil
func (h HeaderBytes) ToClaimsHeader() *ClaimsHeader {
compressionTypeMask := compressionTypeMask(h.extractHeaderAttribute(compressionTypeBitMask.toUint32()))
datapathVersionMask := datapathVersionMask(h.extractHeaderAttribute(datapathVersionBitMask.toUint32()))
return &ClaimsHeader{
compressionType: compressionTypeMask.toType(),
encrypt: uint32ToBool(h.extractHeaderAttribute(encryptionEnabledMask)),
datapathVersion: datapathVersionMask.toType(),
}
}
// extractHeaderAttribute returns the attribute from bytes
// mask - mask specific to the attribute
func (h HeaderBytes) extractHeaderAttribute(mask uint32) uint32 {
data := binary.LittleEndian.Uint32(h)
return data & mask
}