Example #1
0
static void test_mail_html2text(void)
{
	string_t *str = t_str_new(128);
	struct mail_html2text *ht;
	unsigned int i, j;

	test_begin("mail_html2text()");
	for (i = 0; i < N_ELEMENTS(tests); i++) {
		ht = mail_html2text_init(MAIL_HTML2TEXT_FLAG_SKIP_QUOTED);
		for (j = 0; tests[i].input[j] != '\0'; j++) {
			unsigned char c = tests[i].input[j];
			mail_html2text_more(ht, &c, 1, str);
		}
		test_assert_idx(strcmp(str_c(str), tests[i].output) == 0, i);
		mail_html2text_deinit(&ht);
		str_truncate(str, 0);
	}

	/* test without skipping quoted */
	ht = mail_html2text_init(0);
	mail_html2text_more(ht, (const void *)test_blockquote_input,
			    strlen(test_blockquote_input), str);
	test_assert(strcmp(str_c(str), test_blockquote_output) == 0);
	mail_html2text_deinit(&ht);

	test_end();
}
Example #2
0
static void test_mail_html2text_random(void)
{
    string_t *str = t_str_new(128);
    struct mail_html2text *ht;

    test_begin("mail_html2text() random");
    for (unsigned int i = 0; i < 1000; i++) {
        char valid_chars[] = { '0', 'a', '<', '>', '&', ';', '\\', '\'', '"', '/' };
        unsigned char s[2];

        ht = mail_html2text_init(0);
        for (unsigned int i = 0; i < 100; i++) {
            s[0] = valid_chars[rand() % N_ELEMENTS(valid_chars)];
            s[1] = valid_chars[rand() % N_ELEMENTS(valid_chars)];
            mail_html2text_more(ht, s, rand()%2+1, str);
        }
        mail_html2text_deinit(&ht);
        str_truncate(str, 0);
    }
    test_end();
}
Example #3
0
static bool snippet_generate(struct snippet_context *ctx,
			     const unsigned char *data, size_t size)
{
	size_t i, count;

	if (ctx->html2text != NULL) {
		buffer_set_used_size(ctx->plain_output, 0);
		mail_html2text_more(ctx->html2text, data, size,
				    ctx->plain_output);
		data = ctx->plain_output->data;
		size = ctx->plain_output->used;
	}

	/* message-decoder should feed us only valid and complete
	   UTF-8 input */

	for (i = 0; i < size; i += count) {
		count = 1;
		switch (ctx->state) {
		case SNIPPET_STATE_NEWLINE:
			if (data[i] == '>' && ctx->html2text == NULL) {
				ctx->state = SNIPPET_STATE_QUOTED;
				break;
			}
			ctx->state = SNIPPET_STATE_NORMAL;
			/* fallthrough */
		case SNIPPET_STATE_NORMAL:
			if (size-i >= 3 &&
			     ((data[i] == 0xEF && data[i+1] == 0xBB && data[i+2] == 0xBF) ||
			      (data[i] == 0xBF && data[i+1] == 0xBB && data[i+2] == 0xEF))) {
				count += 2; /* because we skip +1 next */
				break;
			}
			if (data[i] == '\r' || data[i] == '\n' ||
			    data[i] == '\t' || data[i] == ' ') {
				/* skip any leading whitespace */
				if (str_len(ctx->snippet) > 1)
					ctx->add_whitespace = TRUE;
				if (data[i] == '\n')
					ctx->state = SNIPPET_STATE_NEWLINE;
				break;
			}
			if (ctx->add_whitespace) {
				str_append_c(ctx->snippet, ' ');
				ctx->add_whitespace = FALSE;
				if (ctx->chars_left-- == 0)
					return FALSE;
			}
			if (ctx->chars_left-- == 0)
				return FALSE;
			count = uni_utf8_char_bytes(data[i]);
			i_assert(i + count <= size);
			str_append_n(ctx->snippet, data + i, count);
			break;
		case SNIPPET_STATE_QUOTED:
			if (data[i] == '\n')
				ctx->state = SNIPPET_STATE_NEWLINE;
			break;
		}
	}
	return TRUE;
}