1
/* Copyright (c) 2002-2009 Dovecot Sieve authors, see the included COPYING file
6
#include "managesieve-parser.h"
7
#include "managesieve-quote.h"
9
/* Turn the value string into a valid MANAGESIEVE string or literal, no matter
10
* what. QUOTED-SPECIALS are escaped, but any invalid (UTF-8) character
11
* is simply removed. Linebreak characters are not considered invalid, but
12
* they do force the generation of a string literal.
14
void managesieve_quote_append(string_t *str, const unsigned char *value,
15
size_t value_len, bool compress_lwsp)
26
str_append(str, "\"\"");
30
if (value_len == (size_t)-1)
31
value_len = strlen((const char *) value);
33
for (i = 0; i < value_len; i++) {
37
if (last_lwsp && compress_lwsp) {
54
/* Enforce valid UTF-8
56
if ( (utf8_len=UTF8_LEN(value[i])) == 0 ) {
65
if ( (i+utf8_len-1) >= value_len ) {
66
/* Value ends in the middle of a UTF-8 character;
67
* Kill the partial UTF-8 character
69
extra += i + utf8_len - value_len;
74
/* Parse the series of UTF8_1 characters */
75
for (i++; c > 0; c--, i++ ) {
76
if (!IS_UTF8_1(value[i])) {
77
extra += utf8_len - c;
89
/* no linebreak chars, return as (escaped) "string" */
90
str_append_c(str, '"');
92
/* return as literal */
93
str_printfa(str, "{%"PRIuSIZE_T"}\r\n", value_len - extra);
96
if (!modify && (literal || !escape))
97
str_append_n(str, value, value_len);
100
for (i = 0; i < value_len; i++) {
106
str_append_c(str, '\\');
107
str_append_c(str, value[i]);
111
if (!last_lwsp || !compress_lwsp)
112
str_append_c(str, ' ');
118
str_append_c(str, value[i]);
121
/* Enforce valid UTF-8
123
if ( (utf8_len=UTF8_LEN(value[i])) == 0 )
126
if ( utf8_len > 1 ) {
127
int c = utf8_len - 1;
130
if ( (i+utf8_len-1) >= value_len ) {
131
/* Value ends in the middle of a UTF-8 character;
132
* Kill the partial character
138
/* Parse the series of UTF8_1 characters */
139
for (j = i+1; c > 0; c--, j++ ) {
140
if (!IS_UTF8_1(value[j])) {
141
/* Skip until after this erroneous character */
147
/* Append the UTF-8 character. Last octet is done later */
149
for (; c > 0; c--, i++ )
150
str_append_c(str, value[i]);
154
str_append_c(str, value[i]);
161
str_append_c(str, '"');
164
char *managesieve_quote(pool_t pool, const unsigned char *value, size_t value_len)
173
str = t_str_new(value_len + MAX_INT_STRLEN + 5);
174
managesieve_quote_append(str, value, value_len, TRUE);
175
ret = p_strndup(pool, str_data(str), str_len(str));