Unverified 提交 b4f16fe3 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #778 from katzyn/misc

Reduce code duplication
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
*/ */
package org.h2.security; package org.h2.security;
import org.h2.util.Bits;
/** /**
* An implementation of the AES block cipher algorithm, * An implementation of the AES block cipher algorithm,
* also known as Rijndael. Only AES-128 is supported by this class. * also known as Rijndael. Only AES-128 is supported by this class.
...@@ -135,14 +137,10 @@ public class AES implements BlockCipher { ...@@ -135,14 +137,10 @@ public class AES implements BlockCipher {
private void encryptBlock(byte[] in, byte[] out, int off) { private void encryptBlock(byte[] in, byte[] out, int off) {
int[] k = encKey; int[] k = encKey;
int x0 = ((in[off] << 24) | ((in[off + 1] & 255) << 16) int x0 = Bits.readInt(in, off) ^ k[0];
| ((in[off + 2] & 255) << 8) | (in[off + 3] & 255)) ^ k[0]; int x1 = Bits.readInt(in, off + 4) ^ k[1];
int x1 = ((in[off + 4] << 24) | ((in[off + 5] & 255) << 16) int x2 = Bits.readInt(in, off + 8) ^ k[2];
| ((in[off + 6] & 255) << 8) | (in[off + 7] & 255)) ^ k[1]; int x3 = Bits.readInt(in, off + 12) ^ k[3];
int x2 = ((in[off + 8] << 24) | ((in[off + 9] & 255) << 16)
| ((in[off + 10] & 255) << 8) | (in[off + 11] & 255)) ^ k[2];
int x3 = ((in[off + 12] << 24) | ((in[off + 13] & 255) << 16)
| ((in[off + 14] & 255) << 8) | (in[off + 15] & 255)) ^ k[3];
int y0 = FT0[(x0 >> 24) & 255] ^ FT1[(x1 >> 16) & 255] int y0 = FT0[(x0 >> 24) & 255] ^ FT1[(x1 >> 16) & 255]
^ FT2[(x2 >> 8) & 255] ^ FT3[x3 & 255] ^ k[4]; ^ FT2[(x2 >> 8) & 255] ^ FT3[x3 & 255] ^ k[4];
int y1 = FT0[(x1 >> 24) & 255] ^ FT1[(x2 >> 16) & 255] int y1 = FT0[(x1 >> 24) & 255] ^ FT1[(x2 >> 16) & 255]
...@@ -223,26 +221,18 @@ public class AES implements BlockCipher { ...@@ -223,26 +221,18 @@ public class AES implements BlockCipher {
| (FS[(y0 >> 8) & 255] << 8) | FS[y1 & 255]) ^ k[42]; | (FS[(y0 >> 8) & 255] << 8) | FS[y1 & 255]) ^ k[42];
x3 = ((FS[(y3 >> 24) & 255] << 24) | (FS[(y0 >> 16) & 255] << 16) x3 = ((FS[(y3 >> 24) & 255] << 24) | (FS[(y0 >> 16) & 255] << 16)
| (FS[(y1 >> 8) & 255] << 8) | FS[y2 & 255]) ^ k[43]; | (FS[(y1 >> 8) & 255] << 8) | FS[y2 & 255]) ^ k[43];
out[off] = (byte) (x0 >> 24); out[off+1] = (byte) (x0 >> 16); Bits.writeInt(out, off, x0);
out[off+2] = (byte) (x0 >> 8); out[off+3] = (byte) x0; Bits.writeInt(out, off + 4, x1);
out[off+4] = (byte) (x1 >> 24); out[off+5] = (byte) (x1 >> 16); Bits.writeInt(out, off + 8, x2);
out[off+6] = (byte) (x1 >> 8); out[off+7] = (byte) x1; Bits.writeInt(out, off + 12, x3);
out[off+8] = (byte) (x2 >> 24); out[off+9] = (byte) (x2 >> 16);
out[off+10] = (byte) (x2 >> 8); out[off+11] = (byte) x2;
out[off+12] = (byte) (x3 >> 24); out[off+13] = (byte) (x3 >> 16);
out[off+14] = (byte) (x3 >> 8); out[off+15] = (byte) x3;
} }
private void decryptBlock(byte[] in, byte[] out, int off) { private void decryptBlock(byte[] in, byte[] out, int off) {
int[] k = decKey; int[] k = decKey;
int x0 = ((in[off] << 24) | ((in[off + 1] & 255) << 16) int x0 = Bits.readInt(in, off) ^ k[0];
| ((in[off + 2] & 255) << 8) | (in[off + 3] & 255)) ^ k[0]; int x1 = Bits.readInt(in, off + 4) ^ k[1];
int x1 = ((in[off + 4] << 24) | ((in[off + 5] & 255) << 16) int x2 = Bits.readInt(in, off + 8) ^ k[2];
| ((in[off + 6] & 255) << 8) | (in[off + 7] & 255)) ^ k[1]; int x3 = Bits.readInt(in, off + 12) ^ k[3];
int x2 = ((in[off + 8] << 24) | ((in[off + 9] & 255) << 16)
| ((in[off + 10] & 255) << 8) | (in[off + 11] & 255)) ^ k[2];
int x3 = ((in[off + 12] << 24) | ((in[off + 13] & 255) << 16)
| ((in[off + 14] & 255) << 8) | (in[off + 15] & 255)) ^ k[3];
int y0 = RT0[(x0 >> 24) & 255] ^ RT1[(x3 >> 16) & 255] int y0 = RT0[(x0 >> 24) & 255] ^ RT1[(x3 >> 16) & 255]
^ RT2[(x2 >> 8) & 255] ^ RT3[x1 & 255] ^ k[4]; ^ RT2[(x2 >> 8) & 255] ^ RT3[x1 & 255] ^ k[4];
int y1 = RT0[(x1 >> 24) & 255] ^ RT1[(x0 >> 16) & 255] int y1 = RT0[(x1 >> 24) & 255] ^ RT1[(x0 >> 16) & 255]
...@@ -323,15 +313,10 @@ public class AES implements BlockCipher { ...@@ -323,15 +313,10 @@ public class AES implements BlockCipher {
| (RS[(y0 >> 8) & 255] << 8) | RS[y3 & 255]) ^ k[42]; | (RS[(y0 >> 8) & 255] << 8) | RS[y3 & 255]) ^ k[42];
x3 = ((RS[(y3 >> 24) & 255] << 24) | (RS[(y2 >> 16) & 255] << 16) x3 = ((RS[(y3 >> 24) & 255] << 24) | (RS[(y2 >> 16) & 255] << 16)
| (RS[(y1 >> 8) & 255] << 8) | RS[y0 & 255]) ^ k[43]; | (RS[(y1 >> 8) & 255] << 8) | RS[y0 & 255]) ^ k[43];
out[off] = (byte) (x0 >> 24); Bits.writeInt(out, off, x0);
out[off + 1] = (byte) (x0 >> 16); Bits.writeInt(out, off + 4, x1);
out[off+2] = (byte) (x0 >> 8); out[off+3] = (byte) x0; Bits.writeInt(out, off + 8, x2);
out[off+4] = (byte) (x1 >> 24); out[off+5] = (byte) (x1 >> 16); Bits.writeInt(out, off + 12, x3);
out[off+6] = (byte) (x1 >> 8); out[off+7] = (byte) x1;
out[off+8] = (byte) (x2 >> 24); out[off+9] = (byte) (x2 >> 16);
out[off+10] = (byte) (x2 >> 8); out[off+11] = (byte) x2;
out[off+12] = (byte) (x3 >> 24); out[off+13] = (byte) (x3 >> 16);
out[off+14] = (byte) (x3 >> 8); out[off+15] = (byte) x3;
} }
@Override @Override
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
package org.h2.security; package org.h2.security;
import org.h2.util.Utils; import org.h2.util.Bits;
/** /**
* A pseudo-encryption algorithm that makes the data appear to be * A pseudo-encryption algorithm that makes the data appear to be
...@@ -31,14 +31,10 @@ public class Fog implements BlockCipher { ...@@ -31,14 +31,10 @@ public class Fog implements BlockCipher {
} }
private void encryptBlock(byte[] in, byte[] out, int off) { private void encryptBlock(byte[] in, byte[] out, int off) {
int x0 = (in[off] << 24) | ((in[off+1] & 255) << 16) | int x0 = Bits.readInt(in, off);
((in[off+2] & 255) << 8) | (in[off+3] & 255); int x1 = Bits.readInt(in, off + 4);
int x1 = (in[off+4] << 24) | ((in[off+5] & 255) << 16) | int x2 = Bits.readInt(in, off + 8);
((in[off+6] & 255) << 8) | (in[off+7] & 255); int x3 = Bits.readInt(in, off + 12);
int x2 = (in[off+8] << 24) | ((in[off+9] & 255) << 16) |
((in[off+10] & 255) << 8) | (in[off+11] & 255);
int x3 = (in[off+12] << 24) | ((in[off+13] & 255) << 16) |
((in[off+14] & 255) << 8) | (in[off+15] & 255);
int k = key; int k = key;
int s = x1 & 31; int s = x1 & 31;
x0 ^= k; x0 ^= k;
...@@ -50,25 +46,17 @@ public class Fog implements BlockCipher { ...@@ -50,25 +46,17 @@ public class Fog implements BlockCipher {
x1 = (x1 << s) | (x1 >>> (32 - s)); x1 = (x1 << s) | (x1 >>> (32 - s));
x3 ^= k; x3 ^= k;
x3 = (x3 << s) | (x3 >>> (32 - s)); x3 = (x3 << s) | (x3 >>> (32 - s));
out[off] = (byte) (x0 >> 24); out[off+1] = (byte) (x0 >> 16); Bits.writeInt(out, off, x0);
out[off+2] = (byte) (x0 >> 8); out[off+3] = (byte) x0; Bits.writeInt(out, off + 4, x1);
out[off+4] = (byte) (x1 >> 24); out[off+5] = (byte) (x1 >> 16); Bits.writeInt(out, off + 8, x2);
out[off+6] = (byte) (x1 >> 8); out[off+7] = (byte) x1; Bits.writeInt(out, off + 12, x3);
out[off+8] = (byte) (x2 >> 24); out[off+9] = (byte) (x2 >> 16);
out[off+10] = (byte) (x2 >> 8); out[off+11] = (byte) x2;
out[off+12] = (byte) (x3 >> 24); out[off+13] = (byte) (x3 >> 16);
out[off+14] = (byte) (x3 >> 8); out[off+15] = (byte) x3;
} }
private void decryptBlock(byte[] in, byte[] out, int off) { private void decryptBlock(byte[] in, byte[] out, int off) {
int x0 = (in[off] << 24) | ((in[off+1] & 255) << 16) | int x0 = Bits.readInt(in, off);
((in[off+2] & 255) << 8) | (in[off+3] & 255); int x1 = Bits.readInt(in, off + 4);
int x1 = (in[off+4] << 24) | ((in[off+5] & 255) << 16) | int x2 = Bits.readInt(in, off + 8);
((in[off+6] & 255) << 8) | (in[off+7] & 255); int x3 = Bits.readInt(in, off + 12);
int x2 = (in[off+8] << 24) | ((in[off+9] & 255) << 16) |
((in[off+10] & 255) << 8) | (in[off+11] & 255);
int x3 = (in[off+12] << 24) | ((in[off+13] & 255) << 16) |
((in[off+14] & 255) << 8) | (in[off+15] & 255);
int k = key; int k = key;
int s = 32 - (x0 & 31); int s = 32 - (x0 & 31);
x1 = (x1 << s) | (x1 >>> (32 - s)); x1 = (x1 << s) | (x1 >>> (32 - s));
...@@ -80,14 +68,10 @@ public class Fog implements BlockCipher { ...@@ -80,14 +68,10 @@ public class Fog implements BlockCipher {
x0 ^= k; x0 ^= k;
x2 = (x2 << s) | (x2 >>> (32 - s)); x2 = (x2 << s) | (x2 >>> (32 - s));
x2 ^= k; x2 ^= k;
out[off] = (byte) (x0 >> 24); out[off+1] = (byte) (x0 >> 16); Bits.writeInt(out, off, x0);
out[off+2] = (byte) (x0 >> 8); out[off+3] = (byte) x0; Bits.writeInt(out, off + 4, x1);
out[off+4] = (byte) (x1 >> 24); out[off+5] = (byte) (x1 >> 16); Bits.writeInt(out, off + 8, x2);
out[off+6] = (byte) (x1 >> 8); out[off+7] = (byte) x1; Bits.writeInt(out, off + 12, x3);
out[off+8] = (byte) (x2 >> 24); out[off+9] = (byte) (x2 >> 16);
out[off+10] = (byte) (x2 >> 8); out[off+11] = (byte) x2;
out[off+12] = (byte) (x3 >> 24); out[off+13] = (byte) (x3 >> 16);
out[off+14] = (byte) (x3 >> 8); out[off+15] = (byte) x3;
} }
@Override @Override
...@@ -97,7 +81,7 @@ public class Fog implements BlockCipher { ...@@ -97,7 +81,7 @@ public class Fog implements BlockCipher {
@Override @Override
public void setKey(byte[] key) { public void setKey(byte[] key) {
this.key = (int) Utils.readLong(key, 0); this.key = (int) Bits.readLong(key, 0);
} }
} }
...@@ -7,6 +7,8 @@ package org.h2.security; ...@@ -7,6 +7,8 @@ package org.h2.security;
import java.util.Arrays; import java.util.Arrays;
import org.h2.util.Bits;
/** /**
* This class implements the cryptographic hash function SHA-256. * This class implements the cryptographic hash function SHA-256.
*/ */
...@@ -159,7 +161,7 @@ public class SHA256 { ...@@ -159,7 +161,7 @@ public class SHA256 {
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
if (i == 0) { if (i == 0) {
System.arraycopy(salt, 0, message, 0, salt.length); System.arraycopy(salt, 0, message, 0, salt.length);
writeInt(message, salt.length, k); Bits.writeInt(message, salt.length, k);
len = salt.length + 4; len = salt.length + 4;
} else { } else {
System.arraycopy(sha.result, 0, message, 0, 32); System.arraycopy(sha.result, 0, message, 0, 32);
...@@ -219,7 +221,7 @@ public class SHA256 { ...@@ -219,7 +221,7 @@ public class SHA256 {
byteBuff[len] = (byte) 0x80; byteBuff[len] = (byte) 0x80;
Arrays.fill(byteBuff, len + 1, intLen * 4, (byte) 0); Arrays.fill(byteBuff, len + 1, intLen * 4, (byte) 0);
for (int i = 0, j = 0; j < intLen; i += 4, j++) { for (int i = 0, j = 0; j < intLen; i += 4, j++) {
intBuff[j] = readInt(byteBuff, i); intBuff[j] = Bits.readInt(byteBuff, i);
} }
intBuff[intLen - 2] = len >>> 29; intBuff[intLen - 2] = len >>> 29;
intBuff[intLen - 1] = len << 3; intBuff[intLen - 1] = len << 3;
...@@ -263,7 +265,7 @@ public class SHA256 { ...@@ -263,7 +265,7 @@ public class SHA256 {
hh[7] += h; hh[7] += h;
} }
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
writeInt(result, i * 4, hh[i]); Bits.writeInt(result, i * 4, hh[i]);
} }
} }
...@@ -271,16 +273,4 @@ public class SHA256 { ...@@ -271,16 +273,4 @@ public class SHA256 {
return Integer.rotateRight(i, count); return Integer.rotateRight(i, count);
} }
private static int readInt(byte[] b, int i) {
return ((b[i] & 0xff) << 24) + ((b[i + 1] & 0xff) << 16)
+ ((b[i + 2] & 0xff) << 8) + (b[i + 3] & 0xff);
}
private static void writeInt(byte[] b, int i, int value) {
b[i] = (byte) (value >> 24);
b[i + 1] = (byte) (value >> 16);
b[i + 2] = (byte) (value >> 8);
b[i + 3] = (byte) value;
}
} }
...@@ -8,6 +8,7 @@ package org.h2.security; ...@@ -8,6 +8,7 @@ package org.h2.security;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.store.DataHandler; import org.h2.store.DataHandler;
import org.h2.store.FileStore; import org.h2.store.FileStore;
import org.h2.util.Bits;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
/** /**
...@@ -98,15 +99,7 @@ public class SecureFileStore extends FileStore { ...@@ -98,15 +99,7 @@ public class SecureFileStore extends FileStore {
byte[] iv = bufferForInitVector; byte[] iv = bufferForInitVector;
while (len > 0) { while (len > 0) {
for (int i = 0; i < Constants.FILE_BLOCK_SIZE; i += 8) { for (int i = 0; i < Constants.FILE_BLOCK_SIZE; i += 8) {
long block = (p + i) >>> 3; Bits.writeLong(iv, i, (p + i) >>> 3);
iv[i] = (byte) (block >> 56);
iv[i + 1] = (byte) (block >> 48);
iv[i + 2] = (byte) (block >> 40);
iv[i + 3] = (byte) (block >> 32);
iv[i + 4] = (byte) (block >> 24);
iv[i + 5] = (byte) (block >> 16);
iv[i + 6] = (byte) (block >> 8);
iv[i + 7] = (byte) block;
} }
cipherForInitVector.encrypt(iv, 0, Constants.FILE_BLOCK_SIZE); cipherForInitVector.encrypt(iv, 0, Constants.FILE_BLOCK_SIZE);
for (int i = 0; i < Constants.FILE_BLOCK_SIZE; i++) { for (int i = 0; i < Constants.FILE_BLOCK_SIZE; i++) {
......
...@@ -7,6 +7,7 @@ package org.h2.security; ...@@ -7,6 +7,7 @@ package org.h2.security;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.Bits;
/** /**
* An implementation of the XTEA block cipher algorithm. * An implementation of the XTEA block cipher algorithm.
...@@ -25,9 +26,8 @@ public class XTEA implements BlockCipher { ...@@ -25,9 +26,8 @@ public class XTEA implements BlockCipher {
@Override @Override
public void setKey(byte[] b) { public void setKey(byte[] b) {
int[] key = new int[4]; int[] key = new int[4];
for (int i = 0; i < 16;) { for (int i = 0; i < 16; i += 4) {
key[i / 4] = (b[i++] << 24) + ((b[i++] & 255) << 16) key[i / 4] = Bits.readInt(b, i);
+ ((b[i++] & 255) << 8) + (b[i++] & 255);
} }
int[] r = new int[32]; int[] r = new int[32];
for (int i = 0, sum = 0; i < 32;) { for (int i = 0, sum = 0; i < 32;) {
...@@ -70,10 +70,8 @@ public class XTEA implements BlockCipher { ...@@ -70,10 +70,8 @@ public class XTEA implements BlockCipher {
} }
private void encryptBlock(byte[] in, byte[] out, int off) { private void encryptBlock(byte[] in, byte[] out, int off) {
int y = (in[off] << 24) | ((in[off + 1] & 255) << 16) int y = Bits.readInt(in, off);
| ((in[off + 2] & 255) << 8) | (in[off + 3] & 255); int z = Bits.readInt(in, off + 4);
int z = (in[off + 4] << 24) | ((in[off + 5] & 255) << 16)
| ((in[off + 6] & 255) << 8) | (in[off + 7] & 255);
y += (((z << 4) ^ (z >>> 5)) + z) ^ k0; y += (((z << 4) ^ (z >>> 5)) + z) ^ k0;
z += (((y >>> 5) ^ (y << 4)) + y) ^ k1; z += (((y >>> 5) ^ (y << 4)) + y) ^ k1;
y += (((z << 4) ^ (z >>> 5)) + z) ^ k2; y += (((z << 4) ^ (z >>> 5)) + z) ^ k2;
...@@ -106,21 +104,13 @@ public class XTEA implements BlockCipher { ...@@ -106,21 +104,13 @@ public class XTEA implements BlockCipher {
z += (((y >>> 5) ^ (y << 4)) + y) ^ k29; z += (((y >>> 5) ^ (y << 4)) + y) ^ k29;
y += (((z << 4) ^ (z >>> 5)) + z) ^ k30; y += (((z << 4) ^ (z >>> 5)) + z) ^ k30;
z += (((y >>> 5) ^ (y << 4)) + y) ^ k31; z += (((y >>> 5) ^ (y << 4)) + y) ^ k31;
out[off] = (byte) (y >> 24); Bits.writeInt(out, off, y);
out[off + 1] = (byte) (y >> 16); Bits.writeInt(out, off + 4, z);
out[off + 2] = (byte) (y >> 8);
out[off + 3] = (byte) y;
out[off + 4] = (byte) (z >> 24);
out[off + 5] = (byte) (z >> 16);
out[off + 6] = (byte) (z >> 8);
out[off + 7] = (byte) z;
} }
private void decryptBlock(byte[] in, byte[] out, int off) { private void decryptBlock(byte[] in, byte[] out, int off) {
int y = (in[off] << 24) | ((in[off + 1] & 255) << 16) int y = Bits.readInt(in, off);
| ((in[off + 2] & 255) << 8) | (in[off + 3] & 255); int z = Bits.readInt(in, off + 4);
int z = (in[off + 4] << 24) | ((in[off + 5] & 255) << 16)
| ((in[off + 6] & 255) << 8) | (in[off + 7] & 255);
z -= (((y >>> 5) ^ (y << 4)) + y) ^ k31; z -= (((y >>> 5) ^ (y << 4)) + y) ^ k31;
y -= (((z << 4) ^ (z >>> 5)) + z) ^ k30; y -= (((z << 4) ^ (z >>> 5)) + z) ^ k30;
z -= (((y >>> 5) ^ (y << 4)) + y) ^ k29; z -= (((y >>> 5) ^ (y << 4)) + y) ^ k29;
...@@ -153,14 +143,8 @@ public class XTEA implements BlockCipher { ...@@ -153,14 +143,8 @@ public class XTEA implements BlockCipher {
y -= (((z << 4) ^ (z >>> 5)) + z) ^ k2; y -= (((z << 4) ^ (z >>> 5)) + z) ^ k2;
z -= (((y >>> 5) ^ (y << 4)) + y) ^ k1; z -= (((y >>> 5) ^ (y << 4)) + y) ^ k1;
y -= (((z << 4) ^ (z >>> 5)) + z) ^ k0; y -= (((z << 4) ^ (z >>> 5)) + z) ^ k0;
out[off] = (byte) (y >> 24); Bits.writeInt(out, off, y);
out[off + 1] = (byte) (y >> 16); Bits.writeInt(out, off + 4, z);
out[off + 2] = (byte) (y >> 8);
out[off + 3] = (byte) y;
out[off + 4] = (byte) (z >> 24);
out[off + 5] = (byte) (z >> 16);
out[off + 6] = (byte) (z >> 8);
out[off + 7] = (byte) z;
} }
@Override @Override
......
...@@ -23,6 +23,7 @@ import org.h2.engine.SysProperties; ...@@ -23,6 +23,7 @@ import org.h2.engine.SysProperties;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils; import org.h2.mvstore.DataUtils;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.Bits;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.value.DataType; import org.h2.value.DataType;
...@@ -127,11 +128,7 @@ public class Data { ...@@ -127,11 +128,7 @@ public class Data {
* @param x the value * @param x the value
*/ */
public void setInt(int pos, int x) { public void setInt(int pos, int x) {
byte[] buff = data; Bits.writeInt(data, pos, x);
buff[pos] = (byte) (x >> 24);
buff[pos + 1] = (byte) (x >> 16);
buff[pos + 2] = (byte) (x >> 8);
buff[pos + 3] = (byte) x;
} }
/** /**
...@@ -141,11 +138,7 @@ public class Data { ...@@ -141,11 +138,7 @@ public class Data {
* @param x the value * @param x the value
*/ */
public void writeInt(int x) { public void writeInt(int x) {
byte[] buff = data; Bits.writeInt(data, pos, x);
buff[pos] = (byte) (x >> 24);
buff[pos + 1] = (byte) (x >> 16);
buff[pos + 2] = (byte) (x >> 8);
buff[pos + 3] = (byte) x;
pos += 4; pos += 4;
} }
...@@ -156,11 +149,7 @@ public class Data { ...@@ -156,11 +149,7 @@ public class Data {
* @return the value * @return the value
*/ */
public int readInt() { public int readInt() {
byte[] buff = data; int x = Bits.readInt(data, pos);
int x = (buff[pos] << 24) +
((buff[pos+1] & 0xff) << 16) +
((buff[pos+2] & 0xff) << 8) +
(buff[pos+3] & 0xff);
pos += 4; pos += 4;
return x; return x;
} }
...@@ -400,7 +389,9 @@ public class Data { ...@@ -400,7 +389,9 @@ public class Data {
* @return the long value * @return the long value
*/ */
public long readLong() { public long readLong() {
return ((long) (readInt()) << 32) + (readInt() & 0xffffffffL); long x = Bits.readLong(data, pos);
pos += 8;
return x;
} }
/** /**
...@@ -409,8 +400,8 @@ public class Data { ...@@ -409,8 +400,8 @@ public class Data {
* @param x the value * @param x the value
*/ */
public void writeLong(long x) { public void writeLong(long x) {
writeInt((int) (x >>> 32)); Bits.writeLong(data, pos, x);
writeInt((int) x); pos += 8;
} }
/** /**
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.store;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* An filter input stream that limits the number of bytes that can be read.
*/
public class LimitInputStream extends FilterInputStream {
private long remaining;
public LimitInputStream(InputStream in, long maxLength) {
super(in);
this.remaining = maxLength;
}
@Override
public int available() throws IOException {
return (int) Math.min(remaining, in.available());
}
@Override
public boolean markSupported() {
return false;
}
@Override
public int read() throws IOException {
if (remaining == 0) {
return -1;
}
int result = in.read();
if (result >= 0) {
remaining--;
}
return result;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (remaining == 0) {
return -1;
}
len = (int) Math.min(len, remaining);
int result = in.read(b, off, len);
if (result >= 0) {
remaining -= result;
}
return result;
}
}
...@@ -157,7 +157,7 @@ public class LobStorageMap implements LobStorageInterface { ...@@ -157,7 +157,7 @@ public class LobStorageMap implements LobStorageInterface {
return ValueLobDb.createSmallLob(type, small); return ValueLobDb.createSmallLob(type, small);
} }
if (maxLength != -1) { if (maxLength != -1) {
in = new LimitInputStream(in, maxLength); in = new RangeInputStream(in, 0L, maxLength);
} }
return createLob(in, type); return createLob(in, type);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
......
...@@ -37,7 +37,7 @@ public final class RangeInputStream extends FilterInputStream { ...@@ -37,7 +37,7 @@ public final class RangeInputStream extends FilterInputStream {
@Override @Override
public int read() throws IOException { public int read() throws IOException {
if (limit < 1) { if (limit <= 0) {
return -1; return -1;
} }
int b = in.read(); int b = in.read();
...@@ -49,6 +49,8 @@ public final class RangeInputStream extends FilterInputStream { ...@@ -49,6 +49,8 @@ public final class RangeInputStream extends FilterInputStream {
@Override @Override
public int read(byte b[], int off, int len) throws IOException { public int read(byte b[], int off, int len) throws IOException {
if (limit <= 0)
return -1;
if (len > limit) { if (len > limit) {
len = (int) limit; len = (int) limit;
} }
......
...@@ -38,7 +38,7 @@ public final class RangeReader extends Reader { ...@@ -38,7 +38,7 @@ public final class RangeReader extends Reader {
@Override @Override
public int read() throws IOException { public int read() throws IOException {
if (limit < 1) { if (limit <= 0) {
return -1; return -1;
} }
int c = r.read(); int c = r.read();
...@@ -50,6 +50,8 @@ public final class RangeReader extends Reader { ...@@ -50,6 +50,8 @@ public final class RangeReader extends Reader {
@Override @Override
public int read(char cbuf[], int off, int len) throws IOException { public int read(char cbuf[], int off, int len) throws IOException {
if (limit <= 0)
return -1;
if (len > limit) { if (len > limit) {
len = (int) limit; len = (int) limit;
} }
......
...@@ -26,6 +26,7 @@ import org.h2.compress.LZFOutputStream; ...@@ -26,6 +26,7 @@ import org.h2.compress.LZFOutputStream;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils; import org.h2.mvstore.DataUtils;
import org.h2.util.Bits;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
/** /**
...@@ -158,10 +159,7 @@ public class CompressTool { ...@@ -158,10 +159,7 @@ public class CompressTool {
((buff[pos++] & 0xff) << 8) + ((buff[pos++] & 0xff) << 8) +
(buff[pos] & 0xff); (buff[pos] & 0xff);
} }
return ((buff[pos++] & 0xff) << 24) + return Bits.readInt(buff, pos);
((buff[pos++] & 0xff) << 16) +
((buff[pos++] & 0xff) << 8) +
(buff[pos] & 0xff);
} }
/** /**
...@@ -176,10 +174,7 @@ public class CompressTool { ...@@ -176,10 +174,7 @@ public class CompressTool {
public static int writeVariableInt(byte[] buff, int pos, int x) { public static int writeVariableInt(byte[] buff, int pos, int x) {
if (x < 0) { if (x < 0) {
buff[pos++] = (byte) 0xf0; buff[pos++] = (byte) 0xf0;
buff[pos++] = (byte) (x >> 24); Bits.writeInt(buff, pos, x);
buff[pos++] = (byte) (x >> 16);
buff[pos++] = (byte) (x >> 8);
buff[pos] = (byte) x;
return 5; return 5;
} else if (x < 0x80) { } else if (x < 0x80) {
buff[pos] = (byte) x; buff[pos] = (byte) x;
...@@ -194,17 +189,11 @@ public class CompressTool { ...@@ -194,17 +189,11 @@ public class CompressTool {
buff[pos] = (byte) x; buff[pos] = (byte) x;
return 3; return 3;
} else if (x < 0x10000000) { } else if (x < 0x10000000) {
buff[pos++] = (byte) (0xe0 | (x >> 24)); Bits.writeInt(buff, pos, x | 0xe0000000);
buff[pos++] = (byte) (x >> 16);
buff[pos++] = (byte) (x >> 8);
buff[pos] = (byte) x;
return 4; return 4;
} else { } else {
buff[pos++] = (byte) 0xf0; buff[pos++] = (byte) 0xf0;
buff[pos++] = (byte) (x >> 24); Bits.writeInt(buff, pos, x);
buff[pos++] = (byte) (x >> 16);
buff[pos++] = (byte) (x >> 8);
buff[pos] = (byte) x;
return 5; return 5;
} }
} }
......
...@@ -33,10 +33,10 @@ import java.util.UUID; ...@@ -33,10 +33,10 @@ import java.util.UUID;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.jdbc.JdbcResultSetBackwardsCompat; import org.h2.jdbc.JdbcResultSetBackwardsCompat;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.Bits;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.Utils;
import org.h2.value.DataType; import org.h2.value.DataType;
/** /**
...@@ -534,7 +534,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData, ...@@ -534,7 +534,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
return (byte[]) o; return (byte[]) o;
} }
if (o instanceof UUID) { if (o instanceof UUID) {
return Utils.uuidToBytes((UUID) o); return Bits.uuidToBytes((UUID) o);
} }
return JdbcUtils.serialize(o, null); return JdbcUtils.serialize(o, null);
} }
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.util;
import java.util.UUID;
/**
* Manipulations with bytes and arrays. This class can be overridden in
* multi-release JAR with more efficient implementation for a newer versions of
* Java.
*/
public final class Bits {
/**
* Reads a int 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 int readInt(byte[] buff, int pos) {
return (buff[pos++] << 24) + ((buff[pos++] & 0xff) << 16) + ((buff[pos++] & 0xff) << 8) + (buff[pos] & 0xff);
}
/**
* 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);
}
/**
* Converts UUID value to byte array in big-endian order.
*
* @param msb
* most significant part of UUID
* @param lsb
* least significant part of UUID
* @return byte array representation
*/
public static byte[] uuidToBytes(long msb, long lsb) {
byte[] buff = new byte[16];
for (int i = 0; i < 8; i++) {
buff[i] = (byte) ((msb >> (8 * (7 - i))) & 0xff);
buff[8 + i] = (byte) ((lsb >> (8 * (7 - i))) & 0xff);
}
return buff;
}
/**
* Converts UUID value to byte array in big-endian order.
*
* @param uuid
* UUID value
* @return byte array representation
*/
public static byte[] uuidToBytes(UUID uuid) {
return uuidToBytes(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
}
/**
* Writes a int value to the byte array at the given position in big-endian
* order.
*
* @param buff
* the byte array
* @param pos
* the position
* @param x
* the value to write
*/
public static void writeInt(byte[] buff, int pos, int x) {
buff[pos++] = (byte) (x >> 24);
buff[pos++] = (byte) (x >> 16);
buff[pos++] = (byte) (x >> 8);
buff[pos] = (byte) x;
}
/**
* Writes a long value to the byte array at the given position in big-endian
* order.
*
* @param buff
* the byte array
* @param pos
* the position
* @param x
* the value to write
*/
public static void writeLong(byte[] buff, int pos, long x) {
writeInt(buff, pos, (int) (x >> 32));
writeInt(buff, pos + 4, (int) x);
}
private Bits() {
}
}
...@@ -18,7 +18,6 @@ import java.util.Arrays; ...@@ -18,7 +18,6 @@ import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
...@@ -53,68 +52,6 @@ public class Utils { ...@@ -53,68 +52,6 @@ public class Utils {
// utility class // utility class
} }
private static int readInt(byte[] buff, int pos) {
return (buff[pos++] << 24) +
((buff[pos++] & 0xff) << 16) +
((buff[pos++] & 0xff) << 8) +
(buff[pos] & 0xff);
}
/**
* Write a long value to the byte array at the given position. The most
* significant byte is written first.
*
* @param buff the byte array
* @param pos the position
* @param x the value to write
*/
public static void writeLong(byte[] buff, int pos, long x) {
writeInt(buff, pos, (int) (x >> 32));
writeInt(buff, pos + 4, (int) x);
}
private static void writeInt(byte[] buff, int pos, int x) {
buff[pos++] = (byte) (x >> 24);
buff[pos++] = (byte) (x >> 16);
buff[pos++] = (byte) (x >> 8);
buff[pos] = (byte) x;
}
/**
* Read a long value from the byte array at the given position. The most
* significant byte is read first.
*
* @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);
}
/**
* @param uuid UUID value
* @return byte array representation
*/
public static byte[] uuidToBytes(UUID uuid) {
return uuidToBytes(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
}
/**
* @param msb most significant part of UUID
* @param lsb least significant part of UUID
* @return byte array representation
*/
public static byte[] uuidToBytes(long msb, long lsb) {
byte[] buff = new byte[16];
for (int i = 0; i < 8; i++) {
buff[i] = (byte) ((msb >> (8 * (7 - i))) & 255);
buff[8 + i] = (byte) ((lsb >> (8 * (7 - i))) & 255);
}
return buff;
}
/** /**
* Calculate the index of the first occurrence of the pattern in the byte * Calculate the index of the first occurrence of the pattern in the byte
* array, starting with the given index. This methods returns -1 if the * array, starting with the given index. This methods returns -1 if the
......
...@@ -28,6 +28,7 @@ import org.h2.security.SHA256; ...@@ -28,6 +28,7 @@ import org.h2.security.SHA256;
import org.h2.store.Data; import org.h2.store.Data;
import org.h2.store.DataReader; import org.h2.store.DataReader;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.Bits;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
...@@ -790,7 +791,7 @@ public class Transfer { ...@@ -790,7 +791,7 @@ public class Transfer {
lobMacSalt = MathUtils.secureRandomBytes(LOB_MAC_SALT_LENGTH); lobMacSalt = MathUtils.secureRandomBytes(LOB_MAC_SALT_LENGTH);
} }
byte[] data = new byte[8]; byte[] data = new byte[8];
Utils.writeLong(data, 0, lobId); Bits.writeLong(data, 0, lobId);
byte[] hmacData = SHA256.getHashWithSalt(data, lobMacSalt); byte[] hmacData = SHA256.getHashWithSalt(data, lobMacSalt);
return hmacData; return hmacData;
} }
......
...@@ -27,11 +27,11 @@ import org.h2.message.DbException; ...@@ -27,11 +27,11 @@ import org.h2.message.DbException;
import org.h2.store.DataHandler; import org.h2.store.DataHandler;
import org.h2.table.Column; import org.h2.table.Column;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.Bits;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Utils;
/** /**
* This is the base class for all value classes. * This is the base class for all value classes.
...@@ -724,7 +724,7 @@ public abstract class Value { ...@@ -724,7 +724,7 @@ public abstract class Value {
// parseLong doesn't work for ffffffffffffffff // parseLong doesn't work for ffffffffffffffff
byte[] d = getBytes(); byte[] d = getBytes();
if (d.length == 8) { if (d.length == 8) {
return ValueLong.get(Utils.readLong(d, 0)); return ValueLong.get(Bits.readLong(d, 0));
} }
return ValueLong.get(Long.parseLong(getString(), 16)); return ValueLong.get(Long.parseLong(getString(), 16));
} }
......
...@@ -11,7 +11,7 @@ import java.util.UUID; ...@@ -11,7 +11,7 @@ import java.util.UUID;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.Utils; import org.h2.util.Bits;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
...@@ -68,8 +68,8 @@ public class ValueUuid extends Value { ...@@ -68,8 +68,8 @@ public class ValueUuid extends Value {
if (binary.length < 16) { if (binary.length < 16) {
return get(StringUtils.convertBytesToHex(binary)); return get(StringUtils.convertBytesToHex(binary));
} }
long high = Utils.readLong(binary, 0); long high = Bits.readLong(binary, 0);
long low = Utils.readLong(binary, 8); long low = Bits.readLong(binary, 8);
return (ValueUuid) Value.cache(new ValueUuid(high, low)); return (ValueUuid) Value.cache(new ValueUuid(high, low));
} }
...@@ -186,7 +186,7 @@ public class ValueUuid extends Value { ...@@ -186,7 +186,7 @@ public class ValueUuid extends Value {
@Override @Override
public byte[] getBytes() { public byte[] getBytes() {
return Utils.uuidToBytes(high, low); return Bits.uuidToBytes(high, low);
} }
@Override @Override
......
...@@ -19,6 +19,7 @@ import java.util.Comparator; ...@@ -19,6 +19,7 @@ import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.Random; import java.util.Random;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.Bits;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
...@@ -96,15 +97,15 @@ public class TestUtils extends TestBase { ...@@ -96,15 +97,15 @@ public class TestUtils extends TestBase {
byte[] buff = new byte[8]; byte[] buff = new byte[8];
for (long x : new long[]{Long.MIN_VALUE, Long.MAX_VALUE, 0, 1, -1, for (long x : new long[]{Long.MIN_VALUE, Long.MAX_VALUE, 0, 1, -1,
Integer.MIN_VALUE, Integer.MAX_VALUE}) { Integer.MIN_VALUE, Integer.MAX_VALUE}) {
Utils.writeLong(buff, 0, x); Bits.writeLong(buff, 0, x);
long y = Utils.readLong(buff, 0); long y = Bits.readLong(buff, 0);
assertEquals(x, y); assertEquals(x, y);
} }
Random r = new Random(1); Random r = new Random(1);
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {
long x = r.nextLong(); long x = r.nextLong();
Utils.writeLong(buff, 0, x); Bits.writeLong(buff, 0, x);
long y = Utils.readLong(buff, 0); long y = Bits.readLong(buff, 0);
assertEquals(x, y); assertEquals(x, y);
} }
} }
......
...@@ -22,7 +22,7 @@ import org.h2.message.DbException; ...@@ -22,7 +22,7 @@ import org.h2.message.DbException;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.test.utils.AssertThrows; import org.h2.test.utils.AssertThrows;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.Utils; import org.h2.util.Bits;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray; import org.h2.value.ValueArray;
...@@ -130,7 +130,7 @@ public class TestValue extends TestBase { ...@@ -130,7 +130,7 @@ public class TestValue extends TestBase {
prep.setObject(1, new Object[] { uuid }); prep.setObject(1, new Object[] { uuid });
rs = prep.executeQuery(); rs = prep.executeQuery();
rs.next(); rs.next();
assertTrue(Arrays.equals(Utils.uuidToBytes(uuid), (byte[]) rs.getObject(1))); assertTrue(Arrays.equals(Bits.uuidToBytes(uuid), (byte[]) rs.getObject(1)));
// Check that type is not changed // Check that type is not changed
prep = conn.prepareStatement("SELECT * FROM TABLE(X UUID=?)"); prep = conn.prepareStatement("SELECT * FROM TABLE(X UUID=?)");
prep.setObject(1, new Object[] { uuid }); prep.setObject(1, new Object[] { uuid });
......
...@@ -757,4 +757,4 @@ chittanoor carrot ...@@ -757,4 +757,4 @@ chittanoor carrot
contextual unknowns enquote respectively sessionid reconnection selfreferential bbddbb instant subprotocol ddbbbb contextual unknowns enquote respectively sessionid reconnection selfreferential bbddbb instant subprotocol ddbbbb
zzbbzz cldr booleans maria enquotes mtc cbuf checksummed nreturn despite bbzz readlimit retries cceecc reconnects zzbbzz cldr booleans maria enquotes mtc cbuf checksummed nreturn despite bbzz readlimit retries cceecc reconnects
unconditionally coco aren eecccc decimals charsets zzbb lsb msb usecount outdir unconditionally coco aren eecccc decimals charsets zzbb lsb msb usecount outdir endian
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论