Util: Move fromLong8()/toLong8() methods to DataHelper

This commit is contained in:
zzz
2020-06-01 12:25:09 +00:00
parent 3cc7374984
commit 957767c985
4 changed files with 36 additions and 64 deletions

View File

@ -807,6 +807,37 @@ public class DataHelper {
throw new IllegalArgumentException("fromLong got a negative? " + rv + ": offset="+ offset +" numBytes="+numBytes); throw new IllegalArgumentException("fromLong got a negative? " + rv + ": offset="+ offset +" numBytes="+numBytes);
return rv; return rv;
} }
/**
* Big endian.
* Same as fromLong(src, offset, 8) but allows negative result
*
* @throws ArrayIndexOutOfBoundsException
* @since 0.9.47 moved from NTCP2Payload
*/
public static long fromLong8(byte src[], int offset) {
long rv = 0;
int limit = offset + 8;
for (int i = offset; i < limit; i++) {
rv <<= 8;
rv |= src[i] & 0xFF;
}
return rv;
}
/**
* Big endian.
* Same as toLong(target, offset, 8, value) but allows negative value
*
* @throws ArrayIndexOutOfBoundsException
* @since 0.9.47 moved from NTCP2Payload
*/
public static void toLong8(byte target[], int offset, long value) {
for (int i = offset + 7; i >= offset; i--) {
target[i] = (byte) value;
value >>= 8;
}
}
/** Read in a date from the stream as specified by the I2P data structure spec. /** Read in a date from the stream as specified by the I2P data structure spec.
* A date is an 8 byte unsigned integer in network byte order specifying the number of * A date is an 8 byte unsigned integer in network byte order specifying the number of

View File

@ -473,35 +473,6 @@ class RatchetPayload {
return off + 2; return off + 2;
} }
} }
/**
* Big endian.
* Same as DataHelper.fromLong(src, offset, 8) but allows negative result
*
* @throws ArrayIndexOutOfBoundsException
*/
static long fromLong8(byte src[], int offset) {
long rv = 0;
int limit = offset + 8;
for (int i = offset; i < limit; i++) {
rv <<= 8;
rv |= src[i] & 0xFF;
}
return rv;
}
/**
* Big endian.
* Same as DataHelper.toLong(target, offset, 8, value) but allows negative value
*
* @throws ArrayIndexOutOfBoundsException
*/
static void toLong8(byte target[], int offset, long value) {
for (int i = offset + 7; i >= offset; i--) {
target[i] = (byte) value;
value >>= 8;
}
}
/** /**
* Big endian. * Big endian.

View File

@ -1,6 +1,7 @@
package net.i2p.router.crypto.ratchet; package net.i2p.router.crypto.ratchet;
import net.i2p.data.Base64; import net.i2p.data.Base64;
import net.i2p.data.DataHelper;
/** /**
* 8 bytes of random data. * 8 bytes of random data.
@ -23,7 +24,7 @@ public class RatchetSessionTag {
public RatchetSessionTag(byte val[]) { public RatchetSessionTag(byte val[]) {
if (val.length < LENGTH) if (val.length < LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
_data = RatchetPayload.fromLong8(val, 0); _data = DataHelper.fromLong8(val, 0);
} }
/** /**
@ -31,7 +32,7 @@ public class RatchetSessionTag {
*/ */
public byte[] getData() { public byte[] getData() {
byte[] rv = new byte[LENGTH]; byte[] rv = new byte[LENGTH];
RatchetPayload.toLong8(rv, 0, _data); DataHelper.toLong8(rv, 0, _data);
return rv; return rv;
} }

View File

@ -135,7 +135,7 @@ class NTCP2Payload {
throw new IOException("Illegal block in handshake: " + type); throw new IOException("Illegal block in handshake: " + type);
if (len < 9) if (len < 9)
throw new IOException("Bad length for TERMINATION: " + len); throw new IOException("Bad length for TERMINATION: " + len);
long last = fromLong8(payload, i); long last = DataHelper.fromLong8(payload, i);
int rsn = payload[i + 8] & 0xff; int rsn = payload[i + 8] & 0xff;
cb.gotTermination(rsn, last); cb.gotTermination(rsn, last);
gotTermination = true; gotTermination = true;
@ -333,40 +333,9 @@ class NTCP2Payload {
} }
public int writeData(byte[] tgt, int off) { public int writeData(byte[] tgt, int off) {
toLong8(tgt, off, rcvd); DataHelper.toLong8(tgt, off, rcvd);
tgt[off + 8] = rsn; tgt[off + 8] = rsn;
return off + 9; return off + 9;
} }
} }
/**
* Big endian.
* Same as DataHelper.fromLong(src, offset, 8) but allows negative result
*
* @throws ArrayIndexOutOfBoundsException
* @since 0.9.36
*/
private static long fromLong8(byte src[], int offset) {
long rv = 0;
int limit = offset + 8;
for (int i = offset; i < limit; i++) {
rv <<= 8;
rv |= src[i] & 0xFF;
}
return rv;
}
/**
* Big endian.
* Same as DataHelper.toLong(target, offset, 8, value) but allows negative value
*
* @throws ArrayIndexOutOfBoundsException
* @since 0.9.36
*/
private static void toLong8(byte target[], int offset, long value) {
for (int i = offset + 7; i >= offset; i--) {
target[i] = (byte) value;
value >>= 8;
}
}
} }