Source
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* A parser for IPv6 address.
*/
class CIPv6Parser extends CParser {
const STATE_NEW = 0;
const STATE_AFTER_DIGITS = 1;
const STATE_AFTER_COLON = 2;
const STATE_AFTER_DBLCOLON = 3;
/**
* @var CIPv4Parser
*/
private $ipv4_parser;
public function __construct() {
$this->ipv4_parser = new CIPv4Parser();
}
/**
* @param string $source
* @param int $pos
*
* @return int
*/
public function parse($source, $pos = 0) {
$this->length = 0;
$this->match = '';
$state = self::STATE_NEW;
$colons = 0;
$dbl_colons = 0;
for ($p = $pos; isset($source[$p]); $p++) {
switch ($state) {
case self::STATE_NEW:
if (self::parseDoubleColon($source, $p)) {
if ($dbl_colons++ == 1) {
return self::PARSE_FAIL;
}
$state = self::STATE_AFTER_DBLCOLON;
}
elseif (self::parseXDigits($source, $p)) {
$state = self::STATE_AFTER_DIGITS;
}
else {
return self::PARSE_FAIL;