Mercurial > dovecot > core-2.2
annotate src/lib/strescape.c @ 15715:36ef72481934
Oops :) Update copyrights to 2013 without breaking all .c files.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sat, 02 Feb 2013 17:02:55 +0200 |
parents | 90710c6c3beb |
children | 9735c6fb7e39 |
rev | line source |
---|---|
15715
36ef72481934
Oops :) Update copyrights to 2013 without breaking all .c files.
Timo Sirainen <tss@iki.fi>
parents:
15714
diff
changeset
|
1 /* Copyright (c) 2003-2013 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 |
15068
002e0a120c2a
Renamed str_tabescape_write() to str_append_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
80 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
|
81 { |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
82 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
|
83 switch (*src) { |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
84 case '\001': |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
85 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
|
86 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
|
87 break; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
88 case '\t': |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 break; |
9232
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
96 case '\n': |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
97 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
|
98 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
|
99 break; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
100 default: |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
101 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
|
102 break; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
103 } |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
104 } |
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 |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
107 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
|
108 { |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
109 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
|
110 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
|
111 |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
112 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 } |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
119 } |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
120 return str; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
121 } |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
122 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
123 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
|
124 { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
125 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
|
126 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
|
127 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
128 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
|
129 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
|
130 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
|
131 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
132 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
133 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
134 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
|
135 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
136 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
|
137 i++; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
138 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
|
139 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
|
140 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
|
141 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
|
142 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
143 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
|
144 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
|
145 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
146 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
|
147 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
|
148 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
149 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
|
150 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
|
151 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
152 default: |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
153 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
|
154 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
155 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
156 i++; |
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 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
159 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
|
160 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
161 } |
9516 | 162 |
163 char *str_tabunescape(char *str) | |
164 { | |
165 /* @UNSAFE */ | |
166 char *dest, *start = str; | |
167 | |
168 while (*str != '\001') { | |
169 if (*str == '\0') | |
170 return start; | |
171 str++; | |
172 } | |
173 | |
174 for (dest = str; *str != '\0'; str++) { | |
175 if (*str != '\001') | |
176 *dest++ = *str; | |
177 else { | |
178 str++; | |
179 if (*str == '\0') | |
180 break; | |
181 switch (*str) { | |
182 case '1': | |
183 *dest++ = '\001'; | |
184 break; | |
185 case 't': | |
186 *dest++ = '\t'; | |
187 break; | |
188 case 'r': | |
189 *dest++ = '\r'; | |
190 break; | |
191 case 'n': | |
192 *dest++ = '\n'; | |
193 break; | |
194 default: | |
195 *dest++ = *str; | |
196 break; | |
197 } | |
198 } | |
199 } | |
200 | |
201 *dest = '\0'; | |
202 return start; | |
203 } | |
13083
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
204 |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
205 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
|
206 { |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
207 char **args; |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
208 unsigned int i; |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
209 |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
210 args = p_strsplit(pool, str, "\t"); |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
211 for (i = 0; args[i] != NULL; i++) |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
212 args[i] = str_tabunescape(args[i]); |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
213 return args; |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
214 } |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
215 |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
216 const char *const *t_strsplit_tabescaped(const char *str) |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
217 { |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
218 return (void *)p_strsplit_tabescaped(pool_datastack_create(), str); |
e07d2e37053d
liblib: Added [tp]_strsplit_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
219 } |