I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 957767c9 authored by zzz's avatar zzz
Browse files

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

parent 3cc73749
No related branches found
No related tags found
No related merge requests found
......@@ -807,6 +807,37 @@ public class DataHelper {
throw new IllegalArgumentException("fromLong got a negative? " + rv + ": offset="+ offset +" numBytes="+numBytes);
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.
* A date is an 8 byte unsigned integer in network byte order specifying the number of
......
......@@ -473,35 +473,6 @@ class RatchetPayload {
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.
......
package net.i2p.router.crypto.ratchet;
import net.i2p.data.Base64;
import net.i2p.data.DataHelper;
/**
* 8 bytes of random data.
......@@ -23,7 +24,7 @@ public class RatchetSessionTag {
public RatchetSessionTag(byte val[]) {
if (val.length < LENGTH)
throw new IllegalArgumentException();
_data = RatchetPayload.fromLong8(val, 0);
_data = DataHelper.fromLong8(val, 0);
}
/**
......@@ -31,7 +32,7 @@ public class RatchetSessionTag {
*/
public byte[] getData() {
byte[] rv = new byte[LENGTH];
RatchetPayload.toLong8(rv, 0, _data);
DataHelper.toLong8(rv, 0, _data);
return rv;
}
......
......@@ -135,7 +135,7 @@ class NTCP2Payload {
throw new IOException("Illegal block in handshake: " + type);
if (len < 9)
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;
cb.gotTermination(rsn, last);
gotTermination = true;
......@@ -333,40 +333,9 @@ class NTCP2Payload {
}
public int writeData(byte[] tgt, int off) {
toLong8(tgt, off, rcvd);
DataHelper.toLong8(tgt, off, rcvd);
tgt[off + 8] = rsn;
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;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment