Mercurial > dovecot > core-2.2
annotate src/lib-imap/imap-quote.c @ 12636:fa4b84059ae2
IMAP LIST: Never return subscribed children state if RECURSIVEMATCH isn't specified.
Not even when backends give it automatically.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 02 Feb 2011 05:31:46 +0200 |
parents | 615eef3139c2 |
children | 447bce266022 |
rev | line source |
---|---|
10582
615eef3139c2
Updated copyright notices to include year 2010.
Timo Sirainen <tss@iki.fi>
parents:
9828
diff
changeset
|
1 /* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */ |
339
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 #include "lib.h" |
833
41ec8cadd238
Replaced TempString with a String which can use any memory pool and uses
Timo Sirainen <tss@iki.fi>
parents:
579
diff
changeset
|
4 #include "str.h" |
579
e524da896d92
Several minor fixes: signess, casting away const, missing static, etc.
Timo Sirainen <tss@iki.fi>
parents:
339
diff
changeset
|
5 #include "imap-quote.h" |
339
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 |
1107
4044c2903ed7
Don't do x-unknown mime encoding. Correct way is to just send them as
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
7 void imap_quote_append(string_t *str, const unsigned char *value, |
9827
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
8 size_t value_len, bool fix_text) |
339
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 { |
1558
fa3c261d3556
LWSP stripping with NUL/8bit chars set wrong literal size which broke things.
Timo Sirainen <tss@iki.fi>
parents:
1536
diff
changeset
|
10 size_t i, extra = 0; |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
11 bool last_lwsp = TRUE, literal = FALSE, modify = FALSE; |
339
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 |
1321 | 13 if (value == NULL) { |
14 str_append(str, "NIL"); | |
15 return; | |
16 } | |
17 | |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
18 if (value_len == (size_t)-1) |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
19 value_len = strlen((const char *) value); |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
20 |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
21 for (i = 0; i < value_len; i++) { |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
22 switch (value[i]) { |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
23 case 0: |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
24 /* it's converted to 8bit char */ |
1139
cfe8bafb61d3
Literals sizes were sometimes set to 2^32-1.
Timo Sirainen <tss@iki.fi>
parents:
1107
diff
changeset
|
25 literal = TRUE; |
9827
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
26 last_lwsp = FALSE; |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
27 modify = TRUE; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
28 break; |
7283
f70c4d501251
imap_quote*() with LWSP compression: Make sure TABs are always converted to
Timo Sirainen <tss@iki.fi>
parents:
7226
diff
changeset
|
29 case '\t': |
f70c4d501251
imap_quote*() with LWSP compression: Make sure TABs are always converted to
Timo Sirainen <tss@iki.fi>
parents:
7226
diff
changeset
|
30 modify = TRUE; |
f70c4d501251
imap_quote*() with LWSP compression: Make sure TABs are always converted to
Timo Sirainen <tss@iki.fi>
parents:
7226
diff
changeset
|
31 /* fall through */ |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
32 case ' ': |
9827
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
33 if (last_lwsp && fix_text) { |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
34 modify = TRUE; |
1558
fa3c261d3556
LWSP stripping with NUL/8bit chars set wrong literal size which broke things.
Timo Sirainen <tss@iki.fi>
parents:
1536
diff
changeset
|
35 extra++; |
fa3c261d3556
LWSP stripping with NUL/8bit chars set wrong literal size which broke things.
Timo Sirainen <tss@iki.fi>
parents:
1536
diff
changeset
|
36 } |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
37 last_lwsp = TRUE; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
38 break; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
39 case 13: |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
40 case 10: |
9828
0b8492a4f219
imap_quote*(): If fix_text=FALSE and input contains CRs or LFs, use literals.
Timo Sirainen <tss@iki.fi>
parents:
9827
diff
changeset
|
41 if (!fix_text) |
0b8492a4f219
imap_quote*(): If fix_text=FALSE and input contains CRs or LFs, use literals.
Timo Sirainen <tss@iki.fi>
parents:
9827
diff
changeset
|
42 literal = TRUE; |
1558
fa3c261d3556
LWSP stripping with NUL/8bit chars set wrong literal size which broke things.
Timo Sirainen <tss@iki.fi>
parents:
1536
diff
changeset
|
43 extra++; |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
44 modify = TRUE; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
45 break; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
46 default: |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
47 if ((value[i] & 0x80) != 0 || |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
48 value[i] == '"' || value[i] == '\\') |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
49 literal = TRUE; |
1558
fa3c261d3556
LWSP stripping with NUL/8bit chars set wrong literal size which broke things.
Timo Sirainen <tss@iki.fi>
parents:
1536
diff
changeset
|
50 last_lwsp = FALSE; |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
51 } |
339
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
52 } |
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
53 |
9827
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
54 if (!fix_text) { |
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
55 extra = 0; |
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
56 modify = FALSE; |
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
57 } |
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
58 |
1139
cfe8bafb61d3
Literals sizes were sometimes set to 2^32-1.
Timo Sirainen <tss@iki.fi>
parents:
1107
diff
changeset
|
59 if (!literal) { |
1267
8ceacaf13c9c
Use literals for strings containing '"' and '\' characters too. This wasn't
Timo Sirainen <tss@iki.fi>
parents:
1166
diff
changeset
|
60 /* no 8bit chars or imapspecials, return as "string" */ |
1107
4044c2903ed7
Don't do x-unknown mime encoding. Correct way is to just send them as
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
61 str_append_c(str, '"'); |
4044c2903ed7
Don't do x-unknown mime encoding. Correct way is to just send them as
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
62 } else { |
4044c2903ed7
Don't do x-unknown mime encoding. Correct way is to just send them as
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
63 /* return as literal */ |
1558
fa3c261d3556
LWSP stripping with NUL/8bit chars set wrong literal size which broke things.
Timo Sirainen <tss@iki.fi>
parents:
1536
diff
changeset
|
64 str_printfa(str, "{%"PRIuSIZE_T"}\r\n", value_len - extra); |
1166
fccf13a1c67a
Quoting didn't remove CR and LF characters, so it could have caused
Timo Sirainen <tss@iki.fi>
parents:
1139
diff
changeset
|
65 } |
fccf13a1c67a
Quoting didn't remove CR and LF characters, so it could have caused
Timo Sirainen <tss@iki.fi>
parents:
1139
diff
changeset
|
66 |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
67 if (!modify) |
1107
4044c2903ed7
Don't do x-unknown mime encoding. Correct way is to just send them as
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
68 str_append_n(str, value, value_len); |
1166
fccf13a1c67a
Quoting didn't remove CR and LF characters, so it could have caused
Timo Sirainen <tss@iki.fi>
parents:
1139
diff
changeset
|
69 else { |
1536
09d4674b1d93
Actually it should assume that beginning of string is LWSP..
Timo Sirainen <tss@iki.fi>
parents:
1535
diff
changeset
|
70 last_lwsp = TRUE; |
1166
fccf13a1c67a
Quoting didn't remove CR and LF characters, so it could have caused
Timo Sirainen <tss@iki.fi>
parents:
1139
diff
changeset
|
71 for (i = 0; i < value_len; i++) { |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
72 switch (value[i]) { |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
73 case 0: |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
74 str_append_c(str, 128); |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
75 last_lwsp = FALSE; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
76 break; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
77 case ' ': |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
78 case '\t': |
9827
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
79 if (!last_lwsp) |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
80 str_append_c(str, ' '); |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
81 last_lwsp = TRUE; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
82 break; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
83 case 13: |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
84 case 10: |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
85 break; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
86 default: |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
87 last_lwsp = FALSE; |
1166
fccf13a1c67a
Quoting didn't remove CR and LF characters, so it could have caused
Timo Sirainen <tss@iki.fi>
parents:
1139
diff
changeset
|
88 str_append_c(str, value[i]); |
1533
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
89 break; |
76d44dda0f32
imap_quote*() will now pack all LWSP into a single space and convert NULs to
Timo Sirainen <tss@iki.fi>
parents:
1322
diff
changeset
|
90 } |
1166
fccf13a1c67a
Quoting didn't remove CR and LF characters, so it could have caused
Timo Sirainen <tss@iki.fi>
parents:
1139
diff
changeset
|
91 } |
339
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
92 } |
1166
fccf13a1c67a
Quoting didn't remove CR and LF characters, so it could have caused
Timo Sirainen <tss@iki.fi>
parents:
1139
diff
changeset
|
93 |
fccf13a1c67a
Quoting didn't remove CR and LF characters, so it could have caused
Timo Sirainen <tss@iki.fi>
parents:
1139
diff
changeset
|
94 if (!literal) |
fccf13a1c67a
Quoting didn't remove CR and LF characters, so it could have caused
Timo Sirainen <tss@iki.fi>
parents:
1139
diff
changeset
|
95 str_append_c(str, '"'); |
339
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
96 } |
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
97 |
4816
8ac2a2d27364
Cleanup: Don't put string literals into non-const pointers.
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
98 const char *imap_quote(pool_t pool, const unsigned char *value, |
9827
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
99 size_t value_len, bool fix_text) |
339
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
100 { |
7357
7d4f7c7095dd
imap_quote(): Don't crash if allocating from data stack pool.
Timo Sirainen <tss@iki.fi>
parents:
7283
diff
changeset
|
101 string_t *str; |
1322
97f8c00b8d4c
Better handling for multiline headers. Before we skipped headers larger than
Timo Sirainen <tss@iki.fi>
parents:
1321
diff
changeset
|
102 char *ret; |
97f8c00b8d4c
Better handling for multiline headers. Before we skipped headers larger than
Timo Sirainen <tss@iki.fi>
parents:
1321
diff
changeset
|
103 |
1107
4044c2903ed7
Don't do x-unknown mime encoding. Correct way is to just send them as
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
104 if (value == NULL) |
4044c2903ed7
Don't do x-unknown mime encoding. Correct way is to just send them as
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
105 return "NIL"; |
4044c2903ed7
Don't do x-unknown mime encoding. Correct way is to just send them as
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
106 |
7357
7d4f7c7095dd
imap_quote(): Don't crash if allocating from data stack pool.
Timo Sirainen <tss@iki.fi>
parents:
7283
diff
changeset
|
107 if (!pool->datastack_pool) |
7d4f7c7095dd
imap_quote(): Don't crash if allocating from data stack pool.
Timo Sirainen <tss@iki.fi>
parents:
7283
diff
changeset
|
108 t_push(); |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
109 |
7357
7d4f7c7095dd
imap_quote(): Don't crash if allocating from data stack pool.
Timo Sirainen <tss@iki.fi>
parents:
7283
diff
changeset
|
110 str = t_str_new(value_len + MAX_INT_STRLEN + 5); |
9827
dad7264633a9
imap_quote*() now have fix_text parameter. If it's not set, input isn't modified at all.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
111 imap_quote_append(str, value, value_len, fix_text); |
7357
7d4f7c7095dd
imap_quote(): Don't crash if allocating from data stack pool.
Timo Sirainen <tss@iki.fi>
parents:
7283
diff
changeset
|
112 ret = p_strndup(pool, str_data(str), str_len(str)); |
1322
97f8c00b8d4c
Better handling for multiline headers. Before we skipped headers larger than
Timo Sirainen <tss@iki.fi>
parents:
1321
diff
changeset
|
113 |
7357
7d4f7c7095dd
imap_quote(): Don't crash if allocating from data stack pool.
Timo Sirainen <tss@iki.fi>
parents:
7283
diff
changeset
|
114 if (!pool->datastack_pool) |
7d4f7c7095dd
imap_quote(): Don't crash if allocating from data stack pool.
Timo Sirainen <tss@iki.fi>
parents:
7283
diff
changeset
|
115 t_pop(); |
1322
97f8c00b8d4c
Better handling for multiline headers. Before we skipped headers larger than
Timo Sirainen <tss@iki.fi>
parents:
1321
diff
changeset
|
116 return ret; |
339
6f4eeb6a0a0d
Several fields in BODY were unquoted.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
117 } |
7916 | 118 |
119 void imap_dquote_append(string_t *dest, const char *src) | |
120 { | |
121 str_append_c(dest, '"'); | |
122 for (; *src != '\0'; src++) { | |
123 switch (*src) { | |
124 case '"': | |
125 case '\\': | |
126 str_append_c(dest, '\\'); | |
127 str_append_c(dest, *src); | |
128 break; | |
129 default: | |
130 if ((unsigned char)*src >= 0x80) { | |
131 /* 8bit input not allowed in dquotes */ | |
132 break; | |
133 } | |
134 | |
135 str_append_c(dest, *src); | |
136 break; | |
137 } | |
138 } | |
139 str_append_c(dest, '"'); | |
140 } |