Advertisement
Guest User

blake

a guest
Dec 19th, 2009
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 29.92 KB | None | 0 0
  1. package org.runeforge.net;
  2.  
  3. /**
  4.  * A legal, more safe, and more efficient alternative to the "stream" class
  5.  * found in most wL-based servers. This class performs capacity ensuring and
  6.  * uses more familiar data-type naming conventions. It also has every method you
  7.  * would ever need (and more, I'm afraid) to implement the RuneScape protocol,
  8.  * and applies encryption to the data in the buffer automatically.
  9.  *
  10.  * @author blakeman8192
  11.  */
  12. public class RSBuffer
  13. {
  14.  
  15.     private byte[] buffer;
  16.     private int offset, sizeOffset, bitPosition, length;
  17.     private ISAACCipher encryption, decryption;
  18.     public static int[] bitMask = new int[32];
  19.  
  20.     static
  21.     {
  22.         for (int i = 0; i < bitMask.length; i++)
  23.             RSBuffer.bitMask[i] = (1 << i) - 1;
  24.     }
  25.  
  26.     /**
  27.      * Initializes offset with value 0 and buffer with capacity of 128.
  28.      */
  29.     public RSBuffer()
  30.     {
  31.         offset = 0;
  32.         buffer = new byte[128];
  33.     }
  34.  
  35.     /**
  36.      * Initializes offset with value 0 and buffer with the argued capacity.
  37.      *
  38.      * @param capacity
  39.      *            The capacity of the buffer.
  40.      */
  41.     public RSBuffer(int capacity)
  42.     {
  43.         offset = 0;
  44.         buffer = new byte[capacity];
  45.     }
  46.  
  47.     public RSBuffer(byte[] buffer, int length)
  48.     {
  49.         offset = 0;
  50.         this.buffer = buffer;
  51.         this.length = length;
  52.     }
  53.  
  54.     public int getRemaining()
  55.     {
  56.         return length - offset;
  57.     }
  58.  
  59.     /**
  60.      * Gets the internal buffer.
  61.      */
  62.     public byte[] getBuffer()
  63.     {
  64.         return buffer;
  65.     }
  66.  
  67.     /**
  68.      * Sets the ISAAC cipher of this buffer. Note that this buffer generates the
  69.      * cipher itself from the two keys.
  70.      *
  71.      * @param serverKey
  72.      *            The server key of the cipher.
  73.      * @param clientKey
  74.      *            The client key of the cipher.
  75.      */
  76.     public void setISAACSeed(long serverKey, long clientKey)
  77.     {
  78.         int[] seed = new int[]
  79.         { (int) (clientKey >> 32), (int) clientKey, (int) (serverKey >> 32), (int) serverKey };
  80.         decryption = new ISAACCipher(seed);
  81.         for (int i : seed)
  82.             i += 50;
  83.         encryption = new ISAACCipher(seed);
  84.     }
  85.  
  86.     /**
  87.      * Clears the buffer: sets every value inside the buffer to 0 and sets the
  88.      * offset to 0.
  89.      */
  90.     public void clear()
  91.     {
  92.         for (int i = 0; i < offset; i++)
  93.             buffer[i] = 0;
  94.         offset = 0;
  95.     }
  96.  
  97.     /**
  98.      * Gets the offset of the buffer.
  99.      */
  100.     public int getOffset()
  101.     {
  102.         return offset;
  103.     }
  104.  
  105.     /**
  106.      * Gets the encryption <code>ISAACCipher</code>.
  107.      */
  108.     public ISAACCipher getEncryption()
  109.     {
  110.         return encryption;
  111.     }
  112.  
  113.     /**
  114.      * Gets the decryption <code>ISAACCipher</code>.
  115.      */
  116.     public ISAACCipher getDecryption()
  117.     {
  118.         return decryption;
  119.     }
  120.  
  121.     /**
  122.      * Ensures that the internal buffer can have another byte written to it, if
  123.      * not, the buffer will be doubled in size.
  124.      */
  125.     protected void ensureCapacity()
  126.     {
  127.         if (offset + 1 < buffer.length)
  128.             return;
  129.         byte[] currentBuffer = new byte[buffer.length];
  130.         System.arraycopy(buffer, 0, currentBuffer, 0, offset);
  131.         buffer = new byte[buffer.length * 2];
  132.         System.arraycopy(currentBuffer, 0, buffer, 0, offset);
  133.     }
  134.  
  135.     /**
  136.      * Starts bit access.
  137.      *
  138.      * @return The instance of this class, for chaining.
  139.      */
  140.     public RSBuffer startBitAccess()
  141.     {
  142.         bitPosition = offset * 8;
  143.         return this;
  144.     }
  145.  
  146.     /**
  147.      * Ends bit access.
  148.      *
  149.      * @return The instance of this class, for chaining.
  150.      */
  151.     public RSBuffer endBitAccess()
  152.     {
  153.         offset = (bitPosition + 7) / 8;
  154.         return this;
  155.     }
  156.  
  157.     /**
  158.      * Puts bits into the buffer.
  159.      *
  160.      * @param amount
  161.      *            The amount of bits.
  162.      * @param value
  163.      *            The value of the bits.
  164.      * @return The instance of this class, for chaining.
  165.      */
  166.     public RSBuffer putBits(int amount, int value)
  167.     {
  168.         int byteOffset = bitPosition >> 3;
  169.         int bitOffset = 8 - (bitPosition & 7);
  170.         bitPosition += amount;
  171.         for (; amount > bitOffset; bitOffset = 8)
  172.         {
  173.             buffer[byteOffset] &= ~bitMask[bitOffset];
  174.             buffer[byteOffset++] |= (value >> (amount - bitOffset)) & bitMask[bitOffset];
  175.  
  176.             amount -= bitOffset;
  177.         }
  178.         if (amount == bitOffset)
  179.         {
  180.             buffer[byteOffset] &= ~bitMask[bitOffset];
  181.             buffer[byteOffset] |= value & bitMask[bitOffset];
  182.         } else
  183.         {
  184.             buffer[byteOffset] &= ~(bitMask[amount] << (bitOffset - amount));
  185.             buffer[byteOffset] |= (value & bitMask[amount]) << (bitOffset - amount);
  186.         }
  187.         return this;
  188.     }
  189.  
  190.     /**
  191.      * Creates a packet.
  192.      *
  193.      * @param i
  194.      *            The opcode of the packet.
  195.      * @return The instance of this class, for chaining.
  196.      */
  197.     public RSBuffer createPacket(int i)
  198.     {
  199.         return put(i + encryption.nextInt());
  200.     }
  201.  
  202.     /**
  203.      * Creates a variable-sized packet.
  204.      *
  205.      * @param i
  206.      *            The opcode of the packet.
  207.      * @return The instance of this class, for chaining.
  208.      */
  209.     public RSBuffer createPacketVarSize(int i)
  210.     {
  211.         createPacket(i);
  212.         sizeOffset = offset;
  213.         return put(0);
  214.     }
  215.  
  216.     /**
  217.      * Ends a variable-sized packet.
  218.      *
  219.      * @return The instance of this class, for chaining.
  220.      */
  221.     public RSBuffer endPacketVarSize()
  222.     {
  223.         put(offset - sizeOffset, sizeOffset);
  224.         return this;
  225.     }
  226.  
  227.     /**
  228.      * Creates a variable-sized packet where the size is written as a
  229.      * <code>short</code>.
  230.      *
  231.      * @param i
  232.      *            The opcode of the packet.
  233.      * @return The instance of this class, for chaining.
  234.      */
  235.     public RSBuffer createPacketVarSizeShort(int i)
  236.     {
  237.         createPacket(i);
  238.         sizeOffset = offset;
  239.         return putShort(0);
  240.     }
  241.  
  242.     /**
  243.      * Ends a variable-sized packet where the size is written as a
  244.      * <code>short</code>.
  245.      *
  246.      * @return The instance of this class, for chaining.
  247.      */
  248.     public RSBuffer endPacketVarSizeShort()
  249.     {
  250.         put((offset - sizeOffset) >> 8, sizeOffset++);
  251.         put(offset - sizeOffset, sizeOffset);
  252.         return this;
  253.     }
  254.  
  255.     /**
  256.      * Puts a <code>byte</code> into the buffer.
  257.      *
  258.      * @param i
  259.      *            The <code>byte</code> value to put.
  260.      * @return The instance of this class, for chaining.
  261.      */
  262.     public RSBuffer put(int i)
  263.     {
  264.         ensureCapacity();
  265.         buffer[offset++] = (byte) i;
  266.         return this;
  267.     }
  268.  
  269.     public int getOpcode()
  270.     {
  271.         return get() - decryption.nextInt() & 0xff;
  272.     }
  273.  
  274.     /**
  275.      * Puts a <code>byte</code> into the argued position in the buffer.
  276.      *
  277.      * @param i
  278.      *            The <code>byte</code> value to put.
  279.      * @param offset
  280.      *            The position of the byte.
  281.      * @return The instance of this class, for chaining.
  282.      */
  283.     public RSBuffer put(int i, int offset)
  284.     {
  285.         buffer[offset] = (byte) i;
  286.         return this;
  287.     }
  288.  
  289.     /**
  290.      * Gets a <code>byte</code> from the buffer, and decrements the offset.
  291.      */
  292.     public int get()
  293.     {
  294.         return buffer[offset++] & 0xff;
  295.     }
  296.  
  297.     /**
  298.      * Gets a special-A <code>byte</code> from the buffer.
  299.      */
  300.     public int getA()
  301.     {
  302.         return get() - 128;
  303.     }
  304.  
  305.     /**
  306.      * Gets a special-C <code>byte</code> from the buffer.
  307.      */
  308.     public int getC()
  309.     {
  310.         return -get();
  311.     }
  312.  
  313.     /**
  314.      * Gets a special-S <code>byte</code> from the buffer.
  315.      */
  316.     public int getS()
  317.     {
  318.         return 128 - get();
  319.     }
  320.  
  321.     /**
  322.      * Gets an <code>short</code> from the buffer.
  323.      */
  324.     public int getShort()
  325.     {
  326.         return (get() << 8) | get();
  327.     }
  328.  
  329.     /**
  330.      * Gets a special-A <code>short</code> from the buffer.
  331.      */
  332.     public int getShortA()
  333.     {
  334.         return (get() << 8) | getA();
  335.     }
  336.  
  337.     /**
  338.      * Gets a special-C <code>short</code> from the buffer.
  339.      */
  340.     public int getShortC()
  341.     {
  342.         return (get() << 8) | getC();
  343.     }
  344.  
  345.     /**
  346.      * Gets a special-S <code>short</code> from the buffer.
  347.      */
  348.     public int getShortS()
  349.     {
  350.         return (get() << 8) | getS();
  351.     }
  352.  
  353.     /**
  354.      * Gets a little-endian <code>short</code> from the buffer.
  355.      */
  356.     public int getLEShort()
  357.     {
  358.         return get() | (get() << 8);
  359.     }
  360.  
  361.     /**
  362.      * Gets a little-endian special-A <code>short</code> from the buffer.
  363.      */
  364.     public int getLEShortA()
  365.     {
  366.         return getA() | (get() << 8);
  367.     }
  368.  
  369.     /**
  370.      * Gets a little-endian special-C <code>short</code> from the buffer.
  371.      */
  372.     public int getLEShortC()
  373.     {
  374.         return getC() | (get() << 8);
  375.     }
  376.  
  377.     /**
  378.      * Gets a little-endian special-S <code>short</code> from the buffer.
  379.      */
  380.     public int getLEShortS()
  381.     {
  382.         return getS() | (get() << 8);
  383.     }
  384.  
  385.     /**
  386.      * Gets a <code>tribyte</code> from the buffer.
  387.      */
  388.     public int getTribyte()
  389.     {
  390.         return (get() << 16) | getShort();
  391.     }
  392.  
  393.     /**
  394.      * Gets a special-A <code>tribyte</code> from the buffer.
  395.      */
  396.     public int getTribyteA()
  397.     {
  398.         return (get() << 16) | getShortA();
  399.     }
  400.  
  401.     /**
  402.      * Gets a special-C <code>tribyte</code> from the buffer.
  403.      */
  404.     public int getTribyteC()
  405.     {
  406.         return (get() << 16) | getShortC();
  407.     }
  408.  
  409.     /**
  410.      * Gets a special-S <code>tribyte</code> from the buffer.
  411.      */
  412.     public int getTribyteS()
  413.     {
  414.         return (get() << 16) | getShortS();
  415.     }
  416.  
  417.     /**
  418.      * Gets a little-endian <code>tribyte</code> from the buffer.
  419.      */
  420.     public int getLETribyte()
  421.     {
  422.         return getShort() | (get() << 16);
  423.     }
  424.  
  425.     /**
  426.      * Gets a little-endian special-A <code>tribyte</code> from the buffer.
  427.      */
  428.     public int getLETribyteA()
  429.     {
  430.         return getShortA() | (get() << 16);
  431.     }
  432.  
  433.     /**
  434.      * Gets a little-endian special-C <code>tribyte</code> from the buffer.
  435.      */
  436.     public int getLETribyteC()
  437.     {
  438.         return getShortC() | (get() << 16);
  439.     }
  440.  
  441.     /**
  442.      * Gets a little-endian special-S <code>tribyte</code> from the buffer.
  443.      */
  444.     public int getLETribyteS()
  445.     {
  446.         return getShortS() | (get() << 16);
  447.     }
  448.  
  449.     /**
  450.      * Gets an <code>int</code> from the buffer.
  451.      */
  452.     public int getInt()
  453.     {
  454.         return (getShort() << 16) | getShort();
  455.     }
  456.  
  457.     /**
  458.      * Gets an <code>int1</code> from the buffer.
  459.      */
  460.     public int getInt1()
  461.     {
  462.         return (get() << 16) | (get() << 24) | getLEShort();
  463.     }
  464.  
  465.     /**
  466.      * Gets a special-A <code>int1</code> from the buffer.
  467.      */
  468.     public int getInt1A()
  469.     {
  470.         return (get() << 16) | (get() << 24) | getLEShortA();
  471.     }
  472.  
  473.     /**
  474.      * Gets a special-C <code>int1</code> from the buffer.
  475.      */
  476.     public int getInt1C()
  477.     {
  478.         return (get() << 16) | (get() << 24) | getLEShortC();
  479.     }
  480.  
  481.     /**
  482.      * Gets a special-S <code>int1</code> from the buffer.
  483.      */
  484.     public int getInt1S()
  485.     {
  486.         return (get() << 16) | (get() << 24) | getLEShortS();
  487.     }
  488.  
  489.     /**
  490.      * Gets an <code>int2</code> from the buffer.
  491.      */
  492.     public int getInt2()
  493.     {
  494.         return getShort() | (get() << 24) | (get() << 16);
  495.     }
  496.  
  497.     /**
  498.      * Gets a special-A <code>int2</code> from the buffer.
  499.      */
  500.     public int getInt2A()
  501.     {
  502.         return getShortA() | (get() << 24) | (get() << 16);
  503.     }
  504.  
  505.     /**
  506.      * Gets a special-C <code>int2</code> from the buffer.
  507.      */
  508.     public int getInt2C()
  509.     {
  510.         return getShortC() | (get() << 24) | (get() << 16);
  511.     }
  512.  
  513.     /**
  514.      * Gets a special-S <code>int2</code> from the buffer.
  515.      */
  516.     public int getInt2S()
  517.     {
  518.         return getShortS() | (get() << 24) | (get() << 16);
  519.     }
  520.  
  521.     /**
  522.      * Gets a special-A <code>int</code> from the buffer.
  523.      */
  524.     public int getIntA()
  525.     {
  526.         return (getShort() << 16) | getShortA();
  527.     }
  528.  
  529.     /**
  530.      * Gets a special-C <code>int</code> from the buffer.
  531.      */
  532.     public int getIntC()
  533.     {
  534.         return (getShort() << 16) | getShortC();
  535.     }
  536.  
  537.     /**
  538.      * Gets a special-S <code>int</code> from the buffer.
  539.      */
  540.     public int getIntS()
  541.     {
  542.         return (getShort() << 16) | getShortS();
  543.     }
  544.  
  545.     /**
  546.      * Gets a little-endian <code>int</code> from the buffer.
  547.      */
  548.     public int getLEInt()
  549.     {
  550.         return getShort() | (getShort() << 16);
  551.     }
  552.  
  553.     /**
  554.      * Gets a little-endian special-A <code>int</code> from the buffer.
  555.      */
  556.     public int getLEIntA()
  557.     {
  558.         return getShortA() | (getShort() << 16);
  559.     }
  560.  
  561.     /**
  562.      * Gets a little-endian special-C <code>int</code> from the buffer.
  563.      */
  564.     public int getLEIntC()
  565.     {
  566.         return getShortC() | (getShort() << 16);
  567.     }
  568.  
  569.     /**
  570.      * Gets a little-endian special-S <code>int</code> from the buffer.
  571.      */
  572.     public int getLEIntS()
  573.     {
  574.         return getShortS() | (getShort() << 16);
  575.     }
  576.  
  577.     /**
  578.      * Gets a <code>long</code> from the buffer.
  579.      */
  580.     public long getLong()
  581.     {
  582.         return ((long) getInt() << 32L) | (long) getInt();
  583.     }
  584.  
  585.     /**
  586.      * Gets a special-A <code>long</code> from the buffer.
  587.      */
  588.     public long getLongA()
  589.     {
  590.         return ((long) getInt() << 32L) | (long) getIntA();
  591.     }
  592.  
  593.     /**
  594.      * Gets a special-C <code>long</code> from the buffer.
  595.      */
  596.     public long getLongC()
  597.     {
  598.         return ((long) getInt() << 32L) | (long) getIntC();
  599.     }
  600.  
  601.     /**
  602.      * Gets a special-S <code>long</code> from the buffer.
  603.      */
  604.     public long getLongS()
  605.     {
  606.         return ((long) getInt() << 32L) | (long) getIntS();
  607.     }
  608.  
  609.     /**
  610.      * Gets a little-endian <code>long</code> from the buffer.
  611.      */
  612.     public long getLELong()
  613.     {
  614.         return (long) getInt() | ((long) getInt() << 32L);
  615.     }
  616.  
  617.     /**
  618.      * Gets a little-endian special-A <code>long</code> from the buffer.
  619.      */
  620.     public long getLELongA()
  621.     {
  622.         return (long) getIntA() | ((long) getInt() << 32L);
  623.     }
  624.  
  625.     /**
  626.      * Gets a little-endian special-C <code>long</code> from the buffer.
  627.      */
  628.     public long getLELongC()
  629.     {
  630.         return (long) getIntC() | ((long) getInt() << 32L);
  631.     }
  632.  
  633.     /**
  634.      * Gets a little-endian special-S <code>long</code> from the buffer.
  635.      */
  636.     public long getLELongS()
  637.     {
  638.         return (long) getIntS() | ((long) getInt() << 32L);
  639.     }
  640.  
  641.     /**
  642.      * Gets an <code>RSString</code> from the buffer.
  643.      */
  644.     public String getRSString()
  645.     {
  646.         int data;
  647.         StringBuilder builder = new StringBuilder();
  648.         while ((data = get()) != '\n')
  649.             builder.append((char) data);
  650.         return builder.toString();
  651.     }
  652.  
  653.     /**
  654.      * Puts a <code>byte[]</code> into the buffer.
  655.      *
  656.      * @param buffer
  657.      *            The <code>byte[]</code> to put.
  658.      * @return The instance of this class, for chaining.
  659.      */
  660.     public RSBuffer put(byte[] buffer)
  661.     {
  662.         for (byte b : buffer)
  663.             put(b);
  664.         return this;
  665.     }
  666.  
  667.     /**
  668.      * Puts a special-A <code>byte</code> into the buffer.
  669.      *
  670.      * @param i
  671.      *            The special-A <code>byte</code> value to put.
  672.      * @return The instance of this class, for chaining.
  673.      */
  674.     public RSBuffer putA(int i)
  675.     {
  676.         return put(i + 128);
  677.     }
  678.  
  679.     /**
  680.      * Puts a special-C <code>byte</code> into the buffer.
  681.      *
  682.      * @param i
  683.      *            The special-C <code>byte</code> value to put.
  684.      * @return The instance of this class, for chaining.
  685.      */
  686.     public RSBuffer putC(int i)
  687.     {
  688.         return put(-i);
  689.     }
  690.  
  691.     /**
  692.      * Puts a special-S <code>byte</code> into the buffer.
  693.      *
  694.      * @param i
  695.      *            The special-S <code>byte</code> value to put.
  696.      * @return The instance of this class, for chaining.
  697.      */
  698.     public RSBuffer putS(int i)
  699.     {
  700.         return put(128 - i);
  701.     }
  702.  
  703.     /**
  704.      * Puts a <code>short</code> into the buffer.
  705.      *
  706.      * @param i
  707.      *            The <code>short</code> value to put.
  708.      * @return The instance of this class, for chaining.
  709.      */
  710.     public RSBuffer putShort(int i)
  711.     {
  712.         return put(i >> 8).put(i);
  713.     }
  714.  
  715.     /**
  716.      * Puts a special-A <code>short</code> into the buffer.
  717.      *
  718.      * @param i
  719.      *            The special-A <code>short</code> value to put.
  720.      * @return The instance of this class, for chaining.
  721.      */
  722.     public RSBuffer putShortA(int i)
  723.     {
  724.         return put(i >> 8).putA(i);
  725.     }
  726.  
  727.     /**
  728.      * Puts a special-C <code>short</code> into the buffer.
  729.      *
  730.      * @param i
  731.      *            The special-C <code>short</code> value to put.
  732.      * @return The instance of this class, for chaining.
  733.      */
  734.     public RSBuffer putShortC(int i)
  735.     {
  736.         return put(i >> 8).putC(i);
  737.     }
  738.  
  739.     /**
  740.      * Puts a special-S <code>short</code> into the buffer.
  741.      *
  742.      * @param i
  743.      *            The special-S <code>short</code> value to put.
  744.      * @return The instance of this class, for chaining.
  745.      */
  746.     public RSBuffer putShortS(int i)
  747.     {
  748.         return put(i >> 8).putS(i);
  749.     }
  750.  
  751.     /**
  752.      * Puts a little-endian <code>short</code> into the buffer.
  753.      *
  754.      * @param i
  755.      *            The <code>short</code> value to put.
  756.      * @return The instance of this class, for chaining.
  757.      */
  758.     public RSBuffer putLEShort(int i)
  759.     {
  760.         return put(i).put(i >> 8);
  761.     }
  762.  
  763.     /**
  764.      * Puts a little-endian special-A <code>short</code> into the buffer.
  765.      *
  766.      * @param i
  767.      *            The special-A <code>short</code> value to put.
  768.      * @return The instance of this class, for chaining.
  769.      */
  770.     public RSBuffer putLEShortA(int i)
  771.     {
  772.         return putA(i).put(i >> 8);
  773.     }
  774.  
  775.     /**
  776.      * Puts a little-endian special-C <code>short</code> into the buffer.
  777.      *
  778.      * @param i
  779.      *            The special-C <code>short</code> value to put.
  780.      * @return The instance of this class, for chaining.
  781.      */
  782.     public RSBuffer putLEShortC(int i)
  783.     {
  784.         return putC(i).put(i >> 8);
  785.     }
  786.  
  787.     /**
  788.      * Puts a little-endian special-S <code>short</code> into the buffer.
  789.      *
  790.      * @param i
  791.      *            The special-S <code>short</code> value to put.
  792.      * @return The instance of this class, for chaining.
  793.      */
  794.     public RSBuffer putLEShortS(int i)
  795.     {
  796.         return putS(i).put(i >> 8);
  797.     }
  798.  
  799.     /**
  800.      * Puts a <code>tribyte</code> into the buffer.
  801.      *
  802.      * @param i
  803.      *            The <code>tribyte</code> value to put.
  804.      * @return The instance of this class, for chaining.
  805.      */
  806.     public RSBuffer putTribyte(int i)
  807.     {
  808.         return put(i >> 16).putShort(i);
  809.     }
  810.  
  811.     /**
  812.      * Puts a special-A <code>tribyte</code> into the buffer.
  813.      *
  814.      * @param i
  815.      *            The special-A <code>tribyte</code> value to put.
  816.      * @return The instance of this class, for chaining.
  817.      */
  818.     public RSBuffer putTribyteA(int i)
  819.     {
  820.         return put(i >> 16).putShortA(i);
  821.     }
  822.  
  823.     /**
  824.      * Puts a special-C <code>tribyte</code> into the buffer.
  825.      *
  826.      * @param i
  827.      *            The special-C <code>tribyte</code> value to put.
  828.      * @return The instance of this class, for chaining.
  829.      */
  830.     public RSBuffer putTribyteC(int i)
  831.     {
  832.         return put(i >> 16).putShortC(i);
  833.     }
  834.  
  835.     /**
  836.      * Puts a special-S <code>tribyte</code> into the buffer.
  837.      *
  838.      * @param i
  839.      *            The special-S <code>tribyte</code> value to put.
  840.      * @return The instance of this class, for chaining.
  841.      */
  842.     public RSBuffer putTribyteS(int i)
  843.     {
  844.         return put(i >> 16).putShortS(i);
  845.     }
  846.  
  847.     /**
  848.      * Puts a little-endian <code>tribyte</code> into the buffer.
  849.      *
  850.      * @param i
  851.      *            The <code>tribyte</code> value to put.
  852.      * @return The instance of this class, for chaining.
  853.      */
  854.     public RSBuffer putLETribyte(int i)
  855.     {
  856.         return putLEShort(i).put(i >> 16);
  857.     }
  858.  
  859.     /**
  860.      * Puts a little-endian special-A <code>tribyte</code> into the buffer.
  861.      *
  862.      * @param i
  863.      *            The special-A <code>tribyte</code> value to put.
  864.      * @return The instance of this class, for chaining.
  865.      */
  866.     public RSBuffer putLETribyteA(int i)
  867.     {
  868.         return putLEShortA(i).put(i >> 16);
  869.     }
  870.  
  871.     /**
  872.      * Puts a little-endian special-C <code>tribyte</code> into the buffer.
  873.      *
  874.      * @param i
  875.      *            The special-C <code>tribyte</code> value to put.
  876.      * @return The instance of this class, for chaining.
  877.      */
  878.     public RSBuffer putLETribyteC(int i)
  879.     {
  880.         return putLEShortC(i).put(i >> 16);
  881.     }
  882.  
  883.     /**
  884.      * Puts a little-endian special-S <code>tribyte</code> into the buffer.
  885.      *
  886.      * @param i
  887.      *            The special-S <code>tribyte</code> value to put.
  888.      * @return The instance of this class, for chaining.
  889.      */
  890.     public RSBuffer putLETribyteS(int i)
  891.     {
  892.         return putLEShortS(i).put(i >> 16);
  893.     }
  894.  
  895.     /**
  896.      * Puts an <code>int</code> into the buffer.
  897.      *
  898.      * @param i
  899.      *            The <code>int</code> value to put.
  900.      * @return The instance of this class, for chaining.
  901.      */
  902.     public RSBuffer putInt(int i)
  903.     {
  904.         return putShort(i >> 16).putShort(i);
  905.     }
  906.  
  907.     /**
  908.      * Puts an <code>int1</code> into the buffer.
  909.      *
  910.      * @param i
  911.      *            The <code>int1</code> value to put.
  912.      * @return The instance of this class, for chaining.
  913.      */
  914.     public RSBuffer putInt1(int i)
  915.     {
  916.         return putShort(i).put(i >> 24).put(i >> 16);
  917.     }
  918.  
  919.     /**
  920.      * Puts a special-A <code>int1</code> into the buffer.
  921.      *
  922.      * @param i
  923.      *            The special-A <code>int1</code> value to put.
  924.      * @return The instance of this class, for chaining.
  925.      */
  926.     public RSBuffer putInt1A(int i)
  927.     {
  928.         return putShortA(i).put(i >> 24).put(i >> 16);
  929.     }
  930.  
  931.     /**
  932.      * Puts a special-C <code>int1</code> into the buffer.
  933.      *
  934.      * @param i
  935.      *            The special-C <code>int1</code> value to put.
  936.      * @return The instance of this class, for chaining.
  937.      */
  938.     public RSBuffer putInt1C(int i)
  939.     {
  940.         return putShortC(i).put(i >> 24).put(i >> 16);
  941.     }
  942.  
  943.     /**
  944.      * Puts a special-S <code>int1</code> into the buffer.
  945.      *
  946.      * @param i
  947.      *            The special-S <code>int1</code> value to put.
  948.      * @return The instance of this class, for chaining.
  949.      */
  950.     public RSBuffer putInt1S(int i)
  951.     {
  952.         return putShortS(i).put(i >> 24).put(i >> 16);
  953.     }
  954.  
  955.     /**
  956.      * Puts an <code>int2</code> into the buffer.
  957.      *
  958.      * @param i
  959.      *            The <code>int2</code> value to put.
  960.      * @return The instance of this class, for chaining.
  961.      */
  962.     public RSBuffer putInt2(int i)
  963.     {
  964.         return put(i >> 16).put(i >> 24).putLEShort(i);
  965.     }
  966.  
  967.     /**
  968.      * Puts a special-A <code>int2</code> into the buffer.
  969.      *
  970.      * @param i
  971.      *            The special-A <code>int2</code> value to put.
  972.      * @return The instance of this class, for chaining.
  973.      */
  974.     public RSBuffer putInt2A(int i)
  975.     {
  976.         return put(i >> 16).put(i >> 24).putLEShortA(i);
  977.     }
  978.  
  979.     /**
  980.      * Puts a special-C <code>int2</code> into the buffer.
  981.      *
  982.      * @param i
  983.      *            The special-C <code>int2</code> value to put.
  984.      * @return The instance of this class, for chaining.
  985.      */
  986.     public RSBuffer putInt2C(int i)
  987.     {
  988.         return put(i >> 16).put(i >> 24).putLEShortC(i);
  989.     }
  990.  
  991.     /**
  992.      * Puts a special-S <code>int2</code> into the buffer.
  993.      *
  994.      * @param i
  995.      *            The special-S <code>int2</code> value to put.
  996.      * @return The instance of this class, for chaining.
  997.      */
  998.     public RSBuffer putInt2S(int i)
  999.     {
  1000.         return put(i >> 16).put(i >> 24).putLEShortS(i);
  1001.     }
  1002.  
  1003.     /**
  1004.      * Puts a special-A <code>int</code> into the buffer.
  1005.      *
  1006.      * @param i
  1007.      *            The special-A <code>int</code> value to put.
  1008.      * @return The instance of this class, for chaining.
  1009.      */
  1010.     public RSBuffer putIntA(int i)
  1011.     {
  1012.         return putShortA(i >> 16).putShortA(i);
  1013.     }
  1014.  
  1015.     /**
  1016.      * Puts a special-C <code>int</code> into the buffer.
  1017.      *
  1018.      * @param i
  1019.      *            The special-C <code>int</code> value to put.
  1020.      * @return The instance of this class, for chaining.
  1021.      */
  1022.     public RSBuffer putIntC(int i)
  1023.     {
  1024.         return putShortC(i >> 16).putShortC(i);
  1025.     }
  1026.  
  1027.     /**
  1028.      * Puts a special-S <code>int</code> into the buffer.
  1029.      *
  1030.      * @param i
  1031.      *            The special-S <code>int</code> value to put.
  1032.      * @return The instance of this class, for chaining.
  1033.      */
  1034.     public RSBuffer putIntS(int i)
  1035.     {
  1036.         return putShortS(i >> 16).putShortS(i);
  1037.     }
  1038.  
  1039.     /**
  1040.      * Puts a little-endian <code>int</code> into the buffer.
  1041.      *
  1042.      * @param i
  1043.      *            The <code>int</code> value to put.
  1044.      * @return The instance of this class, for chaining.
  1045.      */
  1046.     public RSBuffer putLEInt(int i)
  1047.     {
  1048.         return putLEShort(i >> 16).putLEShort(i);
  1049.     }
  1050.  
  1051.     /**
  1052.      * Puts a little-endian special-A <code>int</code> into the buffer.
  1053.      *
  1054.      * @param i
  1055.      *            The special-A <code>int</code> value to put.
  1056.      * @return The instance of this class, for chaining.
  1057.      */
  1058.     public RSBuffer putLEIntA(int i)
  1059.     {
  1060.         return putLEShortA(i >> 16).putLEShortA(i);
  1061.     }
  1062.  
  1063.     /**
  1064.      * Puts a little-endian special-C <code>int</code> into the buffer.
  1065.      *
  1066.      * @param i
  1067.      *            The special-C <code>int</code> value to put.
  1068.      * @return The instance of this class, for chaining.
  1069.      */
  1070.     public RSBuffer putLEIntC(int i)
  1071.     {
  1072.         return putLEShortC(i >> 16).putLEShortC(i);
  1073.     }
  1074.  
  1075.     /**
  1076.      * Puts a little-endian special-S <code>int</code> into the buffer.
  1077.      *
  1078.      * @param i
  1079.      *            The special-S <code>int</code> value to put.
  1080.      * @return The instance of this class, for chaining.
  1081.      */
  1082.     public RSBuffer putLEIntS(int i)
  1083.     {
  1084.         return putLEShortS(i >> 16).putLEShortS(i);
  1085.     }
  1086.  
  1087.     /**
  1088.      * Puts a <code>long</code> into the buffer.
  1089.      *
  1090.      * @param i
  1091.      *            The <code>long</code> value to put.
  1092.      * @return The instance of this class, for chaining.
  1093.      */
  1094.     public RSBuffer putLong(long i)
  1095.     {
  1096.         return putInt((int) (i >> 32)).putInt((int) i);
  1097.     }
  1098.  
  1099.     /**
  1100.      * Puts a special-A <code>long</code> into the buffer.
  1101.      *
  1102.      * @param i
  1103.      *            The special-A <code>long</code> value to put.
  1104.      * @return The instance of this class, for chaining.
  1105.      */
  1106.     public RSBuffer putLongA(long i)
  1107.     {
  1108.         return putIntA((int) (i >> 32)).putIntA((int) i);
  1109.     }
  1110.  
  1111.     /**
  1112.      * Puts a special-C <code>long</code> into the buffer.
  1113.      *
  1114.      * @param i
  1115.      *            The special-C <code>long</code> value to put.
  1116.      * @return The instance of this class, for chaining.
  1117.      */
  1118.     public RSBuffer putLongC(long i)
  1119.     {
  1120.         return putIntC((int) (i >> 32)).putIntC((int) i);
  1121.     }
  1122.  
  1123.     /**
  1124.      * Puts a special-S <code>long</code> into the buffer.
  1125.      *
  1126.      * @param i
  1127.      *            The special-S <code>long</code> value to put.
  1128.      * @return The instance of this class, for chaining.
  1129.      */
  1130.     public RSBuffer putLongS(long i)
  1131.     {
  1132.         return putIntS((int) (i >> 32)).putIntS((int) i);
  1133.     }
  1134.  
  1135.     /**
  1136.      * Puts a little-endian <code>long</code> into the buffer.
  1137.      *
  1138.      * @param i
  1139.      *            The <code>long</code> value to put.
  1140.      * @return The instance of this class, for chaining.
  1141.      */
  1142.     public RSBuffer putLELong(long i)
  1143.     {
  1144.         return putLEInt((int) i).putLEInt((int) (i >> 32));
  1145.     }
  1146.  
  1147.     /**
  1148.      * Puts a little-endian special-A <code>long</code> into the buffer.
  1149.      *
  1150.      * @param i
  1151.      *            The special-A <code>long</code> value to put.
  1152.      * @return The instance of this class, for chaining.
  1153.      */
  1154.     public RSBuffer putLELongA(long i)
  1155.     {
  1156.         return putLEIntA((int) i).putLEIntA((int) (i >> 32));
  1157.     }
  1158.  
  1159.     /**
  1160.      * Puts a little-endian special-C <code>long</code> into the buffer.
  1161.      *
  1162.      * @param i
  1163.      *            The special-C <code>long</code> value to put.
  1164.      * @return The instance of this class, for chaining.
  1165.      */
  1166.     public RSBuffer putLELongC(long i)
  1167.     {
  1168.         return putLEIntC((int) i).putLEIntC((int) (i >> 32));
  1169.     }
  1170.  
  1171.     /**
  1172.      * Puts a little-endian special-S <code>long</code> into the buffer.
  1173.      *
  1174.      * @param i
  1175.      *            The special-S <code>long</code> value to put.
  1176.      * @return The instance of this class, for chaining.
  1177.      */
  1178.     public RSBuffer putLELongS(long i)
  1179.     {
  1180.         return putLEIntS((int) i).putLEIntS((int) (i >> 32));
  1181.     }
  1182.  
  1183.     /**
  1184.      * Puts an <code>RSString</code> into the buffer.
  1185.      *
  1186.      * @param s
  1187.      *            The <code>RSString</code> value to write.
  1188.      * @return The instance of this class, for chaining.
  1189.      */
  1190.     public RSBuffer putRSString(String s)
  1191.     {
  1192.         for (byte b : s.getBytes())
  1193.             put(b);
  1194.         return put('\n');
  1195.     }
  1196.  
  1197.     private class ISAACCipher
  1198.     {
  1199.         private static final int SIZEL = 8;
  1200.         private static final int SIZE = 1 << SIZEL;
  1201.         private static final int MASK = (SIZE - 1) << 2;
  1202.         private int count;
  1203.         private int[] rsl;
  1204.         private int[] mem;
  1205.         private int a;
  1206.         private int b;
  1207.         private int c;
  1208.  
  1209.         /**
  1210.          * This constructor creates and initializes an new instance using a
  1211.          * user-provided seed.<br>
  1212.          * Equivalent to <code>randinit(ctx, TRUE)</code> after putting seed in
  1213.          * <code>randctx</code> in the C implementation.
  1214.          *
  1215.          * @param seed
  1216.          *            The seed.
  1217.          */
  1218.         private ISAACCipher(int[] seed)
  1219.         {
  1220.             mem = new int[SIZE];
  1221.             rsl = new int[SIZE];
  1222.             System.arraycopy(seed, 0, rsl, 0, (seed.length <= rsl.length) ? seed.length : rsl.length);
  1223.             init(true);
  1224.         }
  1225.  
  1226.         /**
  1227.          * Generate 256 results.<br>
  1228.          * This is a small (not fast) implementation.
  1229.          */
  1230.         private final void isaac()
  1231.         {
  1232.             int i, x, y;
  1233.             b += ++c;
  1234.             for (i = 0; i < SIZE; ++i)
  1235.             {
  1236.                 x = mem[i];
  1237.                 switch (i & 3)
  1238.                 {
  1239.                 case 0:
  1240.                     a ^= a << 13;
  1241.                     break;
  1242.                 case 1:
  1243.                     a ^= a >>> 6;
  1244.                     break;
  1245.                 case 2:
  1246.                     a ^= a << 2;
  1247.                     break;
  1248.                 case 3:
  1249.                     a ^= a >>> 16;
  1250.                     break;
  1251.                 }
  1252.                 a += mem[(i + SIZE / 2) & (SIZE - 1)];
  1253.                 mem[i] = y = mem[((x) & MASK) >> 2] + a + b;
  1254.                 rsl[i] = b = mem[((y >> SIZEL) & MASK) >> 2] + x;
  1255.             }
  1256.         }
  1257.  
  1258.         /**
  1259.          * Initialize or reinitialize this instance.
  1260.          *
  1261.          * @param flag
  1262.          *            If <code>true</code> then use the seed (which the
  1263.          *            constructor placed in <code>rsl[]</code>) for
  1264.          *            initialization.
  1265.          */
  1266.         private final void init(boolean flag)
  1267.         {
  1268.             int i;
  1269.             int a, b, c, d, e, f, g, h;
  1270.             a = b = c = d = e = f = g = h = 0x9e3779b9;
  1271.             for (i = 0; i < 4; ++i)
  1272.             {
  1273.                 a ^= b << 11;
  1274.                 d += a;
  1275.                 b += c;
  1276.                 b ^= c >>> 2;
  1277.                 e += b;
  1278.                 c += d;
  1279.                 c ^= d << 8;
  1280.                 f += c;
  1281.                 d += e;
  1282.                 d ^= e >>> 16;
  1283.                 g += d;
  1284.                 e += f;
  1285.                 e ^= f << 10;
  1286.                 h += e;
  1287.                 f += g;
  1288.                 f ^= g >>> 4;
  1289.                 a += f;
  1290.                 g += h;
  1291.                 g ^= h << 8;
  1292.                 b += g;
  1293.                 h += a;
  1294.                 h ^= a >>> 9;
  1295.                 c += h;
  1296.                 a += b;
  1297.             }
  1298.             for (i = 0; i < SIZE; i += 8)
  1299.             {
  1300.                 if (flag)
  1301.                 {
  1302.                     a += rsl[i];
  1303.                     b += rsl[i + 1];
  1304.                     c += rsl[i + 2];
  1305.                     d += rsl[i + 3];
  1306.                     e += rsl[i + 4];
  1307.                     f += rsl[i + 5];
  1308.                     g += rsl[i + 6];
  1309.                     h += rsl[i + 7];
  1310.                 }
  1311.                 a ^= b << 11;
  1312.                 d += a;
  1313.                 b += c;
  1314.                 b ^= c >>> 2;
  1315.                 e += b;
  1316.                 c += d;
  1317.                 c ^= d << 8;
  1318.                 f += c;
  1319.                 d += e;
  1320.                 d ^= e >>> 16;
  1321.                 g += d;
  1322.                 e += f;
  1323.                 e ^= f << 10;
  1324.                 h += e;
  1325.                 f += g;
  1326.                 f ^= g >>> 4;
  1327.                 a += f;
  1328.                 g += h;
  1329.                 g ^= h << 8;
  1330.                 b += g;
  1331.                 h += a;
  1332.                 h ^= a >>> 9;
  1333.                 c += h;
  1334.                 a += b;
  1335.                 mem[i] = a;
  1336.                 mem[i + 1] = b;
  1337.                 mem[i + 2] = c;
  1338.                 mem[i + 3] = d;
  1339.                 mem[i + 4] = e;
  1340.                 mem[i + 5] = f;
  1341.                 mem[i + 6] = g;
  1342.                 mem[i + 7] = h;
  1343.             }
  1344.             if (flag)
  1345.             {
  1346.                 for (i = 0; i < SIZE; i += 8)
  1347.                 {
  1348.                     a += mem[i];
  1349.                     b += mem[i + 1];
  1350.                     c += mem[i + 2];
  1351.                     d += mem[i + 3];
  1352.                     e += mem[i + 4];
  1353.                     f += mem[i + 5];
  1354.                     g += mem[i + 6];
  1355.                     h += mem[i + 7];
  1356.                     a ^= b << 11;
  1357.                     d += a;
  1358.                     b += c;
  1359.                     b ^= c >>> 2;
  1360.                     e += b;
  1361.                     c += d;
  1362.                     c ^= d << 8;
  1363.                     f += c;
  1364.                     d += e;
  1365.                     d ^= e >>> 16;
  1366.                     g += d;
  1367.                     e += f;
  1368.                     e ^= f << 10;
  1369.                     h += e;
  1370.                     f += g;
  1371.                     f ^= g >>> 4;
  1372.                     a += f;
  1373.                     g += h;
  1374.                     g ^= h << 8;
  1375.                     b += g;
  1376.                     h += a;
  1377.                     h ^= a >>> 9;
  1378.                     c += h;
  1379.                     a += b;
  1380.                     mem[i] = a;
  1381.                     mem[i + 1] = b;
  1382.                     mem[i + 2] = c;
  1383.                     mem[i + 3] = d;
  1384.                     mem[i + 4] = e;
  1385.                     mem[i + 5] = f;
  1386.                     mem[i + 6] = g;
  1387.                     mem[i + 7] = h;
  1388.                 }
  1389.             }
  1390.             isaac();
  1391.             count = SIZE;
  1392.         }
  1393.  
  1394.         /**
  1395.          * Get a random integer value.
  1396.          */
  1397.         public final int nextInt()
  1398.         {
  1399.             if (0 == count--)
  1400.             {
  1401.                 isaac();
  1402.                 count = SIZE - 1;
  1403.             }
  1404.  
  1405.             return (rsl[count]);
  1406.         }
  1407.  
  1408.     }
  1409.  
  1410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement