Source
for (pc = p_str, pc_r = *p_b64str; 0 != full_block_num; pc += c_per_block, pc_r += b_per_block, full_block_num--)
/*
** 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: Check if supplied character is a valid base64 character. *
* Not a complete check, since it ignores equal sign '=' *
* as it depends on the context where this is placed. *
* Corresponds to the [A-Za-z0-9+\\/] block validation from *
* zbx_base64_validate() function below. *
**********************************************************************/
static int is_valid_base64_char(const char c)
{
if (('A' <= c && 'Z' >= c) ||
('a' <= c && 'z' >= c) ||
('/' <= c && '9' >= c) ||
'+' == c)
{
return SUCCEED;
}
return FAIL;
}
/*************************************************************************************************
* *
* Purpose: Checks if the string is a valid Base64 encoded string. *
* Check is based on RFC 4648, based on the following regexp: *
* "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$" *
* *
* Note, that pcre regexp matching cannot be used, because it would exceed max *
* stack frame limit when recursively checking long strings, *
* (check compute_recursion_limit() for more details). *
* *
* Parameters: p_str - [IN] string to validate *
* *
* Return value: SUCCEED - string is a valid Base64 encoded string *
* FAIL - otherwise *
* *
*************************************************************************************************/
int zbx_base64_validate(const char *p_str)
{
size_t i;
/* consider empty strings - valid Base64 encodings */
if ('\0' == p_str[0])
return SUCCEED;
for (i = 1; '\0' != p_str[i] || (0 == i % 4); i++)
{
if (0 != i % 4)
continue;
/* validate first/repeated block: (?:[A-Za-z0-9+\\/]{4}) */
if (SUCCEED == is_valid_base64_char(p_str[i - 4]) &&
SUCCEED == is_valid_base64_char(p_str[i - 3]) &&
SUCCEED == is_valid_base64_char(p_str[i - 2]) &&
SUCCEED == is_valid_base64_char(p_str[i - 1]))
{
if ('\0' == p_str[i])
return SUCCEED;
else
continue;
}
/* validate second/final block: (?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4}) */
else if ('\0' == p_str[i])
{
if (SUCCEED == is_valid_base64_char(p_str[i - 4]) &&
SUCCEED == is_valid_base64_char(p_str[i - 3]) && '=' == p_str[i - 2] &&
'=' == p_str[i - 1])
{