From 957767c9852dc8edc1ef86a7c937907023d42e7e Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Mon, 1 Jun 2020 12:25:09 +0000 Subject: [PATCH] Util: Move fromLong8()/toLong8() methods to DataHelper --- core/java/src/net/i2p/data/DataHelper.java | 31 ++++++++++++++++ .../router/crypto/ratchet/RatchetPayload.java | 29 --------------- .../crypto/ratchet/RatchetSessionTag.java | 5 +-- .../router/transport/ntcp/NTCP2Payload.java | 35 ++----------------- 4 files changed, 36 insertions(+), 64 deletions(-) diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index 010d20066a..aaa47797c3 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -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 diff --git a/router/java/src/net/i2p/router/crypto/ratchet/RatchetPayload.java b/router/java/src/net/i2p/router/crypto/ratchet/RatchetPayload.java index bc2d9df5f1..a63043d7e5 100644 --- a/router/java/src/net/i2p/router/crypto/ratchet/RatchetPayload.java +++ b/router/java/src/net/i2p/router/crypto/ratchet/RatchetPayload.java @@ -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. diff --git a/router/java/src/net/i2p/router/crypto/ratchet/RatchetSessionTag.java b/router/java/src/net/i2p/router/crypto/ratchet/RatchetSessionTag.java index 5a22d61dad..3650bda9ad 100644 --- a/router/java/src/net/i2p/router/crypto/ratchet/RatchetSessionTag.java +++ b/router/java/src/net/i2p/router/crypto/ratchet/RatchetSessionTag.java @@ -1,6 +1,7 @@ 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; } diff --git a/router/java/src/net/i2p/router/transport/ntcp/NTCP2Payload.java b/router/java/src/net/i2p/router/transport/ntcp/NTCP2Payload.java index 516aac349b..eac71fc951 100644 --- a/router/java/src/net/i2p/router/transport/ntcp/NTCP2Payload.java +++ b/router/java/src/net/i2p/router/transport/ntcp/NTCP2Payload.java @@ -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; - } - } } -- GitLab