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