Skip to content

Commit f1516ad

Browse files
Unruddeadprogram
authored andcommitted
machine/usb/descriptor: Fix encoding of values
The limit for positive values is incorrect and leads to an overflow (e.g. 0xFF and 0xFFFF become -1)
1 parent 4dc07d6 commit f1516ad

File tree

2 files changed

+34
-51
lines changed

2 files changed

+34
-51
lines changed

src/machine/usb/descriptor/hid.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ var classHID = [ClassHIDTypeLen]byte{
103103
0x00, // CountryCode
104104
0x01, // NumDescriptors
105105
0x22, // ClassType
106-
0x90, // ClassLength L
106+
0x91, // ClassLength L
107107
0x00, // ClassLength H
108108
}
109109

@@ -131,7 +131,7 @@ var CDCHID = Descriptor{
131131
EndpointEP5OUT.Bytes(),
132132
}),
133133
HID: map[uint16][]byte{
134-
2: Append([][]byte{
134+
2: Append([][]byte{ // Update ClassLength in classHID whenever the array length is modified!
135135
HIDUsagePageGenericDesktop,
136136
HIDUsageDesktopKeyboard,
137137
HIDCollectionApplication,
@@ -210,6 +210,7 @@ var CDCHID = Descriptor{
210210
HIDReportSize(16),
211211
HIDReportCount(1),
212212
HIDInputDataAryAbs,
213-
HIDCollectionEnd}),
213+
HIDCollectionEnd,
214+
}),
214215
},
215216
}

src/machine/usb/descriptor/hidreport.go

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package descriptor
22

3+
import "math"
4+
35
const (
46
hidUsagePage = 0x05
57
hidUsage = 0x09
@@ -130,6 +132,28 @@ var (
130132
HIDOutputConstVarAbs = []byte{hidOutput, 0x03}
131133
)
132134

135+
func hidShortItem(tag byte, value uint32) []byte {
136+
switch {
137+
case value <= math.MaxUint8:
138+
return []byte{tag | hidSizeValue1, byte(value)}
139+
case value <= math.MaxUint16:
140+
return []byte{tag | hidSizeValue2, byte(value), byte(value >> 8)}
141+
default:
142+
return []byte{tag | hidSizeValue4, byte(value), byte(value >> 8), byte(value >> 16), byte(value >> 24)}
143+
}
144+
}
145+
146+
func hidShortItemSigned(tag byte, value int32) []byte {
147+
switch {
148+
case math.MinInt8 <= value && value <= math.MaxInt8:
149+
return []byte{tag | hidSizeValue1, byte(value)}
150+
case math.MinInt16 <= value && value <= math.MaxInt16:
151+
return []byte{tag | hidSizeValue2, byte(value), byte(value >> 8)}
152+
default:
153+
return []byte{tag | hidSizeValue4, byte(value), byte(value >> 8), byte(value >> 16), byte(value >> 24)}
154+
}
155+
}
156+
133157
func HIDReportSize(size int) []byte {
134158
return []byte{hidReportSize, byte(size)}
135159
}
@@ -143,69 +167,27 @@ func HIDReportID(id int) []byte {
143167
}
144168

145169
func HIDLogicalMinimum(min int) []byte {
146-
switch {
147-
case min < -32767 || 65535 < min:
148-
return []byte{hidLogicalMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
149-
case min < -127 || 255 < min:
150-
return []byte{hidLogicalMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
151-
default:
152-
return []byte{hidLogicalMinimum + hidSizeValue1, byte(min)}
153-
}
170+
return hidShortItemSigned(hidLogicalMinimum, int32(min))
154171
}
155172

156173
func HIDLogicalMaximum(max int) []byte {
157-
switch {
158-
case max < -32767 || 65535 < max:
159-
return []byte{hidLogicalMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
160-
case max < -127 || 255 < max:
161-
return []byte{hidLogicalMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
162-
default:
163-
return []byte{hidLogicalMaximum + hidSizeValue1, byte(max)}
164-
}
174+
return hidShortItemSigned(hidLogicalMaximum, int32(max))
165175
}
166176

167177
func HIDUsageMinimum(min int) []byte {
168-
switch {
169-
case min < -32767 || 65535 < min:
170-
return []byte{hidUsageMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
171-
case min < -127 || 255 < min:
172-
return []byte{hidUsageMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
173-
default:
174-
return []byte{hidUsageMinimum + hidSizeValue1, byte(min)}
175-
}
178+
return hidShortItem(hidUsageMinimum, uint32(min))
176179
}
177180

178181
func HIDUsageMaximum(max int) []byte {
179-
switch {
180-
case max < -32767 || 65535 < max:
181-
return []byte{hidUsageMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
182-
case max < -127 || 255 < max:
183-
return []byte{hidUsageMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
184-
default:
185-
return []byte{hidUsageMaximum + hidSizeValue1, byte(max)}
186-
}
182+
return hidShortItem(hidUsageMaximum, uint32(max))
187183
}
188184

189185
func HIDPhysicalMinimum(min int) []byte {
190-
switch {
191-
case min < -32767 || 65535 < min:
192-
return []byte{hidPhysicalMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
193-
case min < -127 || 255 < min:
194-
return []byte{hidPhysicalMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
195-
default:
196-
return []byte{hidPhysicalMinimum + hidSizeValue1, byte(min)}
197-
}
186+
return hidShortItemSigned(hidPhysicalMinimum, int32(min))
198187
}
199188

200189
func HIDPhysicalMaximum(max int) []byte {
201-
switch {
202-
case max < -32767 || 65535 < max:
203-
return []byte{hidPhysicalMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
204-
case max < -127 || 255 < max:
205-
return []byte{hidPhysicalMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
206-
default:
207-
return []byte{hidPhysicalMaximum + hidSizeValue1, byte(max)}
208-
}
190+
return hidShortItemSigned(hidPhysicalMaximum, int32(max))
209191
}
210192

211193
func HIDUnitExponent(exp int) []byte {

0 commit comments

Comments
 (0)