/** * Read an integer at the current position. * The current position is incremented. * * @return the value */ public int readInt() { int x = Bits.readInt(data, pos); pos += 4; return x; }
/** * Reads a long value from the byte array at the given position in big-endian * order. * * @param buff * the byte array * @param pos * the position * @return the value */ public static long readLong(byte[] buff, int pos) { return (((long) readInt(buff, pos)) << 32) + (readInt(buff, pos + 4) & 0xffffffffL); }
@Override public void setKey(byte[] b) { int[] key = new int[4]; for (int i = 0; i < 16; i += 4) { key[i / 4] = Bits.readInt(b, i); } int[] r = new int[32]; for (int i = 0, sum = 0; i < 32;) { r[i++] = sum + key[sum & 3]; sum += DELTA; r[i++] = sum + key[ (sum >>> 11) & 3]; } k0 = r[0]; k1 = r[1]; k2 = r[2]; k3 = r[3]; k4 = r[4]; k5 = r[5]; k6 = r[6]; k7 = r[7]; k8 = r[8]; k9 = r[9]; k10 = r[10]; k11 = r[11]; k12 = r[12]; k13 = r[13]; k14 = r[14]; k15 = r[15]; k16 = r[16]; k17 = r[17]; k18 = r[18]; k19 = r[19]; k20 = r[20]; k21 = r[21]; k22 = r[22]; k23 = r[23]; k24 = r[24]; k25 = r[25]; k26 = r[26]; k27 = r[27]; k28 = r[28]; k29 = r[29]; k30 = r[30]; k31 = r[31]; }
/** * Read a variable size integer using Rice coding. * * @param buff the buffer * @param pos the position * @return the integer */ public static int readVariableInt(byte[] buff, int pos) { int x = buff[pos++] & 0xff; if (x < 0x80) { return x; } if (x < 0xc0) { return ((x & 0x3f) << 8) + (buff[pos] & 0xff); } if (x < 0xe0) { return ((x & 0x1f) << 16) + ((buff[pos++] & 0xff) << 8) + (buff[pos] & 0xff); } if (x < 0xf0) { return ((x & 0xf) << 24) + ((buff[pos++] & 0xff) << 16) + ((buff[pos++] & 0xff) << 8) + (buff[pos] & 0xff); } return Bits.readInt(buff, pos); }
private void encryptBlock(byte[] in, byte[] out, int off) { int x0 = Bits.readInt(in, off); int x1 = Bits.readInt(in, off + 4); int x2 = Bits.readInt(in, off + 8); int x3 = Bits.readInt(in, off + 12); int k = key; x0 = Integer.rotateLeft(x0 ^ k, x1); x2 = Integer.rotateLeft(x2 ^ k, x1); x1 = Integer.rotateLeft(x1 ^ k, x0); x3 = Integer.rotateLeft(x3 ^ k, x0); Bits.writeInt(out, off, x0); Bits.writeInt(out, off + 4, x1); Bits.writeInt(out, off + 8, x2); Bits.writeInt(out, off + 12, x3); }
private void decryptBlock(byte[] in, byte[] out, int off) { int x0 = Bits.readInt(in, off); int x1 = Bits.readInt(in, off + 4); int x2 = Bits.readInt(in, off + 8); int x3 = Bits.readInt(in, off + 12); int k = key; x1 = Integer.rotateRight(x1, x0) ^ k; x3 = Integer.rotateRight(x3, x0) ^ k; x0 = Integer.rotateRight(x0, x1) ^ k; x2 = Integer.rotateRight(x2, x1) ^ k; Bits.writeInt(out, off, x0); Bits.writeInt(out, off + 4, x1); Bits.writeInt(out, off + 8, x2); Bits.writeInt(out, off + 12, x3); }
private void decryptBlock(byte[] in, byte[] out, int off) { int[] k = decKey; int x0 = Bits.readInt(in, off) ^ k[0]; int x1 = Bits.readInt(in, off + 4) ^ k[1]; int x2 = Bits.readInt(in, off + 8) ^ k[2]; int x3 = Bits.readInt(in, off + 12) ^ k[3]; int y0 = RT0[(x0 >> 24) & 255] ^ RT1[(x3 >> 16) & 255] ^ RT2[(x2 >> 8) & 255] ^ RT3[x1 & 255] ^ k[4];
private void encryptBlock(byte[] in, byte[] out, int off) { int[] k = encKey; int x0 = Bits.readInt(in, off) ^ k[0]; int x1 = Bits.readInt(in, off + 4) ^ k[1]; int x2 = Bits.readInt(in, off + 8) ^ k[2]; int x3 = Bits.readInt(in, off + 12) ^ k[3]; int y0 = FT0[(x0 >> 24) & 255] ^ FT1[(x1 >> 16) & 255] ^ FT2[(x2 >> 8) & 255] ^ FT3[x3 & 255] ^ k[4];