@Override public boolean matches(InetSocketAddress remoteAddress) { final InetAddress inetAddress = remoteAddress.getAddress(); if (inetAddress instanceof Inet6Address) { BigInteger ipAddress = ipToInt((Inet6Address) inetAddress); return ipAddress.and(subnetMask).equals(networkAddress); } return false; }
private Ip6SubnetFilterRule(Inet6Address ipAddress, int cidrPrefix, IpFilterRuleType ruleType) { if (cidrPrefix < 0 || cidrPrefix > 128) { throw new IllegalArgumentException(String.format("IPv6 requires the subnet prefix to be in range of " + "[0,128]. The prefix was: %d", cidrPrefix)); } subnetMask = prefixToSubnetMask(cidrPrefix); networkAddress = ipToInt(ipAddress).and(subnetMask); this.ruleType = ruleType; }
@Override public boolean matches(InetSocketAddress remoteAddress) { final InetAddress inetAddress = remoteAddress.getAddress(); if (inetAddress instanceof Inet6Address) { BigInteger ipAddress = ipToInt((Inet6Address) inetAddress); return ipAddress.and(subnetMask).equals(networkAddress); } return false; }
public ExpandedDouble(long rawBits) { int biasedExp = (int) (rawBits >> 52); if (biasedExp == 0) { // sub-normal numbers BigInteger frac = BigInteger.valueOf(rawBits).and(BI_FRAC_MASK); int expAdj = 64 - frac.bitLength(); _significand = frac.shiftLeft(expAdj); _binaryExponent = (biasedExp & 0x07FF) - 1023 - expAdj; } else { _significand = getFrac(rawBits); _binaryExponent = (biasedExp & 0x07FF) - 1023; } }
public ExpandedDouble(long rawBits) { int biasedExp = (int) (rawBits >> 52); if (biasedExp == 0) { // sub-normal numbers BigInteger frac = BigInteger.valueOf(rawBits).and(BI_FRAC_MASK); int expAdj = 64 - frac.bitLength(); _significand = frac.shiftLeft(expAdj); _binaryExponent = (biasedExp & 0x07FF) - 1023 - expAdj; } else { BigInteger frac = getFrac(rawBits); _significand = frac; _binaryExponent = (biasedExp & 0x07FF) - 1023; } }
private Ip6SubnetFilterRule(Inet6Address ipAddress, int cidrPrefix, IpFilterRuleType ruleType) { if (cidrPrefix < 0 || cidrPrefix > 128) { throw new IllegalArgumentException(String.format("IPv6 requires the subnet prefix to be in range of " + "[0,128]. The prefix was: %d", cidrPrefix)); } subnetMask = prefixToSubnetMask(cidrPrefix); networkAddress = ipToInt(ipAddress).and(subnetMask); this.ruleType = ruleType; }
private void calculate() throws UnknownHostException { final int targetSize; final BigInteger mask; if (inetAddress.getAddress().length == 4) { targetSize = 4; mask = (new BigInteger(1, MASK_IPV4)).not().shiftRight(prefixLength); } else { targetSize = 16; mask = (new BigInteger(1, MASK_IPV6)).not().shiftRight(prefixLength); } final BigInteger ipVal = new BigInteger(1, inetAddress.getAddress()); final BigInteger startIp = ipVal.and(mask); final BigInteger endIp = startIp.add(mask.not()); final byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize); final byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize); this.startAddress = InetAddress.getByAddress(startIpArr); this.endAddress = InetAddress.getByAddress(endIpArr); }
public void testIsPowerOfTwo() { for (BigInteger x : ALL_BIGINTEGER_CANDIDATES) { // Checks for a single bit set. boolean expected = x.signum() > 0 & x.and(x.subtract(ONE)).equals(ZERO); assertEquals(expected, BigIntegerMath.isPowerOfTwo(x)); } }
public DataWord sMod(DataWord word) { if (word.isZero()) { return ZERO; } BigInteger result = sValue().abs().mod(word.sValue().abs()); result = (sValue().signum() == -1) ? result.negate() : result; return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }
/** * Shift left, both this and input arg are treated as unsigned * @param arg * @return this << arg */ public DataWord shiftLeft(DataWord arg) { if (arg.value().compareTo(BigInteger.valueOf(MAX_POW)) >= 0) { return DataWord.ZERO; } BigInteger result = value().shiftLeft(arg.intValueSafe()); return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }
/** * Shift right, both this and input arg are treated as unsigned * @param arg * @return this >> arg */ public DataWord shiftRight(DataWord arg) { if (arg.value().compareTo(BigInteger.valueOf(MAX_POW)) >= 0) { return DataWord.ZERO; } BigInteger result = value().shiftRight(arg.intValueSafe()); return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }
protected CIDR6(Inet6Address newaddress, int newmask) { cidrMask = newmask; addressBigInt = ipv6AddressToBigInteger(newaddress); BigInteger mask = ipv6CidrMaskToMask(newmask); try { addressBigInt = addressBigInt.and(mask); baseAddress = bigIntToIPv6Address(addressBigInt); } catch (UnknownHostException e) { // this should never happen. } addressEndBigInt = addressBigInt.add(ipv6CidrMaskToBaseAddress(cidrMask)).subtract(BigInteger.ONE); }
public DataWord add2(DataWord word) { BigInteger result = value().add(word.value()); return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }
public DataWord mul(DataWord word) { BigInteger result = value().multiply(word.value()); return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }
public DataWord sub(DataWord word) { BigInteger result = value().subtract(word.value()); return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }
public DataWord addmod(DataWord word1, DataWord word2) { if (word2.isZero()) { return ZERO; } BigInteger result = value().add(word1.value()).mod(word2.value()); return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }
public DataWord sDiv(DataWord word) { if (word.isZero()) { return ZERO; } BigInteger result = sValue().divide(word.sValue()); return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }
public DataWord div(DataWord word) { if (word.isZero()) { return ZERO; } BigInteger result = value().divide(word.value()); return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }
public DataWord mod(DataWord word) { if (word.isZero()) { return ZERO; } BigInteger result = value().mod(word.value()); return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }
public DataWord mulmod(DataWord word1, DataWord word2) { if (this.isZero() || word1.isZero() || word2.isZero()) { return ZERO; } BigInteger result = value().multiply(word1.value()).mod(word2.value()); return new DataWord(ByteUtil.copyToArray(result.and(MAX_VALUE))); }