| 1 |
|
| /* |
| 2 |
|
| * Copyright (c) 2002-2018 Dovecot authors |
| 3 |
|
| * Copyright (c) 2015-2018 Pali <pali@cpan.org> |
| 4 |
|
| * |
| 5 |
|
| * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
|
| * copy of this software and associated documentation files (the "Software"), |
| 7 |
|
| * to deal in the Software without restriction, including without limitation |
| 8 |
|
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
|
| * and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
|
| * Software is furnished to do so, subject to the following conditions: |
| 11 |
|
| * |
| 12 |
|
| * The above copyright notice and this permission notice shall be included in |
| 13 |
|
| * all copies or substantial portions of the Software. |
| 14 |
|
| * |
| 15 |
|
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
|
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
|
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
|
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
|
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
|
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
|
| * DEALINGS IN THE SOFTWARE. |
| 22 |
|
| */ |
| 23 |
|
| |
| 24 |
|
| #include <errno.h> |
| 25 |
|
| #include <stdbool.h> |
| 26 |
|
| #include <stdint.h> |
| 27 |
|
| #include <stdlib.h> |
| 28 |
|
| #include <string.h> |
| 29 |
|
| |
| 30 |
|
| #include "dovecot-parser.h" |
| 31 |
|
| |
| 32 |
|
| #ifndef SIZE_MAX |
| 33 |
|
| #define SIZE_MAX ((size_t)-1) |
| 34 |
|
| #endif |
| 35 |
|
| |
| 36 |
|
| void i_panic(const char *format, ...); |
| 37 |
|
| |
| 38 |
|
| #ifdef DEBUG |
| 39 |
|
| #define i_assert(expr) \ |
| 40 |
|
| do { if (!(expr)) \ |
| 41 |
|
| i_panic("file %s: line %d (%s): assertion failed: (%s)", \ |
| 42 |
|
| __FILE__, \ |
| 43 |
|
| __LINE__, \ |
| 44 |
|
| __FUNCTION__, \ |
| 45 |
|
| #expr); \ |
| 46 |
|
| } while ( 0 ) |
| 47 |
|
| #else |
| 48 |
|
| #define i_assert(expr) |
| 49 |
|
| #endif |
| 50 |
|
| |
| 51 |
|
| typedef struct { |
| 52 |
|
| char *buf; |
| 53 |
|
| size_t len; |
| 54 |
|
| size_t size; |
| 55 |
|
| } string_t; |
| 56 |
|
| |
| 57 |
|
| struct rfc822_parser_context { |
| 58 |
|
| const unsigned char *data, *end; |
| 59 |
|
| string_t *last_comment; |
| 60 |
|
| |
| 61 |
|
| /* Replace NUL characters with this string */ |
| 62 |
|
| const char *nul_replacement_str; |
| 63 |
|
| }; |
| 64 |
|
| |
| 65 |
|
| struct message_address_parser_context { |
| 66 |
|
| struct rfc822_parser_context parser; |
| 67 |
|
| |
| 68 |
|
| struct message_address *first_addr, *last_addr, addr; |
| 69 |
|
| string_t *str; |
| 70 |
|
| |
| 71 |
|
| bool fill_missing, non_strict_dots, non_strict_dots_as_invalid; |
| 72 |
|
| }; |
| 73 |
|
| |
| 74 |
382 |
| static string_t *str_new(size_t initial_size) |
| 75 |
|
| { |
| 76 |
|
| char *buf; |
| 77 |
|
| string_t *str; |
| 78 |
|
| |
| 79 |
382 |
▶ | if (!initial_size) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!initial_size) |
0 |
382 |
382 |
|
| 80 |
0 |
| initial_size = 1; |
| 81 |
|
| |
| 82 |
382 |
▶ | if (initial_size >= SIZE_MAX / 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (initial_size >= SIZE_MAX / 2) |
0 |
382 |
382 |
|
| 83 |
0 |
| i_panic("str_new() failed: %s", "initial_size is too big"); |
| 84 |
|
| |
| 85 |
382 |
| buf = malloc(initial_size); |
| 86 |
382 |
▶ | if (!buf) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) |
0 |
382 |
382 |
|
| 87 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 88 |
|
| |
| 89 |
382 |
| str = malloc(sizeof(string_t)); |
| 90 |
382 |
▶ | if (!str) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!str) |
0 |
382 |
382 |
|
| 91 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 92 |
|
| |
| 93 |
382 |
| buf[0] = 0; |
| 94 |
|
| |
| 95 |
382 |
| str->buf = buf; |
| 96 |
382 |
| str->len = 0; |
| 97 |
382 |
| str->size = initial_size; |
| 98 |
|
| |
| 99 |
382 |
| return str; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
382 |
| static void str_free(string_t **str) |
| 103 |
|
| { |
| 104 |
382 |
| free((*str)->buf); |
| 105 |
382 |
| free(*str); |
| 106 |
382 |
| *str = NULL; |
| 107 |
382 |
| } |
| 108 |
|
| |
| 109 |
21 |
| static const char *str_c(string_t *str) |
| 110 |
|
| { |
| 111 |
21 |
| return str->buf; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
574 |
| static char *str_ccopy(string_t *str) |
| 115 |
|
| { |
| 116 |
|
| char *copy; |
| 117 |
|
| |
| 118 |
574 |
| copy = malloc(str->len+1); |
| 119 |
574 |
▶ | if (!copy) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!copy) |
0 |
574 |
574 |
|
| 120 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 121 |
|
| |
| 122 |
574 |
| memcpy(copy, str->buf, str->len); |
| 123 |
574 |
| copy[str->len] = 0; |
| 124 |
574 |
| return copy; |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
729 |
| static size_t str_len(const string_t *str) |
| 128 |
|
| { |
| 129 |
729 |
| return str->len; |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
2269 |
| static void str_append_data(string_t *str, const void *data, size_t len) |
| 133 |
|
| { |
| 134 |
|
| char *new_buf; |
| 135 |
|
| size_t need_size; |
| 136 |
|
| |
| 137 |
2269 |
| need_size = str->len + len + 1; |
| 138 |
|
| |
| 139 |
2269 |
▶ | if (len >= SIZE_MAX / 2 || need_size >= SIZE_MAX / 2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= SIZE_MAX / 2 || need_size >= SIZE_MAX / 2) |
2269 |
0 |
2269 |
| if (len >= SIZE_MAX / 2 || need_size >= SIZE_MAX / 2) |
0 |
2269 |
2269 |
|
| 140 |
0 |
| i_panic("%s() failed: %s", __FUNCTION__, "len is too big"); |
| 141 |
|
| |
| 142 |
2269 |
▶ | if (need_size > str->size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (need_size > str->size) { |
6 |
2263 |
2269 |
|
| 143 |
6 |
| str->size = 1; |
| 144 |
56 |
▶ | while (str->size < need_size) |
Branch: 1 branch
| Branch | True | False | Total |
| while (str->size < need_size) |
50 |
6 |
56 |
|
| 145 |
50 |
| str->size <<= 1; |
| 146 |
|
| |
| 147 |
6 |
| new_buf = realloc(str->buf, str->size); |
| 148 |
6 |
▶ | if (!new_buf) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!new_buf) |
0 |
6 |
6 |
|
| 149 |
0 |
| i_panic("realloc() failed: %s", strerror(errno)); |
| 150 |
|
| |
| 151 |
6 |
| str->buf = new_buf; |
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
2269 |
| memcpy(str->buf + str->len, data, len); |
| 155 |
2269 |
| str->len += len; |
| 156 |
2269 |
| str->buf[str->len] = 0; |
| 157 |
2269 |
| } |
| 158 |
|
| |
| 159 |
88 |
| static void str_append(string_t *str, const char *cstr) |
| 160 |
|
| { |
| 161 |
88 |
| str_append_data(str, cstr, strlen(cstr)); |
| 162 |
88 |
| } |
| 163 |
|
| |
| 164 |
959 |
| static void str_append_c(string_t *str, unsigned char chr) |
| 165 |
|
| { |
| 166 |
959 |
| str_append_data(str, &chr, 1); |
| 167 |
959 |
| } |
| 168 |
|
| |
| 169 |
573 |
| static void str_truncate(string_t *str, size_t len) |
| 170 |
|
| { |
| 171 |
573 |
▶ | if (str->size - 1 <= len || str->len <= len) |
Branch: 2 branches
| Branch | True | False | Total |
| if (str->size - 1 <= len || str->len <= len) |
573 |
0 |
573 |
| if (str->size - 1 <= len || str->len <= len) |
219 |
354 |
573 |
|
| 172 |
219 |
| return; |
| 173 |
|
| |
| 174 |
354 |
| str->len = len; |
| 175 |
354 |
| str->buf[len] = 0; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
|
| /* |
| 179 |
|
| atext = ALPHA / DIGIT / ; Any character except controls, |
| 180 |
|
| "!" / "#" / ; SP, and specials. |
| 181 |
|
| "$" / "%" / ; Used for atoms |
| 182 |
|
| "&" / "'" / |
| 183 |
|
| "*" / "+" / |
| 184 |
|
| "-" / "/" / |
| 185 |
|
| "=" / "?" / |
| 186 |
|
| "^" / "_" / |
| 187 |
|
| "`" / "{" / |
| 188 |
|
| "|" / "}" / |
| 189 |
|
| "~" |
| 190 |
|
| |
| 191 |
|
| MIME: |
| 192 |
|
| |
| 193 |
|
| token := 1*<any (US-ASCII) CHAR except SPACE, CTLs, |
| 194 |
|
| or tspecials> |
| 195 |
|
| tspecials := "(" / ")" / "<" / ">" / "@" / |
| 196 |
|
| "," / ";" / ":" / "\" / <"> |
| 197 |
|
| "/" / "[" / "]" / "?" / "=" |
| 198 |
|
| |
| 199 |
|
| So token is same as dot-atom, except stops also at '/', '?' and '='. |
| 200 |
|
| */ |
| 201 |
|
| |
| 202 |
|
| /* atext chars are marked with 1, alpha and digits with 2, |
| 203 |
|
| atext-but-mime-tspecials with 4 */ |
| 204 |
|
| unsigned char rfc822_atext_chars[256] = { |
| 205 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0-15 */ |
| 206 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16-31 */ |
| 207 |
|
| 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 4, /* 32-47 */ |
| 208 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 4, 0, 4, /* 48-63 */ |
| 209 |
|
| 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 64-79 */ |
| 210 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1, 1, /* 80-95 */ |
| 211 |
|
| 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 96-111 */ |
| 212 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 0, /* 112-127 */ |
| 213 |
|
| |
| 214 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 215 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 216 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 217 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 218 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 219 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 220 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 221 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 |
| 222 |
|
| }; |
| 223 |
|
| |
| 224 |
|
| #define IS_ATEXT(c) \ |
| 225 |
|
| (rfc822_atext_chars[(int)(unsigned char)(c)] != 0) |
| 226 |
|
| #define IS_ATEXT_NON_TSPECIAL(c) \ |
| 227 |
|
| ((rfc822_atext_chars[(int)(unsigned char)(c)] & 3) != 0) |
| 228 |
|
| |
| 229 |
|
| /* |
| 230 |
|
| qtext = %d33 / ; Printable US-ASCII |
| 231 |
|
| %d35-91 / ; characters not including |
| 232 |
|
| %d93-126 / ; "\" or the quote character |
| 233 |
|
| obs-qtext |
| 234 |
|
| |
| 235 |
|
| obs-qtext = obs-NO-WS-CTL |
| 236 |
|
| |
| 237 |
|
| obs-NO-WS-CTL = %d1-8 / ; US-ASCII control |
| 238 |
|
| %d11 / ; characters that do not |
| 239 |
|
| %d12 / ; include the carriage |
| 240 |
|
| %d14-31 / ; return, line feed, and |
| 241 |
|
| %d127 ; white space characters |
| 242 |
|
| |
| 243 |
|
| So qtext is everything expects '\0', '\t', '\n', '\r', ' ', '"', '\\'. |
| 244 |
|
| */ |
| 245 |
|
| |
| 246 |
|
| /* non-qtext characters */ |
| 247 |
|
| #define CHAR_NEEDS_ESCAPE(c) ((c) == '"' || (c) == '\\' || (c) == '\0' || (c) == '\t' || (c) == '\n' || (c) == '\r') |
| 248 |
|
| |
| 249 |
|
| /* quote with "" and escape all needed characters */ |
| 250 |
278 |
| static void str_append_maybe_escape(string_t *str, const char *data, size_t len, bool quote_dot, bool quote_space) |
| 251 |
|
| { |
| 252 |
|
| const char *p; |
| 253 |
|
| const char *end; |
| 254 |
|
| |
| 255 |
278 |
▶ | if (len == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) { |
2 |
276 |
278 |
|
| 256 |
2 |
| str_append(str, "\"\""); |
| 257 |
2 |
| return; |
| 258 |
|
| } |
| 259 |
|
| |
| 260 |
|
| /* leading or trailing dot needs to be always quoted */ |
| 261 |
276 |
▶ | if (data[0] == '.' || data[len-1] == '.') |
Branch: 2 branches
| Branch | True | False | Total |
| if (data[0] == '.' || data[len-1] == '.') |
275 |
1 |
276 |
| if (data[0] == '.' || data[len-1] == '.') |
3 |
272 |
275 |
|
| 262 |
4 |
| quote_dot = true; |
| 263 |
|
| |
| 264 |
276 |
| end = data + len; |
| 265 |
|
| |
| 266 |
|
| /* see if we need to quote it */ |
| 267 |
2803 |
▶ | for (p = data; p != end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = data; p != end; p++) { |
2574 |
229 |
2803 |
|
| 268 |
2574 |
▶ | if (!IS_ATEXT(*p) && (quote_dot || *p != '.') && (quote_space || *p != ' ')) |
Branch: 5 branches
| Branch | True | False | Total |
| if (!IS_ATEXT(*p) && (quote_dot || *p != '.') && (quote_space || *p != ' ')) |
133 |
2441 |
2574 |
| if (!IS_ATEXT(*p) && (quote_dot || *p != '.') && (quote_space || *p != ' ')) |
79 |
54 |
133 |
| if (!IS_ATEXT(*p) && (quote_dot || *p != '.') && (quote_space || *p != ' ')) |
9 |
70 |
79 |
| if (!IS_ATEXT(*p) && (quote_dot || *p != '.') && (quote_space || *p != ' ')) |
16 |
47 |
63 |
| if (!IS_ATEXT(*p) && (quote_dot || *p != '.') && (quote_space || *p != ' ')) |
16 |
0 |
16 |
|
| 269 |
|
| break; |
| 270 |
|
| } |
| 271 |
|
| |
| 272 |
276 |
▶ | if (p == end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == end) { |
229 |
47 |
276 |
|
| 273 |
229 |
| str_append_data(str, data, len); |
| 274 |
229 |
| return; |
| 275 |
|
| } |
| 276 |
|
| |
| 277 |
|
| /* see if we need to escape it */ |
| 278 |
544 |
▶ | for (p = data; p != end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = data; p != end; p++) { |
511 |
33 |
544 |
|
| 279 |
511 |
▶ | if (CHAR_NEEDS_ESCAPE(*p)) |
Branch: 6 branches
| Branch | True | False | Total |
| if (CHAR_NEEDS_ESCAPE(*p)) |
501 |
10 |
511 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
501 |
0 |
501 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
497 |
4 |
501 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
497 |
0 |
497 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
497 |
0 |
497 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
497 |
0 |
497 |
|
| 280 |
|
| break; |
| 281 |
|
| } |
| 282 |
|
| |
| 283 |
47 |
▶ | if (p == end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == end) { |
33 |
14 |
47 |
|
| 284 |
|
| /* only quote */ |
| 285 |
33 |
| str_append_c(str, '"'); |
| 286 |
33 |
| str_append_data(str, data, len); |
| 287 |
33 |
| str_append_c(str, '"'); |
| 288 |
33 |
| return; |
| 289 |
|
| } |
| 290 |
|
| |
| 291 |
|
| /* quote and escape */ |
| 292 |
14 |
| str_append_c(str, '"'); |
| 293 |
14 |
| str_append_data(str, data, (size_t) (p - data)); |
| 294 |
|
| |
| 295 |
180 |
▶ | for (; p != end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; p != end; p++) { |
166 |
14 |
180 |
|
| 296 |
166 |
▶ | if (CHAR_NEEDS_ESCAPE(*p)) |
Branch: 6 branches
| Branch | True | False | Total |
| if (CHAR_NEEDS_ESCAPE(*p)) |
151 |
15 |
166 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
150 |
1 |
151 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
141 |
9 |
150 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
140 |
1 |
141 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
139 |
1 |
140 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
1 |
138 |
139 |
|
| 297 |
28 |
| str_append_c(str, '\\'); |
| 298 |
166 |
| str_append_c(str, *p); |
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
14 |
| str_append_c(str, '"'); |
| 302 |
|
| } |
| 303 |
|
| |
| 304 |
|
| /* Parse given data using RFC 822 token parser. */ |
| 305 |
148 |
| static void rfc822_parser_init(struct rfc822_parser_context *ctx, |
| 306 |
|
| const unsigned char *data, size_t size, |
| 307 |
|
| string_t *last_comment) |
| 308 |
|
| { |
| 309 |
148 |
| memset(ctx, 0, sizeof(*ctx)); |
| 310 |
148 |
| ctx->data = data; |
| 311 |
148 |
| ctx->end = data + size; |
| 312 |
148 |
| ctx->last_comment = last_comment; |
| 313 |
148 |
| } |
| 314 |
|
| |
| 315 |
140 |
| static void rfc822_parser_deinit(struct rfc822_parser_context *ctx) |
| 316 |
|
| { |
| 317 |
|
| /* make sure the parsing didn't trigger a bug that caused reading |
| 318 |
|
| past the end pointer. */ |
| 319 |
|
| i_assert(ctx->data <= ctx->end); |
| 320 |
|
| /* make sure the parser is no longer accessed */ |
| 321 |
140 |
| ctx->data = ctx->end = NULL; |
| 322 |
140 |
| } |
| 323 |
|
| |
| 324 |
|
| /* The functions below return 1 = more data available, 0 = no more data |
| 325 |
|
| available (but a value might have been returned now), -1 = invalid input. |
| 326 |
|
| |
| 327 |
|
| LWSP is automatically skipped after value, but not before it. So typically |
| 328 |
|
| you begin with skipping LWSP and then start using the parse functions. */ |
| 329 |
|
| |
| 330 |
|
| /* Parse comment. Assumes parser's data points to '(' */ |
| 331 |
41 |
| static int rfc822_skip_comment(struct rfc822_parser_context *ctx) |
| 332 |
|
| { |
| 333 |
|
| const unsigned char *start; |
| 334 |
|
| size_t len; |
| 335 |
41 |
| int level = 1; |
| 336 |
|
| |
| 337 |
|
| i_assert(*ctx->data == '('); |
| 338 |
|
| |
| 339 |
41 |
▶ | if (ctx->last_comment != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->last_comment != NULL) |
28 |
13 |
41 |
|
| 340 |
28 |
| str_truncate(ctx->last_comment, 0); |
| 341 |
|
| |
| 342 |
41 |
| start = ++ctx->data; |
| 343 |
632 |
▶ | for (; ctx->data < ctx->end; ctx->data++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; ctx->data < ctx->end; ctx->data++) { |
629 |
3 |
632 |
|
| 344 |
629 |
| switch (*ctx->data) { |
| 345 |
0 |
| case '\0': |
| 346 |
0 |
▶ | if (ctx->nul_replacement_str != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->nul_replacement_str != NULL) { |
0 |
0 |
0 |
|
| 347 |
0 |
▶ | if (ctx->last_comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->last_comment != NULL) { |
0 |
0 |
0 |
|
| 348 |
0 |
| str_append_data(ctx->last_comment, start, |
| 349 |
0 |
| ctx->data - start); |
| 350 |
0 |
| str_append(ctx->last_comment, |
| 351 |
|
| ctx->nul_replacement_str); |
| 352 |
0 |
| start = ctx->data + 1; |
| 353 |
|
| } |
| 354 |
|
| } else { |
| 355 |
0 |
| return -1; |
| 356 |
|
| } |
| 357 |
0 |
| break; |
| 358 |
14 |
| case '(': |
| 359 |
14 |
| level++; |
| 360 |
14 |
| break; |
| 361 |
52 |
| case ')': |
| 362 |
52 |
▶ | if (--level == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (--level == 0) { |
38 |
14 |
52 |
|
| 363 |
38 |
▶ | if (ctx->last_comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->last_comment != NULL) { |
25 |
13 |
38 |
|
| 364 |
25 |
| str_append_data(ctx->last_comment, start, |
| 365 |
25 |
| ctx->data - start); |
| 366 |
|
| } |
| 367 |
38 |
| ctx->data++; |
| 368 |
38 |
| return ctx->data < ctx->end ? 1 : 0; |
| 369 |
|
| } |
| 370 |
14 |
| break; |
| 371 |
0 |
| case '\n': |
| 372 |
|
| /* folding whitespace, remove the (CR)LF */ |
| 373 |
0 |
▶ | if (ctx->last_comment == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->last_comment == NULL) |
0 |
0 |
0 |
|
| 374 |
0 |
| break; |
| 375 |
0 |
| len = ctx->data - start; |
| 376 |
0 |
▶ | if (len > 0 && start[len-1] == '\r') |
Branch: 2 branches
| Branch | True | False | Total |
| if (len > 0 && start[len-1] == '\r') |
0 |
0 |
0 |
| if (len > 0 && start[len-1] == '\r') |
0 |
0 |
0 |
|
| 377 |
0 |
| len--; |
| 378 |
0 |
| str_append_data(ctx->last_comment, start, len); |
| 379 |
0 |
| start = ctx->data + 1; |
| 380 |
0 |
| break; |
| 381 |
0 |
| case '\\': |
| 382 |
0 |
| ctx->data++; |
| 383 |
0 |
▶ | if (ctx->data >= ctx->end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data >= ctx->end) |
0 |
0 |
0 |
|
| 384 |
0 |
| return -1; |
| 385 |
|
| #if 0 |
| 386 |
|
| if (*ctx->data == '\r' || *ctx->data == '\n' || |
| 387 |
|
| *ctx->data == '\0') { |
| 388 |
|
| /* quoted-pair doesn't allow CR/LF/NUL. |
| 389 |
|
| They are part of the obs-qp though, so don't |
| 390 |
|
| return them as error. */ |
| 391 |
|
| ctx->data--; |
| 392 |
|
| break; |
| 393 |
|
| } |
| 394 |
|
| #endif |
| 395 |
0 |
▶ | if (ctx->last_comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->last_comment != NULL) { |
0 |
0 |
0 |
|
| 396 |
0 |
| str_append_data(ctx->last_comment, start, |
| 397 |
0 |
| ctx->data - start - 1); |
| 398 |
|
| } |
| 399 |
0 |
| start = ctx->data; |
| 400 |
0 |
| break; |
| 401 |
|
| } |
| 402 |
|
| } |
| 403 |
|
| |
| 404 |
|
| /* missing ')' */ |
| 405 |
3 |
| return -1; |
| 406 |
|
| } |
| 407 |
|
| |
| 408 |
|
| /* Skip LWSP if there is any */ |
| 409 |
1555 |
| static int rfc822_skip_lwsp(struct rfc822_parser_context *ctx) |
| 410 |
|
| { |
| 411 |
1766 |
▶ | for (; ctx->data < ctx->end;) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; ctx->data < ctx->end;) { |
1625 |
141 |
1766 |
|
| 412 |
1625 |
▶ | if (*ctx->data == ' ' || *ctx->data == '\t' || |
Branch: 2 branches
| Branch | True | False | Total |
| if (*ctx->data == ' ' || *ctx->data == '\t' || |
1453 |
172 |
1625 |
| if (*ctx->data == ' ' || *ctx->data == '\t' || |
1453 |
0 |
1453 |
|
| 413 |
1453 |
▶ | *ctx->data == '\r' || *ctx->data == '\n') { |
Branch: 2 branches
| Branch | True | False | Total |
| *ctx->data == '\r' || *ctx->data == '\n') { |
1453 |
0 |
1453 |
| *ctx->data == '\r' || *ctx->data == '\n') { |
1 |
1452 |
1453 |
|
| 414 |
173 |
| ctx->data++; |
| 415 |
173 |
| continue; |
| 416 |
|
| } |
| 417 |
|
| |
| 418 |
1452 |
▶ | if (*ctx->data != '(') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data != '(') |
1411 |
41 |
1452 |
|
| 419 |
1411 |
| break; |
| 420 |
|
| |
| 421 |
41 |
▶ | if (rfc822_skip_comment(ctx) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_skip_comment(ctx) < 0) |
3 |
38 |
41 |
|
| 422 |
3 |
| return -1; |
| 423 |
|
| } |
| 424 |
1552 |
| return ctx->data < ctx->end ? 1 : 0; |
| 425 |
|
| } |
| 426 |
|
| |
| 427 |
|
| /* Stop at next non-atext char */ |
| 428 |
0 |
| int rfc822_parse_atom(struct rfc822_parser_context *ctx, string_t *str) |
| 429 |
|
| { |
| 430 |
|
| const unsigned char *start; |
| 431 |
|
| |
| 432 |
|
| /* |
| 433 |
|
| atom = [CFWS] 1*atext [CFWS] |
| 434 |
|
| atext = |
| 435 |
|
| ; Any character except controls, SP, and specials. |
| 436 |
|
| */ |
| 437 |
0 |
▶ | if (ctx->data >= ctx->end || !IS_ATEXT(*ctx->data)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->data >= ctx->end || !IS_ATEXT(*ctx->data)) |
0 |
0 |
0 |
| if (ctx->data >= ctx->end || !IS_ATEXT(*ctx->data)) |
0 |
0 |
0 |
|
| 438 |
0 |
| return -1; |
| 439 |
|
| |
| 440 |
0 |
▶ | for (start = ctx->data++; ctx->data < ctx->end; ctx->data++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (start = ctx->data++; ctx->data < ctx->end; ctx->data++) { |
0 |
0 |
0 |
|
| 441 |
0 |
▶ | if (IS_ATEXT(*ctx->data)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (IS_ATEXT(*ctx->data)) |
0 |
0 |
0 |
|
| 442 |
0 |
| continue; |
| 443 |
|
| |
| 444 |
0 |
| str_append_data(str, start, ctx->data - start); |
| 445 |
0 |
| return rfc822_skip_lwsp(ctx); |
| 446 |
|
| } |
| 447 |
|
| |
| 448 |
0 |
| str_append_data(str, start, ctx->data - start); |
| 449 |
0 |
| return 0; |
| 450 |
|
| } |
| 451 |
|
| |
| 452 |
|
| /* Like parse_atom() but don't stop at '.' */ |
| 453 |
312 |
| static int rfc822_parse_dot_atom(struct rfc822_parser_context *ctx, string_t *str, bool stop_trailing_dot) |
| 454 |
|
| { |
| 455 |
|
| const unsigned char *start; |
| 456 |
|
| const unsigned char *last_dot_ptr; |
| 457 |
|
| bool last_is_dot; |
| 458 |
|
| bool dot_problem; |
| 459 |
|
| int ret; |
| 460 |
|
| |
| 461 |
|
| /* |
| 462 |
|
| dot-atom = [CFWS] dot-atom-text [CFWS] |
| 463 |
|
| dot-atom-text = 1*atext *("." 1*atext) |
| 464 |
|
| |
| 465 |
|
| atext = |
| 466 |
|
| ; Any character except controls, SP, and specials. |
| 467 |
|
| |
| 468 |
|
| For RFC-822 compatibility allow LWSP around '.' |
| 469 |
|
| */ |
| 470 |
312 |
▶ | if (ctx->data >= ctx->end || !IS_ATEXT(*ctx->data)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->data >= ctx->end || !IS_ATEXT(*ctx->data)) |
312 |
0 |
312 |
| if (ctx->data >= ctx->end || !IS_ATEXT(*ctx->data)) |
1 |
311 |
312 |
|
| 471 |
1 |
| return -1; |
| 472 |
|
| |
| 473 |
311 |
| last_dot_ptr = ctx->data; |
| 474 |
311 |
| last_is_dot = false; |
| 475 |
311 |
| dot_problem = false; |
| 476 |
|
| |
| 477 |
3426 |
▶ | for (start = ctx->data++; ctx->data < ctx->end; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (start = ctx->data++; ctx->data < ctx->end; ) { |
3322 |
104 |
3426 |
|
| 478 |
3322 |
▶ | if (IS_ATEXT(*ctx->data)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (IS_ATEXT(*ctx->data)) { |
2922 |
400 |
3322 |
|
| 479 |
2922 |
| ctx->data++; |
| 480 |
2922 |
| continue; |
| 481 |
|
| } |
| 482 |
|
| |
| 483 |
|
| #if 0 |
| 484 |
|
| if (start == ctx->data) |
| 485 |
|
| dot_problem = true; |
| 486 |
|
| #endif |
| 487 |
400 |
| str_append_data(str, start, ctx->data - start); |
| 488 |
|
| |
| 489 |
400 |
▶ | if (ctx->data - start > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data - start > 0) |
398 |
2 |
400 |
|
| 490 |
398 |
| last_is_dot = false; |
| 491 |
|
| |
| 492 |
400 |
▶ | if ((ret = rfc822_skip_lwsp(ctx)) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = rfc822_skip_lwsp(ctx)) <= 0) |
7 |
393 |
400 |
|
| 493 |
7 |
▶ | return (dot_problem && ret >= 0) ? -2 : ret; |
Branch: 2 branches
| Branch | True | False | Total |
| return (dot_problem && ret >= 0) ? -2 : ret; |
0 |
7 |
7 |
| return (dot_problem && ret >= 0) ? -2 : ret; |
0 |
0 |
0 |
|
| 494 |
|
| |
| 495 |
393 |
▶ | if (*ctx->data != '.') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data != '.') { |
200 |
193 |
393 |
|
| 496 |
200 |
▶ | if (last_is_dot && stop_trailing_dot) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (last_is_dot && stop_trailing_dot) { |
2 |
198 |
200 |
| if (last_is_dot && stop_trailing_dot) { |
2 |
0 |
2 |
|
| 497 |
2 |
| ctx->data = last_dot_ptr; |
| 498 |
2 |
▶ | return dot_problem ? -2 : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| return dot_problem ? -2 : 1; |
0 |
2 |
2 |
|
| 499 |
|
| } |
| 500 |
198 |
▶ | return (last_is_dot || dot_problem) ? -2 : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| return (last_is_dot || dot_problem) ? -2 : 1; |
198 |
0 |
198 |
| return (last_is_dot || dot_problem) ? -2 : 1; |
0 |
198 |
198 |
|
| 501 |
|
| } |
| 502 |
|
| |
| 503 |
193 |
▶ | if (last_is_dot) |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_is_dot) |
0 |
193 |
193 |
|
| 504 |
0 |
| dot_problem = true; |
| 505 |
|
| |
| 506 |
193 |
| last_dot_ptr = ctx->data; |
| 507 |
193 |
| ctx->data++; |
| 508 |
193 |
| str_append_c(str, '.'); |
| 509 |
193 |
| last_is_dot = true; |
| 510 |
|
| |
| 511 |
193 |
▶ | if (rfc822_skip_lwsp(ctx) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_skip_lwsp(ctx) <= 0) |
0 |
193 |
193 |
|
| 512 |
0 |
▶ | return (dot_problem && ret >= 0) ? -2 : ret; |
Branch: 2 branches
| Branch | True | False | Total |
| return (dot_problem && ret >= 0) ? -2 : ret; |
0 |
0 |
0 |
| return (dot_problem && ret >= 0) ? -2 : ret; |
0 |
0 |
0 |
|
| 513 |
193 |
| start = ctx->data; |
| 514 |
|
| } |
| 515 |
|
| |
| 516 |
|
| #if 0 |
| 517 |
|
| i_assert(start != ctx->data); |
| 518 |
|
| #endif |
| 519 |
104 |
| str_append_data(str, start, ctx->data - start); |
| 520 |
104 |
▶ | return dot_problem ? -2 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return dot_problem ? -2 : 0; |
0 |
104 |
104 |
|
| 521 |
|
| } |
| 522 |
|
| |
| 523 |
|
| /* "quoted string" */ |
| 524 |
76 |
| static int rfc822_parse_quoted_string(struct rfc822_parser_context *ctx, string_t *str) |
| 525 |
|
| { |
| 526 |
|
| const unsigned char *start; |
| 527 |
|
| bool char_problem; |
| 528 |
|
| int ret; |
| 529 |
|
| size_t len; |
| 530 |
|
| |
| 531 |
|
| i_assert(ctx->data < ctx->end); |
| 532 |
|
| i_assert(*ctx->data == '"'); |
| 533 |
76 |
| ctx->data++; |
| 534 |
76 |
| char_problem = false; |
| 535 |
|
| |
| 536 |
1447 |
▶ | for (start = ctx->data; ctx->data < ctx->end; ctx->data++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (start = ctx->data; ctx->data < ctx->end; ctx->data++) { |
1447 |
0 |
1447 |
|
| 537 |
1447 |
| switch (*ctx->data) { |
| 538 |
0 |
| case '\0': |
| 539 |
0 |
▶ | if (ctx->nul_replacement_str != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->nul_replacement_str != NULL) { |
0 |
0 |
0 |
|
| 540 |
0 |
| str_append_data(str, start, ctx->data - start); |
| 541 |
0 |
| str_append(str, ctx->nul_replacement_str); |
| 542 |
0 |
| start = ctx->data + 1; |
| 543 |
|
| } else { |
| 544 |
0 |
| char_problem = true; |
| 545 |
|
| } |
| 546 |
0 |
| break; |
| 547 |
76 |
| case '"': |
| 548 |
76 |
| str_append_data(str, start, ctx->data - start); |
| 549 |
76 |
| ctx->data++; |
| 550 |
76 |
| ret = rfc822_skip_lwsp(ctx); |
| 551 |
76 |
▶ | return (char_problem && ret >= 0) ? -2 : ret; |
Branch: 2 branches
| Branch | True | False | Total |
| return (char_problem && ret >= 0) ? -2 : ret; |
0 |
76 |
76 |
| return (char_problem && ret >= 0) ? -2 : ret; |
0 |
0 |
0 |
|
| 552 |
0 |
| case '\r': |
| 553 |
0 |
▶ | if (ctx->data+1 < ctx->end && *(ctx->data+1) != '\n') |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->data+1 < ctx->end && *(ctx->data+1) != '\n') |
0 |
0 |
0 |
| if (ctx->data+1 < ctx->end && *(ctx->data+1) != '\n') |
0 |
0 |
0 |
|
| 554 |
0 |
| char_problem = true; |
| 555 |
0 |
| break; |
| 556 |
0 |
| case '\n': |
| 557 |
|
| #if 0 |
| 558 |
|
| /* folding whitespace, remove the (CR)LF */ |
| 559 |
|
| len = ctx->data - start; |
| 560 |
|
| if (len > 0 && start[len-1] == '\r') |
| 561 |
|
| len--; |
| 562 |
|
| str_append_data(str, start, len); |
| 563 |
|
| start = ctx->data + 1; |
| 564 |
|
| #endif |
| 565 |
0 |
| len = ctx->data - start; |
| 566 |
0 |
▶ | if (len <= 0 || start[len-1] != '\r') |
Branch: 2 branches
| Branch | True | False | Total |
| if (len <= 0 || start[len-1] != '\r') |
0 |
0 |
0 |
| if (len <= 0 || start[len-1] != '\r') |
0 |
0 |
0 |
|
| 567 |
0 |
| char_problem = true; |
| 568 |
0 |
| break; |
| 569 |
45 |
| case '\\': |
| 570 |
45 |
| ctx->data++; |
| 571 |
45 |
▶ | if (ctx->data >= ctx->end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data >= ctx->end) |
0 |
45 |
45 |
|
| 572 |
0 |
| return -1; |
| 573 |
|
| #if 0 |
| 574 |
|
| if (*ctx->data == '\r' || *ctx->data == '\n' || |
| 575 |
|
| *ctx->data == '\0') { |
| 576 |
|
| /* quoted-pair doesn't allow CR/LF/NUL. |
| 577 |
|
| They are part of the obs-qp though, so don't |
| 578 |
|
| return them as error. */ |
| 579 |
|
| ctx->data--; |
| 580 |
|
| break; |
| 581 |
|
| } |
| 582 |
|
| #endif |
| 583 |
45 |
| str_append_data(str, start, ctx->data - start - 1); |
| 584 |
45 |
| str_append_c(str, *ctx->data); |
| 585 |
45 |
| start = ctx->data+1; |
| 586 |
45 |
| break; |
| 587 |
|
| } |
| 588 |
|
| } |
| 589 |
|
| |
| 590 |
|
| /* missing '"' */ |
| 591 |
0 |
| return -1; |
| 592 |
|
| } |
| 593 |
|
| |
| 594 |
|
| static int |
| 595 |
78 |
| rfc822_parse_atom_or_dot(struct rfc822_parser_context *ctx, string_t *str) |
| 596 |
|
| { |
| 597 |
|
| const unsigned char *start; |
| 598 |
|
| |
| 599 |
|
| /* |
| 600 |
|
| atom = [CFWS] 1*atext [CFWS] |
| 601 |
|
| atext = |
| 602 |
|
| ; Any character except controls, SP, and specials. |
| 603 |
|
| |
| 604 |
|
| The difference between this function and rfc822_parse_dot_atom() |
| 605 |
|
| is that this doesn't just silently skip over all the whitespace. |
| 606 |
|
| */ |
| 607 |
799 |
▶ | for (start = ctx->data; ctx->data < ctx->end; ctx->data++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (start = ctx->data; ctx->data < ctx->end; ctx->data++) { |
793 |
6 |
799 |
|
| 608 |
793 |
▶ | if (IS_ATEXT(*ctx->data) || *ctx->data == '.') |
Branch: 2 branches
| Branch | True | False | Total |
| if (IS_ATEXT(*ctx->data) || *ctx->data == '.') |
94 |
699 |
793 |
| if (IS_ATEXT(*ctx->data) || *ctx->data == '.') |
22 |
72 |
94 |
|
| 609 |
721 |
| continue; |
| 610 |
|
| |
| 611 |
72 |
| str_append_data(str, start, ctx->data - start); |
| 612 |
72 |
| return rfc822_skip_lwsp(ctx); |
| 613 |
|
| } |
| 614 |
|
| |
| 615 |
6 |
| str_append_data(str, start, ctx->data - start); |
| 616 |
6 |
| return 0; |
| 617 |
|
| } |
| 618 |
|
| |
| 619 |
|
| /* atom or quoted-string */ |
| 620 |
135 |
| static int rfc822_parse_phrase(struct rfc822_parser_context *ctx, string_t *str) |
| 621 |
|
| { |
| 622 |
|
| int ret; |
| 623 |
|
| bool char_problem; |
| 624 |
|
| |
| 625 |
135 |
| char_problem = false; |
| 626 |
|
| |
| 627 |
|
| /* |
| 628 |
|
| phrase = 1*word / obs-phrase |
| 629 |
|
| word = atom / quoted-string |
| 630 |
|
| obs-phrase = word *(word / "." / CFWS) |
| 631 |
|
| */ |
| 632 |
|
| |
| 633 |
135 |
▶ | if (ctx->data >= ctx->end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data >= ctx->end) |
0 |
135 |
135 |
|
| 634 |
0 |
| return 0; |
| 635 |
135 |
▶ | if (*ctx->data == '.') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data == '.') |
135 |
0 |
135 |
|
| 636 |
0 |
| return -1; |
| 637 |
|
| |
| 638 |
|
| for (;;) { |
| 639 |
146 |
▶ | if (*ctx->data == '"') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data == '"') |
68 |
78 |
146 |
|
| 640 |
68 |
| ret = rfc822_parse_quoted_string(ctx, str); |
| 641 |
|
| else |
| 642 |
78 |
| ret = rfc822_parse_atom_or_dot(ctx, str); |
| 643 |
|
| |
| 644 |
146 |
▶ | if (ret <= 0 && ret != -2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ret <= 0 && ret != -2) |
14 |
132 |
146 |
| if (ret <= 0 && ret != -2) |
14 |
0 |
14 |
|
| 645 |
14 |
▶ | return (char_problem && ret == 0) ? -2 : ret; |
Branch: 2 branches
| Branch | True | False | Total |
| return (char_problem && ret == 0) ? -2 : ret; |
0 |
14 |
14 |
| return (char_problem && ret == 0) ? -2 : ret; |
0 |
0 |
0 |
|
| 646 |
|
| |
| 647 |
132 |
▶ | if (ret == -2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) { |
0 |
132 |
132 |
|
| 648 |
0 |
| char_problem = true; |
| 649 |
0 |
▶ | if (ctx->data >= ctx->end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data >= ctx->end) |
0 |
0 |
0 |
|
| 650 |
0 |
| return -2; |
| 651 |
|
| } |
| 652 |
|
| |
| 653 |
132 |
▶ | if (!IS_ATEXT(*ctx->data) && *ctx->data != '"' |
Branch: 2 branches
| Branch | True | False | Total |
| if (!IS_ATEXT(*ctx->data) && *ctx->data != '"' |
123 |
9 |
132 |
| if (!IS_ATEXT(*ctx->data) && *ctx->data != '"' |
121 |
2 |
123 |
|
| 654 |
121 |
▶ | && *ctx->data != '.') |
Branch: 1 branch
| Branch | True | False | Total |
| && *ctx->data != '.') |
121 |
0 |
121 |
|
| 655 |
121 |
| break; |
| 656 |
11 |
| str_append_c(str, ' '); |
| 657 |
|
| } |
| 658 |
121 |
| ret = rfc822_skip_lwsp(ctx); |
| 659 |
121 |
▶ | return (char_problem && ret >= 0) ? -2 : ret; |
Branch: 2 branches
| Branch | True | False | Total |
| return (char_problem && ret >= 0) ? -2 : ret; |
0 |
121 |
121 |
| return (char_problem && ret >= 0) ? -2 : ret; |
0 |
0 |
0 |
|
| 660 |
|
| } |
| 661 |
|
| |
| 662 |
|
| static int |
| 663 |
0 |
| rfc822_parse_domain_literal(struct rfc822_parser_context *ctx, string_t *str) |
| 664 |
|
| { |
| 665 |
|
| const unsigned char *start; |
| 666 |
|
| size_t len; |
| 667 |
|
| |
| 668 |
|
| /* |
| 669 |
|
| domain-literal = [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS] |
| 670 |
|
| dcontent = dtext / quoted-pair |
| 671 |
|
| dtext = NO-WS-CTL / ; Non white space controls |
| 672 |
|
| %d33-90 / ; The rest of the US-ASCII |
| 673 |
|
| %d94-126 ; characters not including "[", |
| 674 |
|
| ; "]", or "\" |
| 675 |
|
| */ |
| 676 |
|
| i_assert(ctx->data < ctx->end); |
| 677 |
|
| i_assert(*ctx->data == '['); |
| 678 |
|
| |
| 679 |
0 |
▶ | for (start = ctx->data++; ctx->data < ctx->end; ctx->data++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (start = ctx->data++; ctx->data < ctx->end; ctx->data++) { |
0 |
0 |
0 |
|
| 680 |
0 |
| switch (*ctx->data) { |
| 681 |
0 |
| case '\0': |
| 682 |
0 |
▶ | if (ctx->nul_replacement_str != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->nul_replacement_str != NULL) { |
0 |
0 |
0 |
|
| 683 |
0 |
| str_append_data(str, start, ctx->data - start); |
| 684 |
0 |
| str_append(str, ctx->nul_replacement_str); |
| 685 |
0 |
| start = ctx->data + 1; |
| 686 |
|
| } else { |
| 687 |
0 |
| return -1; |
| 688 |
|
| } |
| 689 |
0 |
| break; |
| 690 |
0 |
| case '[': |
| 691 |
|
| /* not allowed */ |
| 692 |
0 |
| return -1; |
| 693 |
0 |
| case ']': |
| 694 |
0 |
| str_append_data(str, start, ctx->data - start + 1); |
| 695 |
0 |
| ctx->data++; |
| 696 |
0 |
| return rfc822_skip_lwsp(ctx); |
| 697 |
0 |
| case '\n': |
| 698 |
|
| /* folding whitespace, remove the (CR)LF */ |
| 699 |
0 |
| len = ctx->data - start; |
| 700 |
0 |
▶ | if (len > 0 && start[len-1] == '\r') |
Branch: 2 branches
| Branch | True | False | Total |
| if (len > 0 && start[len-1] == '\r') |
0 |
0 |
0 |
| if (len > 0 && start[len-1] == '\r') |
0 |
0 |
0 |
|
| 701 |
0 |
| len--; |
| 702 |
0 |
| str_append_data(str, start, len); |
| 703 |
0 |
| start = ctx->data + 1; |
| 704 |
0 |
| break; |
| 705 |
0 |
| case '\\': |
| 706 |
|
| /* note: the '\' is preserved in the output */ |
| 707 |
0 |
| ctx->data++; |
| 708 |
0 |
▶ | if (ctx->data >= ctx->end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data >= ctx->end) |
0 |
0 |
0 |
|
| 709 |
0 |
| return -1; |
| 710 |
|
| #if 0 |
| 711 |
|
| if (*ctx->data == '\r' || *ctx->data == '\n' || |
| 712 |
|
| *ctx->data == '\0') { |
| 713 |
|
| /* quoted-pair doesn't allow CR/LF/NUL. |
| 714 |
|
| They are part of the obs-qp though, so don't |
| 715 |
|
| return them as error. */ |
| 716 |
|
| str_append_data(str, start, ctx->data - start); |
| 717 |
|
| start = ctx->data; |
| 718 |
|
| ctx->data--; |
| 719 |
|
| break; |
| 720 |
|
| } |
| 721 |
|
| #endif |
| 722 |
0 |
| break; |
| 723 |
|
| } |
| 724 |
|
| } |
| 725 |
|
| |
| 726 |
|
| /* missing ']' */ |
| 727 |
0 |
| return -1; |
| 728 |
|
| } |
| 729 |
|
| |
| 730 |
|
| /* dot-atom / domain-literal */ |
| 731 |
154 |
| static int rfc822_parse_domain(struct rfc822_parser_context *ctx, string_t *str) |
| 732 |
|
| { |
| 733 |
|
| /* |
| 734 |
|
| domain = dot-atom / domain-literal / obs-domain |
| 735 |
|
| domain-literal = [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS] |
| 736 |
|
| obs-domain = atom *("." atom) |
| 737 |
|
| */ |
| 738 |
|
| i_assert(ctx->data < ctx->end); |
| 739 |
|
| i_assert(*ctx->data == '@'); |
| 740 |
154 |
| ctx->data++; |
| 741 |
|
| |
| 742 |
154 |
▶ | if (rfc822_skip_lwsp(ctx) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_skip_lwsp(ctx) <= 0) |
1 |
153 |
154 |
|
| 743 |
1 |
| return -1; |
| 744 |
|
| |
| 745 |
153 |
▶ | if (*ctx->data == '[') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data == '[') |
0 |
153 |
153 |
|
| 746 |
0 |
| return rfc822_parse_domain_literal(ctx, str); |
| 747 |
|
| else |
| 748 |
153 |
| return rfc822_parse_dot_atom(ctx, str, false); |
| 749 |
|
| } |
| 750 |
|
| |
| 751 |
86 |
| static void add_address(struct message_address_parser_context *ctx) |
| 752 |
|
| { |
| 753 |
|
| struct message_address *addr; |
| 754 |
|
| |
| 755 |
86 |
| addr = malloc(sizeof(struct message_address)); |
| 756 |
86 |
▶ | if (!addr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!addr) |
0 |
86 |
86 |
|
| 757 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 758 |
|
| |
| 759 |
86 |
| memcpy(addr, &ctx->addr, sizeof(ctx->addr)); |
| 760 |
86 |
| memset(&ctx->addr, 0, sizeof(ctx->addr)); |
| 761 |
|
| |
| 762 |
86 |
▶ | if (ctx->first_addr == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->first_addr == NULL) |
41 |
45 |
86 |
|
| 763 |
41 |
| ctx->first_addr = addr; |
| 764 |
|
| else |
| 765 |
45 |
| ctx->last_addr->next = addr; |
| 766 |
86 |
| ctx->last_addr = addr; |
| 767 |
86 |
| } |
| 768 |
|
| |
| 769 |
|
| static int |
| 770 |
0 |
| parse_nonstrict_dot_atom(struct rfc822_parser_context *ctx, string_t *str) |
| 771 |
|
| { |
| 772 |
0 |
| int ret = -1; |
| 773 |
|
| |
| 774 |
|
| do { |
| 775 |
0 |
▶ | while (*ctx->data == '.') { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*ctx->data == '.') { |
0 |
0 |
0 |
|
| 776 |
0 |
| str_append_c(str, '.'); |
| 777 |
0 |
| ctx->data++; |
| 778 |
0 |
▶ | if (ctx->data == ctx->end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data == ctx->end) { |
0 |
0 |
0 |
|
| 779 |
|
| /* @domain is missing, but local-part |
| 780 |
|
| parsing was successful */ |
| 781 |
0 |
| return 0; |
| 782 |
|
| } |
| 783 |
0 |
| ret = 1; |
| 784 |
|
| } |
| 785 |
0 |
▶ | if (*ctx->data == '@') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data == '@') |
0 |
0 |
0 |
|
| 786 |
0 |
| break; |
| 787 |
0 |
| ret = rfc822_parse_atom(ctx, str); |
| 788 |
0 |
▶ | } while (ret > 0 && *ctx->data == '.'); |
Branch: 2 branches
| Branch | True | False | Total |
| } while (ret > 0 && *ctx->data == '.'); |
0 |
0 |
0 |
| } while (ret > 0 && *ctx->data == '.'); |
0 |
0 |
0 |
|
| 789 |
0 |
| return ret; |
| 790 |
|
| } |
| 791 |
|
| |
| 792 |
167 |
| static int parse_local_part(struct message_address_parser_context *ctx) |
| 793 |
|
| { |
| 794 |
|
| int ret; |
| 795 |
|
| bool char_problem; |
| 796 |
|
| |
| 797 |
|
| /* |
| 798 |
|
| local-part = dot-atom / quoted-string / obs-local-part |
| 799 |
|
| obs-local-part = word *("." word) |
| 800 |
|
| */ |
| 801 |
|
| i_assert(ctx->parser.data < ctx->parser.end); |
| 802 |
|
| |
| 803 |
167 |
| str_truncate(ctx->str, 0); |
| 804 |
167 |
| char_problem = false; |
| 805 |
|
| |
| 806 |
167 |
▶ | while (ctx->parser.data < ctx->parser.end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ctx->parser.data < ctx->parser.end) { |
167 |
0 |
167 |
|
| 807 |
167 |
▶ | if (*ctx->parser.data == '"') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data == '"') |
8 |
159 |
167 |
|
| 808 |
8 |
| ret = rfc822_parse_quoted_string(&ctx->parser, ctx->str); |
| 809 |
159 |
▶ | else if (!ctx->non_strict_dots || ctx->non_strict_dots_as_invalid) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (!ctx->non_strict_dots || ctx->non_strict_dots_as_invalid) |
60 |
99 |
159 |
| else if (!ctx->non_strict_dots || ctx->non_strict_dots_as_invalid) |
60 |
0 |
60 |
|
| 810 |
159 |
| ret = rfc822_parse_dot_atom(&ctx->parser, ctx->str, true); |
| 811 |
|
| else |
| 812 |
0 |
| ret = parse_nonstrict_dot_atom(&ctx->parser, ctx->str); |
| 813 |
167 |
▶ | if (ret < 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
Branch: 4 branches
| Branch | True | False | Total |
| if (ret < 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
2 |
165 |
167 |
| if (ret < 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
2 |
2 |
| if (ret < 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
| if (ret < 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
|
| 814 |
2 |
| return -1; |
| 815 |
165 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
0 |
165 |
165 |
|
| 816 |
0 |
| char_problem = true; |
| 817 |
165 |
▶ | if (ctx->parser.data >= ctx->parser.end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end) |
10 |
155 |
165 |
|
| 818 |
10 |
| break; |
| 819 |
155 |
▶ | if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) |
0 |
155 |
155 |
|
| 820 |
0 |
| break; |
| 821 |
155 |
▶ | if (*ctx->parser.data != '.') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data != '.') |
153 |
2 |
155 |
|
| 822 |
153 |
| break; |
| 823 |
2 |
| ctx->parser.data++; |
| 824 |
2 |
▶ | if (ctx->parser.data >= ctx->parser.end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end) { |
0 |
2 |
2 |
|
| 825 |
0 |
| char_problem = true; |
| 826 |
0 |
| break; |
| 827 |
|
| } |
| 828 |
2 |
▶ | if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) |
0 |
2 |
2 |
|
| 829 |
0 |
| break; |
| 830 |
2 |
▶ | if (ctx->parser.data >= ctx->parser.end || *ctx->parser.data == '@') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end || *ctx->parser.data == '@') { |
2 |
0 |
2 |
| if (ctx->parser.data >= ctx->parser.end || *ctx->parser.data == '@') { |
2 |
0 |
2 |
|
| 831 |
2 |
| char_problem = true; |
| 832 |
2 |
| break; |
| 833 |
|
| } |
| 834 |
|
| } |
| 835 |
|
| |
| 836 |
165 |
▶ | if (char_problem || ret < 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (char_problem || ret < 0) |
163 |
2 |
165 |
| if (char_problem || ret < 0) |
0 |
163 |
163 |
|
| 837 |
2 |
| ctx->addr.invalid_syntax = true; |
| 838 |
|
| |
| 839 |
165 |
| ctx->addr.mailbox = str_ccopy(ctx->str); |
| 840 |
165 |
| ctx->addr.mailbox_len = str_len(ctx->str); |
| 841 |
165 |
| return ret; |
| 842 |
|
| } |
| 843 |
|
| |
| 844 |
154 |
| static int parse_domain(struct message_address_parser_context *ctx) |
| 845 |
|
| { |
| 846 |
|
| int ret; |
| 847 |
|
| |
| 848 |
154 |
| str_truncate(ctx->str, 0); |
| 849 |
154 |
▶ | if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) < 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) < 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
1 |
153 |
154 |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) < 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
1 |
1 |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) < 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) < 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
|
| 850 |
1 |
| return -1; |
| 851 |
|
| |
| 852 |
153 |
| ctx->addr.domain = str_ccopy(ctx->str); |
| 853 |
153 |
| ctx->addr.domain_len = str_len(ctx->str); |
| 854 |
153 |
| return ret; |
| 855 |
|
| } |
| 856 |
|
| |
| 857 |
0 |
| static int parse_domain_list(struct message_address_parser_context *ctx) |
| 858 |
|
| { |
| 859 |
|
| int ret; |
| 860 |
|
| bool dot_problem; |
| 861 |
|
| |
| 862 |
|
| /* obs-domain-list = "@" domain *(*(CFWS / "," ) [CFWS] "@" domain) */ |
| 863 |
0 |
| str_truncate(ctx->str, 0); |
| 864 |
0 |
| dot_problem = false; |
| 865 |
|
| for (;;) { |
| 866 |
0 |
▶ | if (ctx->parser.data >= ctx->parser.end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end) |
0 |
0 |
0 |
|
| 867 |
0 |
▶ | return dot_problem ? -2 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return dot_problem ? -2 : 0; |
0 |
0 |
0 |
|
| 868 |
|
| |
| 869 |
0 |
▶ | if (*ctx->parser.data != '@') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data != '@') |
0 |
0 |
0 |
|
| 870 |
0 |
| break; |
| 871 |
|
| |
| 872 |
0 |
▶ | if (str_len(ctx->str) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_len(ctx->str) > 0) |
0 |
0 |
0 |
|
| 873 |
0 |
| str_append_c(ctx->str, ','); |
| 874 |
|
| |
| 875 |
0 |
| str_append_c(ctx->str, '@'); |
| 876 |
0 |
▶ | if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
|
| 877 |
0 |
| return ret; |
| 878 |
|
| |
| 879 |
0 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
0 |
0 |
0 |
|
| 880 |
0 |
| dot_problem = true; |
| 881 |
|
| |
| 882 |
0 |
▶ | while (rfc822_skip_lwsp(&ctx->parser) > 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| while (rfc822_skip_lwsp(&ctx->parser) > 0 && |
0 |
0 |
0 |
|
| 883 |
0 |
▶ | *ctx->parser.data == ',') |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data == ',') |
0 |
0 |
0 |
|
| 884 |
0 |
| ctx->parser.data++; |
| 885 |
|
| } |
| 886 |
0 |
| ctx->addr.route = str_ccopy(ctx->str); |
| 887 |
0 |
| ctx->addr.route_len = str_len(ctx->str); |
| 888 |
0 |
▶ | return dot_problem ? -2 : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| return dot_problem ? -2 : 1; |
0 |
0 |
0 |
|
| 889 |
|
| } |
| 890 |
|
| |
| 891 |
45 |
| static int parse_angle_addr(struct message_address_parser_context *ctx, |
| 892 |
|
| bool parsing_path) |
| 893 |
|
| { |
| 894 |
|
| int ret; |
| 895 |
|
| |
| 896 |
|
| /* "<" [ "@" route ":" ] local-part "@" domain ">" */ |
| 897 |
|
| i_assert(*ctx->parser.data == '<'); |
| 898 |
45 |
| ctx->parser.data++; |
| 899 |
|
| |
| 900 |
45 |
▶ | if (rfc822_skip_lwsp(&ctx->parser) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_skip_lwsp(&ctx->parser) <= 0) |
0 |
45 |
45 |
|
| 901 |
0 |
| return -1; |
| 902 |
|
| |
| 903 |
45 |
▶ | if (*ctx->parser.data == '@') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data == '@') { |
0 |
45 |
45 |
|
| 904 |
0 |
▶ | if ((ret = parse_domain_list(ctx)) > 0 && *ctx->parser.data == ':') { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((ret = parse_domain_list(ctx)) > 0 && *ctx->parser.data == ':') { |
0 |
0 |
0 |
| if ((ret = parse_domain_list(ctx)) > 0 && *ctx->parser.data == ':') { |
0 |
0 |
0 |
|
| 905 |
0 |
| ctx->parser.data++; |
| 906 |
0 |
▶ | } else if (parsing_path && (ctx->parser.data >= ctx->parser.end || *ctx->parser.data != ':')) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (parsing_path && (ctx->parser.data >= ctx->parser.end || *ctx->parser.data != ':')) { |
0 |
0 |
0 |
| } else if (parsing_path && (ctx->parser.data >= ctx->parser.end || *ctx->parser.data != ':')) { |
0 |
0 |
0 |
| } else if (parsing_path && (ctx->parser.data >= ctx->parser.end || *ctx->parser.data != ':')) { |
0 |
0 |
0 |
|
| 907 |
0 |
| return -1; |
| 908 |
|
| } else { |
| 909 |
0 |
▶ | if (ctx->fill_missing && ret != -2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->fill_missing && ret != -2) |
0 |
0 |
0 |
| if (ctx->fill_missing && ret != -2) |
0 |
0 |
0 |
|
| 910 |
0 |
| ctx->addr.route = strdup("INVALID_ROUTE"); |
| 911 |
0 |
| ctx->addr.invalid_syntax = true; |
| 912 |
0 |
▶ | if (ctx->parser.data >= ctx->parser.end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end) |
0 |
0 |
0 |
|
| 913 |
0 |
| return -1; |
| 914 |
0 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
0 |
0 |
0 |
|
| 915 |
0 |
| ctx->parser.data++; |
| 916 |
|
| /* try to continue anyway */ |
| 917 |
|
| } |
| 918 |
0 |
▶ | if (rfc822_skip_lwsp(&ctx->parser) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_skip_lwsp(&ctx->parser) <= 0) |
0 |
0 |
0 |
|
| 919 |
0 |
| return -1; |
| 920 |
|
| } |
| 921 |
|
| |
| 922 |
45 |
▶ | if (*ctx->parser.data == '>') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data == '>') { |
45 |
0 |
45 |
|
| 923 |
|
| /* <> address isn't valid */ |
| 924 |
|
| } else { |
| 925 |
45 |
▶ | if ((ret = parse_local_part(ctx)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((ret = parse_local_part(ctx)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
45 |
45 |
| if ((ret = parse_local_part(ctx)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
| if ((ret = parse_local_part(ctx)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
| if ((ret = parse_local_part(ctx)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
|
| 926 |
0 |
| return -1; |
| 927 |
45 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
0 |
45 |
45 |
|
| 928 |
0 |
| ctx->addr.invalid_syntax = true; |
| 929 |
45 |
▶ | if (ctx->parser.data >= ctx->parser.end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end) |
0 |
45 |
45 |
|
| 930 |
0 |
| return 0; |
| 931 |
45 |
▶ | if (*ctx->parser.data == '@') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data == '@') { |
45 |
0 |
45 |
|
| 932 |
45 |
▶ | if ((ret = parse_domain(ctx)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((ret = parse_domain(ctx)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
45 |
45 |
| if ((ret = parse_domain(ctx)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
| if ((ret = parse_domain(ctx)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
| if ((ret = parse_domain(ctx)) <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) |
0 |
0 |
0 |
|
| 933 |
0 |
| return -1; |
| 934 |
45 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
0 |
45 |
45 |
|
| 935 |
0 |
| ctx->addr.invalid_syntax = true; |
| 936 |
45 |
▶ | if (ctx->parser.data >= ctx->parser.end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end) |
0 |
45 |
45 |
|
| 937 |
0 |
| return 0; |
| 938 |
|
| } |
| 939 |
|
| } |
| 940 |
|
| |
| 941 |
45 |
▶ | if (*ctx->parser.data != '>') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data != '>') |
0 |
45 |
45 |
|
| 942 |
0 |
| return -1; |
| 943 |
45 |
| ctx->parser.data++; |
| 944 |
|
| |
| 945 |
45 |
| return rfc822_skip_lwsp(&ctx->parser); |
| 946 |
|
| } |
| 947 |
|
| |
| 948 |
68 |
| static int parse_name_addr(struct message_address_parser_context *ctx) |
| 949 |
|
| { |
| 950 |
|
| int ret; |
| 951 |
|
| |
| 952 |
|
| /* |
| 953 |
|
| name-addr = [display-name] angle-addr |
| 954 |
|
| display-name = phrase |
| 955 |
|
| */ |
| 956 |
68 |
| str_truncate(ctx->str, 0); |
| 957 |
68 |
| ret = rfc822_parse_phrase(&ctx->parser, ctx->str); |
| 958 |
68 |
▶ | if ((ret <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) || |
Branch: 4 branches
| Branch | True | False | Total |
| if ((ret <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) || |
7 |
61 |
68 |
| if ((ret <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) || |
0 |
7 |
7 |
| if ((ret <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) || |
0 |
0 |
0 |
| if ((ret <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) || |
0 |
0 |
0 |
|
| 959 |
61 |
▶ | *ctx->parser.data != '<') |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data != '<') |
16 |
45 |
61 |
|
| 960 |
23 |
| return -1; |
| 961 |
|
| |
| 962 |
45 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
0 |
45 |
45 |
|
| 963 |
0 |
| ctx->addr.invalid_syntax = true; |
| 964 |
|
| |
| 965 |
45 |
▶ | if (str_len(ctx->str) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_len(ctx->str) == 0) { |
1 |
44 |
45 |
|
| 966 |
|
| /* Cope with "<address>" without display name */ |
| 967 |
1 |
| ctx->addr.name = NULL; |
| 968 |
|
| } else { |
| 969 |
44 |
| ctx->addr.name = str_ccopy(ctx->str); |
| 970 |
44 |
| ctx->addr.name_len = str_len(ctx->str); |
| 971 |
|
| } |
| 972 |
|
| |
| 973 |
45 |
▶ | if (ctx->parser.last_comment != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.last_comment != NULL) |
45 |
0 |
45 |
|
| 974 |
45 |
| str_truncate(ctx->parser.last_comment, 0); |
| 975 |
|
| |
| 976 |
45 |
▶ | if (parse_angle_addr(ctx, false) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_angle_addr(ctx, false) < 0) { |
0 |
45 |
45 |
|
| 977 |
|
| /* broken */ |
| 978 |
0 |
▶ | if (ctx->fill_missing) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->fill_missing) |
0 |
0 |
0 |
|
| 979 |
0 |
| ctx->addr.domain = strdup("SYNTAX_ERROR"); |
| 980 |
0 |
| ctx->addr.invalid_syntax = true; |
| 981 |
|
| } |
| 982 |
|
| |
| 983 |
45 |
▶ | if (ctx->parser.last_comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.last_comment != NULL) { |
45 |
0 |
45 |
|
| 984 |
45 |
▶ | if (str_len(ctx->parser.last_comment) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_len(ctx->parser.last_comment) > 0) { |
7 |
38 |
45 |
|
| 985 |
7 |
| ctx->addr.comment = |
| 986 |
7 |
| str_ccopy(ctx->parser.last_comment); |
| 987 |
7 |
| ctx->addr.comment_len = |
| 988 |
7 |
| str_len(ctx->parser.last_comment); |
| 989 |
|
| } |
| 990 |
|
| } |
| 991 |
|
| |
| 992 |
45 |
| return ctx->parser.data < ctx->parser.end ? 1 : 0; |
| 993 |
|
| } |
| 994 |
|
| |
| 995 |
122 |
| static int parse_addr_spec(struct message_address_parser_context *ctx) |
| 996 |
|
| { |
| 997 |
|
| /* addr-spec = local-part "@" domain */ |
| 998 |
122 |
| int ret, ret2 = -3; |
| 999 |
|
| |
| 1000 |
|
| i_assert(ctx->parser.data < ctx->parser.end); |
| 1001 |
|
| |
| 1002 |
122 |
▶ | if (ctx->parser.last_comment != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.last_comment != NULL) |
23 |
99 |
122 |
|
| 1003 |
23 |
| str_truncate(ctx->parser.last_comment, 0); |
| 1004 |
|
| |
| 1005 |
|
| #if 0 |
| 1006 |
|
| bool quoted_string = *ctx->parser.data == '"'; |
| 1007 |
|
| #endif |
| 1008 |
122 |
| ret = parse_local_part(ctx); |
| 1009 |
122 |
▶ | if (ret <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret <= 0) { |
12 |
110 |
122 |
|
| 1010 |
|
| /* end of input or parsing local-part failed */ |
| 1011 |
12 |
| ctx->addr.invalid_syntax = true; |
| 1012 |
|
| } |
| 1013 |
122 |
▶ | if (ret != 0 && ctx->parser.data < ctx->parser.end && |
Branch: 2 branches
| Branch | True | False | Total |
| if (ret != 0 && ctx->parser.data < ctx->parser.end && |
112 |
10 |
122 |
| if (ret != 0 && ctx->parser.data < ctx->parser.end && |
111 |
1 |
112 |
|
| 1014 |
111 |
▶ | *ctx->parser.data == '@') { |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data == '@') { |
109 |
2 |
111 |
|
| 1015 |
109 |
| ret2 = parse_domain(ctx); |
| 1016 |
109 |
▶ | if (ret2 <= 0 && ret != -2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ret2 <= 0 && ret != -2) |
104 |
5 |
109 |
| if (ret2 <= 0 && ret != -2) |
104 |
0 |
104 |
|
| 1017 |
104 |
| ret = ret2; |
| 1018 |
109 |
▶ | if (ret2 == -2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret2 == -2) { |
0 |
109 |
109 |
|
| 1019 |
0 |
| ctx->addr.invalid_syntax = true; |
| 1020 |
0 |
▶ | if (ctx->parser.data >= ctx->parser.end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end) |
0 |
0 |
0 |
|
| 1021 |
0 |
| ret = 0; |
| 1022 |
|
| } |
| 1023 |
|
| } |
| 1024 |
|
| |
| 1025 |
122 |
▶ | if (ctx->parser.last_comment != NULL && str_len(ctx->parser.last_comment) > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->parser.last_comment != NULL && str_len(ctx->parser.last_comment) > 0) { |
23 |
99 |
122 |
| if (ctx->parser.last_comment != NULL && str_len(ctx->parser.last_comment) > 0) { |
3 |
20 |
23 |
|
| 1026 |
3 |
| ctx->addr.comment = str_ccopy(ctx->parser.last_comment); |
| 1027 |
3 |
| ctx->addr.comment_len = str_len(ctx->parser.last_comment); |
| 1028 |
|
| } else if (ret2 == -3) { |
| 1029 |
|
| #if 0 |
| 1030 |
|
| /* So far we've read user without @domain and without |
| 1031 |
|
| (Display Name). We'll assume that a single "user" (already |
| 1032 |
|
| read into addr.mailbox) is a mailbox, but if it's followed |
| 1033 |
|
| by anything else it's a display-name. */ |
| 1034 |
|
| str_append_c(ctx->str, ' '); |
| 1035 |
|
| size_t orig_str_len = str_len(ctx->str); |
| 1036 |
|
| (void)rfc822_parse_phrase(&ctx->parser, ctx->str); |
| 1037 |
|
| if (str_len(ctx->str) != orig_str_len) { |
| 1038 |
|
| ctx->addr.mailbox = NULL; |
| 1039 |
|
| ctx->addr.name = str_ccopy(ctx->str); |
| 1040 |
|
| ctx->addr.name_len = str_len(ctx->str); |
| 1041 |
|
| } else { |
| 1042 |
|
| if (!quoted_string) |
| 1043 |
|
| ctx->addr.domain = strdup(""); |
| 1044 |
|
| } |
| 1045 |
|
| ctx->addr.invalid_syntax = true; |
| 1046 |
|
| ret = -1; |
| 1047 |
|
| #endif |
| 1048 |
|
| } |
| 1049 |
122 |
| return ret; |
| 1050 |
|
| } |
| 1051 |
|
| |
| 1052 |
68 |
| static void add_fixed_address(struct message_address_parser_context *ctx) |
| 1053 |
|
| { |
| 1054 |
68 |
▶ | if (ctx->addr.mailbox == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.mailbox == NULL) { |
7 |
61 |
68 |
|
| 1055 |
7 |
▶ | ctx->addr.mailbox = strdup(!ctx->fill_missing ? "" : "MISSING_MAILBOX"); |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->addr.mailbox = strdup(!ctx->fill_missing ? "" : "MISSING_MAILBOX"); |
7 |
0 |
7 |
|
| 1056 |
7 |
| ctx->addr.invalid_syntax = true; |
| 1057 |
|
| } |
| 1058 |
68 |
▶ | if (ctx->addr.domain == NULL || ctx->addr.domain_len == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->addr.domain == NULL || ctx->addr.domain_len == 0) { |
60 |
8 |
68 |
| if (ctx->addr.domain == NULL || ctx->addr.domain_len == 0) { |
0 |
60 |
60 |
|
| 1059 |
8 |
| free(ctx->addr.domain); |
| 1060 |
8 |
▶ | ctx->addr.domain = strdup(!ctx->fill_missing ? "" : "MISSING_DOMAIN"); |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->addr.domain = strdup(!ctx->fill_missing ? "" : "MISSING_DOMAIN"); |
8 |
0 |
8 |
|
| 1061 |
8 |
| ctx->addr.invalid_syntax = true; |
| 1062 |
|
| } |
| 1063 |
68 |
| add_address(ctx); |
| 1064 |
68 |
| } |
| 1065 |
|
| |
| 1066 |
68 |
| static int parse_mailbox(struct message_address_parser_context *ctx) |
| 1067 |
|
| { |
| 1068 |
|
| const unsigned char *start; |
| 1069 |
|
| size_t len; |
| 1070 |
|
| int ret; |
| 1071 |
|
| |
| 1072 |
|
| /* mailbox = name-addr / addr-spec */ |
| 1073 |
68 |
| start = ctx->parser.data; |
| 1074 |
68 |
▶ | if ((ret = parse_name_addr(ctx)) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = parse_name_addr(ctx)) < 0) { |
23 |
45 |
68 |
|
| 1075 |
|
| /* nope, should be addr-spec */ |
| 1076 |
23 |
▶ | if (ctx->addr.name != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.name != NULL) { |
0 |
23 |
23 |
|
| 1077 |
0 |
| free(ctx->addr.name); |
| 1078 |
0 |
| ctx->addr.name = NULL; |
| 1079 |
|
| } |
| 1080 |
23 |
▶ | if (ctx->addr.route != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.route != NULL) { |
0 |
23 |
23 |
|
| 1081 |
0 |
| free(ctx->addr.route); |
| 1082 |
0 |
| ctx->addr.route = NULL; |
| 1083 |
|
| } |
| 1084 |
23 |
▶ | if (ctx->addr.mailbox != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.mailbox != NULL) { |
0 |
23 |
23 |
|
| 1085 |
0 |
| free(ctx->addr.mailbox); |
| 1086 |
0 |
| ctx->addr.mailbox = NULL; |
| 1087 |
|
| } |
| 1088 |
23 |
▶ | if (ctx->addr.domain != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.domain != NULL) { |
0 |
23 |
23 |
|
| 1089 |
0 |
| free(ctx->addr.domain); |
| 1090 |
0 |
| ctx->addr.domain = NULL; |
| 1091 |
|
| } |
| 1092 |
23 |
▶ | if (ctx->addr.comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.comment != NULL) { |
0 |
23 |
23 |
|
| 1093 |
0 |
| free(ctx->addr.comment); |
| 1094 |
0 |
| ctx->addr.comment = NULL; |
| 1095 |
|
| } |
| 1096 |
23 |
▶ | if (ctx->addr.original != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.original != NULL) { |
0 |
23 |
23 |
|
| 1097 |
0 |
| free(ctx->addr.original); |
| 1098 |
0 |
| ctx->addr.original = NULL; |
| 1099 |
|
| } |
| 1100 |
23 |
| ctx->parser.data = start; |
| 1101 |
23 |
| ret = parse_addr_spec(ctx); |
| 1102 |
23 |
▶ | if (ctx->addr.invalid_syntax && ctx->addr.name == NULL && |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->addr.invalid_syntax && ctx->addr.name == NULL && |
8 |
15 |
23 |
| if (ctx->addr.invalid_syntax && ctx->addr.name == NULL && |
8 |
0 |
8 |
|
| 1103 |
8 |
▶ | ctx->addr.mailbox != NULL && ctx->addr.domain == NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| ctx->addr.mailbox != NULL && ctx->addr.domain == NULL) { |
7 |
1 |
8 |
| ctx->addr.mailbox != NULL && ctx->addr.domain == NULL) { |
6 |
1 |
7 |
|
| 1104 |
6 |
| ctx->addr.name = ctx->addr.mailbox; |
| 1105 |
6 |
| ctx->addr.name_len = ctx->addr.mailbox_len; |
| 1106 |
6 |
| ctx->addr.mailbox = NULL; |
| 1107 |
6 |
| ctx->addr.mailbox_len = 0; |
| 1108 |
|
| } |
| 1109 |
|
| } |
| 1110 |
|
| |
| 1111 |
68 |
▶ | if (ret < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) |
2 |
66 |
68 |
|
| 1112 |
2 |
| ctx->addr.invalid_syntax = true; |
| 1113 |
|
| |
| 1114 |
68 |
| len = ctx->parser.data - start; |
| 1115 |
68 |
| ctx->addr.original = malloc(len + 1); |
| 1116 |
68 |
▶ | if (!ctx->addr.original) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx->addr.original) |
0 |
68 |
68 |
|
| 1117 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 1118 |
|
| |
| 1119 |
68 |
| memcpy(ctx->addr.original, start, len); |
| 1120 |
68 |
| ctx->addr.original[len] = 0; |
| 1121 |
68 |
| ctx->addr.original_len = len; |
| 1122 |
|
| |
| 1123 |
68 |
| add_fixed_address(ctx); |
| 1124 |
|
| |
| 1125 |
68 |
| free(ctx->addr.original); |
| 1126 |
68 |
| ctx->addr.original = NULL; |
| 1127 |
68 |
| return ret; |
| 1128 |
|
| } |
| 1129 |
|
| |
| 1130 |
67 |
| static int parse_group(struct message_address_parser_context *ctx) |
| 1131 |
|
| { |
| 1132 |
|
| int ret; |
| 1133 |
|
| |
| 1134 |
|
| /* |
| 1135 |
|
| group = display-name ":" [mailbox-list / CFWS] ";" [CFWS] |
| 1136 |
|
| display-name = phrase |
| 1137 |
|
| */ |
| 1138 |
67 |
| str_truncate(ctx->str, 0); |
| 1139 |
67 |
| ret = rfc822_parse_phrase(&ctx->parser, ctx->str); |
| 1140 |
67 |
▶ | if ((ret <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) || |
Branch: 4 branches
| Branch | True | False | Total |
| if ((ret <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) || |
7 |
60 |
67 |
| if ((ret <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) || |
0 |
7 |
7 |
| if ((ret <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) || |
0 |
0 |
0 |
| if ((ret <= 0 && (ret != -2 || (!ctx->non_strict_dots && !ctx->non_strict_dots_as_invalid))) || |
0 |
0 |
0 |
|
| 1141 |
60 |
▶ | *ctx->parser.data != ':') |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data != ':') |
51 |
9 |
60 |
|
| 1142 |
58 |
| return -1; |
| 1143 |
|
| |
| 1144 |
9 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
0 |
9 |
9 |
|
| 1145 |
0 |
| ctx->addr.invalid_syntax = true; |
| 1146 |
|
| |
| 1147 |
|
| /* from now on don't return -1 even if there are problems, so that |
| 1148 |
|
| the caller knows this is a group */ |
| 1149 |
9 |
| ctx->parser.data++; |
| 1150 |
9 |
▶ | if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) |
0 |
9 |
9 |
|
| 1151 |
0 |
| ctx->addr.invalid_syntax = true; |
| 1152 |
|
| |
| 1153 |
9 |
| ctx->addr.mailbox = str_ccopy(ctx->str); |
| 1154 |
9 |
| ctx->addr.mailbox_len = str_len(ctx->str); |
| 1155 |
9 |
| add_address(ctx); |
| 1156 |
|
| |
| 1157 |
9 |
▶ | if (ret > 0 && *ctx->parser.data != ';') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ret > 0 && *ctx->parser.data != ';') { |
9 |
0 |
9 |
| if (ret > 0 && *ctx->parser.data != ';') { |
7 |
2 |
9 |
|
| 1158 |
|
| for (;;) { |
| 1159 |
|
| /* mailbox-list = |
| 1160 |
|
| (mailbox *("," mailbox)) / obs-mbox-list */ |
| 1161 |
10 |
| if (parse_mailbox(ctx) <= 0) { |
| 1162 |
|
| /* broken mailbox - try to continue anyway. */ |
| 1163 |
|
| } |
| 1164 |
10 |
▶ | if (ctx->parser.data >= ctx->parser.end || |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end || |
9 |
1 |
10 |
|
| 1165 |
9 |
▶ | *ctx->parser.data != ',') |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data != ',') |
3 |
6 |
9 |
|
| 1166 |
|
| break; |
| 1167 |
3 |
| ctx->parser.data++; |
| 1168 |
3 |
▶ | if (rfc822_skip_lwsp(&ctx->parser) <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_skip_lwsp(&ctx->parser) <= 0) { |
0 |
3 |
3 |
|
| 1169 |
0 |
| ret = -1; |
| 1170 |
0 |
| break; |
| 1171 |
|
| } |
| 1172 |
|
| } |
| 1173 |
|
| } |
| 1174 |
9 |
▶ | if (ret >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret >= 0) { |
9 |
0 |
9 |
|
| 1175 |
9 |
▶ | if (ctx->parser.data >= ctx->parser.end || |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end || |
8 |
1 |
9 |
|
| 1176 |
8 |
▶ | *ctx->parser.data != ';') |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data != ';') |
0 |
8 |
8 |
|
| 1177 |
1 |
| ret = -1; |
| 1178 |
|
| else { |
| 1179 |
8 |
| ctx->parser.data++; |
| 1180 |
8 |
| ret = rfc822_skip_lwsp(&ctx->parser); |
| 1181 |
|
| } |
| 1182 |
|
| } |
| 1183 |
9 |
▶ | if (ret < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) |
1 |
8 |
9 |
|
| 1184 |
1 |
| ctx->addr.invalid_syntax = true; |
| 1185 |
|
| |
| 1186 |
9 |
| add_address(ctx); |
| 1187 |
9 |
| return ret == 0 ? 0 : 1; |
| 1188 |
|
| } |
| 1189 |
|
| |
| 1190 |
67 |
| static int parse_address(struct message_address_parser_context *ctx) |
| 1191 |
|
| { |
| 1192 |
|
| const unsigned char *start; |
| 1193 |
|
| int ret; |
| 1194 |
|
| |
| 1195 |
|
| /* address = mailbox / group */ |
| 1196 |
67 |
| start = ctx->parser.data; |
| 1197 |
67 |
▶ | if ((ret = parse_group(ctx)) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = parse_group(ctx)) < 0) { |
58 |
9 |
67 |
|
| 1198 |
|
| /* not a group, try mailbox */ |
| 1199 |
58 |
| ctx->parser.data = start; |
| 1200 |
58 |
| ret = parse_mailbox(ctx); |
| 1201 |
|
| } |
| 1202 |
67 |
| return ret; |
| 1203 |
|
| } |
| 1204 |
|
| |
| 1205 |
41 |
| static int parse_address_list(struct message_address_parser_context *ctx, |
| 1206 |
|
| unsigned int max_addresses) |
| 1207 |
|
| { |
| 1208 |
|
| const unsigned char *start; |
| 1209 |
|
| size_t len; |
| 1210 |
41 |
| int ret = 0; |
| 1211 |
|
| |
| 1212 |
|
| /* address-list = (address *("," address)) / obs-addr-list */ |
| 1213 |
67 |
▶ | while (max_addresses > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (max_addresses > 0) { |
67 |
0 |
67 |
|
| 1214 |
67 |
| max_addresses--; |
| 1215 |
67 |
▶ | if ((ret = parse_address(ctx)) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = parse_address(ctx)) == 0) |
37 |
30 |
67 |
|
| 1216 |
37 |
| break; |
| 1217 |
30 |
▶ | if (ctx->parser.data >= ctx->parser.end || |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end || |
27 |
3 |
30 |
|
| 1218 |
27 |
▶ | *ctx->parser.data != ',') { |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data != ',') { |
1 |
26 |
27 |
|
| 1219 |
4 |
| ctx->last_addr->invalid_syntax = true; |
| 1220 |
4 |
| ret = -1; |
| 1221 |
4 |
| break; |
| 1222 |
|
| } |
| 1223 |
26 |
| ctx->parser.data++; |
| 1224 |
26 |
| start = ctx->parser.data; |
| 1225 |
26 |
▶ | if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) { |
0 |
26 |
26 |
|
| 1226 |
0 |
▶ | if (ret < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) { |
0 |
0 |
0 |
|
| 1227 |
|
| /* ends with some garbage */ |
| 1228 |
0 |
| len = ctx->parser.data - start; |
| 1229 |
0 |
| ctx->addr.original = malloc(len + 1); |
| 1230 |
0 |
▶ | if (!ctx->addr.original) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx->addr.original) |
0 |
0 |
0 |
|
| 1231 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 1232 |
|
| |
| 1233 |
0 |
| memcpy(ctx->addr.original, start, len); |
| 1234 |
0 |
| ctx->addr.original[len] = 0; |
| 1235 |
0 |
| ctx->addr.original_len = len; |
| 1236 |
|
| |
| 1237 |
0 |
| add_fixed_address(ctx); |
| 1238 |
|
| |
| 1239 |
0 |
| free(ctx->addr.original); |
| 1240 |
0 |
| ctx->addr.original = NULL; |
| 1241 |
|
| } |
| 1242 |
0 |
| break; |
| 1243 |
|
| } |
| 1244 |
|
| } |
| 1245 |
41 |
| return ret; |
| 1246 |
|
| } |
| 1247 |
|
| |
| 1248 |
279 |
| static char *mem_copy(const char *mem, size_t len) |
| 1249 |
|
| { |
| 1250 |
|
| char *copy; |
| 1251 |
|
| |
| 1252 |
279 |
| copy = malloc(len+1); |
| 1253 |
279 |
▶ | if (!copy) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!copy) |
0 |
279 |
279 |
|
| 1254 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 1255 |
|
| |
| 1256 |
279 |
| memcpy(copy, mem, len); |
| 1257 |
279 |
| copy[len] = 0; |
| 1258 |
279 |
| return copy; |
| 1259 |
|
| } |
| 1260 |
|
| |
| 1261 |
130 |
| void message_address_add(struct message_address **first, struct message_address **last, |
| 1262 |
|
| const char *name, size_t name_len, const char *route, size_t route_len, |
| 1263 |
|
| const char *mailbox, size_t mailbox_len, const char *domain, size_t domain_len, |
| 1264 |
|
| const char *comment, size_t comment_len) |
| 1265 |
|
| { |
| 1266 |
|
| struct message_address *message; |
| 1267 |
|
| |
| 1268 |
130 |
| message = malloc(sizeof(struct message_address)); |
| 1269 |
130 |
▶ | if (!message) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!message) |
0 |
130 |
130 |
|
| 1270 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 1271 |
|
| |
| 1272 |
130 |
▶ | message->name = name ? mem_copy(name, name_len) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| message->name = name ? mem_copy(name, name_len) : NULL; |
61 |
69 |
130 |
|
| 1273 |
130 |
| message->name_len = name_len; |
| 1274 |
130 |
▶ | message->route = route ? mem_copy(route, route_len) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| message->route = route ? mem_copy(route, route_len) : NULL; |
0 |
130 |
130 |
|
| 1275 |
130 |
| message->route_len = route_len; |
| 1276 |
130 |
▶ | message->mailbox = mailbox ? mem_copy(mailbox, mailbox_len) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| message->mailbox = mailbox ? mem_copy(mailbox, mailbox_len) : NULL; |
109 |
21 |
130 |
|
| 1277 |
130 |
| message->mailbox_len = mailbox_len; |
| 1278 |
130 |
▶ | message->domain = domain ? mem_copy(domain, domain_len) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| message->domain = domain ? mem_copy(domain, domain_len) : NULL; |
88 |
42 |
130 |
|
| 1279 |
130 |
| message->domain_len = domain_len; |
| 1280 |
130 |
▶ | message->comment = comment ? mem_copy(comment, comment_len) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| message->comment = comment ? mem_copy(comment, comment_len) : NULL; |
21 |
109 |
130 |
|
| 1281 |
130 |
| message->comment_len = comment_len; |
| 1282 |
130 |
| message->original = NULL; |
| 1283 |
130 |
| message->original_len = 0; |
| 1284 |
130 |
| message->next = NULL; |
| 1285 |
|
| |
| 1286 |
130 |
▶ | if (!*first) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*first) |
66 |
64 |
130 |
|
| 1287 |
66 |
| *first = message; |
| 1288 |
|
| else |
| 1289 |
64 |
| (*last)->next = message; |
| 1290 |
|
| |
| 1291 |
130 |
| *last = message; |
| 1292 |
130 |
| } |
| 1293 |
|
| |
| 1294 |
133 |
| void message_address_free(struct message_address **addr) |
| 1295 |
|
| { |
| 1296 |
|
| struct message_address *current; |
| 1297 |
|
| struct message_address *next; |
| 1298 |
|
| |
| 1299 |
133 |
| current = *addr; |
| 1300 |
|
| |
| 1301 |
349 |
▶ | while (current) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (current) { |
216 |
133 |
349 |
|
| 1302 |
216 |
| next = current->next; |
| 1303 |
216 |
| free(current->name); |
| 1304 |
216 |
| free(current->route); |
| 1305 |
216 |
| free(current->mailbox); |
| 1306 |
216 |
| free(current->domain); |
| 1307 |
216 |
| free(current->comment); |
| 1308 |
216 |
| free(current->original); |
| 1309 |
216 |
| free(current); |
| 1310 |
216 |
| current = next; |
| 1311 |
|
| } |
| 1312 |
|
| |
| 1313 |
133 |
| *addr = NULL; |
| 1314 |
133 |
| } |
| 1315 |
|
| |
| 1316 |
|
| struct message_address * |
| 1317 |
49 |
| message_address_parse(const char *input, size_t input_len, |
| 1318 |
|
| unsigned int max_addresses, |
| 1319 |
|
| enum message_address_parse_flags flags) |
| 1320 |
|
| { |
| 1321 |
|
| string_t *str; |
| 1322 |
|
| struct message_address_parser_context ctx; |
| 1323 |
|
| |
| 1324 |
49 |
| memset(&ctx, 0, sizeof(ctx)); |
| 1325 |
|
| |
| 1326 |
49 |
| str = str_new(128); |
| 1327 |
|
| |
| 1328 |
49 |
| rfc822_parser_init(&ctx.parser, (const unsigned char *)input, input_len, str); |
| 1329 |
|
| |
| 1330 |
49 |
▶ | if (rfc822_skip_lwsp(&ctx.parser) <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_skip_lwsp(&ctx.parser) <= 0) { |
8 |
41 |
49 |
|
| 1331 |
|
| /* no addresses */ |
| 1332 |
8 |
| str_free(&str); |
| 1333 |
8 |
| return NULL; |
| 1334 |
|
| } |
| 1335 |
|
| |
| 1336 |
41 |
| ctx.str = str_new(128); |
| 1337 |
41 |
| ctx.fill_missing = (flags & MESSAGE_ADDRESS_PARSE_FLAG_FILL_MISSING) != 0; |
| 1338 |
41 |
| ctx.non_strict_dots = (flags & MESSAGE_ADDRESS_PARSE_FLAG_STRICT_DOTS) == 0; |
| 1339 |
41 |
| ctx.non_strict_dots_as_invalid = (flags & MESSAGE_ADDRESS_PARSE_FLAG_NON_STRICT_DOTS_AS_INVALID) != 0; |
| 1340 |
|
| |
| 1341 |
41 |
| (void)parse_address_list(&ctx, max_addresses); |
| 1342 |
|
| |
| 1343 |
41 |
| str_free(&ctx.str); |
| 1344 |
41 |
| str_free(&str); |
| 1345 |
|
| |
| 1346 |
41 |
| rfc822_parser_deinit(&ctx.parser); |
| 1347 |
|
| |
| 1348 |
41 |
| return ctx.first_addr; |
| 1349 |
|
| } |
| 1350 |
|
| |
| 1351 |
81 |
| static bool has_only_mime_with_unquoted_words(const char *data, size_t len) |
| 1352 |
|
| { |
| 1353 |
|
| const char *p; |
| 1354 |
81 |
| const char *end = data + len; |
| 1355 |
81 |
| size_t qmarks = 0; |
| 1356 |
81 |
| bool in_mime_word = false; |
| 1357 |
81 |
| bool in_word = false; |
| 1358 |
81 |
| bool has_mime_word = false; |
| 1359 |
|
| |
| 1360 |
|
| /* Do few simple checks: |
| 1361 |
|
| * - input contains only atext and spaces (valid for MIME) |
| 1362 |
|
| * - input does not contain '"' and '\\' (so no quoted words) |
| 1363 |
|
| * - input contains at least one MIME word |
| 1364 |
|
| * - every MIME word starts with "=?" |
| 1365 |
|
| * - every MIME word ends with "?=" |
| 1366 |
|
| * - every MIME word has four '?' |
| 1367 |
|
| * - words are separated by spaces |
| 1368 |
|
| */ |
| 1369 |
1119 |
▶ | for (p = data; p != end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = data; p != end; p++) { |
1051 |
68 |
1119 |
|
| 1370 |
1051 |
▶ | if (!IS_ATEXT(*p) && *p != ' ') |
Branch: 2 branches
| Branch | True | False | Total |
| if (!IS_ATEXT(*p) && *p != ' ') |
54 |
997 |
1051 |
| if (!IS_ATEXT(*p) && *p != ' ') |
12 |
42 |
54 |
|
| 1371 |
12 |
| return false; |
| 1372 |
1039 |
▶ | if (*p == '"' || *p == '\\') |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '"' || *p == '\\') |
1039 |
0 |
1039 |
| if (*p == '"' || *p == '\\') |
0 |
1039 |
1039 |
|
| 1373 |
0 |
| return false; |
| 1374 |
1039 |
▶ | if (*p != ' ' && !in_word) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p != ' ' && !in_word) { |
997 |
42 |
1039 |
| if (*p != ' ' && !in_word) { |
117 |
880 |
997 |
|
| 1375 |
117 |
| in_word = true; |
| 1376 |
117 |
| qmarks = 0; |
| 1377 |
117 |
▶ | if (p+1 < end && *p == '=' && *(p+1) == '?') |
Branch: 3 branches
| Branch | True | False | Total |
| if (p+1 < end && *p == '=' && *(p+1) == '?') |
117 |
0 |
117 |
| if (p+1 < end && *p == '=' && *(p+1) == '?') |
18 |
99 |
117 |
| if (p+1 < end && *p == '=' && *(p+1) == '?') |
18 |
0 |
18 |
|
| 1378 |
18 |
| in_mime_word = true; |
| 1379 |
922 |
▶ | } else if (*p == ' ' && in_word) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (*p == ' ' && in_word) { |
42 |
880 |
922 |
| } else if (*p == ' ' && in_word) { |
42 |
0 |
42 |
|
| 1380 |
42 |
| in_word = false; |
| 1381 |
42 |
▶ | if (in_mime_word) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_mime_word) { |
11 |
31 |
42 |
|
| 1382 |
11 |
▶ | if (data > p-2 || *(p-2) != '?' || *(p-1) != '=') |
Branch: 3 branches
| Branch | True | False | Total |
| if (data > p-2 || *(p-2) != '?' || *(p-1) != '=') |
11 |
0 |
11 |
| if (data > p-2 || *(p-2) != '?' || *(p-1) != '=') |
10 |
1 |
11 |
| if (data > p-2 || *(p-2) != '?' || *(p-1) != '=') |
0 |
10 |
10 |
|
| 1383 |
1 |
| return false; |
| 1384 |
10 |
▶ | if (qmarks != 4) |
Branch: 1 branch
| Branch | True | False | Total |
| if (qmarks != 4) |
0 |
10 |
10 |
|
| 1385 |
0 |
| return false; |
| 1386 |
10 |
| has_mime_word = true; |
| 1387 |
10 |
| in_mime_word = false; |
| 1388 |
|
| } |
| 1389 |
|
| } |
| 1390 |
1038 |
▶ | if (*p == '?') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '?') |
69 |
969 |
1038 |
|
| 1391 |
69 |
| qmarks++; |
| 1392 |
|
| } |
| 1393 |
|
| |
| 1394 |
68 |
▶ | if (in_mime_word) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_mime_word) { |
7 |
61 |
68 |
|
| 1395 |
7 |
▶ | if (len <= 2 || *(end-2) != '?' || *(end-1) != '=') |
Branch: 3 branches
| Branch | True | False | Total |
| if (len <= 2 || *(end-2) != '?' || *(end-1) != '=') |
7 |
0 |
7 |
| if (len <= 2 || *(end-2) != '?' || *(end-1) != '=') |
7 |
0 |
7 |
| if (len <= 2 || *(end-2) != '?' || *(end-1) != '=') |
0 |
7 |
7 |
|
| 1396 |
0 |
| return false; |
| 1397 |
7 |
▶ | if (qmarks != 4) |
Branch: 1 branch
| Branch | True | False | Total |
| if (qmarks != 4) |
0 |
7 |
7 |
|
| 1398 |
0 |
| return false; |
| 1399 |
7 |
| has_mime_word = true; |
| 1400 |
|
| } |
| 1401 |
|
| |
| 1402 |
68 |
| return has_mime_word; |
| 1403 |
|
| } |
| 1404 |
|
| |
| 1405 |
84 |
| void message_address_write(char **output, size_t *output_len, const struct message_address *addr) |
| 1406 |
|
| { |
| 1407 |
|
| string_t *str; |
| 1408 |
|
| const char *tmp; |
| 1409 |
84 |
| bool first = true, in_group = false; |
| 1410 |
|
| |
| 1411 |
84 |
| str = str_new(128); |
| 1412 |
|
| |
| 1413 |
|
| #if 0 |
| 1414 |
|
| if (addr == NULL) |
| 1415 |
|
| return; |
| 1416 |
|
| |
| 1417 |
|
| /* <> path */ |
| 1418 |
|
| if (addr->mailbox == NULL && addr->domain == NULL) { |
| 1419 |
|
| i_assert(addr->next == NULL); |
| 1420 |
|
| str_append(str, "<>"); |
| 1421 |
|
| return; |
| 1422 |
|
| } |
| 1423 |
|
| #endif |
| 1424 |
|
| |
| 1425 |
|
| /* a) mailbox@domain |
| 1426 |
|
| b) name <@route:mailbox@domain> |
| 1427 |
|
| c) group: .. ; */ |
| 1428 |
|
| |
| 1429 |
214 |
▶ | while (addr != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (addr != NULL) { |
130 |
84 |
214 |
|
| 1430 |
130 |
▶ | if (first) |
Branch: 1 branch
| Branch | True | False | Total |
| if (first) |
87 |
43 |
130 |
|
| 1431 |
87 |
| first = false; |
| 1432 |
|
| else |
| 1433 |
43 |
| str_append(str, ", "); |
| 1434 |
|
| |
| 1435 |
130 |
▶ | if (addr->domain == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->domain == NULL) { |
42 |
88 |
130 |
|
| 1436 |
42 |
▶ | if (!in_group) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in_group) { |
21 |
21 |
42 |
|
| 1437 |
|
| /* beginning of group. mailbox is the group |
| 1438 |
|
| name, others are NULL. */ |
| 1439 |
41 |
▶ | if (addr->mailbox != NULL && addr->mailbox_len != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (addr->mailbox != NULL && addr->mailbox_len != 0) { |
21 |
0 |
21 |
| if (addr->mailbox != NULL && addr->mailbox_len != 0) { |
20 |
1 |
21 |
|
| 1440 |
|
| /* check if the input has only MIME 'encoded-word's |
| 1441 |
|
| with possible additional unquoted words. |
| 1442 |
|
| MIME encoded-word MUST NOT appear within a 'quoted-string' |
| 1443 |
|
| so escaping and quoting of phrase is not possible, |
| 1444 |
|
| group name can contain more words separated by spaces */ |
| 1445 |
20 |
| bool quote_space = !has_only_mime_with_unquoted_words(addr->mailbox, addr->mailbox_len); |
| 1446 |
20 |
| str_append_maybe_escape(str, addr->mailbox, addr->mailbox_len, true, quote_space); |
| 1447 |
|
| } else { |
| 1448 |
|
| /* empty group name needs to be quoted */ |
| 1449 |
1 |
| str_append(str, "\"\""); |
| 1450 |
|
| } |
| 1451 |
21 |
| str_append(str, ": "); |
| 1452 |
21 |
| first = true; |
| 1453 |
|
| } else { |
| 1454 |
|
| /* end of group. all fields should be NULL. */ |
| 1455 |
|
| i_assert(addr->mailbox == NULL); |
| 1456 |
|
| |
| 1457 |
|
| /* cut out the ", " */ |
| 1458 |
21 |
| tmp = str_c(str)+str_len(str)-2; |
| 1459 |
|
| i_assert((tmp[0] == ',' || tmp[0] == ':') && tmp[1] == ' '); |
| 1460 |
21 |
▶ | if (tmp[0] == ',' && tmp[1] == ' ') |
Branch: 2 branches
| Branch | True | False | Total |
| if (tmp[0] == ',' && tmp[1] == ' ') |
15 |
6 |
21 |
| if (tmp[0] == ',' && tmp[1] == ' ') |
15 |
0 |
15 |
|
| 1461 |
15 |
| str_truncate(str, str_len(str)-2); |
| 1462 |
6 |
▶ | else if (tmp[0] == ':' && tmp[1] == ' ') |
Branch: 2 branches
| Branch | True | False | Total |
| else if (tmp[0] == ':' && tmp[1] == ' ') |
6 |
0 |
6 |
| else if (tmp[0] == ':' && tmp[1] == ' ') |
6 |
0 |
6 |
|
| 1463 |
6 |
| str_truncate(str, str_len(str)-1); |
| 1464 |
21 |
| str_append_c(str, ';'); |
| 1465 |
|
| } |
| 1466 |
|
| |
| 1467 |
42 |
| in_group = !in_group; |
| 1468 |
88 |
▶ | } else if ((addr->name == NULL || addr->name_len == 0) && |
Branch: 2 branches
| Branch | True | False | Total |
| } else if ((addr->name == NULL || addr->name_len == 0) && |
61 |
27 |
88 |
| } else if ((addr->name == NULL || addr->name_len == 0) && |
0 |
61 |
61 |
|
| 1469 |
27 |
▶ | addr->route == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| addr->route == NULL) { |
27 |
0 |
27 |
|
| 1470 |
|
| /* no name and no route. use only mailbox@domain */ |
| 1471 |
|
| i_assert(addr->mailbox != NULL); |
| 1472 |
|
| |
| 1473 |
27 |
| str_append_maybe_escape(str, addr->mailbox, addr->mailbox_len, false, true); |
| 1474 |
27 |
| str_append_c(str, '@'); |
| 1475 |
27 |
| str_append_data(str, addr->domain, addr->domain_len); |
| 1476 |
|
| |
| 1477 |
27 |
▶ | if (addr->comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->comment != NULL) { |
2 |
25 |
27 |
|
| 1478 |
2 |
| str_append(str, " ("); |
| 1479 |
2 |
| str_append_data(str, addr->comment, addr->comment_len); |
| 1480 |
2 |
| str_append_c(str, ')'); |
| 1481 |
|
| } |
| 1482 |
|
| } else { |
| 1483 |
|
| /* name and/or route. use full <mailbox@domain> Name */ |
| 1484 |
|
| i_assert(addr->mailbox != NULL); |
| 1485 |
|
| |
| 1486 |
61 |
▶ | if (addr->name != NULL && addr->name_len != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (addr->name != NULL && addr->name_len != 0) { |
61 |
0 |
61 |
| if (addr->name != NULL && addr->name_len != 0) { |
61 |
0 |
61 |
|
| 1487 |
|
| /* check if the input has only MIME 'encoded-word's |
| 1488 |
|
| with possible additional unquoted words. |
| 1489 |
|
| MIME encoded-word MUST NOT appear within a 'quoted-string' |
| 1490 |
|
| so escaping and quoting of phrase is not possible, |
| 1491 |
|
| display-name can contain more words separated by spaces */ |
| 1492 |
61 |
| bool quote_space = !has_only_mime_with_unquoted_words(addr->name, addr->name_len); |
| 1493 |
61 |
| str_append_maybe_escape(str, addr->name, addr->name_len, true, quote_space); |
| 1494 |
|
| } |
| 1495 |
61 |
▶ | if (addr->route != NULL || |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->route != NULL || |
61 |
0 |
61 |
|
| 1496 |
61 |
▶ | addr->mailbox_len != 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| addr->mailbox_len != 0 || |
1 |
60 |
61 |
|
| 1497 |
1 |
▶ | addr->domain_len != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| addr->domain_len != 0) { |
1 |
0 |
1 |
|
| 1498 |
61 |
▶ | if (addr->name != NULL && addr->name_len != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (addr->name != NULL && addr->name_len != 0) |
61 |
0 |
61 |
| if (addr->name != NULL && addr->name_len != 0) |
61 |
0 |
61 |
|
| 1499 |
61 |
| str_append_c(str, ' '); |
| 1500 |
61 |
| str_append_c(str, '<'); |
| 1501 |
61 |
▶ | if (addr->route != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->route != NULL) { |
0 |
61 |
61 |
|
| 1502 |
0 |
| str_append_data(str, addr->route, addr->route_len); |
| 1503 |
0 |
| str_append_c(str, ':'); |
| 1504 |
|
| } |
| 1505 |
61 |
| str_append_maybe_escape(str, addr->mailbox, addr->mailbox_len, false, true); |
| 1506 |
61 |
▶ | if (addr->domain_len != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->domain_len != 0) { |
61 |
0 |
61 |
|
| 1507 |
61 |
| str_append_c(str, '@'); |
| 1508 |
61 |
| str_append_data(str, addr->domain, addr->domain_len); |
| 1509 |
|
| } |
| 1510 |
61 |
| str_append_c(str, '>'); |
| 1511 |
|
| } |
| 1512 |
61 |
▶ | if (addr->comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->comment != NULL) { |
19 |
42 |
61 |
|
| 1513 |
19 |
| str_append(str, " ("); |
| 1514 |
19 |
| str_append_data(str, addr->comment, addr->comment_len); |
| 1515 |
19 |
| str_append_c(str, ')'); |
| 1516 |
|
| } |
| 1517 |
|
| } |
| 1518 |
|
| |
| 1519 |
130 |
| addr = addr->next; |
| 1520 |
|
| } |
| 1521 |
|
| |
| 1522 |
84 |
| *output = str_ccopy(str); |
| 1523 |
84 |
| *output_len = str_len(str); |
| 1524 |
84 |
| str_free(&str); |
| 1525 |
84 |
| } |
| 1526 |
|
| |
| 1527 |
109 |
| void compose_address(char **output, size_t *output_len, const char *mailbox, size_t mailbox_len, const char *domain, size_t domain_len) |
| 1528 |
|
| { |
| 1529 |
|
| string_t *str; |
| 1530 |
|
| |
| 1531 |
109 |
| str = str_new(128); |
| 1532 |
|
| |
| 1533 |
109 |
| str_append_maybe_escape(str, mailbox, mailbox_len, false, true); |
| 1534 |
109 |
| str_append_c(str, '@'); |
| 1535 |
109 |
| str_append_data(str, domain, domain_len); |
| 1536 |
|
| |
| 1537 |
109 |
| *output = str_ccopy(str); |
| 1538 |
109 |
| *output_len = str_len(str); |
| 1539 |
109 |
| str_free(&str); |
| 1540 |
109 |
| } |
| 1541 |
|
| |
| 1542 |
100 |
| void split_address(const char *input, size_t input_len, char **mailbox, size_t *mailbox_len, char **domain, size_t *domain_len) |
| 1543 |
|
| { |
| 1544 |
|
| struct message_address_parser_context ctx; |
| 1545 |
|
| int ret; |
| 1546 |
|
| |
| 1547 |
100 |
▶ | if (!input || !input[0]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!input || !input[0]) { |
100 |
0 |
100 |
| if (!input || !input[0]) { |
1 |
99 |
100 |
|
| 1548 |
1 |
| *mailbox = NULL; |
| 1549 |
1 |
| *mailbox_len = 0; |
| 1550 |
1 |
| *domain = NULL; |
| 1551 |
1 |
| *domain_len = 0; |
| 1552 |
1 |
| return; |
| 1553 |
|
| } |
| 1554 |
|
| |
| 1555 |
99 |
| memset(&ctx, 0, sizeof(ctx)); |
| 1556 |
|
| |
| 1557 |
99 |
| rfc822_parser_init(&ctx.parser, (const unsigned char *)input, input_len, NULL); |
| 1558 |
|
| |
| 1559 |
99 |
| ctx.str = str_new(128); |
| 1560 |
99 |
| ctx.fill_missing = false; |
| 1561 |
99 |
| ctx.non_strict_dots = false; |
| 1562 |
99 |
| ctx.non_strict_dots_as_invalid = false; |
| 1563 |
|
| |
| 1564 |
99 |
| ret = rfc822_skip_lwsp(&ctx.parser); |
| 1565 |
|
| |
| 1566 |
99 |
▶ | if (ret > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret > 0) |
99 |
0 |
99 |
|
| 1567 |
99 |
| ret = parse_addr_spec(&ctx); |
| 1568 |
|
| else |
| 1569 |
0 |
| ret = -1; |
| 1570 |
|
| |
| 1571 |
99 |
▶ | if (ret >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret >= 0) |
98 |
1 |
99 |
|
| 1572 |
98 |
| ret = rfc822_skip_lwsp(&ctx.parser); |
| 1573 |
|
| |
| 1574 |
99 |
▶ | if (ret < 0 || ctx.parser.data != ctx.parser.end || ctx.addr.invalid_syntax) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (ret < 0 || ctx.parser.data != ctx.parser.end || ctx.addr.invalid_syntax) { |
98 |
1 |
99 |
| if (ret < 0 || ctx.parser.data != ctx.parser.end || ctx.addr.invalid_syntax) { |
96 |
2 |
98 |
| if (ret < 0 || ctx.parser.data != ctx.parser.end || ctx.addr.invalid_syntax) { |
4 |
92 |
96 |
|
| 1575 |
7 |
| free(ctx.addr.mailbox); |
| 1576 |
7 |
| free(ctx.addr.domain); |
| 1577 |
7 |
| *mailbox = NULL; |
| 1578 |
7 |
| *mailbox_len = 0; |
| 1579 |
7 |
| *domain = NULL; |
| 1580 |
7 |
| *domain_len = 0; |
| 1581 |
|
| } else { |
| 1582 |
92 |
| *mailbox = ctx.addr.mailbox; |
| 1583 |
92 |
| *mailbox_len = ctx.addr.mailbox_len; |
| 1584 |
92 |
| *domain = ctx.addr.domain; |
| 1585 |
92 |
| *domain_len = ctx.addr.domain_len; |
| 1586 |
|
| } |
| 1587 |
|
| |
| 1588 |
99 |
| free(ctx.addr.comment); |
| 1589 |
99 |
| free(ctx.addr.route); |
| 1590 |
99 |
| free(ctx.addr.name); |
| 1591 |
99 |
| free(ctx.addr.original); |
| 1592 |
|
| |
| 1593 |
99 |
| rfc822_parser_deinit(&ctx.parser); |
| 1594 |
|
| |
| 1595 |
99 |
| str_free(&ctx.str); |
| 1596 |
|
| } |
| 1597 |
|
| |
| 1598 |
393 |
| void string_free(char *string) |
| 1599 |
|
| { |
| 1600 |
393 |
| free(string); |
| 1601 |
393 |
| } |