示例#1
0
static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
			 char *buf, struct ast_str **str, ssize_t len)
{
	if (ast_strlen_zero(data)) {
		ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
		return -1;
	}

	if (cmd[7] == 'E') {
		if (buf) {
			ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
		} else {
			if (len >= 0) {
				ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
			}
			ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
			ast_str_update(*str);
		}
	} else {
		int decoded_len;
		if (buf) {
			decoded_len = ast_base64decode((unsigned char *) buf, data, len);
			/* add a terminating null at the end of buf, or at the
			 * end of our decoded string, which ever is less */
			buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
		} else {
			if (len >= 0) {
				ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
			}
			decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
			if (len)
				/* add a terminating null at the end of our
				 * buffer, or at the end of our decoded string,
				 * which ever is less */
				ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
			else
				/* space for the null is allocated above */
				ast_str_buffer(*str)[decoded_len] = '\0';

			ast_str_update(*str);
		}
	}

	return 0;
}
static int blacklist_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
{
	/* 2 bytes is a single integer, plus terminating null */
	if (ast_str_size(*str) - ast_str_strlen(*str) < 2) {
		if (len > ast_str_size(*str) || len == 0) {
			ast_str_make_space(str, len ? len : ast_str_strlen(*str) + 2);
		}
	}
	if (ast_str_size(*str) - ast_str_strlen(*str) >= 2) {
		int res = blacklist_read(chan, cmd, data, ast_str_buffer(*str) + ast_str_strlen(*str), 2);
		ast_str_update(*str);
		return res;
	}
	return -1;
}
static void xpidf_to_string(void *body, struct ast_str **str)
{
	pjxpidf_pres *pres = body;
	int growths = 0;
	int size;

	do {
		size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
		if (size <= AST_PJSIP_XML_PROLOG_LEN) {
			ast_str_make_space(str, ast_str_size(*str) * 2);
			++growths;
		}
	} while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
	if (size <= AST_PJSIP_XML_PROLOG_LEN) {
		ast_log(LOG_WARNING, "XPIDF body text too large\n");
		return;
	}

	*(ast_str_buffer(*str) + size) = '\0';
	ast_str_update(*str);
}
static void dialog_info_to_string(void *body, struct ast_str **str)
{
	pj_xml_node *dialog_info = body;
	int growths = 0;
	int size;

	do {
		size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str) - 1, PJ_TRUE);
		if (size <= AST_PJSIP_XML_PROLOG_LEN) {
			ast_str_make_space(str, ast_str_size(*str) * 2);
			++growths;
		}
	} while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
	if (size <= AST_PJSIP_XML_PROLOG_LEN) {
		ast_log(LOG_WARNING, "dialog-info+xml body text too large\n");
		return;
	}

	*(ast_str_buffer(*str) + size) = '\0';
	ast_str_update(*str);
}