Change rQslice() to rQpeekBytes()

We don't need any full slice functionality, so let's change this to
better march rQpeek8() and rQshiftBytes().
This commit is contained in:
Pierre Ossman 2023-05-11 12:33:22 +02:00
parent 87143b361e
commit ae9b042df1
5 changed files with 14 additions and 13 deletions

View File

@ -124,7 +124,7 @@ export default class JPEGDecoder {
if (sock.rQwait("JPEG", length-2+extra, 4)) { if (sock.rQwait("JPEG", length-2+extra, 4)) {
return null; 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 && if (data.at(-2) === 0xFF && data.at(-1) !== 0x00 &&
!(data.at(-1) >= 0xD0 && data.at(-1) <= 0xD7)) { !(data.at(-1) >= 0xD0 && data.at(-1) <= 0xD7)) {
extra -= 2; extra -= 2;

View File

@ -139,7 +139,7 @@ export default class RSAAESAuthenticationState extends EventTargetMixin {
this._hasStarted = true; this._hasStarted = true;
// 1: Receive server public key // 1: Receive server public key
await this._waitSockAsync(4); await this._waitSockAsync(4);
const serverKeyLengthBuffer = this._sock.rQslice(0, 4); const serverKeyLengthBuffer = this._sock.rQpeekBytes(4);
const serverKeyLength = this._sock.rQshift32(); const serverKeyLength = this._sock.rQshift32();
if (serverKeyLength < 1024) { if (serverKeyLength < 1024) {
throw new Error("RA2: server public key is too short: " + serverKeyLength); throw new Error("RA2: server public key is too short: " + serverKeyLength);

View File

@ -2455,7 +2455,7 @@ export default class RFB extends EventTargetMixin {
default: default:
this._fail("Unexpected server message (type " + msgType + ")"); 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; return true;
} }
} }

View File

@ -168,8 +168,9 @@ export default class Websock {
this._rQi += len; this._rQi += len;
} }
rQslice(start, end = this.rQlen) { rQpeekBytes(len) {
return new Uint8Array(this._rQ.buffer, this._rQi + start, end - start); 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) // Check to see if we must wait for 'num' bytes (default to FBU.bytes)

View File

@ -126,32 +126,32 @@ describe('Websock', function () {
}); });
}); });
describe('rQslice', function () { describe('rQpeekBytes', function () {
beforeEach(function () { beforeEach(function () {
sock.rQi = 0; sock.rQi = 0;
}); });
it('should not modify the receive queue', function () { it('should not modify the receive queue', function () {
const befLen = sock.rQlen; const befLen = sock.rQlen;
sock.rQslice(0, 2); sock.rQpeekBytes(2);
expect(sock.rQlen).to.equal(befLen); expect(sock.rQlen).to.equal(befLen);
}); });
it('should return an array containing the given slice of the receive queue', function () { it('should return an array containing the requested bytes of the receive queue', function () {
const sl = sock.rQslice(0, 2); const sl = sock.rQpeekBytes(2);
expect(sl).to.be.an.instanceof(Uint8Array); expect(sl).to.be.an.instanceof(Uint8Array);
expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2)); 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 () { it('should use the rest of the receive queue if no end is given', function () {
const sl = sock.rQslice(1); const sl = sock.rQpeekBytes();
expect(sl).to.have.length(RQ_TEMPLATE.length - 1); expect(sl).to.have.length(RQ_TEMPLATE.length);
expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1)); expect(sl).to.array.equal(RQ_TEMPLATE);
}); });
it('should take the current rQi in to account', function () { it('should take the current rQi in to account', function () {
sock.rQi = 1; 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));
}); });
}); });