From ae9b042df1c17bb702ca78cda2aa2ce1a30002f0 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 11 May 2023 12:33:22 +0200 Subject: [PATCH] Change rQslice() to rQpeekBytes() We don't need any full slice functionality, so let's change this to better march rQpeek8() and rQshiftBytes(). --- core/decoders/jpeg.js | 2 +- core/ra2.js | 2 +- core/rfb.js | 2 +- core/websock.js | 5 +++-- tests/test.websock.js | 16 ++++++++-------- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/core/decoders/jpeg.js b/core/decoders/jpeg.js index 157bf4f3..5f5cc265 100644 --- a/core/decoders/jpeg.js +++ b/core/decoders/jpeg.js @@ -124,7 +124,7 @@ export default class JPEGDecoder { if (sock.rQwait("JPEG", length-2+extra, 4)) { return null; } - let data = sock.rQslice(0, length-2+extra); + let data = sock.rQpeekBytes(length-2+extra); if (data.at(-2) === 0xFF && data.at(-1) !== 0x00 && !(data.at(-1) >= 0xD0 && data.at(-1) <= 0xD7)) { extra -= 2; diff --git a/core/ra2.js b/core/ra2.js index 9557054f..b2bfb50a 100644 --- a/core/ra2.js +++ b/core/ra2.js @@ -139,7 +139,7 @@ export default class RSAAESAuthenticationState extends EventTargetMixin { this._hasStarted = true; // 1: Receive server public key await this._waitSockAsync(4); - const serverKeyLengthBuffer = this._sock.rQslice(0, 4); + const serverKeyLengthBuffer = this._sock.rQpeekBytes(4); const serverKeyLength = this._sock.rQshift32(); if (serverKeyLength < 1024) { throw new Error("RA2: server public key is too short: " + serverKeyLength); diff --git a/core/rfb.js b/core/rfb.js index ea8dc0ff..eba2e1da 100644 --- a/core/rfb.js +++ b/core/rfb.js @@ -2455,7 +2455,7 @@ export default class RFB extends EventTargetMixin { default: this._fail("Unexpected server message (type " + msgType + ")"); - Log.Debug("sock.rQslice(0, 30): " + this._sock.rQslice(0, 30)); + Log.Debug("sock.rQpeekBytes(30): " + this._sock.rQpeekBytes(30)); return true; } } diff --git a/core/websock.js b/core/websock.js index e07e31ba..3813af1c 100644 --- a/core/websock.js +++ b/core/websock.js @@ -168,8 +168,9 @@ export default class Websock { this._rQi += len; } - rQslice(start, end = this.rQlen) { - return new Uint8Array(this._rQ.buffer, this._rQi + start, end - start); + rQpeekBytes(len) { + if (typeof(len) === 'undefined') { len = this.rQlen; } + return new Uint8Array(this._rQ.buffer, this._rQi, len); } // Check to see if we must wait for 'num' bytes (default to FBU.bytes) diff --git a/tests/test.websock.js b/tests/test.websock.js index 1a0ba233..d2e20e0d 100644 --- a/tests/test.websock.js +++ b/tests/test.websock.js @@ -126,32 +126,32 @@ describe('Websock', function () { }); }); - describe('rQslice', function () { + describe('rQpeekBytes', function () { beforeEach(function () { sock.rQi = 0; }); it('should not modify the receive queue', function () { const befLen = sock.rQlen; - sock.rQslice(0, 2); + sock.rQpeekBytes(2); expect(sock.rQlen).to.equal(befLen); }); - it('should return an array containing the given slice of the receive queue', function () { - const sl = sock.rQslice(0, 2); + it('should return an array containing the requested bytes of the receive queue', function () { + const sl = sock.rQpeekBytes(2); expect(sl).to.be.an.instanceof(Uint8Array); expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2)); }); it('should use the rest of the receive queue if no end is given', function () { - const sl = sock.rQslice(1); - expect(sl).to.have.length(RQ_TEMPLATE.length - 1); - expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1)); + const sl = sock.rQpeekBytes(); + expect(sl).to.have.length(RQ_TEMPLATE.length); + expect(sl).to.array.equal(RQ_TEMPLATE); }); it('should take the current rQi in to account', function () { sock.rQi = 1; - expect(sock.rQslice(0, 2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2)); + expect(sock.rQpeekBytes(2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2)); }); });