Mercurial > dovecot > core-2.2
annotate src/lib/strescape.c @ 20339:327fcf6d2205
lib: Added t_str_tabunescape()
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Fri, 06 May 2016 17:24:42 +0300 |
parents | 0f22db71df7a |
children | 9af7c7196540 |
rev | line source |
---|---|
19552
0f22db71df7a
global: freshen copyright
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
18137
diff
changeset
|
1 /* Copyright (c) 2003-2016 Dovecot authors, see the included COPYING file */ |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 #include "lib.h" |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 #include "str.h" |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 #include "strescape.h" |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 const char *str_escape(const char *str) |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 { |
1329
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
9 const char *p; |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
10 string_t *ret; |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
11 |
1329
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
12 /* see if we need to quote it */ |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
13 for (p = str; *p != '\0'; p++) { |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
14 if (IS_ESCAPED_CHAR(*p)) |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
15 break; |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 } |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 |
1329
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
18 if (*p == '\0') |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 return str; |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 |
1329
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
21 /* quote */ |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
22 ret = t_str_new((size_t) (p - str) + 128); |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
23 str_append_n(ret, str, (size_t) (p - str)); |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
24 |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
25 for (; *p != '\0'; p++) { |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
26 if (IS_ESCAPED_CHAR(*p)) |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
27 str_append_c(ret, '\\'); |
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
28 str_append_c(ret, *p); |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
29 } |
1329
ae229b7acb4c
Mailbox names are now sent through imap-quoter instead of just escaping it.
Timo Sirainen <tss@iki.fi>
parents:
1216
diff
changeset
|
30 return str_c(ret); |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
31 } |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
32 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
898
diff
changeset
|
33 void str_append_unescaped(string_t *dest, const void *src, size_t src_size) |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
34 { |
898
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
35 const unsigned char *src_c = src; |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
36 size_t start = 0, i = 0; |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
37 |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
38 while (i < src_size) { |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
39 for (; i < src_size; i++) { |
898
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
40 if (src_c[i] == '\\') |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
41 break; |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
42 } |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 |
898
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
44 str_append_n(dest, src_c + start, i-start); |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
45 |
9483
464116e1d0ae
str_append_unescaped() didn't unescape \\ correctly.
Timo Sirainen <tss@iki.fi>
parents:
9232
diff
changeset
|
46 if (i < src_size) { |
464116e1d0ae
str_append_unescaped() didn't unescape \\ correctly.
Timo Sirainen <tss@iki.fi>
parents:
9232
diff
changeset
|
47 if (++i == src_size) |
464116e1d0ae
str_append_unescaped() didn't unescape \\ correctly.
Timo Sirainen <tss@iki.fi>
parents:
9232
diff
changeset
|
48 break; |
464116e1d0ae
str_append_unescaped() didn't unescape \\ correctly.
Timo Sirainen <tss@iki.fi>
parents:
9232
diff
changeset
|
49 str_append_c(dest, src_c[i++]); |
464116e1d0ae
str_append_unescaped() didn't unescape \\ correctly.
Timo Sirainen <tss@iki.fi>
parents:
9232
diff
changeset
|
50 } |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
51 start = i; |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
52 } |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
53 } |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
54 |
1216 | 55 char *str_unescape(char *str) |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
56 { |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
57 /* @UNSAFE */ |
1216 | 58 char *dest, *start = str; |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
59 |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
60 while (*str != '\\') { |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
61 if (*str == '\0') |
1216 | 62 return start; |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
63 str++; |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
64 } |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
65 |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
66 for (dest = str; *str != '\0'; str++) { |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
67 if (*str == '\\') { |
1651 | 68 str++; |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
69 if (*str == '\0') |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
70 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
71 } |
1651 | 72 |
73 *dest++ = *str; | |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
74 } |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
75 |
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
76 *dest = '\0'; |
1216 | 77 return start; |
896
21ffcce83c70
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
78 } |
9232
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
79 |
17112
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
80 int str_unescape_next(const char **str, const char **unescaped_r) |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
81 { |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
82 const char *p; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
83 char *escaped; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
84 bool esc_found = FALSE; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
85 |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
86 for (p = *str; *p != '\0'; p++) { |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
87 if (*p == '"') |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
88 break; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
89 else if (*p == '\\') { |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
90 if (p[1] == '\0') |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
91 return -1; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
92 esc_found = TRUE; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
93 p++; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
94 } |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
95 } |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
96 if (*p != '"') |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
97 return -1; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
98 escaped = p_strdup_until(unsafe_data_stack_pool, *str, p); |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
99 *str = p+1; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
100 *unescaped_r = !esc_found ? escaped : str_unescape(escaped); |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
101 return 0; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
102 } |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
103 |
15068
002e0a120c2a
Renamed str_tabescape_write() to str_append_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
104 void str_append_tabescaped(string_t *dest, const char *src) |
9232
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
105 { |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
106 for (; *src != '\0'; src++) { |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
107 switch (*src) { |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
108 case '\001': |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
109 str_append_c(dest, '\001'); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
110 str_append_c(dest, '1'); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
111 break; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
112 case '\t': |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
113 str_append_c(dest, '\001'); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
114 str_append_c(dest, 't'); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
115 break; |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
116 case '\r': |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
117 str_append_c(dest, '\001'); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
118 str_append_c(dest, 'r'); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
119 break; |
9232
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
120 case '\n': |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
121 str_append_c(dest, '\001'); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
122 str_append_c(dest, 'n'); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
123 break; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
124 default: |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
125 str_append_c(dest, *src); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
126 break; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
127 } |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
128 } |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
129 } |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
130 |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
131 const char *str_tabescape(const char *str) |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
132 { |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
133 string_t *tmp; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
134 const char *p; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
135 |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
136 for (p = str; *p != '\0'; p++) { |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
137 if (*p <= '\r') { |
9232
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
138 tmp = t_str_new(128); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
139 str_append_n(tmp, str, p-str); |
15068
002e0a120c2a
Renamed str_tabescape_write() to str_append_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
140 str_append_tabescaped(tmp, p); |
9232
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
141 return str_c(tmp); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
142 } |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
143 } |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
144 return str; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
145 } |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
146 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
147 void str_append_tabunescaped(string_t *dest, const void *src, size_t src_size) |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
148 { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
149 const unsigned char *src_c = src; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
150 size_t start = 0, i = 0; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
151 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
152 while (i < src_size) { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
153 for (; i < src_size; i++) { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
154 if (src_c[i] == '\001') |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
155 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
156 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
157 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
158 str_append_n(dest, src_c + start, i-start); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
159 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
160 if (i < src_size) { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
161 i++; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
162 if (i < src_size) { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
163 switch (src_c[i]) { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
164 case '1': |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
165 str_append_c(dest, '\001'); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
166 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
167 case 't': |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
168 str_append_c(dest, '\t'); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
169 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
170 case 'r': |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
171 str_append_c(dest, '\r'); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
172 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
173 case 'n': |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
174 str_append_c(dest, '\n'); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
175 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
176 default: |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
177 str_append_c(dest, src_c[i]); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
178 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
179 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
180 i++; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
181 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
182 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
183 start = i; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
184 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
185 } |
9516 | 186 |
187 char *str_tabunescape(char *str) | |
188 { | |
189 /* @UNSAFE */ | |
190 char *dest, *start = str; | |
191 | |
192 while (*str != '\001') { | |
193 if (*str == '\0') | |
194 return start; | |
195 str++; | |
196 } | |
197 | |
198 for (dest = str; *str != '\0'; str++) { | |
199 if (*str != '\001') | |
200 *dest++ = *str; | |
201 else { | |
202 str++; | |
203 if (*str == '\0') | |
204 break; | |
205 switch (*str) { | |
206 case '1': | |
207 *dest++ = '\001'; | |
208 break; | |
209 case 't': | |
210 *dest++ = '\t'; | |
211 break; | |
212 case 'r': | |
213 *dest++ = '\r'; | |
214 break; | |
215 case 'n': | |
216 *dest++ = '\n'; | |
217 break; | |
218 default: | |
219 *dest++ = *str; | |
220 break; | |
221 } | |
222 } | |
223 } | |
224 | |
225 *dest = '\0'; | |
226 return start; | |
227 } | |
13083
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
228 |
20339
327fcf6d2205
lib: Added t_str_tabunescape()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
229 const char *t_str_tabunescape(const char *str) |
327fcf6d2205
lib: Added t_str_tabunescape()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
230 { |
327fcf6d2205
lib: Added t_str_tabunescape()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
231 if (strchr(str, '\001') == NULL) |
327fcf6d2205
lib: Added t_str_tabunescape()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
232 return str; |
327fcf6d2205
lib: Added t_str_tabunescape()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
233 else |
327fcf6d2205
lib: Added t_str_tabunescape()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
234 return str_tabunescape(t_strdup_noconst(str)); |
327fcf6d2205
lib: Added t_str_tabunescape()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
235 } |
327fcf6d2205
lib: Added t_str_tabunescape()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
236 |
13083
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
237 char **p_strsplit_tabescaped(pool_t pool, const char *str) |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
238 { |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
239 char **args; |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
240 unsigned int i; |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
241 |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
242 args = p_strsplit(pool, str, "\t"); |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
243 for (i = 0; args[i] != NULL; i++) |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
244 args[i] = str_tabunescape(args[i]); |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
245 return args; |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
246 } |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
247 |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
248 const char *const *t_strsplit_tabescaped(const char *str) |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
249 { |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
250 return (void *)p_strsplit_tabescaped(pool_datastack_create(), str); |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
251 } |