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