Mercurial > dovecot > core-2.2
annotate src/lib/strescape.c @ 9484:5a6fe52a0cfc HEAD
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Tue, 16 Jun 2009 13:49:18 -0400 |
parents | 464116e1d0ae |
children | aad42b172e1a |
rev | line source |
---|---|
8590
b9faf4db2a9f
Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
1 /* Copyright (c) 2003-2009 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 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
|
40 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
|
41 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
|
42 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
|
43 } |
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
|
44 |
898
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
45 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
|
46 |
9483
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 if (++i == src_size) |
464116e1d0ae
str_append_unescaped() didn't unescape \\ correctly.
Timo Sirainen <tss@iki.fi>
parents:
9232
diff
changeset
|
49 break; |
464116e1d0ae
str_append_unescaped() didn't unescape \\ correctly.
Timo Sirainen <tss@iki.fi>
parents:
9232
diff
changeset
|
50 str_append_c(dest, src_c[i++]); |
464116e1d0ae
str_append_unescaped() didn't unescape \\ correctly.
Timo Sirainen <tss@iki.fi>
parents:
9232
diff
changeset
|
51 } |
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
|
52 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
|
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 } |
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
|
55 |
1216 | 56 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
|
57 { |
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
|
58 /* @UNSAFE */ |
1216 | 59 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
|
60 |
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 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
|
62 if (*str == '\0') |
1216 | 63 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
|
64 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
|
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 |
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
|
67 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
|
68 if (*str == '\\') { |
1651 | 69 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
|
70 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
|
71 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
72 } |
1651 | 73 |
74 *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
|
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 |
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
|
77 *dest = '\0'; |
1216 | 78 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
|
79 } |
9232
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
80 |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
81 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
|
82 { |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
83 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
|
84 switch (*src) { |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
85 case '\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, '\001'); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
87 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
|
88 break; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
89 case '\t': |
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, '\001'); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 break; |
9232
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
97 case '\n': |
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, '\001'); |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
99 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
|
100 break; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
101 default: |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
102 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
|
103 break; |
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 |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
108 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
|
109 { |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
110 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
|
111 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
|
112 |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 } |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
120 } |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
121 return str; |
2dcf2f313329
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
122 } |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
123 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
124 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
|
125 { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
126 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
|
127 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
|
128 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
134 } |
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 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
|
137 |
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 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 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
|
141 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
|
142 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
|
143 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
|
144 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
145 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
|
146 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
|
147 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
148 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
|
149 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
|
150 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
151 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
|
152 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
|
153 break; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
154 default: |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
155 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
|
156 break; |
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 i++; |
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 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
161 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
|
162 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
9483
diff
changeset
|
163 } |