Source
static int smtp_parse_mailbox(const char *mailbox, char *error, size_t max_error_len, zbx_vector_ptr_t *mailaddrs)
/*
** 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.
**/
/* number of characters per line when wrapping Base64 data in Email */
/* number of characters per "encoded-word" in RFC-2047 message header */
/* multiple 'encoded-word's should be separated by <CR><LF><SPACE> */
/* separator for multipart mixed messages */
extern char *CONFIG_SSL_CA_LOCATION;
/* SMTP security options */
/******************************************************************************
* *
* Purpose: Encode a string into a base64 string as required by rfc2047. *
* Used for encoding e-mail headers. *
* *
* Parameters: src - [IN] a null-terminated UTF-8 string to encode *
* p_base64 - [OUT] a pointer to the encoded string *
* *
* Comments: Based on the patch submitted by *
* Jairo Eduardo Lopez Fuentes Nacarino *
* *
******************************************************************************/
static void str_base64_encode_rfc2047(const char *src, char **p_base64)
{
const char *p0; /* pointer in src to start encoding from */
const char *p1; /* pointer in src: 1st byte of UTF-8 character */
size_t c_len; /* length of UTF-8 character sequence */
size_t p_base64_alloc; /* allocated memory size for subject */
size_t p_base64_offset = 0; /* offset for writing into subject */
assert(src);
assert(NULL == *p_base64); /* do not accept already allocated memory */
p_base64_alloc = ZBX_EMAIL_B64_MAXWORD_RFC2047 + sizeof(ZBX_EMAIL_ENCODED_WORD_SEPARATOR);
*p_base64 = (char *)zbx_malloc(NULL, p_base64_alloc);
**p_base64 = '\0';
for (p0 = src; '\0' != *p0; p0 = p1)
{
/* Max length of line is 76 characters (without line separator). */
/* Max length of "encoded-word" is 75 characters (without word separator). */
/* 3 characters are taken by word separator "<CR><LF><Space>" which also includes the line separator. */
/* 12 characters are taken by header "=?UTF-8?B?" and trailer "?=". */
/* So, one "encoded-word" can hold up to 63 characters of Base64-encoded string. */
/* Encoding 45 bytes produces a 61 byte long Base64-encoded string which meets the limit. */
/* Encoding 46 bytes produces a 65 byte long Base64-encoded string which exceeds the limit. */
for (p1 = p0; '\0' != *p1; p1 += c_len)
{
/* an invalid UTF-8 character or length of a string more than 45 bytes */
if (0 == (c_len = zbx_utf8_char_len(p1)) || 45 < p1 - p0 + c_len)
break;
}