Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | 2x 2x 2x 36x 7x 5x 73x 73x 73x 73x 11x 11x 11x 11x 9x 9x 9x 9x 34x 9x 8x 8x 8x 8x 41x 8x 25x 25x 25x 25x 193x 25x 17x 17x 17x 17x 44x 17x 20x 20x 4x 16x 1x | import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import {JsonPackMpint} from '../JsonPackMpint';
import type {IReader, IReaderResettable} from '@jsonjoy.com/buffers/lib';
import type {BinaryJsonDecoder} from '../types';
/**
* SSH 2.0 binary decoder for SSH protocol data types.
* Implements SSH binary decoding according to RFC 4251.
*
* Key SSH decoding principles:
* - Multi-byte quantities are transmitted in big-endian byte order (network byte order)
* - Strings are length-prefixed with uint32
* - No padding is used (unlike XDR)
*/
export class SshDecoder<R extends IReader & IReaderResettable = IReader & IReaderResettable>
implements BinaryJsonDecoder
{
public constructor(public reader: R = new Reader() as any) {}
public read(uint8: Uint8Array): unknown {
this.reader.reset(uint8);
return this.readAny();
}
public decode(uint8: Uint8Array): unknown {
this.reader.reset(uint8);
return this.readAny();
}
public readAny(): unknown {
// Basic implementation - in practice this would need schema info
// For now, we'll throw as this should be used with explicit type methods
throw new Error('SshDecoder.readAny() requires explicit type methods');
}
/**
* Reads an SSH boolean value as a single byte.
* Returns true for non-zero values, false for zero.
*/
public readBoolean(): boolean {
return this.reader.u8() !== 0;
}
/**
* Reads an SSH byte value (8-bit).
*/
public readByte(): number {
return this.reader.u8();
}
/**
* Reads an SSH uint32 value in big-endian format.
*/
public readUint32(): number {
const reader = this.reader;
const value = reader.view.getUint32(reader.x, false); // false = big-endian
reader.x += 4;
return value;
}
/**
* Reads an SSH uint64 value in big-endian format.
*/
public readUint64(): bigint {
const reader = this.reader;
const value = reader.view.getBigUint64(reader.x, false); // false = big-endian
reader.x += 8;
return value;
}
/**
* Reads an SSH string as binary data (Uint8Array).
* Format: uint32 length + data bytes (no padding).
*/
public readBinStr(): Uint8Array {
const length = this.readUint32();
const reader = this.reader;
const data = new Uint8Array(length);
for (let i = 0; i < length; i++) {
data[i] = reader.u8();
}
return data;
}
/**
* Reads an SSH string with UTF-8 encoding.
* Format: uint32 length + UTF-8 bytes (no padding).
*/
public readStr(): string {
const length = this.readUint32();
const reader = this.reader;
// Read UTF-8 bytes
const utf8Bytes = new Uint8Array(length);
for (let i = 0; i < length; i++) {
utf8Bytes[i] = reader.u8();
}
// Decode UTF-8 to string
return new TextDecoder('utf-8').decode(utf8Bytes);
}
/**
* Reads an SSH string with ASCII encoding.
* Format: uint32 length + ASCII bytes (no padding).
*/
public readAsciiStr(): string {
const length = this.readUint32();
const reader = this.reader;
let str = '';
for (let i = 0; i < length; i++) {
str += String.fromCharCode(reader.u8());
}
return str;
}
/**
* Reads an SSH mpint (multiple precision integer).
* Format: uint32 length + data bytes in two's complement format, MSB first.
*/
public readMpint(): JsonPackMpint {
const length = this.readUint32();
const reader = this.reader;
const data = new Uint8Array(length);
for (let i = 0; i < length; i++) {
data[i] = reader.u8();
}
return new JsonPackMpint(data);
}
/**
* Reads an SSH name-list.
* Format: uint32 length + comma-separated names.
* Returns an array of name strings.
*/
public readNameList(): string[] {
const nameListStr = this.readAsciiStr();
if (nameListStr === '') {
return [];
}
return nameListStr.split(',');
}
/**
* Reads binary data as SSH string (alias for readBinStr)
*/
public readBin(): Uint8Array {
return this.readBinStr();
}
}
|