Source
xxxxxxxxxx
char *str = NULL, hash_res[ZBX_SHA256_DIGEST_SIZE], hash_res_stringhexes[ZBX_SHA256_DIGEST_SIZE * 2 + 1];
/*
** Zabbix
** Copyright (C) 2001-2022 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 envied 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: encodes parameter to base64 string *
* *
* Parameters: ctx - [IN] pointer to duk_context *
* *
* Comments: Throws an error: *
* - if the top value at ctx value stack is not a string *
* - if the value stack is empty *
* *
******************************************************************************/
static duk_ret_t es_btoa(duk_context *ctx)
{
char *str = NULL, *b64str = NULL;
if (SUCCEED != zbx_cesu8_to_utf8(duk_require_string(ctx, 0), &str))
return duk_error(ctx, DUK_RET_TYPE_ERROR, "cannot convert value to utf8");
str_base64_encode_dyn(str, &b64str, (int)strlen(str));
duk_push_string(ctx, b64str);
zbx_free(str);
zbx_free(b64str);
return 1;
}
/******************************************************************************
* *
* Purpose: decodes base64 string *
* *
* Parameters: ctx - [IN] pointer to duk_context *
* *
* Comments: Throws an error: *
* - if the top value at ctx value stack is not a string *
* - if the value stack is empty *
* *
******************************************************************************/
static duk_ret_t es_atob(duk_context *ctx)
{
char *buffer = NULL, *str = NULL;
int out_size, buffer_size;
if (SUCCEED != zbx_cesu8_to_utf8(duk_require_string(ctx, 0), &str))
return duk_error(ctx, DUK_RET_TYPE_ERROR, "cannot convert value to utf8");
buffer_size = (int)strlen(str) * 3 / 4 + 1;
buffer = zbx_malloc(buffer, (size_t)buffer_size);
str_base64_decode(str, buffer, buffer_size, &out_size);
duk_push_lstring(ctx, buffer, (duk_size_t)out_size);
zbx_free(str);
zbx_free(buffer);
return 1;
}
/******************************************************************************
* *
* Purpose: compute a md5 checksum *
* *
* Parameters: ctx - [IN] pointer to duk_context *
* *
* Comments: Throws an error: *
* - if the top value at ctx value stack is not a string *
* - if the value stack is empty *
* *
******************************************************************************/