[go: nahoru, domu]

Skip to content

Commit

Permalink
Parsing of md5 hash out of message
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewML committed Dec 13, 2020
1 parent 6f6101d commit 91029b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/MultiMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export class MultiMap<K, V> {
return this.map.has(key);
}

/**
* @summary Get the first item added for a given key
*/
first(key: K) {
return this.get(key)[0];
}

/**
* @summary Set a new value for a key.
*/
Expand Down
32 changes: 18 additions & 14 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,44 @@ export function parseSnac(rawSnac: Buffer): Snac {
/**
* @see http://iserverd1.khstu.ru/oscar/snac_17_06.html
*/
export function parseAuthRequest(buf: Buffer) {
const tlvs = parseTLVs(buf);
const [screennameTLV] = tlvs.get(TLVS.SCREENNAME);
assert(
screennameTLV && screennameTLV.value,
'Screen-name TLV missing in parseAuthRequest',
);
export function parseAuthRequest(data: Buffer) {
const tlvs = parseTLVs(data);
const screennameTLV = tlvs.first(TLVS.SCREENNAME);

return {
screenname: screennameTLV.value.toString('ascii'),
};
}

export function parseMD5LoginRequest(buf: Buffer) {
// TODO: Parse TLVs and return structured data
/**
* @see http://iserverd1.khstu.ru/oscar/snac_17_02.html
*/
export function parseMD5LoginRequest(data: Buffer) {
const tlvs = parseTLVs(data);
const screenname = tlvs.first(TLVS.SCREENNAME);
const passwordHash = tlvs.first(TLVS.PASSWORD_HASH);
const clientID = tlvs.first(TLVS.CLIENT_ID_STRING);

return { screenname, passwordHash, clientID };
}

/**
* @see http://iserverd1.khstu.ru/oscar/basic.html#b0003
*/
export function parseTLVs(buf: Buffer) {
export function parseTLVs(data: Buffer) {
const tlvs = new MultiMap<number, TLV>();

for (let tlvStart = 0; tlvStart < buf.byteLength; ) {
const type = buf.readUInt16BE(tlvStart);
for (let tlvStart = 0; tlvStart < data.byteLength; ) {
const type = data.readUInt16BE(tlvStart);

const lengthStart = tlvStart + 2;
const length = buf.readUInt16BE(lengthStart);
const length = data.readUInt16BE(lengthStart);

const valueStart = lengthStart + 2;
// A TLV's value can be 0 bytes. Odd that they're
// not just excluded from the request, but ¯\_(ツ)_/¯
const value = length
? buf.subarray(valueStart, valueStart + length)
? data.subarray(valueStart, valueStart + length)
: // Empty buffer so we don't have to explicitly handle
// a null/undefined anywhere a TLV type propagates
Buffer.alloc(0);
Expand Down

0 comments on commit 91029b7

Please sign in to comment.