Advertisement
Guest User

Untitled

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