Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/zbxcommon/str.c')
-rw-r--r--src/libs/zbxcommon/str.c343
1 files changed, 52 insertions, 291 deletions
diff --git a/src/libs/zbxcommon/str.c b/src/libs/zbxcommon/str.c
index 0134d0d9309..95f2fe1f7c5 100644
--- a/src/libs/zbxcommon/str.c
+++ b/src/libs/zbxcommon/str.c
@@ -486,210 +486,48 @@ void zbx_remove_chars(register char *str, const char *charlist)
/******************************************************************************
* *
- * Function: compress_signs *
- * *
- * Purpose: convert all repeating pluses and minuses *
- * *
- * Parameters: c - string to convert *
- * *
- * Return value: string without minuses *
- * *
- * Author: Alexei Vladishev *
- * *
- * Comments: -3*--8+5-7*-4+++5 -> N3*8+5+N7*N4+5 *
- * *
- ******************************************************************************/
-void compress_signs(char *str)
-{
- int i, j, len, loop = 1;
- char cur, next, prev;
-
- /* compress '--' '+-' '++' '-+' */
- while (1 == loop)
- {
- loop = 0;
-
- for (i = 0; '\0' != str[i]; i++)
- {
- cur = str[i];
- next = str[i + 1];
-
- if (('-' == cur && '-' == next) || ('+' == cur && '+' == next))
- {
- str[i] = '+';
- for (j = i + 1; '\0' != str[j]; j++)
- str[j] = str[j + 1];
- loop = 1;
- }
-
- if (('-' == cur && '+' == next) || ('+' == cur && '-' == next))
- {
- str[i] = '-';
- for (j = i + 1; '\0' != str[j]; j++)
- str[j] = str[j + 1];
- loop = 1;
- }
- }
- }
-
- /* remove '-', '+' where needed, convert -123 to +N123 */
- for (i = 0; '\0' != str[i]; i++)
- {
- cur = str[i];
-
- if ('+' == cur)
- {
- /* plus is the first sign in the expression */
- if (0 == i)
- {
- for (j = i; '\0' != str[j]; j++)
- str[j] = str[j + 1];
- }
- else
- {
- prev = str[i - 1];
-
- if (0 == isdigit(prev) && '.' != prev)
- {
- for (j = i; '\0' != str[j]; j++)
- str[j] = str[j + 1];
- }
- }
- }
- else if ('-' == cur)
- {
- /* minus is the first sign in the expression */
- if (0 == i)
- {
- str[i] = 'N';
- }
- else
- {
- prev = str[i - 1];
-
- if (0 == isdigit(prev) && '.' != prev)
- {
- str[i] = 'N';
- }
- else
- {
- len = (int)strlen(str);
-
- for (j = len; j > i; j--)
- str[j] = str[j - 1];
-
- str[i] = '+';
- str[i + 1] = 'N';
- str[len + 1] = '\0';
- i++;
- }
- }
- }
- }
-}
-
-/*
- * Function: strlcpy, strlcat
- * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-/******************************************************************************
- * *
* Function: zbx_strlcpy *
* *
- * Purpose: replacement of insecure strncpy, same as OpenBSD's strlcpy *
+ * Purpose: Copy src to string dst of size siz. At most siz - 1 characters *
+ * will be copied. Always null terminates (unless siz == 0). *
* *
- * Copy src to string dst of size siz. At most siz-1 characters *
- * will be copied. Always NUL terminates (unless siz == 0). *
- * Returns strlen(src); if retval >= siz, truncation occurred. *
- * *
- * Author: Todd C. Miller <Todd.Miller@courtesan.com> *
+ * Return value : the number of characters copied (excluding the null byte) *
* *
******************************************************************************/
size_t zbx_strlcpy(char *dst, const char *src, size_t siz)
{
- char *d = dst;
const char *s = src;
- size_t n = siz;
- /* copy as many bytes as will fit */
- if (0 != n)
+ if (0 != siz)
{
- while (0 != --n)
- {
- if ('\0' == (*d++ = *s++))
- break;
- }
- }
+ while (0 != --siz && '\0' != *s)
+ *dst++ = *s++;
- /* not enough room in dst, add NUL and traverse rest of src */
- if (0 == n)
- {
- if (0 != siz)
- *d = '\0'; /* NUL-terminate dst */
- while ('\0' != *s++)
- ;
+ *dst = '\0';
}
- return s - src - 1; /* count does not include NUL */
+ return s - src; /* count does not include null */
}
/******************************************************************************
* *
* Function: zbx_strlcat *
* *
- * Purpose: replacement of insecure strncat, same as OpenBSD's strlcat *
- * *
- * Appends src to string dst of size siz (unlike strncat, size is the *
- * full size of dst, not space left). At most siz-1 characters *
- * will be copied. Always NUL terminates (unless siz <= strlen(dst)). *
- * Returns strlen(src) + MIN(siz, strlen(initial dst)). *
- * If retval >= siz, truncation occurred. *
- * *
- * Author: Todd C. Miller <Todd.Miller@courtesan.com> *
+ * Purpose: Appends src to string dst of size siz (unlike strncat, size is *
+ * the full size of dst, not space left). At most siz - 1 characters *
+ * will be copied. Always null terminates (unless *
+ * siz <= strlen(dst)). *
* *
******************************************************************************/
-size_t zbx_strlcat(char *dst, const char *src, size_t siz)
+void zbx_strlcat(char *dst, const char *src, size_t siz)
{
- char *d = dst;
- const char *s = src;
- size_t n = siz;
- size_t dlen;
-
- /* find the end of dst and adjust bytes left but don't go past end */
- while (0 != n-- && '\0' != *d)
- d++;
- dlen = d - dst;
- n = siz - dlen;
-
- if (0 == n)
- return dlen + strlen(s);
-
- while ('\0' != *s)
+ while ('\0' != *dst)
{
- if (1 != n)
- {
- *d++ = *s;
- n--;
- }
- s++;
+ dst++;
+ siz--;
}
- *d = '\0';
-
- return dlen + (s - src); /* count does not include NUL */
+ zbx_strlcpy(dst, src, siz);
}
/******************************************************************************
@@ -834,6 +672,7 @@ char *__zbx_zbx_strdcatf(char *dest, const char *f, ...)
* Purpose: check a byte stream for a valid hostname *
* *
* Parameters: hostname - pointer to the first char of hostname *
+ * error - pointer to the error message (can be NULL) *
* *
* Return value: return SUCCEED if hostname is valid *
* or FAIL if hostname contains invalid chars, is empty *
@@ -842,18 +681,35 @@ char *__zbx_zbx_strdcatf(char *dest, const char *f, ...)
* Author: Alexander Vladishev *
* *
******************************************************************************/
-int zbx_check_hostname(const char *hostname)
+int zbx_check_hostname(const char *hostname, char **error)
{
int len = 0;
while ('\0' != hostname[len])
{
- if (FAIL == is_hostname_char(hostname[len++]))
+ if (FAIL == is_hostname_char(hostname[len]))
+ {
+ if (NULL != error)
+ *error = zbx_dsprintf(NULL, "name contains invalid character '%c'", hostname[len]);
return FAIL;
+ }
+
+ len++;
}
- if (0 == len || MAX_ZBX_HOSTNAME_LEN < len)
+ if (0 == len)
+ {
+ if (NULL != error)
+ *error = zbx_strdup(NULL, "name is empty");
return FAIL;
+ }
+
+ if (MAX_ZBX_HOSTNAME_LEN < len)
+ {
+ if (NULL != error)
+ *error = zbx_dsprintf(NULL, "name is too long (max %d characters)", MAX_ZBX_HOSTNAME_LEN);
+ return FAIL;
+ }
return SUCCEED;
}
@@ -1890,101 +1746,6 @@ void remove_param(char *p, int num)
/******************************************************************************
* *
- * Function: get_string *
- * *
- * Purpose: get current string from the quoted or unquoted string list, *
- * delimited by blanks *
- * *
- * Parameters: *
- * p - [IN] parameter list, delimited by blanks (' ' or '\t') *
- * buf - [OUT] output buffer *
- * bufsize - [IN] output buffer size *
- * *
- * Return value: pointer to the next string *
- * *
- * Author: Alexander Vladishev *
- * *
- * Comments: delimeter for parameters is ',' *
- * *
- ******************************************************************************/
-const char *get_string(const char *p, char *buf, size_t bufsize)
-{
-/* 0 - init, 1 - inside quoted param, 2 - inside unquoted param */
- int state;
- size_t buf_i = 0;
-
- bufsize--; /* '\0' */
-
- for (state = 0; '\0' != *p; p++)
- {
- switch (state) {
- /* Init state */
- case 0:
- if (' ' == *p || '\t' == *p)
- /* Skip of leading spaces */;
- else if ('"' == *p)
- state = 1;
- else
- {
- state = 2;
- p--;
- }
- break;
- /* Quoted */
- case 1:
- if ('"' == *p)
- {
- if (' ' != p[1] && '\t' != p[1] && '\0' != p[1])
- return NULL; /* incorrect syntax */
-
- while (' ' == p[1] || '\t' == p[1])
- p++;
-
- buf[buf_i] = '\0';
- return ++p;
- }
- else if ('\\' == *p && ('"' == p[1] || '\\' == p[1]))
- {
- p++;
- if (buf_i < bufsize)
- buf[buf_i++] = *p;
- }
- else if ('\\' == *p && 'n' == p[1])
- {
- p++;
- if (buf_i < bufsize)
- buf[buf_i++] = '\n';
- }
- else if (buf_i < bufsize)
- buf[buf_i++] = *p;
- break;
- /* Unquoted */
- case 2:
- if (' ' == *p || '\t' == *p)
- {
- while (' ' == *p || '\t' == *p)
- p++;
-
- buf[buf_i] = '\0';
- return p;
- }
- else if (buf_i < bufsize)
- buf[buf_i++] = *p;
- break;
- }
- }
-
- /* missing terminating '"' character */
- if (state == 1)
- return NULL;
-
- buf[buf_i] = '\0';
-
- return p;
-}
-
-/******************************************************************************
- * *
* Function: zbx_num2hex *
* *
* Purpose: convert parameter c (0-15) to hexadecimal value ('0'-'f') *
@@ -2780,13 +2541,13 @@ static int get_codepage(const char *encoding, unsigned int *codepage)
}
/* convert from selected code page to unicode */
-static LPTSTR zbx_to_unicode(unsigned int codepage, LPCSTR cp_string)
+static wchar_t *zbx_to_unicode(unsigned int codepage, const char *cp_string)
{
- LPTSTR wide_string = NULL;
+ wchar_t *wide_string = NULL;
int wide_size;
wide_size = MultiByteToWideChar(codepage, 0, cp_string, -1, NULL, 0);
- wide_string = (LPTSTR)zbx_malloc(wide_string, (size_t)wide_size * sizeof(TCHAR));
+ wide_string = (wchar_t *)zbx_malloc(wide_string, (size_t)wide_size * sizeof(wchar_t));
/* convert from cp_string to wide_string */
MultiByteToWideChar(codepage, 0, cp_string, -1, wide_string, wide_size);
@@ -2795,18 +2556,18 @@ static LPTSTR zbx_to_unicode(unsigned int codepage, LPCSTR cp_string)
}
/* convert from Windows ANSI code page to unicode */
-LPTSTR zbx_acp_to_unicode(LPCSTR acp_string)
+wchar_t *zbx_acp_to_unicode(const char *acp_string)
{
return zbx_to_unicode(CP_ACP, acp_string);
}
/* convert from Windows OEM code page to unicode */
-LPTSTR zbx_oemcp_to_unicode(LPCSTR oemcp_string)
+wchar_t *zbx_oemcp_to_unicode(const char *oemcp_string)
{
return zbx_to_unicode(CP_OEMCP, oemcp_string);
}
-int zbx_acp_to_unicode_static(LPCSTR acp_string, LPTSTR wide_string, int wide_size)
+int zbx_acp_to_unicode_static(const char *acp_string, wchar_t *wide_string, int wide_size)
{
/* convert from acp_string to wide_string */
if (0 == MultiByteToWideChar(CP_ACP, 0, acp_string, -1, wide_string, wide_size))
@@ -2816,19 +2577,19 @@ int zbx_acp_to_unicode_static(LPCSTR acp_string, LPTSTR wide_string, int wide_si
}
/* convert from UTF-8 to unicode */
-LPTSTR zbx_utf8_to_unicode(LPCSTR utf8_string)
+wchar_t *zbx_utf8_to_unicode(const char *utf8_string)
{
return zbx_to_unicode(CP_UTF8, utf8_string);
}
/* convert from unicode to utf8 */
-LPSTR zbx_unicode_to_utf8(LPCTSTR wide_string)
+char *zbx_unicode_to_utf8(const wchar_t *wide_string)
{
- LPSTR utf8_string = NULL;
+ char *utf8_string = NULL;
int utf8_size;
utf8_size = WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, NULL, 0, NULL, NULL);
- utf8_string = (LPSTR)zbx_malloc(utf8_string, (size_t)utf8_size);
+ utf8_string = (char *)zbx_malloc(utf8_string, (size_t)utf8_size);
/* convert from wide_string to utf8_string */
WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, utf8_string, utf8_size, NULL, NULL);
@@ -2837,7 +2598,7 @@ LPSTR zbx_unicode_to_utf8(LPCTSTR wide_string)
}
/* convert from unicode to utf8 */
-LPSTR zbx_unicode_to_utf8_static(LPCTSTR wide_string, LPSTR utf8_string, int utf8_size)
+char *zbx_unicode_to_utf8_static(const wchar_t *wide_string, char *utf8_string, int utf8_size)
{
/* convert from wide_string to utf8_string */
if (0 == WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, utf8_string, utf8_size, NULL, NULL))
@@ -2864,7 +2625,7 @@ void zbx_strupper(char *str)
char *convert_to_utf8(char *in, size_t in_size, const char *encoding)
{
#define STATIC_SIZE 1024
- wchar_t wide_string_static[STATIC_SIZE], *wide_string = NULL;
+ wchar_t wide_string_static[STATIC_SIZE], *wide_string = NULL;
int wide_size;
char *utf8_string = NULL;
int utf8_size;
@@ -2885,7 +2646,7 @@ char *convert_to_utf8(char *in, size_t in_size, const char *encoding)
{
wide_size = MultiByteToWideChar(codepage, 0, in, (int)in_size, NULL, 0);
if (wide_size > STATIC_SIZE)
- wide_string = (LPTSTR)zbx_malloc(wide_string, (size_t)wide_size * sizeof(TCHAR));
+ wide_string = (wchar_t *)zbx_malloc(wide_string, (size_t)wide_size * sizeof(wchar_t));
else
wide_string = wide_string_static;
@@ -2899,7 +2660,7 @@ char *convert_to_utf8(char *in, size_t in_size, const char *encoding)
}
utf8_size = WideCharToMultiByte(CP_UTF8, 0, wide_string, wide_size, NULL, 0, NULL, NULL);
- utf8_string = (LPSTR)zbx_malloc(utf8_string, (size_t)utf8_size + 1/* '\0' */);
+ utf8_string = (char *)zbx_malloc(utf8_string, (size_t)utf8_size + 1/* '\0' */);
/* convert from wide_string to utf8_string */
WideCharToMultiByte(CP_UTF8, 0, wide_string, wide_size, utf8_string, utf8_size, NULL, NULL);