Source
char *zbx_find_buf_newline(char *p, char **p_next, const char *p_end, const char *cr, const char *lf, size_t szbyte)
/*
** 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/>.
**/
void zbx_find_cr_lf_szbyte(const char *encoding, const char **cr, const char **lf, size_t *szbyte)
{
/* default is single-byte character set */
*cr = "\r";
*lf = "\n";
*szbyte = 1;
if ('\0' != *encoding)
{
if (0 == strcasecmp(encoding, "UNICODE") || 0 == strcasecmp(encoding, "UNICODELITTLE") ||
0 == strcasecmp(encoding, "UTF-16") || 0 == strcasecmp(encoding, "UTF-16LE") ||
0 == strcasecmp(encoding, "UTF16") || 0 == strcasecmp(encoding, "UTF16LE") ||
0 == strcasecmp(encoding, "UCS-2") || 0 == strcasecmp(encoding, "UCS-2LE"))
{
*cr = "\r\0";
*lf = "\n\0";
*szbyte = 2;
}
else if (0 == strcasecmp(encoding, "UNICODEBIG") || 0 == strcasecmp(encoding, "UNICODEFFFE") ||
0 == strcasecmp(encoding, "UTF-16BE") || 0 == strcasecmp(encoding, "UTF16BE") ||
0 == strcasecmp(encoding, "UCS-2BE"))
{
*cr = "\0\r";
*lf = "\0\n";
*szbyte = 2;
}
else if (0 == strcasecmp(encoding, "UTF-32") || 0 == strcasecmp(encoding, "UTF-32LE") ||
0 == strcasecmp(encoding, "UTF32") || 0 == strcasecmp(encoding, "UTF32LE"))
{
*cr = "\r\0\0\0";
*lf = "\n\0\0\0";
*szbyte = 4;
}
else if (0 == strcasecmp(encoding, "UTF-32BE") || 0 == strcasecmp(encoding, "UTF32BE"))
{
*cr = "\0\0\0\r";
*lf = "\0\0\0\n";
*szbyte = 4;
}
}
}
/******************************************************************************
* *
* Purpose: Read one text line from a file descriptor into buffer *
* *
* Parameters: fd - [IN] file descriptor to read from *
* buf - [OUT] buffer to read into *
* count - [IN] buffer size in bytes *
* encoding - [IN] pointer to a text string describing encoding. *
* See function zbx_find_cr_lf_szbyte() for supported *
* encodings. *
* "" (empty string) means a single-byte character set.*
* *
* Return value: On success, the number of bytes read is returned (0 (zero) *
* indicates end of file). *
* On error, -1 (ZBX_READ_ERR) is returned and errno is set *
* appropriately. *
* If the wrong decoding is detected, -2 *
* (ZBX_READ_WRONG_ENCODING) is returned. *
* *
* Comments: Reading stops after a newline. If the newline is read, it is *
* stored into the buffer. *
* *
* Note: This function is left for testing purposes. *
* *
******************************************************************************/
int zbx_read_text_line_from_file(int fd, char *buf, size_t count, const char *encoding)
{
size_t i, szbyte;
ssize_t nbytes;
const char *cr, *lf;
zbx_offset_t offset;
if ((zbx_offset_t)-1 == (offset = zbx_lseek(fd, 0, SEEK_CUR)))