bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen/* Zero-width space (​) apparently also belongs here, but that gets a
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen bit tricky to handle.. is it actually used anywhere? */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* regular text */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* tag outside "quoted string" */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* tag inside "double quoted string" */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* tag -> "escape\ */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* tag inside 'single quoted string' */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* tag -> 'escape\ */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* comment */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* comment is ending, we've seen "--" and now just waiting for ">" */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* (java)script */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* CSS style */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* <![CDATA[...]]> */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainenstatic struct {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainenmail_html2text_init(enum mail_html2text_flags flags)
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen ht->input = buffer_create_dynamic(default_pool, 512);
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (size >= 3 && memcmp(data, "!--", 3) == 0) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (size >= 7 && i_memcasecmp(data, "script", 6) == 0 &&
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen (HTML_WHITESPACE(data[6]) || data[6] == '>')) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (size >= 6 && i_memcasecmp(data, "style", 5) == 0 &&
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen (HTML_WHITESPACE(data[5]) || data[5] == '>')) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (size >= 8 && i_memcasecmp(data, "![CDATA[", 8) == 0) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if ((ht->flags & MAIL_HTML2TEXT_FLAG_SKIP_QUOTED) != 0) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (size >= 10 && i_memcasecmp(data, "blockquote", 10) == 0 &&
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen (HTML_WHITESPACE(data[10]) || data[10] == '>')) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen size >= 12 && i_memcasecmp(data, "/blockquote>", 12) == 0) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* can we see the whole tag name? */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen for (i = 0; i < size; i++) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (HTML_WHITESPACE(data[i]) || data[i] == '>')
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* need more data */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainenstatic bool html_entity_get_unichar(const char *name, unichar_t *chr_r)
eb0cfb4f325faba850d85ee96ddec5ac6deedf89Aki Tuomi for (size_t i = 0; i < N_ELEMENTS(html_entities); i++) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (strcasecmp(html_entities[i].name, name) == 0) {
eb0cfb4f325faba850d85ee96ddec5ac6deedf89Aki Tuomi /* maybe it's just encoded binary byte
eb0cfb4f325faba850d85ee96ddec5ac6deedf89Aki Tuomi it can be &#nnn; or &#xnnn;
c9141125278100269eb3a907c911afe78c46717cTimo Sirainenstatic size_t parse_entity(const unsigned char *data, size_t size,
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen for (i = 0; i < size; i++) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (HTML_WHITESPACE(data[i]) || i >= sizeof(entity)) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* broken entity */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainenstatic void mail_html2text_add_space(buffer_t *output)
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (output->used > 0 && data[output->used-1] != ' ')
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen const unsigned char *data, size_t size, buffer_t *output)
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen for (i = 0; i < size; i++) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (c == '<') {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen } else if (c == '&') {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen ret = parse_entity(data+i+1, size-i-1, output);
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (c == '"')
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen else if (c == '\'')
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen else if (c == '>') {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (c == '"')
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen else if (c == '\\')
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (c == '\'')
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen else if (c == '\\')
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (c == '-') {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (c == '>')
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (c == '<') {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (i_memcasecmp(data+i, "</script>", max_len) == 0) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (c == '<') {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (i_memcasecmp(data+i, "</style>", max_len) == 0) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (c == ']') {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen if (i_memcasecmp(data+i, "]]>", max_len) == 0) {
c9141125278100269eb3a907c911afe78c46717cTimo Sirainenvoid mail_html2text_more(struct mail_html2text *ht,
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* we didn't get enough input the last time to know
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen what to do. */
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen /* we need to add more data into buffer */
e61d5a4bad247e62a257bb1ed6c483923d10c2a8Timo Sirainen /* we parsed forward */
e61d5a4bad247e62a257bb1ed6c483923d10c2a8Timo Sirainen /* invalid input - eat away what we parsed so far
e61d5a4bad247e62a257bb1ed6c483923d10c2a8Timo Sirainen buffer_set_used_size(ht->input, buf_orig_size);
c9141125278100269eb3a907c911afe78c46717cTimo Sirainen buffer_append(ht->input, data + pos, size - pos);