Advertisement
fiveriverflow

Untitled

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