Skip to content

Commit baf77a9

Browse files
Copilotstreamich
andcommitted
fix: apply linter fixes for JsonPackMpint
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent 2d019d3 commit baf77a9

File tree

6 files changed

+233
-88
lines changed

6 files changed

+233
-88
lines changed

src/JsonPackMpint.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Represents an SSH multiprecision integer (mpint).
3-
*
3+
*
44
* An mpint is stored in two's complement format, 8 bits per byte, MSB first.
55
* According to RFC 4251:
66
* - Negative numbers have the value 1 as the most significant bit of the first byte
@@ -27,33 +27,33 @@ export class JsonPackMpint {
2727
}
2828

2929
const negative = value < BigInt(0);
30-
let bytes: number[] = [];
30+
const bytes: number[] = [];
3131

3232
if (negative) {
3333
// For negative numbers, work with two's complement
34-
let absValue = -value;
35-
let bitLength = absValue.toString(2).length;
36-
let byteLength = Math.ceil((bitLength + 1) / 8); // +1 for sign bit
37-
34+
const absValue = -value;
35+
const bitLength = absValue.toString(2).length;
36+
const byteLength = Math.ceil((bitLength + 1) / 8); // +1 for sign bit
37+
3838
// Calculate two's complement
39-
let twoComplement = (BigInt(1) << BigInt(byteLength * 8)) + value;
40-
39+
const twoComplement = (BigInt(1) << BigInt(byteLength * 8)) + value;
40+
4141
for (let i = byteLength - 1; i >= 0; i--) {
42-
bytes.push(Number((twoComplement >> BigInt(i * 8)) & BigInt(0xFF)));
42+
bytes.push(Number((twoComplement >> BigInt(i * 8)) & BigInt(0xff)));
4343
}
44-
44+
4545
// Ensure MSB is 1 for negative numbers
46-
while (bytes.length > 0 && bytes[0] === 0xFF && bytes.length > 1 && (bytes[1] & 0x80) !== 0) {
46+
while (bytes.length > 0 && bytes[0] === 0xff && bytes.length > 1 && (bytes[1] & 0x80) !== 0) {
4747
bytes.shift();
4848
}
4949
} else {
5050
// For positive numbers
5151
let tempValue = value;
5252
while (tempValue > BigInt(0)) {
53-
bytes.unshift(Number(tempValue & BigInt(0xFF)));
53+
bytes.unshift(Number(tempValue & BigInt(0xff)));
5454
tempValue >>= BigInt(8);
5555
}
56-
56+
5757
// Add leading zero if MSB is set (to indicate positive number)
5858
if (bytes[0] & 0x80) {
5959
bytes.unshift(0);
@@ -72,7 +72,7 @@ export class JsonPackMpint {
7272
}
7373

7474
const negative = (this.data[0] & 0x80) !== 0;
75-
75+
7676
if (negative) {
7777
// Two's complement for negative numbers
7878
let value = BigInt(0);

src/ssh/SshDecoder.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ export class SshDecoder<R extends IReader & IReaderResettable = IReader & IReade
7676
const length = this.readUint32();
7777
const reader = this.reader;
7878
const data = new Uint8Array(length);
79-
79+
8080
for (let i = 0; i < length; i++) {
8181
data[i] = reader.u8();
8282
}
83-
83+
8484
return data;
8585
}
8686

@@ -91,13 +91,13 @@ export class SshDecoder<R extends IReader & IReaderResettable = IReader & IReade
9191
public readStr(): string {
9292
const length = this.readUint32();
9393
const reader = this.reader;
94-
94+
9595
// Read UTF-8 bytes
9696
const utf8Bytes = new Uint8Array(length);
9797
for (let i = 0; i < length; i++) {
9898
utf8Bytes[i] = reader.u8();
9999
}
100-
100+
101101
// Decode UTF-8 to string
102102
return new TextDecoder('utf-8').decode(utf8Bytes);
103103
}
@@ -110,11 +110,11 @@ export class SshDecoder<R extends IReader & IReaderResettable = IReader & IReade
110110
const length = this.readUint32();
111111
const reader = this.reader;
112112
let str = '';
113-
113+
114114
for (let i = 0; i < length; i++) {
115115
str += String.fromCharCode(reader.u8());
116116
}
117-
117+
118118
return str;
119119
}
120120

@@ -126,11 +126,11 @@ export class SshDecoder<R extends IReader & IReaderResettable = IReader & IReade
126126
const length = this.readUint32();
127127
const reader = this.reader;
128128
const data = new Uint8Array(length);
129-
129+
130130
for (let i = 0; i < length; i++) {
131131
data[i] = reader.u8();
132132
}
133-
133+
134134
return new JsonPackMpint(data);
135135
}
136136

src/ssh/SshEncoder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class SshEncoder implements BinaryJsonEncoder {
7878
* Writes an SSH byte value (8-bit).
7979
*/
8080
public writeByte(byte: number): void {
81-
this.writer.u8(byte & 0xFF);
81+
this.writer.u8(byte & 0xff);
8282
}
8383

8484
/**
@@ -152,7 +152,7 @@ export class SshEncoder implements BinaryJsonEncoder {
152152

153153
this.writeUint32(str.length);
154154
for (let i = 0; i < str.length; i++) {
155-
writer.u8(str.charCodeAt(i) & 0x7F); // ASCII only
155+
writer.u8(str.charCodeAt(i) & 0x7f); // ASCII only
156156
}
157157
}
158158

@@ -181,7 +181,7 @@ export class SshEncoder implements BinaryJsonEncoder {
181181
*/
182182
public writeNumber(num: number): void {
183183
if (Number.isInteger(num)) {
184-
if (num >= 0 && num <= 0xFFFFFFFF) {
184+
if (num >= 0 && num <= 0xffffffff) {
185185
this.writeUint32(num);
186186
} else {
187187
this.writeUint64(num);

src/ssh/__tests__/SshDecoder.spec.ts

Lines changed: 91 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ describe('SshDecoder', () => {
4343
});
4444

4545
test('decodes uint32 max value', () => {
46-
reader.reset(new Uint8Array([0xFF, 0xFF, 0xFF, 0xFF]));
47-
expect(decoder.readUint32()).toBe(0xFFFFFFFF);
46+
reader.reset(new Uint8Array([0xff, 0xff, 0xff, 0xff]));
47+
expect(decoder.readUint32()).toBe(0xffffffff);
4848
});
4949

5050
test('decodes uint64', () => {
51-
reader.reset(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]));
51+
reader.reset(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]));
5252
expect(decoder.readUint64()).toBe(BigInt('0x123456789ABCDEF0'));
5353
});
5454

@@ -66,26 +66,46 @@ describe('SshDecoder', () => {
6666

6767
test('decodes ASCII string "testing" (UTF-8)', () => {
6868
const data = new Uint8Array([
69-
0, 0, 0, 7, // length
70-
0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67 // "testing"
69+
0,
70+
0,
71+
0,
72+
7, // length
73+
0x74,
74+
0x65,
75+
0x73,
76+
0x74,
77+
0x69,
78+
0x6e,
79+
0x67, // "testing"
7180
]);
7281
reader.reset(data);
7382
expect(decoder.readStr()).toBe('testing');
7483
});
7584

7685
test('decodes ASCII string', () => {
7786
const data = new Uint8Array([
78-
0, 0, 0, 4, // length
79-
0x74, 0x65, 0x73, 0x74 // "test"
87+
0,
88+
0,
89+
0,
90+
4, // length
91+
0x74,
92+
0x65,
93+
0x73,
94+
0x74, // "test"
8095
]);
8196
reader.reset(data);
8297
expect(decoder.readAsciiStr()).toBe('test');
8398
});
8499

85100
test('decodes binary string', () => {
86101
const data = new Uint8Array([
87-
0, 0, 0, 3, // length
88-
0x01, 0x02, 0x03
102+
0,
103+
0,
104+
0,
105+
3, // length
106+
0x01,
107+
0x02,
108+
0x03,
89109
]);
90110
reader.reset(data);
91111
const result = decoder.readBinStr();
@@ -100,8 +120,13 @@ describe('SshDecoder', () => {
100120

101121
test('readBin is alias for readBinStr', () => {
102122
const data = new Uint8Array([
103-
0, 0, 0, 3, // length
104-
0x01, 0x02, 0x03
123+
0,
124+
0,
125+
0,
126+
3, // length
127+
0x01,
128+
0x02,
129+
0x03,
105130
]);
106131
reader.reset(data);
107132
const result = decoder.readBin();
@@ -119,8 +144,18 @@ describe('SshDecoder', () => {
119144

120145
test('decodes mpint 0x9a378f9b2e332a7', () => {
121146
const data = new Uint8Array([
122-
0, 0, 0, 8, // length
123-
0x09, 0xa3, 0x78, 0xf9, 0xb2, 0xe3, 0x32, 0xa7
147+
0,
148+
0,
149+
0,
150+
8, // length
151+
0x09,
152+
0xa3,
153+
0x78,
154+
0xf9,
155+
0xb2,
156+
0xe3,
157+
0x32,
158+
0xa7,
124159
]);
125160
reader.reset(data);
126161
const mpint = decoder.readMpint();
@@ -129,8 +164,12 @@ describe('SshDecoder', () => {
129164

130165
test('decodes mpint 0x80', () => {
131166
const data = new Uint8Array([
132-
0, 0, 0, 2, // length
133-
0x00, 0x80
167+
0,
168+
0,
169+
0,
170+
2, // length
171+
0x00,
172+
0x80,
134173
]);
135174
reader.reset(data);
136175
const mpint = decoder.readMpint();
@@ -139,8 +178,12 @@ describe('SshDecoder', () => {
139178

140179
test('decodes mpint -1234', () => {
141180
const data = new Uint8Array([
142-
0, 0, 0, 2, // length
143-
0xfb, 0x2e
181+
0,
182+
0,
183+
0,
184+
2, // length
185+
0xfb,
186+
0x2e,
144187
]);
145188
reader.reset(data);
146189
const mpint = decoder.readMpint();
@@ -149,8 +192,15 @@ describe('SshDecoder', () => {
149192

150193
test('decodes mpint -0xdeadbeef', () => {
151194
const data = new Uint8Array([
152-
0, 0, 0, 5, // length
153-
0xff, 0x21, 0x52, 0x41, 0x11
195+
0,
196+
0,
197+
0,
198+
5, // length
199+
0xff,
200+
0x21,
201+
0x52,
202+
0x41,
203+
0x11,
154204
]);
155205
reader.reset(data);
156206
const mpint = decoder.readMpint();
@@ -166,17 +216,34 @@ describe('SshDecoder', () => {
166216

167217
test('decodes single name "zlib"', () => {
168218
const data = new Uint8Array([
169-
0, 0, 0, 4, // length
170-
0x7a, 0x6c, 0x69, 0x62 // "zlib"
219+
0,
220+
0,
221+
0,
222+
4, // length
223+
0x7a,
224+
0x6c,
225+
0x69,
226+
0x62, // "zlib"
171227
]);
172228
reader.reset(data);
173229
expect(decoder.readNameList()).toEqual(['zlib']);
174230
});
175231

176232
test('decodes name-list "zlib,none"', () => {
177233
const data = new Uint8Array([
178-
0, 0, 0, 9, // length
179-
0x7a, 0x6c, 0x69, 0x62, 0x2c, 0x6e, 0x6f, 0x6e, 0x65 // "zlib,none"
234+
0,
235+
0,
236+
0,
237+
9, // length
238+
0x7a,
239+
0x6c,
240+
0x69,
241+
0x62,
242+
0x2c,
243+
0x6e,
244+
0x6f,
245+
0x6e,
246+
0x65, // "zlib,none"
180247
]);
181248
reader.reset(data);
182249
expect(decoder.readNameList()).toEqual(['zlib', 'none']);
@@ -191,7 +258,7 @@ describe('SshDecoder', () => {
191258
data[2] = 0;
192259
data[3] = bytes.length;
193260
data.set(bytes, 4);
194-
261+
195262
reader.reset(data);
196263
expect(decoder.readNameList()).toEqual(['one', 'two', 'three']);
197264
});

0 commit comments

Comments
 (0)