@@ -4,7 +4,6 @@ package machine
4
4
5
5
import (
6
6
"bytes"
7
- "encoding/binary"
8
7
"errors"
9
8
"runtime/volatile"
10
9
)
@@ -41,22 +40,26 @@ func NewDeviceDescriptor(class, subClass, proto, packetSize0 uint8, vid, pid, ve
41
40
42
41
// Bytes returns DeviceDescriptor data
43
42
func (d DeviceDescriptor ) Bytes () []byte {
44
- buf := bytes .NewBuffer (make ([]byte , 0 , deviceDescriptorSize ))
45
- binary .Write (buf , binary .LittleEndian , d .bLength )
46
- binary .Write (buf , binary .LittleEndian , d .bDescriptorType )
47
- binary .Write (buf , binary .LittleEndian , d .bcdUSB )
48
- binary .Write (buf , binary .LittleEndian , d .bDeviceClass )
49
- binary .Write (buf , binary .LittleEndian , d .bDeviceSubClass )
50
- binary .Write (buf , binary .LittleEndian , d .bDeviceProtocol )
51
- binary .Write (buf , binary .LittleEndian , d .bMaxPacketSize0 )
52
- binary .Write (buf , binary .LittleEndian , d .idVendor )
53
- binary .Write (buf , binary .LittleEndian , d .idProduct )
54
- binary .Write (buf , binary .LittleEndian , d .bcdDevice )
55
- binary .Write (buf , binary .LittleEndian , d .iManufacturer )
56
- binary .Write (buf , binary .LittleEndian , d .iProduct )
57
- binary .Write (buf , binary .LittleEndian , d .iSerialNumber )
58
- binary .Write (buf , binary .LittleEndian , d .bNumConfigurations )
59
- return buf .Bytes ()
43
+ b := make ([]byte , deviceDescriptorSize )
44
+ b [0 ] = byte (d .bLength )
45
+ b [1 ] = byte (d .bDescriptorType )
46
+ b [2 ] = byte (d .bcdUSB )
47
+ b [3 ] = byte (d .bcdUSB >> 8 )
48
+ b [4 ] = byte (d .bDeviceClass )
49
+ b [5 ] = byte (d .bDeviceSubClass )
50
+ b [6 ] = byte (d .bDeviceProtocol )
51
+ b [7 ] = byte (d .bMaxPacketSize0 )
52
+ b [8 ] = byte (d .idVendor )
53
+ b [9 ] = byte (d .idVendor >> 8 )
54
+ b [10 ] = byte (d .idProduct )
55
+ b [11 ] = byte (d .idProduct >> 8 )
56
+ b [12 ] = byte (d .bcdDevice )
57
+ b [13 ] = byte (d .bcdDevice >> 8 )
58
+ b [14 ] = byte (d .iManufacturer )
59
+ b [15 ] = byte (d .iProduct )
60
+ b [16 ] = byte (d .iSerialNumber )
61
+ b [17 ] = byte (d .bNumConfigurations )
62
+ return b
60
63
}
61
64
62
65
const configDescriptorSize = 9
@@ -85,16 +88,17 @@ func NewConfigDescriptor(totalLength uint16, interfaces uint8) ConfigDescriptor
85
88
86
89
// Bytes returns ConfigDescriptor data.
87
90
func (d ConfigDescriptor ) Bytes () []byte {
88
- buf := bytes .NewBuffer (make ([]byte , 0 , configDescriptorSize ))
89
- binary .Write (buf , binary .LittleEndian , d .bLength )
90
- binary .Write (buf , binary .LittleEndian , d .bDescriptorType )
91
- binary .Write (buf , binary .LittleEndian , d .wTotalLength )
92
- binary .Write (buf , binary .LittleEndian , d .bNumInterfaces )
93
- binary .Write (buf , binary .LittleEndian , d .bConfigurationValue )
94
- binary .Write (buf , binary .LittleEndian , d .iConfiguration )
95
- binary .Write (buf , binary .LittleEndian , d .bmAttributes )
96
- binary .Write (buf , binary .LittleEndian , d .bMaxPower )
97
- return buf .Bytes ()
91
+ b := make ([]byte , configDescriptorSize )
92
+ b [0 ] = byte (d .bLength )
93
+ b [1 ] = byte (d .bDescriptorType )
94
+ b [2 ] = byte (d .wTotalLength )
95
+ b [3 ] = byte (d .wTotalLength >> 8 )
96
+ b [4 ] = byte (d .bNumInterfaces )
97
+ b [5 ] = byte (d .bConfigurationValue )
98
+ b [6 ] = byte (d .iConfiguration )
99
+ b [7 ] = byte (d .bmAttributes )
100
+ b [8 ] = byte (d .bMaxPower )
101
+ return b
98
102
}
99
103
100
104
const interfaceDescriptorSize = 9
@@ -124,17 +128,17 @@ func NewInterfaceDescriptor(n, numEndpoints, class, subClass, protocol uint8) In
124
128
125
129
// Bytes returns InterfaceDescriptor data.
126
130
func (d InterfaceDescriptor ) Bytes () []byte {
127
- buf := bytes . NewBuffer ( make ([]byte , 0 , interfaceDescriptorSize ) )
128
- binary . Write ( buf , binary . LittleEndian , d .bLength )
129
- binary . Write ( buf , binary . LittleEndian , d .bDescriptorType )
130
- binary . Write ( buf , binary . LittleEndian , d .bInterfaceNumber )
131
- binary . Write ( buf , binary . LittleEndian , d .bAlternateSetting )
132
- binary . Write ( buf , binary . LittleEndian , d .bNumEndpoints )
133
- binary . Write ( buf , binary . LittleEndian , d .bInterfaceClass )
134
- binary . Write ( buf , binary . LittleEndian , d .bInterfaceSubClass )
135
- binary . Write ( buf , binary . LittleEndian , d .bInterfaceProtocol )
136
- binary . Write ( buf , binary . LittleEndian , d .iInterface )
137
- return buf . Bytes ()
131
+ b := make ([]byte , interfaceDescriptorSize )
132
+ b [ 0 ] = byte ( d .bLength )
133
+ b [ 1 ] = byte ( d .bDescriptorType )
134
+ b [ 2 ] = byte ( d .bInterfaceNumber )
135
+ b [ 3 ] = byte ( d .bAlternateSetting )
136
+ b [ 4 ] = byte ( d .bNumEndpoints )
137
+ b [ 5 ] = byte ( d .bInterfaceClass )
138
+ b [ 6 ] = byte ( d .bInterfaceSubClass )
139
+ b [ 7 ] = byte ( d .bInterfaceProtocol )
140
+ b [ 8 ] = byte ( d .iInterface )
141
+ return b
138
142
}
139
143
140
144
const endpointDescriptorSize = 7
@@ -160,14 +164,15 @@ func NewEndpointDescriptor(addr, attr uint8, packetSize uint16, interval uint8)
160
164
161
165
// Bytes returns EndpointDescriptor data.
162
166
func (d EndpointDescriptor ) Bytes () []byte {
163
- buf := bytes .NewBuffer (make ([]byte , 0 , endpointDescriptorSize ))
164
- binary .Write (buf , binary .LittleEndian , d .bLength )
165
- binary .Write (buf , binary .LittleEndian , d .bDescriptorType )
166
- binary .Write (buf , binary .LittleEndian , d .bEndpointAddress )
167
- binary .Write (buf , binary .LittleEndian , d .bmAttributes )
168
- binary .Write (buf , binary .LittleEndian , d .wMaxPacketSize )
169
- binary .Write (buf , binary .LittleEndian , d .bInterval )
170
- return buf .Bytes ()
167
+ b := make ([]byte , endpointDescriptorSize )
168
+ b [0 ] = byte (d .bLength )
169
+ b [1 ] = byte (d .bDescriptorType )
170
+ b [2 ] = byte (d .bEndpointAddress )
171
+ b [3 ] = byte (d .bmAttributes )
172
+ b [4 ] = byte (d .wMaxPacketSize )
173
+ b [5 ] = byte (d .wMaxPacketSize >> 8 )
174
+ b [6 ] = byte (d .bInterval )
175
+ return b
171
176
}
172
177
173
178
const iadDescriptorSize = 8
@@ -197,16 +202,16 @@ func NewIADDescriptor(firstInterface, count, class, subClass, protocol uint8) IA
197
202
198
203
// Bytes returns IADDescriptor data.
199
204
func (d IADDescriptor ) Bytes () []byte {
200
- buf := bytes . NewBuffer ( make ([]byte , 0 , iadDescriptorSize ) )
201
- binary . Write ( buf , binary . LittleEndian , d .bLength )
202
- binary . Write ( buf , binary . LittleEndian , d .bDescriptorType )
203
- binary . Write ( buf , binary . LittleEndian , d .bFirstInterface )
204
- binary . Write ( buf , binary . LittleEndian , d .bInterfaceCount )
205
- binary . Write ( buf , binary . LittleEndian , d .bFunctionClass )
206
- binary . Write ( buf , binary . LittleEndian , d .bFunctionSubClass )
207
- binary . Write ( buf , binary . LittleEndian , d .bFunctionProtocol )
208
- binary . Write ( buf , binary . LittleEndian , d .iFunction )
209
- return buf . Bytes ()
205
+ b := make ([]byte , iadDescriptorSize )
206
+ b [ 0 ] = byte ( d .bLength )
207
+ b [ 1 ] = byte ( d .bDescriptorType )
208
+ b [ 2 ] = byte ( d .bFirstInterface )
209
+ b [ 3 ] = byte ( d .bInterfaceCount )
210
+ b [ 4 ] = byte ( d .bFunctionClass )
211
+ b [ 5 ] = byte ( d .bFunctionSubClass )
212
+ b [ 6 ] = byte ( d .bFunctionProtocol )
213
+ b [ 7 ] = byte ( d .iFunction )
214
+ return b
210
215
}
211
216
212
217
const cdcCSInterfaceDescriptorSize = 5
@@ -227,13 +232,13 @@ func NewCDCCSInterfaceDescriptor(subtype, d0, d1 uint8) CDCCSInterfaceDescriptor
227
232
228
233
// Bytes returns CDCCSInterfaceDescriptor data.
229
234
func (d CDCCSInterfaceDescriptor ) Bytes () []byte {
230
- buf := bytes . NewBuffer ( make ([]byte , 0 , cdcCSInterfaceDescriptorSize ) )
231
- binary . Write ( buf , binary . LittleEndian , d .len )
232
- binary . Write ( buf , binary . LittleEndian , d .dtype )
233
- binary . Write ( buf , binary . LittleEndian , d .subtype )
234
- binary . Write ( buf , binary . LittleEndian , d .d0 )
235
- binary . Write ( buf , binary . LittleEndian , d .d1 )
236
- return buf . Bytes ()
235
+ b := make ([]byte , cdcCSInterfaceDescriptorSize )
236
+ b [ 0 ] = byte ( d .len )
237
+ b [ 1 ] = byte ( d .dtype )
238
+ b [ 2 ] = byte ( d .subtype )
239
+ b [ 3 ] = byte ( d .d0 )
240
+ b [ 4 ] = byte ( d .d1 )
241
+ return b
237
242
}
238
243
239
244
const cmFunctionalDescriptorSize = 5
@@ -254,13 +259,13 @@ func NewCMFunctionalDescriptor(subtype, d0, d1 uint8) CMFunctionalDescriptor {
254
259
255
260
// Bytes returns the CMFunctionalDescriptor data.
256
261
func (d CMFunctionalDescriptor ) Bytes () []byte {
257
- buf := bytes . NewBuffer ( make ([]byte , 0 , cmFunctionalDescriptorSize ) )
258
- binary . Write ( buf , binary . LittleEndian , d .bFunctionLength )
259
- binary . Write ( buf , binary . LittleEndian , d .bDescriptorType )
260
- binary . Write ( buf , binary . LittleEndian , d .bDescriptorSubtype )
261
- binary . Write ( buf , binary . LittleEndian , d .bmCapabilities )
262
- binary . Write ( buf , binary . LittleEndian , d . bDataInterface )
263
- return buf . Bytes ()
262
+ b := make ([]byte , cmFunctionalDescriptorSize )
263
+ b [ 0 ] = byte ( d .bFunctionLength )
264
+ b [ 1 ] = byte ( d .bDescriptorType )
265
+ b [ 2 ] = byte ( d .bDescriptorSubtype )
266
+ b [ 3 ] = byte ( d .bmCapabilities )
267
+ b [ 4 ] = byte ( d . bDescriptorSubtype )
268
+ return b
264
269
}
265
270
266
271
const acmFunctionalDescriptorSize = 4
@@ -280,12 +285,12 @@ func NewACMFunctionalDescriptor(subtype, d0 uint8) ACMFunctionalDescriptor {
280
285
281
286
// Bytes returns the ACMFunctionalDescriptor data.
282
287
func (d ACMFunctionalDescriptor ) Bytes () []byte {
283
- buf := bytes . NewBuffer ( make ([]byte , 0 , acmFunctionalDescriptorSize ) )
284
- binary . Write ( buf , binary . LittleEndian , d .len )
285
- binary . Write ( buf , binary . LittleEndian , d .dtype )
286
- binary . Write ( buf , binary . LittleEndian , d .subtype )
287
- binary . Write ( buf , binary . LittleEndian , d .bmCapabilities )
288
- return buf . Bytes ()
288
+ b := make ([]byte , acmFunctionalDescriptorSize )
289
+ b [ 0 ] = byte ( d .len )
290
+ b [ 1 ] = byte ( d .dtype )
291
+ b [ 2 ] = byte ( d .subtype )
292
+ b [ 3 ] = byte ( d .bmCapabilities )
293
+ return b
289
294
}
290
295
291
296
// CDCDescriptor is the Communication Device Class (CDC) descriptor.
@@ -343,7 +348,7 @@ const cdcSize = iadDescriptorSize +
343
348
344
349
// Bytes returns CDCDescriptor data.
345
350
func (d CDCDescriptor ) Bytes () []byte {
346
- buf := bytes .NewBuffer (make ([]byte , 0 , cdcSize ))
351
+ buf := bytes .NewBuffer (make ([]byte , 0 ))
347
352
buf .Write (d .iad .Bytes ())
348
353
buf .Write (d .cif .Bytes ())
349
354
buf .Write (d .header .Bytes ())
@@ -523,14 +528,13 @@ type usbSetup struct {
523
528
}
524
529
525
530
func newUSBSetup (data []byte ) usbSetup {
526
- buf := bytes .NewBuffer (data )
527
531
u := usbSetup {}
528
- binary . Read ( buf , binary . LittleEndian , & ( u . bmRequestType ) )
529
- binary . Read ( buf , binary . LittleEndian , & ( u . bRequest ) )
530
- binary . Read ( buf , binary . LittleEndian , & ( u . wValueL ) )
531
- binary . Read ( buf , binary . LittleEndian , & ( u . wValueH ) )
532
- binary . Read ( buf , binary . LittleEndian , & ( u . wIndex ) )
533
- binary . Read ( buf , binary . LittleEndian , & ( u . wLength ) )
532
+ u . bmRequestType = uint8 ( data [ 0 ] )
533
+ u . bRequest = uint8 ( data [ 1 ] )
534
+ u . wValueL = uint8 ( data [ 2 ] )
535
+ u . wValueH = uint8 ( data [ 3 ] )
536
+ u . wIndex = uint16 ( data [ 4 ]) | uint16 ( data [ 5 ] << 8 )
537
+ u . wLength = uint16 ( data [ 6 ]) | uint16 ( data [ 7 ] << 8 )
534
538
return u
535
539
}
536
540
0 commit comments