annotate src/lib/strescape.c @ 14133:ba770cba5598

Updated copyright notices to include year 2012.
author Timo Sirainen <tss@iki.fi>
date Sun, 12 Feb 2012 18:55:28 +0200
parents e07d2e37053d
children 002e0a120c2a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14133
ba770cba5598 Updated copyright notices to include year 2012.
Timo Sirainen <tss@iki.fi>
parents: 13083
diff changeset
1 /* Copyright (c) 2003-2012 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
3784730cfcd8 str_unescape(): return char*
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
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
3784730cfcd8 str_unescape(): return char*
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
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
3784730cfcd8 str_unescape(): return char*
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
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
43fdcf8d9a0d unescaping was broken
Timo Sirainen <tss@iki.fi>
parents: 1329
diff changeset
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
43fdcf8d9a0d unescaping was broken
Timo Sirainen <tss@iki.fi>
parents: 1329
diff changeset
72
43fdcf8d9a0d unescaping was broken
Timo Sirainen <tss@iki.fi>
parents: 1329
diff changeset
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
3784730cfcd8 str_unescape(): return char*
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
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
2dcf2f313329 Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
80 void str_tabescape_write(string_t *dest, const char *src)
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);
2dcf2f313329 Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
116 str_tabescape_write(tmp, p);
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
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
162
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
163 char *str_tabunescape(char *str)
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
164 {
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
165 /* @UNSAFE */
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
166 char *dest, *start = str;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
167
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
168 while (*str != '\001') {
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
169 if (*str == '\0')
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
170 return start;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
171 str++;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
172 }
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
173
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
174 for (dest = str; *str != '\0'; str++) {
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
175 if (*str != '\001')
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
176 *dest++ = *str;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
177 else {
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
178 str++;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
179 if (*str == '\0')
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
180 break;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
181 switch (*str) {
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
182 case '1':
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
183 *dest++ = '\001';
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
184 break;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
185 case 't':
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
186 *dest++ = '\t';
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
187 break;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
188 case 'r':
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
189 *dest++ = '\r';
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
190 break;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
191 case 'n':
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
192 *dest++ = '\n';
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
193 break;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
194 default:
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
195 *dest++ = *str;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
196 break;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
197 }
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
198 }
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
199 }
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
200
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
201 *dest = '\0';
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
202 return start;
aad42b172e1a Added str_tabunescape().
Timo Sirainen <tss@iki.fi>
parents: 9484
diff changeset
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 }