<commit_before><?php

namespace X509\Certificate\Extension\NameConstraints;

use ASN1\Element;
use ASN1\Type\Constructed\Sequence;
use ASN1\Type\Primitive\Integer;
use ASN1\Type\Tagged\ImplicitlyTaggedType;
use X509\GeneralName\GeneralName;


/**
 * Implements <i>GeneralSubtree</i> ASN.1 type used by
 * 'Name Constraints' certificate extension.
 *
 * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.10
 */
class GeneralSubtree
{
	/**
	 * Constraint.
	 *
	 * @var GeneralName
	 */
	protected $_base;
	
	/**
	 * Not used, must be zero.
	 *
	 * @var int $_min
	 */
	protected $_min;
	
	/**
	 * Not used, must be null.
	 *
	 * @var int|null $_max
	 */
	protected $_max;
	
	/**
	 * Constructor
	 *
	 * @param GeneralName $base
	 * @param int $min
	 * @param int|null $max
	 */
	public function __construct(GeneralName $base, $min = 0, $max = null) {
		$this->_base = $base;
		$this->_min = $min;
		$this->_max = $max;
	}
	
	/**
	 * Initialize from ASN.1.
	 *
	 * @param Sequence $seq
	 * @return self
	 */
	public static function fromASN1(Sequence $seq) {
		$base = GeneralName::fromASN1($seq->at(0)->asTagged());
		$min = 0;
		$max = null;
		if ($seq->hasTagged(0)) {
			$min = $seq->getTagged(0)
				->asImplicit(Element::TYPE_INTEGER)
				->asInteger()
				->number();
		}
		if ($seq->hasTagged(1)) {
			$max = $seq->getTagged(1)
				->asImplicit(Element::TYPE_INTEGER)
				->asInteger()
				->number();
		}
		return new self($base, $min, $max);
	}
	
	/**
	 * Get constraint.
	 *
	 * @return GeneralName
	 */
	public function base() {
		return $this->_base;
	}
	
	/**
	 * Generate ASN.1 structure.
	 *
	 * @return Sequence
	 */
	public function toASN1() {
		$elements = array($this->_base->toASN1());
		if (isset($this->_min) && $this->_max != 0) {
			$elements[] = new ImplicitlyTaggedType(0, new Integer($this->_min));
		}
		if (isset($this->_max)) {
			$elements[] = new ImplicitlyTaggedType(1, new Integer($this->_max));
		}
		return new Sequence(...$elements);
	}
}<commit_msg>Fix ASN1 encoding<commit_after><?php

namespace X509\Certificate\Extension\NameConstraints;

use ASN1\Element;
use ASN1\Type\Constructed\Sequence;
use ASN1\Type\Primitive\Integer;
use ASN1\Type\Tagged\ImplicitlyTaggedType;
use X509\GeneralName\GeneralName;


/**
 * Implements <i>GeneralSubtree</i> ASN.1 type used by
 * 'Name Constraints' certificate extension.
 *
 * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.10
 */
class GeneralSubtree
{
	/**
	 * Constraint.
	 *
	 * @var GeneralName
	 */
	protected $_base;
	
	/**
	 * Not used, must be zero.
	 *
	 * @var int $_min
	 */
	protected $_min;
	
	/**
	 * Not used, must be null.
	 *
	 * @var int|null $_max
	 */
	protected $_max;
	
	/**
	 * Constructor
	 *
	 * @param GeneralName $base
	 * @param int $min
	 * @param int|null $max
	 */
	public function __construct(GeneralName $base, $min = 0, $max = null) {
		$this->_base = $base;
		$this->_min = $min;
		$this->_max = $max;
	}
	
	/**
	 * Initialize from ASN.1.
	 *
	 * @param Sequence $seq
	 * @return self
	 */
	public static function fromASN1(Sequence $seq) {
		$base = GeneralName::fromASN1($seq->at(0)->asTagged());
		$min = 0;
		$max = null;
		if ($seq->hasTagged(0)) {
			$min = $seq->getTagged(0)
				->asImplicit(Element::TYPE_INTEGER)
				->asInteger()
				->number();
		}
		if ($seq->hasTagged(1)) {
			$max = $seq->getTagged(1)
				->asImplicit(Element::TYPE_INTEGER)
				->asInteger()
				->number();
		}
		return new self($base, $min, $max);
	}
	
	/**
	 * Get constraint.
	 *
	 * @return GeneralName
	 */
	public function base() {
		return $this->_base;
	}
	
	/**
	 * Generate ASN.1 structure.
	 *
	 * @return Sequence
	 */
	public function toASN1() {
		$elements = array($this->_base->toASN1());
		if (isset($this->_min) && $this->_min != 0) {
			$elements[] = new ImplicitlyTaggedType(0, new Integer($this->_min));
		}
		if (isset($this->_max)) {
			$elements[] = new ImplicitlyTaggedType(1, new Integer($this->_max));
		}
		return new Sequence(...$elements);
	}
}
{'fraction_non_alphanumeric': 0.18072289156626506, 'fraction_numerical': 0.018323293172690762, 'mean_word_length': 3.1467221644120706, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 53, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3750389', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1447, 'n_words': 306}