Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ed8d2363 authored by Chalard Jean's avatar Chalard Jean Committed by Gerrit Code Review
Browse files

Merge "Add some useful helpers and constants."

parents 3506aaf9 e5659bd3
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -91,6 +91,10 @@ public final class BitUtils {
        return s & 0xffff;
    }

    public static int uint16(byte hi, byte lo) {
        return ((hi & 0xff) << 8) | (lo & 0xff);
    }

    public static long uint32(int i) {
        return i & 0xffffffffL;
    }
+14 −0
Original line number Diff line number Diff line
@@ -106,6 +106,20 @@ public final class NetworkConstants {
    public static final int RFC7421_PREFIX_LENGTH = 64;
    public static final int RFC6177_MIN_PREFIX_LENGTH = 48;

    /**
     * ICMP common (v4/v6) constants.
     *
     * See also:
     *     - https://tools.ietf.org/html/rfc792
     *     - https://tools.ietf.org/html/rfc4443
     */
    public static final int ICMP_HEADER_TYPE_OFFSET = 0;
    public static final int ICMP_HEADER_CODE_OFFSET = 1;
    public static final int ICMP_HEADER_CHECKSUM_OFFSET = 2;
    public static final int ICMP_ECHO_IDENTIFIER_OFFSET = 4;
    public static final int ICMP_ECHO_SEQUENCE_NUMBER_OFFSET = 6;
    public static final int ICMP_ECHO_DATA_OFFSET = 8;

    /**
     * ICMPv6 constants.
     *
+19 −0
Original line number Diff line number Diff line
@@ -55,6 +55,25 @@ public class BitUtilsTest {
        assertEquals(65535, uint16((short)65535));
    }

    @Test
    public void testUnsignedShortComposition() {
        byte b0 = 0;
        byte b1 = 1;
        byte b2 = 2;
        byte b10 = 10;
        byte b16 = 16;
        byte b128 = -128;
        byte b224 = -32;
        byte b255 = -1;
        assertEquals(0x0000, uint16(b0, b0));
        assertEquals(0xffff, uint16(b255, b255));
        assertEquals(0x0a01, uint16(b10, b1));
        assertEquals(0x8002, uint16(b128, b2));
        assertEquals(0x01ff, uint16(b1, b255));
        assertEquals(0x80ff, uint16(b128, b255));
        assertEquals(0xe010, uint16(b224, b16));
    }

    @Test
    public void testUnsignedIntWideningConversions() {
        assertEquals(0, uint32(0));