static char* comms_get_xml_b64_value (const char* tag, const char* val) { char data_b64[ZBX_MAX_B64_LEN]; data_b64[0] = '\0'; str_base64_encode(val, data_b64, (int)strlen(val)); return zbx_strdcatf(NULL, "<%s>%s</%s>", tag, data_b64, tag); }
/****************************************************************************** * * * Function: str_base64_encode_rfc2047 * * * * 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 = 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, c_len = 0; '\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; } if (0 < p1 - p0) { /* 12 characters are taken by header "=?UTF-8?B?" and trailer "?=" plus '\0' */ char b64_buf[ZBX_EMAIL_B64_MAXWORD_RFC2047 - 12 + 1]; str_base64_encode(p0, b64_buf, p1 - p0); if (0 != p_base64_offset) /* not the first "encoded-word" ? */ { zbx_strcpy_alloc(p_base64, &p_base64_alloc, &p_base64_offset, ZBX_EMAIL_ENCODED_WORD_SEPARATOR); } zbx_snprintf_alloc(p_base64, &p_base64_alloc, &p_base64_offset, "=?UTF-8?B?%s?=", b64_buf); } else break; } }