RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Buffer.java
1package com.osroyale.fs.cache.decoder;
2
3import com.osroyale.net.codec.IsaacCipher;
4
5import java.math.BigInteger;
6import java.nio.ByteBuffer;
7
42
43public final class Buffer {
44
45
46 public void setOffset(int offset) {
47 this.position = offset;
48 }
49
50 public int readSmart() {
51 int value = array[position] & 0xff;
52 if (value < 128)
53 return readUnsignedByte() - 64;
54 else
55 return readUShort() - 49152;
56 }
57 public String readStringNew() {
58 int i = position;
59 while (array[position++] != 0)
60 ;
61 return new String(array, i, position - i - 1);
62 }
63
64 public int readShort2() {
65 position += 2;
66 int i = ((array[position - 2] & 0xff) << 8) + (array[position - 1] & 0xff);
67 if (i > 60000)
68 i = -65535 + i;
69 return i;
70
71 }
72
73 public int readSignedShort() {
74 this.position += 2;
75 int value = ((this.array[this.position - 2] & 0xff) << 8) + (this.array[this.position - 1] & 0xff);
76 if (value > 32767)
77 value -= 0x10000; //theres no errors its just gradle being slow as fck
78
79 return value;
80 }
81
82 public int readShortSmart()
83 {
84 int peek = array[position] & 0xFF;
85 return peek < 128 ? this.readUnsignedByte() - 64 : this.readUnsignedShort() - 0xc000;
86 }
87
88 public int read24Int() {
89 position += 3;
90 return ((array[position - 3] & 0xff) << 16) + ((array[position - 2] & 0xff) << 8) + (array[position - 1] & 0xff);
91 }
92
93 public int readMedium() {
94 this.position += 3;
95 return ((this.array[this.position - 3] & 255) << 16) + (this.array[this.position - 1] & 255) + ((this.array[this.position - 2] & 255) << 8);
96 }
97
98
99 public int readUShort() {
100 position += 2;
101 return ((array[position - 2] & 0xff) << 8)
102 + (array[position - 1] & 0xff);
103 }
104
105 public int readInt() {
106 position += 4;
107 return ((array[position - 4] & 0xff) << 24)
108 + ((array[position - 3] & 0xff) << 16)
109 + ((array[position - 2] & 0xff) << 8)
110 + (array[position - 1] & 0xff);
111 }
112
113 public int readShort() {
114 position += 2;
115 int value = ((array[position - 2] & 0xff) << 8)
116 + (array[position - 1] & 0xff);
117
118 if (value > 32767) {
119 value -= 0x10000;
120 }
121 return value;
122 }
123
124 public int readShortOSRS() {
125 this.position += 2;
126 int var1 = (this.array[this.position - 1] & 255) + ((this.array[this.position - 2] & 255) << 8);
127 if (var1 > 32767) {
128 var1 -= 65536;
129 }
130
131 return var1;
132 }
133
134 final int v(int i) {
135 position += 3;
136 return (0xff & array[position - 3] << 16) + (0xff & array[position - 2] << 8) + (0xff & array[position - 1]);
137 }
138
139 private Buffer() {
140 }
141
142 public Buffer(byte[] array) {
143 this.array = array;
144 position = 0;
145 }
146
147 public static Buffer fromByteBuffer(final ByteBuffer byteBuffer) {
148 final byte[] array = new byte[byteBuffer.capacity()];
149 byteBuffer.get(array);
150 return new Buffer(array);
151 }
152
153 public int readUnsignedShortSmart()
154 {
155 int peek = this.peek() & 0xFF;
156 return peek < 128 ? this.readUnsignedByte() : this.readUnsignedShort() - 0x8000;
157 }
158
159 public int readUnsignedIntSmartShortCompat()
160 {
161 int var1 = 0;
162
163 int var2;
164 for (var2 = this.readUnsignedShortSmart(); var2 == 32767; var2 = this.readUnsignedShortSmart())
165 {
166 var1 += 32767;
167 }
168
169 var1 += var2;
170 return var1;
171 }
172
173 public int readUSmart2() {
174 int baseVal = 0;
175 int lastVal = 0;
176 while ((lastVal = method422()) == 32767) {
177 baseVal += 32767;
178 }
179 return baseVal + lastVal;
180 }
181
182 public String readNewString() {
183 int i = position;
184 while (array[position++] != 0)
185 ;
186 return new String(array, i, position - i - 1);
187 }
188
189 public void writeOpcode(int i) {
190 // System.out.println("Frame: " + i);
191 array[position++] = (byte) (i + encryption.getKey());
192 }
193
194 public void writeByte(int i) {
195 array[position++] = (byte) i;
196 }
197
198 public void writeShort(int i) {
199 array[position++] = (byte) (i >> 8);
200 array[position++] = (byte) i;
201 }
202
203 public void writeDWordBigEndian(int i) {
204 array[position++] = (byte) (i >> 16);
205 array[position++] = (byte) (i >> 8);
206 array[position++] = (byte) i;
207 }
208
209 public void writeDWord(int i) {
210 array[position++] = (byte) (i >> 24);
211 array[position++] = (byte) (i >> 16);
212 array[position++] = (byte) (i >> 8);
213 array[position++] = (byte) i;
214 }
215
216 public void method403(int j) {
217 array[position++] = (byte) j;
218 array[position++] = (byte) (j >> 8);
219 array[position++] = (byte) (j >> 16);
220 array[position++] = (byte) (j >> 24);
221 }
222
223 public void writeQWord(long l) {
224 try {
225 array[position++] = (byte) (int) (l >> 56);
226 array[position++] = (byte) (int) (l >> 48);
227 array[position++] = (byte) (int) (l >> 40);
228 array[position++] = (byte) (int) (l >> 32);
229 array[position++] = (byte) (int) (l >> 24);
230 array[position++] = (byte) (int) (l >> 16);
231 array[position++] = (byte) (int) (l >> 8);
232 array[position++] = (byte) (int) l;
233 } catch (RuntimeException runtimeexception) {
234 throw new RuntimeException();
235 }
236 }
237
238 public void writeString(String s) {
239 // s.getBytes(0, s.length(), buffer, currentOffset); //deprecated
240 System.arraycopy(s.getBytes(), 0, array, position, s.length());
241 position += s.length();
242 array[position++] = 10;
243 }
244
245 public void writeBytes(byte[] abyte0, int i, int j) {
246 for (int k = j; k < j + i; k++)
247 array[position++] = abyte0[k];
248 }
249
250 public void writeBytes(int i) {
251 array[position - i - 1] = (byte) i;
252 }
253
254 public int readUnsignedByte() {
255 return array[position++] & 0xff;
256 }
257
258 public int readSignedSmart() {
259 int value = peek() & 0xff;
260 if (value < 128)
261 return this.readUnsignedByte() - 64;
262 else
263 return this.readUnsignedShort() - 49152;
264 }
265
266 public byte peek()
267 {
268 return array[position];
269 }
270
271 public int readUnsignedShortSmartMinusOne()
272 {
273 int peek = this.peek() & 0xFF;
274 return peek < 128 ? this.readUnsignedByte() - 1 : this.readUnsignedShort() - 0x8001;
275 }
276
277 public int readBigSmart2()
278 {
279 if (peek() < 0)
280 {
281 return readInt() & Integer.MAX_VALUE; // and off sign bit
282 }
283 int value = readUnsignedShort();
284 return value == 32767 ? -1 : value;
285 }
286
287 public byte readSignedByte() {
288 return array[position++];
289 }
290
291 public int readUnsignedShort() {
292 position += 2;
293 return ((array[position - 2] & 0xff) << 8) + (array[position - 1] & 0xff);
294 }
295
296 public int readSignedWord() {
297 position += 2;
298 int i = ((array[position - 2] & 0xff) << 8) + (array[position - 1] & 0xff);
299 if (i > 32767) {
300 i -= 0x10000;
301 }
302 return i;
303 }
304
305 public int getUIncrementalSmart() {
306 int value = 0, remainder;
307 for (remainder = method422(); remainder == 32767; remainder = method422()) {
308 value += 32767;
309 }
310 value += remainder;
311 return value;
312 }
313
314 public int readUnsignedTriByte() {
315 position += 3;
316 return ((array[position - 3] & 0xff) << 16) + ((array[position - 2] & 0xff) << 8) + (array[position - 1] & 0xff);
317 }
318
319 public int readUnsignedInt() {
320 position += 4;
321 return ((array[position - 4] & 0xff) << 24) + ((array[position - 3] & 0xff) << 16) + ((array[position - 2] & 0xff) << 8) + (array[position - 1] & 0xff);
322 }
323
324 public long readUnsignedLong() {
325 long big = (long) readUnsignedInt() & 0xFFFF_FFFFL;
326 long little = (long) readUnsignedInt() & 0xFFFF_FFFFL;
327 return (big << 32) + little;
328 }
329
330 public String readString() {
331 int start = position;
332 while (array[position++] != 10);
333 return new String(array, start, position - start - 1);
334 }
335
336 public String readStringCp1252NullTerminated() {
337 int var1 = this.position;
338
339 while(this.array[++this.position - 1] != 0) {
340 ;
341 }
342
343 int var2 = this.position - var1 - 1;
344 return var2 == 0 ? "" : decodeStringCp1252(this.array, var1, var2);
345 }
346
347 public static String decodeStringCp1252(byte[] var0, int var1, int var2) {
348 char[] var3 = new char[var2];
349 int var4 = 0;
350
351 for(int var5 = 0; var5 < var2; ++var5) {
352 int var6 = var0[var5 + var1] & 255;
353 if (var6 != 0) {
354 if (var6 >= 128 && var6 < 160) {
355 char var7 = cp1252AsciiExtension[var6 - 128];
356 if (var7 == 0) {
357 var7 = '?';
358 }
359
360 var6 = var7;
361 }
362
363 var3[var4++] = (char)var6;
364 }
365 }
366
367 return new String(var3, 0, var4);
368 }
369
370 public static final char[] cp1252AsciiExtension = new char[]{'€', '\u0000', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u0000', 'Ž', '\u0000', '\u0000', '‘', '’', '“', '”', '•', '–', '—', '˜', '™', 'š', '›', 'œ', '\u0000', 'ž', 'Ÿ'};
371
372 public byte[] readBytes() {
373 int start = position;
374 while (array[position++] != 10);
375 byte[] bytes = new byte[position - start - 1];
376 System.arraycopy(array, start, bytes, start - start, position - 1 - start);
377 return bytes;
378 }
379
380 public void readBytes(int amount, int offset, byte[] destination) {
381 for (int l = offset; l < offset + amount; l++)
382 destination[l] = array[position++];
383 }
384
385 public void initBitAccess() {
386 bitPosition = position * 8;
387 }
388
389 public int readBits(int amount) {
390 int bytePos = bitPosition >> 3;
391 int bitOffset = 8 - (bitPosition & 7);
392 int value = 0;
393
394 bitPosition += amount;
395
396 for (; amount > bitOffset; bitOffset = 8) {
397 value += (array[bytePos++] & BIT_MASK[bitOffset]) << amount - bitOffset;
398 amount -= bitOffset;
399 }
400
401 if (amount == bitOffset) {
402 value += array[bytePos] & BIT_MASK[bitOffset];
403 } else {
404 value += array[bytePos] >> bitOffset - amount & BIT_MASK[amount];
405 }
406
407 return value;
408 }
409
410 public void finishBitAccess() {
411 position = (bitPosition + 7) / 8;
412 }
413
414 public int method421() {
415 int i = array[position] & 0xff;
416 if (i < 128)
417 return readUnsignedByte() - 64;
418 else
419 return readUnsignedShort() - 49152;
420 }
421
422 public int method422() {
423 int i = array[position] & 0xff;
424 if (i < 128)
425 return readUnsignedByte();
426 else
427 return readUnsignedShort() - 32768;
428 }
429
430 public void encodeRSA(BigInteger exponent, BigInteger modulus) {
431 int i = position;
432 position = 0;
433 byte[] abyte0 = new byte[i];
434 readBytes(i, 0, abyte0);
435
436 byte[] rsa = new BigInteger(abyte0).modPow(exponent, modulus).toByteArray();
437
438 position = 0;
439 writeByte(rsa.length);
440 writeBytes(rsa, rsa.length, 0);
441 }
442
443 public void writeNegatedByte(int i) {
444 array[position++] = (byte) (-i);
445 }
446
447 public void method425(int j) {
448 array[position++] = (byte) (128 - j);
449 }
450
451 public int readUByteA() {
452 return array[position++] - 128 & 0xff;
453 }
454
455 public int readNegUByte() {
456 return -array[position++] & 0xff;
457 }
458
459 public int readUByteS() {
460 return 128 - array[position++] & 0xff;
461 }
462
463 public byte method429() {
464 return (byte) (-array[position++]);
465 }
466
467 public byte method430() {
468 return (byte) (128 - array[position++]);
469 }
470
471 public void writeLEShort(int i) {
472 array[position++] = (byte) i;
473 array[position++] = (byte) (i >> 8);
474 }
475
476 public void writeShortA(int j) {
477 array[position++] = (byte) (j >> 8);
478 array[position++] = (byte) (j + 128);
479 }
480
481 public void writeLEShortA(int j) {
482 array[position++] = (byte) (j + 128);
483 array[position++] = (byte) (j >> 8);
484 }
485
486 public int readLEUShort() {
487 position += 2;
488 return ((array[position - 1] & 0xff) << 8) + (array[position - 2] & 0xff);
489 }
490
491 public int readUShortA() {
492 position += 2;
493 return ((array[position - 2] & 0xff) << 8) + (array[position - 1] - 128 & 0xff);
494 }
495
496 public int readLEUShortA() {
497 position += 2;
498 return ((array[position - 1] & 0xff) << 8) + (array[position - 2] - 128 & 0xff);
499 }
500
501 public int readLEShort() {
502 position += 2;
503 int j = ((array[position - 1] & 0xff) << 8) + (array[position - 2] & 0xff);
504 if (j > 32767)
505 j -= 0x10000;
506 return j;
507 }
508
509 public int method438() {
510 position += 2;
511 int j = ((array[position - 1] & 0xff) << 8) + (array[position - 2] - 128 & 0xff);
512 if (j > 32767)
513 j -= 0x10000;
514 return j;
515 }
516
517 public int method439() {
518 position += 4;
519 return ((array[position - 2] & 0xff) << 24) + ((array[position - 1] & 0xff) << 16) + ((array[position - 4] & 0xff) << 8) + (array[position - 3] & 0xff);
520 }
521
522 public int method440() {
523 position += 4;
524 return ((array[position - 3] & 0xff) << 24) + ((array[position - 4] & 0xff) << 16) + ((array[position - 1] & 0xff) << 8) + (array[position - 2] & 0xff);
525 }
526
527 public void method441(int i, byte[] abyte0, int j) {
528 for (int k = (i + j) - 1; k >= i; k--)
529 array[position++] = (byte) (abyte0[k] + 128);
530
531 }
532
533 public void readReverseData(int i, int j, byte[] abyte0) {
534 for (int k = (j + i) - 1; k >= j; k--)
535 abyte0[k] = array[position++];
536
537 }
538
539 public float readFloat() {
540 return Float.intBitsToFloat(readInt());
541 }
542
543 public byte[] readBytes(final int length) {
544 final byte[] bytes = new byte[length];
545 System.arraycopy(array, position, bytes, 0, length);
546 position += length;
547 return bytes;
548 }
549
550 public byte[] array;
551 public int position;
552 public int bitPosition;
553 private static final int[] BIT_MASK = {0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 0x1ffff, 0x3ffff, 0x7ffff, 0xfffff, 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff, 0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff, -1};
554 public IsaacCipher encryption;
555 private static int anInt1412;
556
557 public void setPosition(int offset) {
558 this.position = offset;
559 }
560}