<commit_before>// Copyright (c) 1999-2004 <NAME> (<EMAIL>)

package org.xbill.DNS;

import java.net.*;
import java.net.Inet6Address;
import java.util.*;

/**
 * Routines dealing with IP addresses.  Includes functions similar to
 * those in the java.net.InetAddress class.
 *
 * @author <NAME>
 */

public final class Address {

public static final int IPv4 = 1;
public static final int IPv6 = 2;

private
Address() {}

private static byte []
parseV4(String s) {
	int numDigits;
	int currentOctet;
	byte [] values = new byte[4];
	int currentValue;
	int length = s.length();

	currentOctet = 0;
	currentValue = 0;
	numDigits = 0;
	for (int i = 0; i < length; i++) {
		char c = s.charAt(i);
		if (c >= '0' && c <= '9') {
			/* Can't have more than 3 digits per octet. */
			if (numDigits == 3)
				return null;
			/* Octets shouldn't start with 0, unless they are 0. */
			if (numDigits > 0 && currentValue == 0)
				return null;
			numDigits++;
			currentValue *= 10;
			currentValue += (c - '0');
			/* 255 is the maximum value for an octet. */
			if (currentValue > 255)
				return null;
		} else if (c == '.') {
			/* Can't have more than 3 dots. */
			if (currentOctet == 3)
				return null;
			/* Two consecutive dots are bad. */
			if (numDigits == 0)
				return null;
			values[currentOctet++] = (byte) currentValue;
			currentValue = 0;
			numDigits = 0;
		} else
			return null;
	}
	/* Must have 4 octets. */
	if (currentOctet != 3)
		return null;
	/* The fourth octet can't be empty. */
	if (numDigits == 0)
		return null;
	values[currentOctet] = (byte) currentValue;
	return values;
}

private static byte []
parseV6(String s) {
	boolean parsev4 = false;
	List l = new ArrayList();
	int range = -1;

	byte [] data = new byte[16];

	StringTokenizer st = new StringTokenizer(s, ":", true);
	while (st.hasMoreTokens())
		l.add(st.nextToken());
	l.add("");
	l.add("");

	String [] tokens = (String []) l.toArray(new String[l.size()]);

	int i = 0, j = 0;
	while (i < tokens.length - 2) {
		if (tokens[i].equals(":")) {
			if (tokens[i+1].equals(":")) {
				if (tokens[i+2].equals(":") || range >= 0)
					return null;
				range = j;
				if (tokens[i+2].equals(""))
					break;
				i++;
			}
			i++;
		}

		if (tokens[i].indexOf('.') >= 0) {
			parsev4 = true;
			if (!tokens[i+1].equals(""))
				return null;
			break;
		}

		try {
			int x = Integer.parseInt(tokens[i], 16);
			if (x > 0xFFFF || x < 0)
				return null;
			if (j > 16 - 2)
				return null;
			data[j++] = (byte)(x >>> 8);
			data[j++] = (byte)(x & 0xFF);
		}
		catch (NumberFormatException e) {
			return null;
		}
		i++;
	}

	if (parsev4) {
		byte [] v4addr = Address.toByteArray(tokens[i], IPv4);
		if (v4addr == null)
			return null;
		for (int k = 0; k < 4; k++)
			data[j++] = v4addr[k];
	}
	if (range >= 0) {
		int left = 16 - j;
		for (int k = 15; k >= 0; k--) {
			if (k >= range + left)
				data[k] = data[k - left];
			else if (k >= range)
				data[k] = 0;
		}
	} else if (j < 16)
		return null;
	return data;
}

/**
 * Convert a string containing an IP address to an array of 4 or 16 integers.
 * @param s The address, in text format.
 * @param family The address family.
 * @return The address
 */
public static int []
toArray(String s, int family) {
	byte [] byteArray = toByteArray(s, family);
	if (byteArray == null)
		return null;
	int [] intArray = new int[byteArray.length];
	for (int i = 0; i < byteArray.length; i++)
		intArray[i] = byteArray[i] & 0xFF;
	return intArray;
}

/**
 * Convert a string containing an IPv4 address to an array of 4 integers.
 * @param s The address, in text format.
 * @return The address
 */<commit_msg>simplify v6 address parsing<commit_after>// Copyright (c) 1999-2004 <NAME> (<EMAIL>)

package org.xbill.DNS;

import java.net.*;
import java.net.Inet6Address;
import java.util.*;

/**
 * Routines dealing with IP addresses.  Includes functions similar to
 * those in the java.net.InetAddress class.
 *
 * @author <NAME>
 */

public final class Address {

public static final int IPv4 = 1;
public static final int IPv6 = 2;

private
Address() {}

private static byte []
parseV4(String s) {
	int numDigits;
	int currentOctet;
	byte [] values = new byte[4];
	int currentValue;
	int length = s.length();

	currentOctet = 0;
	currentValue = 0;
	numDigits = 0;
	for (int i = 0; i < length; i++) {
		char c = s.charAt(i);
		if (c >= '0' && c <= '9') {
			/* Can't have more than 3 digits per octet. */
			if (numDigits == 3)
				return null;
			/* Octets shouldn't start with 0, unless they are 0. */
			if (numDigits > 0 && currentValue == 0)
				return null;
			numDigits++;
			currentValue *= 10;
			currentValue += (c - '0');
			/* 255 is the maximum value for an octet. */
			if (currentValue > 255)
				return null;
		} else if (c == '.') {
			/* Can't have more than 3 dots. */
			if (currentOctet == 3)
				return null;
			/* Two consecutive dots are bad. */
			if (numDigits == 0)
				return null;
			values[currentOctet++] = (byte) currentValue;
			currentValue = 0;
			numDigits = 0;
		} else
			return null;
	}
	/* Must have 4 octets. */
	if (currentOctet != 3)
		return null;
	/* The fourth octet can't be empty. */
	if (numDigits == 0)
		return null;
	values[currentOctet] = (byte) currentValue;
	return values;
}

private static byte []
parseV6(String s) {
	boolean parsev4 = false;
	List l = new ArrayList();
	int range = -1;
	byte [] data = new byte[16];

	String [] tokens = s.split(":", -1);

	int first = 0;
	int last = tokens.length - 1;

	if (tokens[0].length() == 0) {
		// If the first two tokens are empty, it means the string
		// started with ::, which is fine.  If only the first is
		// empty, the string started with :, which is bad.
		if (last - first > 0 && tokens[1].length() == 0)
			first++;
		else
			return null;
	}

	if (tokens[last].length() == 0) {
		// If the last two tokens are empty, it means the string
		// ended with ::, which is fine.  If only the last is
		// empty, the string started with :, which is bad.
		if (last - first > 0 && tokens[last - 1].length() == 0)
			last--;
		else
			return null;
	}

	if (last - first + 1 > 8)
		return null;

	int i, j;
	for (i = first, j = 0; i <= last; i++) {
		if (tokens[i].length() == 0) {
			if (range >= 0)
				return null;
			range = j;
			continue;
		}

		if (tokens[i].indexOf('.') >= 0) {
			parsev4 = true;
			// An IPv4 address must be the last component
			if (i < last)
				return null;
			// There can't have been more than 6 components.
			if (i > 6)
				return null;
			byte [] v4addr = Address.toByteArray(tokens[i], IPv4);
			if (v4addr == null)
				return null;
			for (int k = 0; k < 4; k++)
				data[j++] = v4addr[k];
			break;
		}

		try {
			for (int k = 0; k < tokens[i].length(); k++) {
				char c = tokens[i].charAt(k);
				if (Character.digit(c, 16) < 0)
					return null;
			}
			int x = Integer.parseInt(tokens[i], 16);
			if (x > 0xFFFF || x < 0)
				return null;
			data[j++] = (byte)(x >>> 8);
			data[j++] = (byte)(x & 0xFF);
		}
		catch (NumberFormatException e) {
			return null;
		}
	}

	if (j < 16 && range < 0)
		return null;

	if (range >= 0) {
		int empty = 16 - j;
		System.arraycopy(data, range, data, range + empty, j - range);
		for (i = range; i < range + empty; i++)
			data[i] = 0;
	}

	return data;
}

/**
 * Convert a string containing an IP address to an array of 4 or 16 integers.
 * @param s The address, in text format.
 * @param family The address family.
 * @return The address
 */
public static int []
toArray(String s, int family) {
	byte [] byteArray = toByteArray(s, family);
	if (byteArray == null)
		return null;
	int [] intArray = new int[byteArray.length];
	for (int i = 0; i < byteArray.length; i++)
		intArray[i] = byteArray[i] & 0xFF;
	return intArray;
}

/**
 * Convert a string containing an IPv4 address to an array of 4 integers.
 * @param s The address, in text format.
 * @return The address
 */
{'fraction_non_alphanumeric': 0.15853658536585366, 'fraction_numerical': 0.024517276422764227, 'mean_word_length': 2.7472632079961925, 'pattern_counts': {'":': 5, '<': 28, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1431661', 'n_tokens_mistral': 3317, 'n_tokens_neox': 2830, 'n_words': 981}