<commit_before>// Copyright (C) 2012 jOVAL.org.  All rights reserved.
// This software is licensed under the AGPL 3.0 license available at http://www.joval.org/agpl_v3.txt

package org.joval.scap.oval.types;

import java.math.BigInteger;
import java.util.StringTokenizer;

import org.joval.intf.scap.oval.IType;

/**
 * A type class for dealing with individual addresses or CIDR ranges for IPv4.
 *
 * @author <NAME>
 * @version %I% %G%
 */
public class Ip4AddressType extends AbstractType {
    private short[] addr = new short[4];
    private short[] mask = new short[4];
    private int maskVal;

    public Ip4AddressType(String str) throws IllegalArgumentException {
	str = str.trim();
	if (str.length() == 0) {
	    throw new IllegalArgumentException(str);
	}
	int ptr = str.indexOf("/");
	maskVal = 32;
	String ipStr = null;
	if (ptr == -1) {
	    ipStr = str;
	} else {
	    maskVal = Integer.parseInt(str.substring(ptr+1));
	    ipStr = str.substring(0, ptr);
	}

	//
	// Create the netmask
	//
	short[] maskBits = new short[32];
	for (int i=0; i < 32; i++) {
	    if (i < maskVal) {
		maskBits[i] = 1;
	    } else {
		maskBits[i] = 0;
	    }
	}
	for (int i=0; i < 4; i++) {
	    mask[i] = (short)(maskBits[8*i + 0] * 128 +<commit_msg>Add support for octet-style CIDR masks in IPv4 OVAL address type string input.<commit_after>// Copyright (C) 2012 jOVAL.org.  All rights reserved.
// This software is licensed under the AGPL 3.0 license available at http://www.joval.org/agpl_v3.txt

package org.joval.scap.oval.types;

import java.math.BigInteger;
import java.util.StringTokenizer;

import org.joval.intf.scap.oval.IType;

/**
 * A type class for dealing with individual addresses or CIDR ranges for IPv4.
 *
 * @author <NAME>
 * @version %I% %G%
 */
public class Ip4AddressType extends AbstractType {
    private short[] addr = new short[4];
    private short[] mask = new short[4];
    private int maskVal;

    public Ip4AddressType(String str) throws IllegalArgumentException {
	str = str.trim();
	if (str.length() == 0) {
	    throw new IllegalArgumentException(str);
	}
	int ptr = str.indexOf("/");
	maskVal = 32;
	String ipStr = null;
	if (ptr == -1) {
	    ipStr = str;
	} else {
	    ipStr = str.substring(0, ptr);

	    String s = str.substring(ptr+1);
	    if (s.indexOf(".") == -1)  {
		//
		// Mask is expressed in the form of an integer, from 0-32
		//
		maskVal = Integer.parseInt(str.substring(ptr+1));
	    } else {
		//
		// Mask is expressed as a subnet, in octets
		//
		StringTokenizer tok = new StringTokenizer(".");
		if (tok.countTokens() > 4) {
		    throw new IllegalArgumentException(str);
		}
		maskVal = 0;
		while (tok.hasMoreTokens()) {
		    short n = (short)(Short.parseShort(tok.nextToken()));
		    if (128 == (n & 128)) {
			maskVal++;
		    } else {
			break;
		    }
		    if (64 == (n & 64)) {
			maskVal++;
		    } else {
			break;
		    }
		    if (32 == (n & 32)) {
			maskVal++;
		    } else {
			break;
		    }
		    if (16 == (n & 16)) {
			maskVal++;
		    } else {
			break;
		    }
		    if (8 == (n & 8)) {
			maskVal++;
		    } else {
			break;
		    }
		    if (4 == (n & 4)) {
			maskVal++;
		    } else {
			break;
		    }
		    if (2 == (n & 2)) {
			maskVal++;
		    } else {
			break;
		    }
		    if (1 == (n & 1)) {
			maskVal++;
		    } else {
			break;
		    }
		}
	    }
	}

	//
	// Create the netmask
	//
	short[] maskBits = new short[32];
	for (int i=0; i < 32; i++) {
	    if (i < maskVal) {
		maskBits[i] = 1;
	    } else {
		maskBits[i] = 0;
	    }
	}
	for (int i=0; i < 4; i++) {
	    mask[i] = (short)(maskBits[8*i + 0] * 128 +
{'fraction_non_alphanumeric': 0.15719434433046853, 'fraction_numerical': 0.027169392847241475, 'mean_word_length': 2.4069877242681774, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3864583', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1299, 'n_words': 365}