Guest User

Untitled

a guest
Apr 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.        
  2. var bigint = require('bigint');
  3.  
  4.  
  5. function encodeName(s) {
  6.     var l = bigint(0);
  7.     for (var i = 0; i < s.length && i < 12; i++) {
  8.         var c = s.charCodeAt(i);
  9.         l = l.mul(37);
  10.         l = (c >= 65 && c <= 90) ? l.add(l.add(c - 65)) : (c >= 97 && c <= 122) ? l.add(l.add(c - 97)) : (c >= 48 && c <= 57) ? l.add(27 + c - 48) : bigint(0);
  11.     }
  12.     for (;
  13.     (l.mod(37).eq(0)) && l.ne(0); l = l.div(37));
  14.     return l.toBuffer();
  15. }
  16.  
  17. function getSizeForType(type) {
  18.     switch (type) {
  19.     default:
  20.     case 'byte':
  21.         return 1;
  22.     case 'short':
  23.         return 2;
  24.     case 'medium':
  25.         return 3;
  26.     case 'int':
  27.         return 4;
  28.     case 'long':
  29.         return 8;
  30.     }
  31. }
  32.  
  33. function toArray(val, size) {
  34.     var shift, _ref, _results, _step;
  35.     _results = [];
  36.     for (shift = 0, _ref = (size - 1) << 3, _step = 8;
  37.         0 <= _ref ? shift <= _ref : shift >= _ref; shift += _step)
  38.     _results.push((val >> shift) & 0xFF);
  39.     return _results;
  40. }
  41.  
  42. function PacketBuffer(buffer) {
  43.     if (typeof buffer === 'number') {
  44.         buffer = new Buffer(buffer);
  45.         for (var i = 0; i < buffer.length; i++)
  46.             buffer[i] = 0;
  47.     }
  48.     this.buffer = buffer;
  49.     this.position = 0;
  50.     this.bitPosition = 0;
  51.     this.bitAccess = false;
  52. }
  53.  
  54. require('util').inherits(PacketBuffer, require('events').EventEmitter);
  55.  
  56. PacketBuffer.prototype.getByte = function getByte(index) {
  57.     if (this.bitAccess) throw 'Can\'t do that in bit mode!';
  58.     return this.buffer[index || this.position++];
  59. };
  60.  
  61. PacketBuffer.prototype.getShort = function getShort(index) {
  62.     var b1 = this.getByte(index) & 0xFF;
  63.     var b2 = this.getByte(index) & 0xFF;
  64.     return (b1 << 8) | b2;    
  65. }
  66.  
  67. PacketBuffer.prototype.getInt = function getInt(index) {
  68.     var b1 = this.getByte(index) & 0xFF;
  69.     var b2 = this.getByte(index) & 0xFF;
  70.     var b3 = this.getByte(index) & 0xFF;
  71.     var b4 = this.getByte(index) & 0xFF;
  72.     return (b1 << 24) | (b2 << 16) |(b3 << 8)  | b4 ;
  73. }
  74.  
  75.  
  76. PacketBuffer.prototype.getString = function getString(index) {
  77.     var string = '';
  78.     var read = 0;
  79.     while((read = this.getByte() & 0xFF) != 10) {
  80.         string += String.fromCharCode(read);
  81.     }
  82.     return string;  
  83. }
  84. PacketBuffer.prototype.remaining = function () {
  85.     return this.buffer.length - this.position;
  86. }
  87.  
  88. PacketBuffer.prototype.putByte = function putByte(index, value) {
  89.     if (this.bitAccess) throw 'Can\'t do that in bit mode!';
  90.     if (value !== undefined) {
  91.         this.buffer[index] = value;
  92.     } else {
  93.         this.buffer[this.position++] = index;
  94.     }
  95.     return this;
  96. };
  97.  
  98. PacketBuffer.prototype.startBitAccess = function () {
  99.     this.bitAccess = true;
  100.     this.bitPosition = this.position * 8;
  101.     return this;
  102. };
  103.  
  104. PacketBuffer.prototype.finishBitAccess = function () {
  105.     this.bitAccess = false;
  106.     this.position = this.size = Math.ceil((this.bitPosition + 7) / 8)
  107.     return this;
  108. };
  109.  
  110.  
  111. PacketBuffer.prototype.putBits = function putBits(numBits, value) {
  112.     if (!this.bitAccess) throw 'Can\'t do that in byte mode!';
  113.     var bytePos = this.bitPosition >> 3;
  114.     var bitOffset = 8 - (this.bitPosition & 7);
  115.     this.bitPosition += numBits;
  116.  
  117.     for (; numBits > bitOffset; bitOffset = 8) {
  118.         this.buffer[bytePos] &= ~ ((1 << numBits) - 1);
  119.         this.buffer[bytePos++] |= (value >> (numBits - bitOffset)) & ((1 << numBits) - 1);
  120.         numBits -= bitOffset;
  121.     }
  122.     if (numBits == bitOffset) {
  123.         this.buffer[bytePos] &= ~ ((1 << numBits) - 1);
  124.         this.buffer[bytePos] |= value & ((1 << numBits) - 1);
  125.     } else {
  126.         this.buffer[bytePos] &= ~ (((1 << numBits) - 1) << (bitOffset - numBits));
  127.         this.buffer[bytePos] |= (value & ((1 << numBits) - 1)) << (bitOffset - numBits);
  128.     }
  129.     return this;
  130. };
  131.  
  132. PacketBuffer.prototype.get = function (info) {
  133.     switch (info.type) {
  134.         case 'short': return this.getShort() & 0xFFFF;
  135.         case 'byte': return this.getByte() & 0xFF;
  136.         case 'int': return this.getInt();
  137.         case 'string': return this.getString();
  138.     }
  139.     if (typeof info.type === 'object') {
  140.         var retval = [];
  141.         var type = info.type[0];
  142.         var nelem = info.type[1];
  143.  
  144.         for(var i = 0; i < nelem; i++) {
  145.             retval.push(this.get({type:type}));
  146.         }
  147.         return retval;
  148.     }
  149. }
  150. PacketBuffer.prototype.put = function (info, data) {
  151.     if (info.value !== undefined) {
  152.         data = info.value;
  153.     }
  154.     if (info.type === 'bits') {
  155.         this.putBits(info.size, data);
  156.         return this;
  157.     }
  158.     if (info.type === 'string') {
  159.         for (var i = 0; i < data.length; i++)
  160.             this.putByte(data.charCodeAt(i));
  161.         this.putByte(10);
  162.         return this;
  163.     }
  164.  
  165.     if (info.type === 'name') {
  166.         var tmp = encodeName(data);
  167.         for (var i = 0; i < 8; i++)
  168.         this.putByte(0);
  169.         this.position -= tmp.length;
  170.         tmp.copy(this.buffer, this.position, 0, tmp.length);
  171.         this.position += tmp.length;
  172.         return;
  173.     }
  174.     var size = getSizeForType(info.type);
  175.     var tmp = toArray(data, size);
  176.     switch (info.transform) {
  177.     case 'a':
  178.     case 'add':
  179.         tmp[0] += 128;
  180.         break;
  181.     }
  182.     switch (info.order) {
  183.     default:
  184.     case 'big':
  185.     case 'network':
  186.         tmp = tmp.reverse();
  187.         break;
  188.     }
  189.     for (var j = 0; j < size; j++) {
  190.         if (info.position !== undefined) {
  191.             this.putByte(info.position, tmp[j]);
  192.         } else {
  193.             this.putByte(tmp[j]);
  194.         }
  195.     }
  196.     return this;
  197. };
  198.  
  199. PacketBuffer.prototype.finallyPut = function (info, dataCB) {
  200.     oldPos = this.position;
  201.     this.put(info, 0);
  202.     this.on('pack', function () {
  203.         info.position = oldPos;
  204.         this.put(info, dataCB(this.position, info.position + getSizeForType(info.type)));
  205.     });
  206.     return this;
  207. };
  208.  
  209. PacketBuffer.prototype.pack = function pack(info, data) {
  210.     var i = 0;
  211.     data = data || [];
  212.     this.emit('pack');
  213.     for (entry in info)
  214.         this.put(info[entry], data[i++]);
  215.     return this.buffer.slice(0, this.position);
  216. };
  217.  
  218. PacketBuffer.prototype.unpack = function unpack(info) {
  219.     var i = 0;
  220.     var packet = {};
  221.     for (entry in info)
  222.         packet[entry] = this.get(info[entry]);
  223.     return packet;
  224. }
  225.  
  226. module.exports.PacketBuffer = PacketBuffer;
  227.  
  228. function Builder () {
  229.     this.data = [];
  230. }
  231.  
  232. Builder.prototype.put = function (val,size,order,transform) {
  233.     switch (typeof val) {
  234.         case 'string':
  235.             for(var i = 0; i < val.length; i++) {
  236.                 this.data = this.data.concat(val.charCodeAt(i));
  237.             }
  238.             break;
  239.         case 'number':
  240.             if(val > 255 || size !== undefined) {
  241.                 if(size === undefined) throw 'you must define a size!';
  242.                 val = toArray(val,size);
  243.                 switch (transform) {
  244.                     case 'A':
  245.                     case 'add':
  246.                         val[0]+=128;
  247.                         break;
  248.                 }
  249.                 if(order === 'big' || order === 'network' ||order === undefined) {
  250.                     val = val.reverse();           
  251.                 }
  252.             }
  253.         default:
  254.             this.data = this.data.concat(val);
  255.             break;
  256.  
  257.     }
  258.     return this;
  259. };
  260. Builder.prototype.build = function () {
  261.     return new Buffer(this.data);
  262. };
  263. module.exports.builder = function () { return new Builder()};
  264.  
  265.  
  266. module.exports.encodeName = encodeName;
Add Comment
Please, Sign In to add comment