MCPcopy Create free account
hub / github.com/mscdex/ssh2 / parseHeader

Function parseHeader

lib/protocol/Protocol.js:1923–2025  ·  view source on GitHub ↗
(chunk, p, len)

Source from the content-addressed store, hash-verified

1921// TODO: optimize this by starting n bytes from the end of this._buffer instead
1922// of the beginning
1923function parseHeader(chunk, p, len) {
1924 let data;
1925 let chunkOffset;
1926 if (this._buffer) {
1927 data = Buffer.allocUnsafe(this._buffer.length + (len - p));
1928 data.set(this._buffer, 0);
1929 if (p === 0) {
1930 data.set(chunk, this._buffer.length);
1931 } else {
1932 data.set(new Uint8Array(chunk.buffer,
1933 chunk.byteOffset + p,
1934 (len - p)),
1935 this._buffer.length);
1936 }
1937 chunkOffset = this._buffer.length;
1938 p = 0;
1939 } else {
1940 data = chunk;
1941 chunkOffset = 0;
1942 }
1943 const op = p;
1944 let start = p;
1945 let end = p;
1946 let needNL = false;
1947 let lineLen = 0;
1948 let lines = 0;
1949 for (; p < data.length; ++p) {
1950 const ch = data[p];
1951
1952 if (ch === 13 /* '\r' */) {
1953 needNL = true;
1954 continue;
1955 }
1956
1957 if (ch === 10 /* '\n' */) {
1958 if (end > start
1959 && end - start > 4
1960 && data[start] === 83 /* 'S' */
1961 && data[start + 1] === 83 /* 'S' */
1962 && data[start + 2] === 72 /* 'H' */
1963 && data[start + 3] === 45 /* '-' */) {
1964
1965 const full = data.latin1Slice(op, end + 1);
1966 const identRaw = (start === op ? full : full.slice(start - op));
1967 const m = RE_IDENT.exec(identRaw);
1968 if (!m)
1969 throw new Error('Invalid identification string');
1970
1971 const header = {
1972 greeting: (start === op ? '' : full.slice(0, start - op)),
1973 identRaw,
1974 versions: {
1975 protocol: m[1],
1976 software: m[2],
1977 },
1978 comments: m[3]
1979 };
1980

Callers

nothing calls this directly

Calls 4

getCompatFlagsFunction · 0.85
kexinitFunction · 0.85
bufferSliceFunction · 0.85
execMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…