var: add a '64dec' function that can base64 decode a string

Verified in test 455 and 487.

If the provided string cannot be base64-decoded, it will instead use
"[64dec-fail]" (without the quotes).

Documented

Ref: #16288
Closes #16330
This commit is contained in:
Daniel Stenberg 2025-02-22 10:49:19 +01:00
parent 54c07fd6c5
commit a6e8ead533
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
6 changed files with 101 additions and 11 deletions

View file

@ -91,6 +91,8 @@ static const struct tool_var *varcontent(struct GlobalConfig *global,
#define FUNC_URL_LEN (sizeof(FUNC_URL) - 1)
#define FUNC_B64 "b64"
#define FUNC_B64_LEN (sizeof(FUNC_B64) - 1)
#define FUNC_64DEC "64dec" /* base64 decode */
#define FUNC_64DEC_LEN (sizeof(FUNC_64DEC) - 1)
static ParameterError varfunc(struct GlobalConfig *global,
char *c, /* content */
@ -179,6 +181,27 @@ static ParameterError varfunc(struct GlobalConfig *global,
break;
}
}
else if(FUNCMATCH(f, FUNC_64DEC, FUNC_64DEC_LEN)) {
f += FUNC_64DEC_LEN;
curlx_dyn_reset(out);
if(clen) {
unsigned char *enc;
size_t elen;
CURLcode result = curlx_base64_decode(c, &enc, &elen);
/* put it in the output */
if(result) {
if(curlx_dyn_add(out, "[64dec-fail]"))
err = PARAM_NO_MEM;
}
else {
if(curlx_dyn_addn(out, enc, elen))
err = PARAM_NO_MEM;
curl_free(enc);
}
if(err)
break;
}
}
else {
/* unsupported function */
errorf(global, "unknown variable function in '%.*s'",