From df93f5e69258a89690c7b61a67095e2bdf9f5acb Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 15 Jan 2019 14:55:48 -0700 Subject: [PATCH] Change max message size to 128KB. Add comments. --- src/webUSBDevice.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/webUSBDevice.ts b/src/webUSBDevice.ts index 3a98448..8520e9e 100644 --- a/src/webUSBDevice.ts +++ b/src/webUSBDevice.ts @@ -77,10 +77,12 @@ export default class WebUSBDevice extends Device { protected async read (): Promise { let first = await this.readChunk() - // Check that buffer starts with: [ 0x3f, 0x23, 0x23, 0x00 ] + // Check that buffer starts with: "?##" [ 0x3f, 0x23, 0x23 ] + // "?" = USB marker, "##" = KeepKey magic bytes + // Message ID is bytes 4-5. Message length starts at byte 6. const valid = (first.getUint32(0) & 0xffffff00) === 0x3f232300 const msgLength = first.getUint32(5) - if (valid && msgLength >= 0 && msgLength < 4194304) { // 4 MB sanity check + if (valid && msgLength >= 0 && msgLength < 131072) { // 128KB max message size // FIXME: why doesn't ByteBuffer.concat() work? const buffer = new Uint8Array(9 + 2 + msgLength) for (let k = 0; k < first.byteLength; k++) { @@ -90,6 +92,7 @@ export default class WebUSBDevice extends Device { while (offset < buffer.length) { const next = await this.readChunk() + // Drop USB "?" packet identifier in the first byte for (let k = 1; (k < next.byteLength && offset < buffer.length); k++) { buffer[offset] = next.getUint8(k) offset++