Source
xxxxxxxxxx
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/******************************************************************************
* *
* Purpose: after each delta is encoded or decoded, bias should be set for *
* the next delta (should be adapted) *
* *
* Parameters: delta - [IN] punycode delta (generalized variable-length *
* integer) *
* count - [IN] is the total number of code points encoded / *
* decoded so far *
* divisor - [IN] delta divisor (to avoid overflow) *
* *
* Return value: adapted bias *
* *
******************************************************************************/
static zbx_uint32_t punycode_adapt(zbx_uint32_t delta, int count, int divisor)
{
zbx_uint32_t i;
delta /= divisor;
delta += delta / count;
for (i = 0; PUNYCODE_BIAS_LIMIT < delta; i += PUNYCODE_BASE)
delta /= PUNYCODE_BASE_MAX;
return ((PUNYCODE_BASE * delta) / (delta + PUNYCODE_SKEW)) + i;
}
/******************************************************************************
* *
* Purpose: encodes punycode digit into ansi character [a-z0-9] *
* *
* Parameters: digit - [IN] digit to encode *
* *
* Return value: encoded character *
* *
******************************************************************************/
static char punycode_encode_digit(int digit)
{
if (0 <= digit && 25 >= digit)
return digit + 'a';
else if (25 < digit && PUNYCODE_BASE > digit)
return digit + 22;
THIS_SHOULD_NEVER_HAPPEN;
return '\0';
}
/******************************************************************************
* *
* Purpose: encodes array of unicode codepoints into punycode (RFC 3492) *
* *
* Parameters: codepoints - [IN] codepoints to encode *
* count - [IN] codepoint count *
* output - [OUT] encoded result *
* length - [IN] length of result buffer *
* *
* Return value: SUCCEED if encoding was successful. FAIL on error. *
* *
******************************************************************************/
static int punycode_encode_codepoints(zbx_uint32_t *codepoints, size_t count, char *output, size_t length)
{
int ret = FAIL;
zbx_uint32_t n, delta = 0, bias, max_codepoint, q, k, t;
size_t h = 0, out = 0, offset, j;
n = PUNYCODE_INITIAL_N;
bias = PUNYCODE_INITIAL_BIAS;