fiveriverflow

Stream

Feb 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.98 KB | None | 0 0
  1. package server.net.packet;
  2.  
  3. import server.Config;
  4. import server.util.ISAACRandomGen;
  5.  
  6. public class Stream {
  7.  
  8.     public Stream() {
  9.     }
  10.  
  11.     public Stream(byte abyte0[]) {
  12.     buffer = abyte0;
  13.     currentOffset = 0;
  14.     }
  15.  
  16.     public byte readSignedByteA() {
  17.     return (byte) (buffer[currentOffset++] - 128);
  18.     }
  19.  
  20.     public byte readSignedByteC() {
  21.     return (byte) (-buffer[currentOffset++]);
  22.     }
  23.  
  24.     public byte readSignedByteS() {
  25.     return (byte) (128 - buffer[currentOffset++]);
  26.     }
  27.  
  28.     public int readUnsignedByteA() {
  29.     return buffer[currentOffset++] - 128 & 0xff;
  30.     }
  31.  
  32.     public int readUnsignedByteC() {
  33.     return -buffer[currentOffset++] & 0xff;
  34.     }
  35.  
  36.     public int readUnsignedByteS() {
  37.     return 128 - buffer[currentOffset++] & 0xff;
  38.     }
  39.  
  40.     public void writeByteA(int i) {
  41.     ensureCapacity(1);
  42.     buffer[currentOffset++] = (byte) (i + 128);
  43.     }
  44.  
  45.     public void writeByteS(int i) {
  46.     ensureCapacity(1);
  47.     buffer[currentOffset++] = (byte) (128 - i);
  48.     }
  49.  
  50.     public void writeByteC(int i) {
  51.     ensureCapacity(1);
  52.     buffer[currentOffset++] = (byte) (-i);
  53.     }
  54.  
  55.     public int readSignedWordBigEndian() {
  56.     currentOffset += 2;
  57.     int i = ((buffer[currentOffset - 1] & 0xff) << 8) + (buffer[currentOffset - 2] & 0xff);
  58.     if (i > 32767)
  59.         i -= 0x10000;
  60.     return i;
  61.     }
  62.  
  63.     public int readSignedWordA() {
  64.     currentOffset += 2;
  65.     int i = ((buffer[currentOffset - 2] & 0xff) << 8) + (buffer[currentOffset - 1] - 128 & 0xff);
  66.     if (i > 32767)
  67.         i -= 0x10000;
  68.     return i;
  69.     }
  70.  
  71.     public int readSignedWordBigEndianA() {
  72.     currentOffset += 2;
  73.     int i = ((buffer[currentOffset - 1] & 0xff) << 8) + (buffer[currentOffset - 2] - 128 & 0xff);
  74.     if (i > 32767)
  75.         i -= 0x10000;
  76.     return i;
  77.     }
  78.  
  79.     public int readUnsignedWordBigEndian() {
  80.     currentOffset += 2;
  81.     return ((buffer[currentOffset - 1] & 0xff) << 8) + (buffer[currentOffset - 2] & 0xff);
  82.     }
  83.  
  84.     public int readUnsignedWordA() {
  85.     currentOffset += 2;
  86.     return ((buffer[currentOffset - 2] & 0xff) << 8) + (buffer[currentOffset - 1] - 128 & 0xff);
  87.     }
  88.  
  89.     public int readUnsignedWordBigEndianA() {
  90.     currentOffset += 2;
  91.     return ((buffer[currentOffset - 1] & 0xff) << 8) + (buffer[currentOffset - 2] - 128 & 0xff);
  92.     }
  93.  
  94.     public void writeWordBigEndianA(int i) {
  95.     ensureCapacity(2);
  96.     buffer[currentOffset++] = (byte) (i + 128);
  97.     buffer[currentOffset++] = (byte) (i >> 8);
  98.     }
  99.  
  100.     public void writeWordA(int i) {
  101.     ensureCapacity(2);
  102.     buffer[currentOffset++] = (byte) (i >> 8);
  103.     buffer[currentOffset++] = (byte) (i + 128);
  104.     }
  105.  
  106.     public void writeWordBigEndian_dup(int i) {
  107.     ensureCapacity(2);
  108.     buffer[currentOffset++] = (byte) i;
  109.     buffer[currentOffset++] = (byte) (i >> 8);
  110.     }
  111.  
  112.     public int readDWord_v1() {
  113.     currentOffset += 4;
  114.     return ((buffer[currentOffset - 2] & 0xff) << 24) + ((buffer[currentOffset - 1] & 0xff) << 16)
  115.         + ((buffer[currentOffset - 4] & 0xff) << 8) + (buffer[currentOffset - 3] & 0xff);
  116.     }
  117.  
  118.     public int readDWord_v2() {
  119.     currentOffset += 4;
  120.     return ((buffer[currentOffset - 3] & 0xff) << 24) + ((buffer[currentOffset - 4] & 0xff) << 16)
  121.         + ((buffer[currentOffset - 1] & 0xff) << 8) + (buffer[currentOffset - 2] & 0xff);
  122.     }
  123.  
  124.     public void writeDWord_v1(int i) {
  125.     ensureCapacity(4);
  126.     buffer[currentOffset++] = (byte) (i >> 8);
  127.     buffer[currentOffset++] = (byte) i;
  128.     buffer[currentOffset++] = (byte) (i >> 24);
  129.     buffer[currentOffset++] = (byte) (i >> 16);
  130.     }
  131.  
  132.     public void writeDWord_v2(int i) {
  133.     ensureCapacity(4);
  134.     buffer[currentOffset++] = (byte) (i >> 16);
  135.     buffer[currentOffset++] = (byte) (i >> 24);
  136.     buffer[currentOffset++] = (byte) i;
  137.     buffer[currentOffset++] = (byte) (i >> 8);
  138.     }
  139.  
  140.     public void readBytes_reverse(byte abyte0[], int i, int j) {
  141.     for (int k = (j + i) - 1; k >= j; k--)
  142.         abyte0[k] = buffer[currentOffset++];
  143.  
  144.     }
  145.  
  146.     public void writeBytes_reverse(byte abyte0[], int i, int j) {
  147.     ensureCapacity(i);
  148.     for (int k = (j + i) - 1; k >= j; k--)
  149.         buffer[currentOffset++] = abyte0[k];
  150.  
  151.     }
  152.  
  153.     public void readBytes_reverseA(byte abyte0[], int i, int j) {
  154.     ensureCapacity(i);
  155.     for (int k = (j + i) - 1; k >= j; k--)
  156.         abyte0[k] = (byte) (buffer[currentOffset++] - 128);
  157.  
  158.     }
  159.  
  160.     public void writeBytes_reverseA(byte abyte0[], int i, int j) {
  161.     ensureCapacity(i);
  162.     for (int k = (j + i) - 1; k >= j; k--)
  163.         buffer[currentOffset++] = (byte) (abyte0[k] + 128);
  164.  
  165.     }
  166.  
  167.     public void createFrame(int id) {
  168.     ensureCapacity(1);
  169.     buffer[currentOffset++] = (byte) (id + packetEncryption.getNextKey());
  170.     }
  171.  
  172.     private static final int frameStackSize = 10;
  173.     private int frameStackPtr = -1;
  174.     private int frameStack[] = new int[frameStackSize];
  175.  
  176.     public void createFrameVarSize(int id) {
  177.     ensureCapacity(3);
  178.     buffer[currentOffset++] = (byte) (id + packetEncryption.getNextKey());
  179.     buffer[currentOffset++] = 0;
  180.  
  181.     if (frameStackPtr >= frameStackSize - 1) {
  182.         throw new RuntimeException("Stack overflow");
  183.     } else {
  184.         frameStack[++frameStackPtr] = currentOffset;
  185.     }
  186.     }
  187.  
  188.     public void createFrameVarSizeWord(int id) {
  189.     ensureCapacity(2);
  190.     buffer[currentOffset++] = (byte) (id + packetEncryption.getNextKey());
  191.     writeWord(0);
  192.     if (frameStackPtr >= frameStackSize - 1) {
  193.         throw new RuntimeException("Stack overflow");
  194.     } else
  195.         frameStack[++frameStackPtr] = currentOffset;
  196.     }
  197.  
  198.     public void endFrameVarSize() {
  199.     if (frameStackPtr < 0) {
  200.         throw new RuntimeException("Stack empty");
  201.     } else {
  202.         writeFrameSize(currentOffset - frameStack[frameStackPtr--]);
  203.     }
  204.     }
  205.  
  206.     public void endFrameVarSizeWord() {
  207.     if (frameStackPtr < 0)
  208.         throw new RuntimeException("Stack empty");
  209.     else
  210.         writeFrameSizeWord(currentOffset - frameStack[frameStackPtr--]);
  211.     }
  212.  
  213.     public void writeByte(int i) {
  214.     ensureCapacity(1);
  215.     buffer[currentOffset++] = (byte) i;
  216.     }
  217.  
  218.     public void writeWord(int i) {
  219.     ensureCapacity(2);
  220.     buffer[currentOffset++] = (byte) (i >> 8);
  221.     buffer[currentOffset++] = (byte) i;
  222.     }
  223.  
  224.     public void writeWordBigEndian(int i) {
  225.     ensureCapacity(2);
  226.     buffer[currentOffset++] = (byte) i;
  227.     buffer[currentOffset++] = (byte) (i >> 8);
  228.     }
  229.  
  230.     public void write3Byte(int i) {
  231.     ensureCapacity(3);
  232.     buffer[currentOffset++] = (byte) (i >> 16);
  233.     buffer[currentOffset++] = (byte) (i >> 8);
  234.     buffer[currentOffset++] = (byte) i;
  235.     }
  236.  
  237.     public void writeDWord(int i) {
  238.     ensureCapacity(4);
  239.     buffer[currentOffset++] = (byte) (i >> 24);
  240.     buffer[currentOffset++] = (byte) (i >> 16);
  241.     buffer[currentOffset++] = (byte) (i >> 8);
  242.     buffer[currentOffset++] = (byte) i;
  243.     }
  244.  
  245.     public void writeDWordBigEndian(int i) {
  246.     ensureCapacity(4);
  247.     buffer[currentOffset++] = (byte) i;
  248.     buffer[currentOffset++] = (byte) (i >> 8);
  249.     buffer[currentOffset++] = (byte) (i >> 16);
  250.     buffer[currentOffset++] = (byte) (i >> 24);
  251.     }
  252.  
  253.     public void writeQWord(long l) {
  254.     ensureCapacity(8);
  255.     buffer[currentOffset++] = (byte) (int) (l >> 56);
  256.     buffer[currentOffset++] = (byte) (int) (l >> 48);
  257.     buffer[currentOffset++] = (byte) (int) (l >> 40);
  258.     buffer[currentOffset++] = (byte) (int) (l >> 32);
  259.     buffer[currentOffset++] = (byte) (int) (l >> 24);
  260.     buffer[currentOffset++] = (byte) (int) (l >> 16);
  261.     buffer[currentOffset++] = (byte) (int) (l >> 8);
  262.     buffer[currentOffset++] = (byte) (int) l;
  263.     }
  264.  
  265.     public void writeString(java.lang.String s) {
  266.     ensureCapacity(s.length());
  267.     System.arraycopy(s.getBytes(), 0, buffer, currentOffset, s.length());
  268.     currentOffset += s.length();
  269.     buffer[currentOffset++] = 10;
  270.     }
  271.  
  272.     public void writeBytes(byte abyte0[], int i, int j) {
  273.     ensureCapacity(i);
  274.     for (int k = j; k < j + i; k++)
  275.         buffer[currentOffset++] = abyte0[k];
  276.     }
  277.  
  278.     public void writeFrameSize(int i) {
  279.     buffer[currentOffset - i - 1] = (byte) i;
  280.     }
  281.  
  282.     public void writeFrameSizeWord(int i) {
  283.     buffer[currentOffset - i - 2] = (byte) (i >> 8);
  284.     buffer[currentOffset - i - 1] = (byte) i;
  285.     }
  286.  
  287.     public int readUnsignedByte() {
  288.     return buffer[currentOffset++] & 0xff;
  289.     }
  290.  
  291.     public byte readSignedByte() {
  292.     return buffer[currentOffset++];
  293.     }
  294.  
  295.     public int readUnsignedWord() {
  296.     currentOffset += 2;
  297.     return ((buffer[currentOffset - 2] & 0xff) << 8) + (buffer[currentOffset - 1] & 0xff);
  298.     }
  299.  
  300.     public int readSignedWord() {
  301.     currentOffset += 2;
  302.     int i = ((buffer[currentOffset - 2] & 0xff) << 8) + (buffer[currentOffset - 1] & 0xff);
  303.     if (i > 32767)
  304.         i -= 0x10000;
  305.     return i;
  306.     }
  307.  
  308.     public int readDWord() {
  309.     currentOffset += 4;
  310.     return ((buffer[currentOffset - 4] & 0xff) << 24) + ((buffer[currentOffset - 3] & 0xff) << 16)
  311.         + ((buffer[currentOffset - 2] & 0xff) << 8) + (buffer[currentOffset - 1] & 0xff);
  312.     }
  313.  
  314.     public long readQWord() {
  315.     long l = (long) readDWord() & 0xffffffffL;
  316.     long l1 = (long) readDWord() & 0xffffffffL;
  317.     return (l << 32) + l1;
  318.     }
  319.  
  320.     public long readQWord2() {
  321.     final long l = readDWord() & 0xffffffffL;
  322.     final long l1 = readDWord() & 0xffffffffL;
  323.     return (l << 32) + l1;
  324.     }
  325.  
  326.     public java.lang.String readString() {
  327.     int i = currentOffset;
  328.     while (buffer[currentOffset++] != 10)
  329.         ;
  330.     return new String(buffer, i, currentOffset - i - 1);
  331.     }
  332.  
  333.     public void readBytes(byte abyte0[], int i, int j) {
  334.     for (int k = j; k < j + i; k++)
  335.         abyte0[k] = buffer[currentOffset++];
  336.  
  337.     }
  338.  
  339.     public void initBitAccess() {
  340.     bitPosition = currentOffset * 8;
  341.     }
  342.  
  343.     public void writeBits(int numBits, int value) {
  344.     ensureCapacity(((int) Math.ceil(numBits * 8)) * 4);
  345.     int bytePos = bitPosition >> 3;
  346.     int bitOffset = 8 - (bitPosition & 7);
  347.     bitPosition += numBits;
  348.  
  349.     for (; numBits > bitOffset; bitOffset = 8) {
  350.         buffer[bytePos] &= ~bitMaskOut[bitOffset];
  351.         buffer[bytePos++] |= (value >> (numBits - bitOffset)) & bitMaskOut[bitOffset];
  352.  
  353.         numBits -= bitOffset;
  354.     }
  355.     if (numBits == bitOffset) {
  356.         buffer[bytePos] &= ~bitMaskOut[bitOffset];
  357.         buffer[bytePos] |= value & bitMaskOut[bitOffset];
  358.     } else {
  359.         buffer[bytePos] &= ~(bitMaskOut[numBits] << (bitOffset - numBits));
  360.         buffer[bytePos] |= (value & bitMaskOut[numBits]) << (bitOffset - numBits);
  361.     }
  362.     }
  363.  
  364.     public void finishBitAccess() {
  365.     currentOffset = (bitPosition + 7) / 8;
  366.     }
  367.  
  368.     public byte buffer[] = null;
  369.     public int currentOffset = 0;
  370.     public int bitPosition = 0;
  371.  
  372.     public static int bitMaskOut[] = new int[32];
  373.     static {
  374.     for (int i = 0; i < 32; i++)
  375.         bitMaskOut[i] = (1 << i) - 1;
  376.     }
  377.  
  378.     public void ensureCapacity(int len) {
  379.     if ((currentOffset + len + 1) >= buffer.length) {
  380.         byte[] oldBuffer = buffer;
  381.         int newLength = (buffer.length * 2);
  382.         buffer = new byte[newLength];
  383.         System.arraycopy(oldBuffer, 0, buffer, 0, oldBuffer.length);
  384.         ensureCapacity(len);
  385.     }
  386.     }
  387.  
  388.     public void reset() {
  389.     if (!(currentOffset > Config.BUFFER_SIZE)) {
  390.         byte[] oldBuffer = buffer;
  391.         buffer = new byte[Config.BUFFER_SIZE];
  392.         for (int i = 0; i < currentOffset; i++) {
  393.         buffer[i] = oldBuffer[i];
  394.         }
  395.     }
  396.     }
  397.  
  398.     public ISAACRandomGen packetEncryption = null;
  399.  
  400. }
Add Comment
Please, Sign In to add comment