annotate src/lib/strescape.c @ 8590:b9faf4db2a9f HEAD

Updated copyright notices to include year 2009.
author Timo Sirainen <tss@iki.fi>
date Tue, 06 Jan 2009 09:25:38 -0500
parents 7ed926ed7aa4
children 2dcf2f313329
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1651
43fdcf8d9a0d unescaping was broken
Timo Sirainen <tss@iki.fi>
parents: 1329
diff changeset
47 if (i < 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
48 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
49 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
50 }
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 }
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
1216
3784730cfcd8 str_unescape(): return char*
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
53 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
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 /* @UNSAFE */
1216
3784730cfcd8 str_unescape(): return char*
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
56 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
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 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
59 if (*str == '\0')
1216
3784730cfcd8 str_unescape(): return char*
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
60 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
61 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 }
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
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 for (dest = str; *str != '\0'; str++) {
1651
43fdcf8d9a0d unescaping was broken
Timo Sirainen <tss@iki.fi>
parents: 1329
diff changeset
65 if (*str == '\\' && str[1] != '\0')
43fdcf8d9a0d unescaping was broken
Timo Sirainen <tss@iki.fi>
parents: 1329
diff changeset
66 str++;
43fdcf8d9a0d unescaping was broken
Timo Sirainen <tss@iki.fi>
parents: 1329
diff changeset
67
43fdcf8d9a0d unescaping was broken
Timo Sirainen <tss@iki.fi>
parents: 1329
diff changeset
68 *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
69 }
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
70
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
71 *dest = '\0';
1216
3784730cfcd8 str_unescape(): return char*
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
72 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
73 }