Advertisement
Guest User

Untitled

a guest
Jan 10th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { TargetTypes } from "@/mitm/types";
  2. import { SmartBuffer } from "smart-buffer";
  3.  
  4. export interface IMessageInfos {
  5.   messageId: number;
  6.   dataLength: number;
  7.   buffer?: Buffer;
  8. }
  9.  
  10. export default class MessageParser {
  11.   public static tryParse(
  12.     reader: SmartBuffer,
  13.     messageInfos: IMessageInfos,
  14.     target?: TargetTypes
  15.   ) {
  16.     messageInfos.messageId = 0;
  17.     messageInfos.dataLength = 0;
  18.     messageInfos.buffer = undefined;
  19.  
  20.     if (reader.remaining() < 2) {
  21.       return false;
  22.     }
  23.  
  24.     const header = reader.readInt16BE();
  25.     messageInfos.messageId = header >> 2;
  26.  
  27.     if (target === TargetTypes.CLIENT) {
  28.       const instanceId = reader.readUInt32BE();
  29.     }
  30.  
  31.     const dataLengthBytesCount = header & 0x3;
  32.  
  33.     if (reader.remaining() >= dataLengthBytesCount) {
  34.       if (dataLengthBytesCount < 0 || dataLengthBytesCount > 3) {
  35.         throw new RangeError(
  36.           "[MessageParser] Out of range: " + dataLengthBytesCount
  37.         );
  38.       }
  39.  
  40.       for (let i = dataLengthBytesCount - 1; i >= 0; i--) {
  41.         messageInfos.dataLength |= reader.readInt8() << (i * 8);
  42.       }
  43.     }
  44.  
  45.     if (messageInfos.dataLength <= 0) {
  46.       return false;
  47.     }
  48.  
  49.     if (messageInfos.dataLength > reader.remaining()) {
  50.       return false;
  51.     }
  52.  
  53.     if (messageInfos.buffer === undefined && messageInfos.dataLength >= 0) {
  54.       if (messageInfos.dataLength === 0) {
  55.         messageInfos.buffer = Buffer.from([]);
  56.       }
  57.  
  58.       // enough bytes in the buffer to build a complete message
  59.       if (reader.remaining() >= messageInfos.dataLength) {
  60.         messageInfos.buffer = reader.readBuffer(messageInfos.dataLength);
  61.       } else if (messageInfos.dataLength > reader.remaining()) {
  62.         // not enough bytes, so we read what we can
  63.         messageInfos.buffer = reader.readBuffer(reader.remaining());
  64.       }
  65.     }
  66.  
  67.     // second case : the message was split and it missed some bytes
  68.     if (
  69.       !messageInfos.buffer ||
  70.       messageInfos.dataLength === 0 ||
  71.       messageInfos.buffer.length >= messageInfos.dataLength
  72.     ) {
  73.       return true;
  74.     }
  75.  
  76.     let bytesToRead = 0;
  77.  
  78.     // still miss some bytes ...
  79.     if (
  80.       messageInfos.buffer.length + reader.remaining() <
  81.       messageInfos.dataLength
  82.     ) {
  83.       bytesToRead = reader.remaining();
  84.     } else if (
  85.       messageInfos.buffer.length + reader.remaining() >=
  86.       messageInfos.dataLength
  87.     ) {
  88.       // there is enough bytes in the buffer to complete the message :)
  89.       bytesToRead = messageInfos.dataLength - messageInfos.buffer.length;
  90.     }
  91.  
  92.     if (bytesToRead === 0) {
  93.       return true;
  94.     }
  95.  
  96.     messageInfos.buffer = Buffer.concat([
  97.       messageInfos.buffer,
  98.       reader.readBuffer(bytesToRead)
  99.     ]);
  100.  
  101.     return true;
  102.   }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement