@@ -22,70 +22,68 @@ func parseRangeSection(metadata ConsentMetadata, maxVendorID uint16, startbit ui
22
22
// Parse out the "exceptions" here.
23
23
currentOffset := startbit + 12
24
24
consents := make ([]rangeConsent , numEntries )
25
- for i := uint16 ( 0 ); i < numEntries ; i ++ {
26
- thisConsent , bitsConsumed , err := parseRangeConsent (data , currentOffset , maxVendorID )
25
+ for i := range consents {
26
+ bitsConsumed , err := parseRangeConsent (& consents [ i ], data , currentOffset , maxVendorID )
27
27
if err != nil {
28
28
return nil , 0 , err
29
29
}
30
- consents [i ] = thisConsent
31
30
currentOffset = currentOffset + bitsConsumed
32
31
}
33
32
34
33
return & rangeSection {
35
- consentData : data ,
36
34
consents : consents ,
37
35
maxVendorID : maxVendorID ,
38
36
}, currentOffset , nil
39
37
}
40
38
41
- // RangeSection Exception implemnetations
39
+ // RangeSection Exception implementations
42
40
43
41
// parseRangeConsents parses a RangeSection starting from the initial bit.
44
- // It returns the exception, as well as the number of bits consumed by the parsing.
45
- func parseRangeConsent (data []byte , initialBit uint , maxVendorID uint16 ) (rangeConsent , uint , error ) {
42
+ // It returns the number of bits consumed by the parsing.
43
+ func parseRangeConsent (dst * rangeConsent , data []byte , initialBit uint , maxVendorID uint16 ) (uint , error ) {
46
44
// Fixes #10
47
45
if uint (len (data )) <= initialBit / 8 {
48
- return nil , 0 , fmt .Errorf ("bit %d was supposed to start a new RangeEntry, but the consent string was only %d bytes long" , initialBit , len (data ))
46
+ return 0 , fmt .Errorf ("bit %d was supposed to start a new RangeEntry, but the consent string was only %d bytes long" , initialBit , len (data ))
49
47
}
50
48
// If the first bit is set, it's a Range of IDs
51
49
if isSet (data , initialBit ) {
52
50
start , err := bitutils .ParseUInt16 (data , initialBit + 1 )
53
51
if err != nil {
54
- return nil , 0 , err
52
+ return 0 , err
55
53
}
56
54
end , err := bitutils .ParseUInt16 (data , initialBit + 17 )
57
55
if err != nil {
58
- return nil , 0 , err
56
+ return 0 , err
59
57
}
60
58
if start == 0 {
61
- return nil , 0 , fmt .Errorf ("bit %d range entry exclusion starts at 0, but the min vendor ID is 1" , initialBit )
59
+ return 0 , fmt .Errorf ("bit %d range entry exclusion starts at 0, but the min vendor ID is 1" , initialBit )
62
60
}
63
61
if end > maxVendorID {
64
- return nil , 0 , fmt .Errorf ("bit %d range entry exclusion ends at %d, but the max vendor ID is %d" , initialBit , end , maxVendorID )
62
+ return 0 , fmt .Errorf ("bit %d range entry exclusion ends at %d, but the max vendor ID is %d" , initialBit , end , maxVendorID )
65
63
}
66
64
if end <= start {
67
- return nil , 0 , fmt .Errorf ("bit %d range entry excludes vendors [%d, %d]. The start should be less than the end" , initialBit , start , end )
65
+ return 0 , fmt .Errorf ("bit %d range entry excludes vendors [%d, %d]. The start should be less than the end" , initialBit , start , end )
68
66
}
69
- return rangeVendorConsent {
70
- startID : start ,
71
- endID : end ,
72
- }, uint (33 ), nil
67
+ dst .startID = start
68
+ dst .endID = end
69
+ return 33 , nil
73
70
}
74
71
75
72
vendorID , err := bitutils .ParseUInt16 (data , initialBit + 1 )
76
73
if err != nil {
77
- return nil , 0 , err
74
+ return 0 , err
78
75
}
79
76
if vendorID == 0 || vendorID > maxVendorID {
80
- return nil , 0 , fmt .Errorf ("bit %d range entry excludes vendor %d, but only vendors [1, %d] are valid" , initialBit , vendorID , maxVendorID )
77
+ return 0 , fmt .Errorf ("bit %d range entry excludes vendor %d, but only vendors [1, %d] are valid" , initialBit , vendorID , maxVendorID )
81
78
}
82
79
83
- return singleVendorConsent (vendorID ), 17 , nil
80
+ dst .startID = vendorID
81
+ dst .endID = vendorID
82
+ return 17 , nil
84
83
}
85
84
86
85
// A RangeConsents encodes consents that have been registered.
87
86
type rangeSection struct {
88
- consentData []byte
89
87
consents []rangeConsent
90
88
maxVendorID uint16
91
89
}
@@ -99,38 +97,26 @@ func (p *rangeSection) MaxVendorID() uint16 {
99
97
}
100
98
101
99
// VendorConsents implementation
102
- func (p rangeSection ) VendorConsent (id uint16 ) bool { // TODO consider convert to pointer receiver
100
+ func (p * rangeSection ) VendorConsent (id uint16 ) bool {
103
101
if id < 1 || id > p .maxVendorID {
104
102
return false
105
103
}
106
104
107
- for i := 0 ; i < len ( p .consents ); i ++ {
105
+ for i := range p .consents {
108
106
if p .consents [i ].Contains (id ) {
109
107
return true
110
108
}
111
109
}
112
110
return false
113
111
}
114
112
115
- // A RangeSection has a default consent value and a list of "exceptions". This represents an "exception" blob
116
- type rangeConsent interface {
117
- Contains (id uint16 ) bool
118
- }
119
-
120
- // This is a RangeSection exception for a single vendor.
121
- type singleVendorConsent uint16
122
-
123
- func (e singleVendorConsent ) Contains (id uint16 ) bool {
124
- return uint16 (e ) == id
125
- }
126
-
127
113
// This is a RangeSection exception for a range of IDs.
128
114
// The start and end bounds here are inclusive.
129
- type rangeVendorConsent struct {
115
+ type rangeConsent struct {
130
116
startID uint16
131
117
endID uint16
132
118
}
133
119
134
- func (e rangeVendorConsent ) Contains (id uint16 ) bool { // TODO consider convert to pointer receiver
120
+ func (e rangeConsent ) Contains (id uint16 ) bool {
135
121
return e .startID <= id && e .endID >= id
136
122
}
0 commit comments