提交 9cae63c1 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Extract multi-byte reading and writing to Bits

上级 ec4e821e
......@@ -5,6 +5,8 @@
*/
package org.h2.security;
import org.h2.util.Bits;
/**
* An implementation of the AES block cipher algorithm,
* also known as Rijndael. Only AES-128 is supported by this class.
......@@ -135,14 +137,10 @@ public class AES implements BlockCipher {
private void encryptBlock(byte[] in, byte[] out, int off) {
int[] k = encKey;
int x0 = ((in[off] << 24) | ((in[off + 1] & 255) << 16)
| ((in[off + 2] & 255) << 8) | (in[off + 3] & 255)) ^ k[0];
int x1 = ((in[off + 4] << 24) | ((in[off + 5] & 255) << 16)
| ((in[off + 6] & 255) << 8) | (in[off + 7] & 255)) ^ k[1];
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 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];
int y1 = FT0[(x1 >> 24) & 255] ^ FT1[(x2 >> 16) & 255]
......@@ -223,26 +221,18 @@ public class AES implements BlockCipher {
| (FS[(y0 >> 8) & 255] << 8) | FS[y1 & 255]) ^ k[42];
x3 = ((FS[(y3 >> 24) & 255] << 24) | (FS[(y0 >> 16) & 255] << 16)
| (FS[(y1 >> 8) & 255] << 8) | FS[y2 & 255]) ^ k[43];
out[off] = (byte) (x0 >> 24); out[off+1] = (byte) (x0 >> 16);
out[off+2] = (byte) (x0 >> 8); out[off+3] = (byte) x0;
out[off+4] = (byte) (x1 >> 24); out[off+5] = (byte) (x1 >> 16);
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;
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 = ((in[off] << 24) | ((in[off + 1] & 255) << 16)
| ((in[off + 2] & 255) << 8) | (in[off + 3] & 255)) ^ k[0];
int x1 = ((in[off + 4] << 24) | ((in[off + 5] & 255) << 16)
| ((in[off + 6] & 255) << 8) | (in[off + 7] & 255)) ^ k[1];
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 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];
int y1 = RT0[(x1 >> 24) & 255] ^ RT1[(x0 >> 16) & 255]
......@@ -323,15 +313,10 @@ public class AES implements BlockCipher {
| (RS[(y0 >> 8) & 255] << 8) | RS[y3 & 255]) ^ k[42];
x3 = ((RS[(y3 >> 24) & 255] << 24) | (RS[(y2 >> 16) & 255] << 16)
| (RS[(y1 >> 8) & 255] << 8) | RS[y0 & 255]) ^ k[43];
out[off] = (byte) (x0 >> 24);
out[off + 1] = (byte) (x0 >> 16);
out[off+2] = (byte) (x0 >> 8); out[off+3] = (byte) x0;
out[off+4] = (byte) (x1 >> 24); out[off+5] = (byte) (x1 >> 16);
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;
Bits.writeInt(out, off, x0);
Bits.writeInt(out, off + 4, x1);
Bits.writeInt(out, off + 8, x2);
Bits.writeInt(out, off + 12, x3);
}
@Override
......
......@@ -5,7 +5,7 @@
*/
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
......@@ -31,14 +31,10 @@ public class Fog implements BlockCipher {
}
private void encryptBlock(byte[] in, byte[] out, int off) {
int x0 = (in[off] << 24) | ((in[off+1] & 255) << 16) |
((in[off+2] & 255) << 8) | (in[off+3] & 255);
int x1 = (in[off+4] << 24) | ((in[off+5] & 255) << 16) |
((in[off+6] & 255) << 8) | (in[off+7] & 255);
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 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;
int s = x1 & 31;
x0 ^= k;
......@@ -50,25 +46,17 @@ public class Fog implements BlockCipher {
x1 = (x1 << s) | (x1 >>> (32 - s));
x3 ^= k;
x3 = (x3 << s) | (x3 >>> (32 - s));
out[off] = (byte) (x0 >> 24); out[off+1] = (byte) (x0 >> 16);
out[off+2] = (byte) (x0 >> 8); out[off+3] = (byte) x0;
out[off+4] = (byte) (x1 >> 24); out[off+5] = (byte) (x1 >> 16);
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;
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 = (in[off] << 24) | ((in[off+1] & 255) << 16) |
((in[off+2] & 255) << 8) | (in[off+3] & 255);
int x1 = (in[off+4] << 24) | ((in[off+5] & 255) << 16) |
((in[off+6] & 255) << 8) | (in[off+7] & 255);
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 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;
int s = 32 - (x0 & 31);
x1 = (x1 << s) | (x1 >>> (32 - s));
......@@ -80,14 +68,10 @@ public class Fog implements BlockCipher {
x0 ^= k;
x2 = (x2 << s) | (x2 >>> (32 - s));
x2 ^= k;
out[off] = (byte) (x0 >> 24); out[off+1] = (byte) (x0 >> 16);
out[off+2] = (byte) (x0 >> 8); out[off+3] = (byte) x0;
out[off+4] = (byte) (x1 >> 24); out[off+5] = (byte) (x1 >> 16);
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;
Bits.writeInt(out, off, x0);
Bits.writeInt(out, off + 4, x1);
Bits.writeInt(out, off + 8, x2);
Bits.writeInt(out, off + 12, x3);
}
@Override
......@@ -97,7 +81,7 @@ public class Fog implements BlockCipher {
@Override
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;
import java.util.Arrays;
import org.h2.util.Bits;
/**
* This class implements the cryptographic hash function SHA-256.
*/
......@@ -159,7 +161,7 @@ public class SHA256 {
for (int i = 0; i < iterations; i++) {
if (i == 0) {
System.arraycopy(salt, 0, message, 0, salt.length);
writeInt(message, salt.length, k);
Bits.writeInt(message, salt.length, k);
len = salt.length + 4;
} else {
System.arraycopy(sha.result, 0, message, 0, 32);
......@@ -219,7 +221,7 @@ public class SHA256 {
byteBuff[len] = (byte) 0x80;
Arrays.fill(byteBuff, len + 1, intLen * 4, (byte) 0);
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 - 1] = len << 3;
......@@ -263,7 +265,7 @@ public class SHA256 {
hh[7] += h;
}
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 {
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;
import org.h2.engine.Constants;
import org.h2.store.DataHandler;
import org.h2.store.FileStore;
import org.h2.util.Bits;
import org.h2.util.MathUtils;
/**
......@@ -98,15 +99,7 @@ public class SecureFileStore extends FileStore {
byte[] iv = bufferForInitVector;
while (len > 0) {
for (int i = 0; i < Constants.FILE_BLOCK_SIZE; i += 8) {
long block = (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;
Bits.writeLong(iv, i, (p + i) >>> 3);
}
cipherForInitVector.encrypt(iv, 0, Constants.FILE_BLOCK_SIZE);
for (int i = 0; i < Constants.FILE_BLOCK_SIZE; i++) {
......
......@@ -7,6 +7,7 @@ package org.h2.security;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.util.Bits;
/**
* An implementation of the XTEA block cipher algorithm.
......@@ -25,9 +26,8 @@ public class XTEA implements BlockCipher {
@Override
public void setKey(byte[] b) {
int[] key = new int[4];
for (int i = 0; i < 16;) {
key[i / 4] = (b[i++] << 24) + ((b[i++] & 255) << 16)
+ ((b[i++] & 255) << 8) + (b[i++] & 255);
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;) {
......@@ -70,10 +70,8 @@ public class XTEA implements BlockCipher {
}
private void encryptBlock(byte[] in, byte[] out, int off) {
int y = (in[off] << 24) | ((in[off + 1] & 255) << 16)
| ((in[off + 2] & 255) << 8) | (in[off + 3] & 255);
int z = (in[off + 4] << 24) | ((in[off + 5] & 255) << 16)
| ((in[off + 6] & 255) << 8) | (in[off + 7] & 255);
int y = Bits.readInt(in, off);
int z = Bits.readInt(in, off + 4);
y += (((z << 4) ^ (z >>> 5)) + z) ^ k0;
z += (((y >>> 5) ^ (y << 4)) + y) ^ k1;
y += (((z << 4) ^ (z >>> 5)) + z) ^ k2;
......@@ -106,21 +104,13 @@ public class XTEA implements BlockCipher {
z += (((y >>> 5) ^ (y << 4)) + y) ^ k29;
y += (((z << 4) ^ (z >>> 5)) + z) ^ k30;
z += (((y >>> 5) ^ (y << 4)) + y) ^ k31;
out[off] = (byte) (y >> 24);
out[off + 1] = (byte) (y >> 16);
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;
Bits.writeInt(out, off, y);
Bits.writeInt(out, off + 4, z);
}
private void decryptBlock(byte[] in, byte[] out, int off) {
int y = (in[off] << 24) | ((in[off + 1] & 255) << 16)
| ((in[off + 2] & 255) << 8) | (in[off + 3] & 255);
int z = (in[off + 4] << 24) | ((in[off + 5] & 255) << 16)
| ((in[off + 6] & 255) << 8) | (in[off + 7] & 255);
int y = Bits.readInt(in, off);
int z = Bits.readInt(in, off + 4);
z -= (((y >>> 5) ^ (y << 4)) + y) ^ k31;
y -= (((z << 4) ^ (z >>> 5)) + z) ^ k30;
z -= (((y >>> 5) ^ (y << 4)) + y) ^ k29;
......@@ -153,14 +143,8 @@ public class XTEA implements BlockCipher {
y -= (((z << 4) ^ (z >>> 5)) + z) ^ k2;
z -= (((y >>> 5) ^ (y << 4)) + y) ^ k1;
y -= (((z << 4) ^ (z >>> 5)) + z) ^ k0;
out[off] = (byte) (y >> 24);
out[off + 1] = (byte) (y >> 16);
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;
Bits.writeInt(out, off, y);
Bits.writeInt(out, off + 4, z);
}
@Override
......
......@@ -23,6 +23,7 @@ import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.tools.SimpleResultSet;
import org.h2.util.Bits;
import org.h2.util.DateTimeUtils;
import org.h2.util.MathUtils;
import org.h2.value.DataType;
......@@ -127,11 +128,7 @@ public class Data {
* @param x the value
*/
public void setInt(int pos, int x) {
byte[] buff = data;
buff[pos] = (byte) (x >> 24);
buff[pos + 1] = (byte) (x >> 16);
buff[pos + 2] = (byte) (x >> 8);
buff[pos + 3] = (byte) x;
Bits.writeInt(data, pos, x);
}
/**
......@@ -141,11 +138,7 @@ public class Data {
* @param x the value
*/
public void writeInt(int x) {
byte[] buff = data;
buff[pos] = (byte) (x >> 24);
buff[pos + 1] = (byte) (x >> 16);
buff[pos + 2] = (byte) (x >> 8);
buff[pos + 3] = (byte) x;
Bits.writeInt(data, pos, x);
pos += 4;
}
......@@ -156,11 +149,7 @@ public class Data {
* @return the value
*/
public int readInt() {
byte[] buff = data;
int x = (buff[pos] << 24) +
((buff[pos+1] & 0xff) << 16) +
((buff[pos+2] & 0xff) << 8) +
(buff[pos+3] & 0xff);
int x = Bits.readInt(data, pos);
pos += 4;
return x;
}
......@@ -400,7 +389,9 @@ public class Data {
* @return the long value
*/
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 {
* @param x the value
*/
public void writeLong(long x) {
writeInt((int) (x >>> 32));
writeInt((int) x);
Bits.writeLong(data, pos, x);
pos += 8;
}
/**
......
......@@ -26,6 +26,7 @@ import org.h2.compress.LZFOutputStream;
import org.h2.engine.Constants;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.util.Bits;
import org.h2.util.StringUtils;
/**
......@@ -158,10 +159,7 @@ public class CompressTool {
((buff[pos++] & 0xff) << 8) +
(buff[pos] & 0xff);
}
return ((buff[pos++] & 0xff) << 24) +
((buff[pos++] & 0xff) << 16) +
((buff[pos++] & 0xff) << 8) +
(buff[pos] & 0xff);
return Bits.readInt(buff, pos);
}
/**
......@@ -176,10 +174,7 @@ public class CompressTool {
public static int writeVariableInt(byte[] buff, int pos, int x) {
if (x < 0) {
buff[pos++] = (byte) 0xf0;
buff[pos++] = (byte) (x >> 24);
buff[pos++] = (byte) (x >> 16);
buff[pos++] = (byte) (x >> 8);
buff[pos] = (byte) x;
Bits.writeInt(buff, pos, x);
return 5;
} else if (x < 0x80) {
buff[pos] = (byte) x;
......@@ -194,17 +189,11 @@ public class CompressTool {
buff[pos] = (byte) x;
return 3;
} else if (x < 0x10000000) {
buff[pos++] = (byte) (0xe0 | (x >> 24));
buff[pos++] = (byte) (x >> 16);
buff[pos++] = (byte) (x >> 8);
buff[pos] = (byte) x;
Bits.writeInt(buff, pos, x | 0xe0000000);
return 4;
} else {
buff[pos++] = (byte) 0xf0;
buff[pos++] = (byte) (x >> 24);
buff[pos++] = (byte) (x >> 16);
buff[pos++] = (byte) (x >> 8);
buff[pos] = (byte) x;
Bits.writeInt(buff, pos, x);
return 5;
}
}
......
......@@ -33,10 +33,10 @@ import java.util.UUID;
import org.h2.api.ErrorCode;
import org.h2.jdbc.JdbcResultSetBackwardsCompat;
import org.h2.message.DbException;
import org.h2.util.Bits;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
import org.h2.util.New;
import org.h2.util.Utils;
import org.h2.value.DataType;
/**
......@@ -534,7 +534,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
return (byte[]) o;
}
if (o instanceof UUID) {
return Utils.uuidToBytes((UUID) o);
return Bits.uuidToBytes((UUID) o);
}
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;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
......@@ -53,68 +52,6 @@ public class Utils {
// 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
* array, starting with the given index. This methods returns -1 if the
......
......@@ -28,6 +28,7 @@ import org.h2.security.SHA256;
import org.h2.store.Data;
import org.h2.store.DataReader;
import org.h2.tools.SimpleResultSet;
import org.h2.util.Bits;
import org.h2.util.DateTimeUtils;
import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils;
......@@ -790,7 +791,7 @@ public class Transfer {
lobMacSalt = MathUtils.secureRandomBytes(LOB_MAC_SALT_LENGTH);
}
byte[] data = new byte[8];
Utils.writeLong(data, 0, lobId);
Bits.writeLong(data, 0, lobId);
byte[] hmacData = SHA256.getHashWithSalt(data, lobMacSalt);
return hmacData;
}
......
......@@ -27,11 +27,11 @@ import org.h2.message.DbException;
import org.h2.store.DataHandler;
import org.h2.table.Column;
import org.h2.tools.SimpleResultSet;
import org.h2.util.Bits;
import org.h2.util.DateTimeUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
/**
* This is the base class for all value classes.
......@@ -724,7 +724,7 @@ public abstract class Value {
// parseLong doesn't work for ffffffffffffffff
byte[] d = getBytes();
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));
}
......
......@@ -11,7 +11,7 @@ import java.util.UUID;
import org.h2.api.ErrorCode;
import org.h2.message.DbException;
import org.h2.util.Utils;
import org.h2.util.Bits;
import org.h2.util.MathUtils;
import org.h2.util.StringUtils;
......@@ -68,8 +68,8 @@ public class ValueUuid extends Value {
if (binary.length < 16) {
return get(StringUtils.convertBytesToHex(binary));
}
long high = Utils.readLong(binary, 0);
long low = Utils.readLong(binary, 8);
long high = Bits.readLong(binary, 0);
long low = Bits.readLong(binary, 8);
return (ValueUuid) Value.cache(new ValueUuid(high, low));
}
......@@ -186,7 +186,7 @@ public class ValueUuid extends Value {
@Override
public byte[] getBytes() {
return Utils.uuidToBytes(high, low);
return Bits.uuidToBytes(high, low);
}
@Override
......
......@@ -19,6 +19,7 @@ import java.util.Comparator;
import java.util.Date;
import java.util.Random;
import org.h2.test.TestBase;
import org.h2.util.Bits;
import org.h2.util.IOUtils;
import org.h2.util.Utils;
......@@ -96,15 +97,15 @@ public class TestUtils extends TestBase {
byte[] buff = new byte[8];
for (long x : new long[]{Long.MIN_VALUE, Long.MAX_VALUE, 0, 1, -1,
Integer.MIN_VALUE, Integer.MAX_VALUE}) {
Utils.writeLong(buff, 0, x);
long y = Utils.readLong(buff, 0);
Bits.writeLong(buff, 0, x);
long y = Bits.readLong(buff, 0);
assertEquals(x, y);
}
Random r = new Random(1);
for (int i = 0; i < 1000; i++) {
long x = r.nextLong();
Utils.writeLong(buff, 0, x);
long y = Utils.readLong(buff, 0);
Bits.writeLong(buff, 0, x);
long y = Bits.readLong(buff, 0);
assertEquals(x, y);
}
}
......
......@@ -22,7 +22,7 @@ import org.h2.message.DbException;
import org.h2.test.TestBase;
import org.h2.test.utils.AssertThrows;
import org.h2.tools.SimpleResultSet;
import org.h2.util.Utils;
import org.h2.util.Bits;
import org.h2.value.DataType;
import org.h2.value.Value;
import org.h2.value.ValueArray;
......@@ -130,7 +130,7 @@ public class TestValue extends TestBase {
prep.setObject(1, new Object[] { uuid });
rs = prep.executeQuery();
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
prep = conn.prepareStatement("SELECT * FROM TABLE(X UUID=?)");
prep.setObject(1, new Object[] { uuid });
......
......@@ -757,4 +757,4 @@ chittanoor carrot
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
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论