| 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 |
|
| |
| 62 |
|
| struct message_address_parser_context { |
| 63 |
|
| struct rfc822_parser_context parser; |
| 64 |
|
| |
| 65 |
|
| struct message_address *first_addr, *last_addr, addr; |
| 66 |
|
| string_t *str; |
| 67 |
|
| |
| 68 |
|
| bool fill_missing; |
| 69 |
|
| }; |
| 70 |
|
| |
| 71 |
357 |
| static string_t *str_new(size_t initial_size) |
| 72 |
|
| { |
| 73 |
|
| char *buf; |
| 74 |
|
| string_t *str; |
| 75 |
|
| |
| 76 |
357 |
▶ | if (!initial_size) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!initial_size) |
0 |
357 |
357 |
|
| 77 |
0 |
| initial_size = 1; |
| 78 |
|
| |
| 79 |
357 |
▶ | if (initial_size >= SIZE_MAX / 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (initial_size >= SIZE_MAX / 2) |
0 |
357 |
357 |
|
| 80 |
0 |
| i_panic("str_new() failed: %s", "initial_size is too big"); |
| 81 |
|
| |
| 82 |
357 |
| buf = malloc(initial_size); |
| 83 |
357 |
▶ | if (!buf) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) |
0 |
357 |
357 |
|
| 84 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 85 |
|
| |
| 86 |
357 |
| str = malloc(sizeof(string_t)); |
| 87 |
357 |
▶ | if (!str) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!str) |
0 |
357 |
357 |
|
| 88 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 89 |
|
| |
| 90 |
357 |
| buf[0] = 0; |
| 91 |
|
| |
| 92 |
357 |
| str->buf = buf; |
| 93 |
357 |
| str->len = 0; |
| 94 |
357 |
| str->size = initial_size; |
| 95 |
|
| |
| 96 |
357 |
| return str; |
| 97 |
|
| } |
| 98 |
|
| |
| 99 |
357 |
| static void str_free(string_t **str) |
| 100 |
|
| { |
| 101 |
357 |
| free((*str)->buf); |
| 102 |
357 |
| free(*str); |
| 103 |
357 |
| *str = NULL; |
| 104 |
357 |
| } |
| 105 |
|
| |
| 106 |
21 |
| static const char *str_c(string_t *str) |
| 107 |
|
| { |
| 108 |
21 |
| return str->buf; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
542 |
| static char *str_ccopy(string_t *str) |
| 112 |
|
| { |
| 113 |
|
| char *copy; |
| 114 |
|
| |
| 115 |
542 |
| copy = malloc(str->len+1); |
| 116 |
542 |
▶ | if (!copy) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!copy) |
0 |
542 |
542 |
|
| 117 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 118 |
|
| |
| 119 |
542 |
| memcpy(copy, str->buf, str->len); |
| 120 |
542 |
| copy[str->len] = 0; |
| 121 |
542 |
| return copy; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
696 |
| static size_t str_len(const string_t *str) |
| 125 |
|
| { |
| 126 |
696 |
| return str->len; |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
2120 |
| static void str_append_data(string_t *str, const void *data, size_t len) |
| 130 |
|
| { |
| 131 |
|
| char *new_buf; |
| 132 |
|
| size_t need_size; |
| 133 |
|
| |
| 134 |
2120 |
| need_size = str->len + len + 1; |
| 135 |
|
| |
| 136 |
2120 |
▶ | 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) |
2120 |
0 |
2120 |
| if (len >= SIZE_MAX / 2 || need_size >= SIZE_MAX / 2) |
0 |
2120 |
2120 |
|
| 137 |
0 |
| i_panic("%s() failed: %s", __FUNCTION__, "len is too big"); |
| 138 |
|
| |
| 139 |
2120 |
▶ | if (need_size > str->size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (need_size > str->size) { |
6 |
2114 |
2120 |
|
| 140 |
6 |
| str->size = 1; |
| 141 |
56 |
▶ | while (str->size < need_size) |
Branch: 1 branch
| Branch | True | False | Total |
| while (str->size < need_size) |
50 |
6 |
56 |
|
| 142 |
50 |
| str->size <<= 1; |
| 143 |
|
| |
| 144 |
6 |
| new_buf = realloc(str->buf, str->size); |
| 145 |
6 |
▶ | if (!new_buf) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!new_buf) |
0 |
6 |
6 |
|
| 146 |
0 |
| i_panic("realloc() failed: %s", strerror(errno)); |
| 147 |
|
| |
| 148 |
6 |
| str->buf = new_buf; |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
2120 |
| memcpy(str->buf + str->len, data, len); |
| 152 |
2120 |
| str->len += len; |
| 153 |
2120 |
| str->buf[str->len] = 0; |
| 154 |
2120 |
| } |
| 155 |
|
| |
| 156 |
88 |
| static void str_append(string_t *str, const char *cstr) |
| 157 |
|
| { |
| 158 |
88 |
| str_append_data(str, cstr, strlen(cstr)); |
| 159 |
88 |
| } |
| 160 |
|
| |
| 161 |
870 |
| static void str_append_c(string_t *str, unsigned char chr) |
| 162 |
|
| { |
| 163 |
870 |
| str_append_data(str, &chr, 1); |
| 164 |
870 |
| } |
| 165 |
|
| |
| 166 |
554 |
| static void str_truncate(string_t *str, size_t len) |
| 167 |
|
| { |
| 168 |
554 |
▶ | if (str->size - 1 <= len || str->len <= len) |
Branch: 2 branches
| Branch | True | False | Total |
| if (str->size - 1 <= len || str->len <= len) |
554 |
0 |
554 |
| if (str->size - 1 <= len || str->len <= len) |
210 |
344 |
554 |
|
| 169 |
210 |
| return; |
| 170 |
|
| |
| 171 |
344 |
| str->len = len; |
| 172 |
344 |
| str->buf[len] = 0; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
|
| /* |
| 176 |
|
| atext = ALPHA / DIGIT / ; Any character except controls, |
| 177 |
|
| "!" / "#" / ; SP, and specials. |
| 178 |
|
| "$" / "%" / ; Used for atoms |
| 179 |
|
| "&" / "'" / |
| 180 |
|
| "*" / "+" / |
| 181 |
|
| "-" / "/" / |
| 182 |
|
| "=" / "?" / |
| 183 |
|
| "^" / "_" / |
| 184 |
|
| "`" / "{" / |
| 185 |
|
| "|" / "}" / |
| 186 |
|
| "~" |
| 187 |
|
| |
| 188 |
|
| MIME: |
| 189 |
|
| |
| 190 |
|
| token := 1*<any (US-ASCII) CHAR except SPACE, CTLs, |
| 191 |
|
| or tspecials> |
| 192 |
|
| tspecials := "(" / ")" / "<" / ">" / "@" / |
| 193 |
|
| "," / ";" / ":" / "\" / <"> |
| 194 |
|
| "/" / "[" / "]" / "?" / "=" |
| 195 |
|
| |
| 196 |
|
| So token is same as dot-atom, except stops also at '/', '?' and '='. |
| 197 |
|
| */ |
| 198 |
|
| |
| 199 |
|
| /* atext chars are marked with 1, alpha and digits with 2, |
| 200 |
|
| atext-but-mime-tspecials with 4 */ |
| 201 |
|
| unsigned char rfc822_atext_chars[256] = { |
| 202 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0-15 */ |
| 203 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16-31 */ |
| 204 |
|
| 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 4, /* 32-47 */ |
| 205 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 4, 0, 4, /* 48-63 */ |
| 206 |
|
| 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 64-79 */ |
| 207 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1, 1, /* 80-95 */ |
| 208 |
|
| 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 96-111 */ |
| 209 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 0, /* 112-127 */ |
| 210 |
|
| |
| 211 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 212 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 213 |
|
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 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 |
|
| }; |
| 220 |
|
| |
| 221 |
|
| #define IS_ATEXT(c) \ |
| 222 |
|
| (rfc822_atext_chars[(int)(unsigned char)(c)] != 0) |
| 223 |
|
| #define IS_ATEXT_NON_TSPECIAL(c) \ |
| 224 |
|
| ((rfc822_atext_chars[(int)(unsigned char)(c)] & 3) != 0) |
| 225 |
|
| |
| 226 |
|
| /* |
| 227 |
|
| qtext = %d33 / ; Printable US-ASCII |
| 228 |
|
| %d35-91 / ; characters not including |
| 229 |
|
| %d93-126 / ; "\" or the quote character |
| 230 |
|
| obs-qtext |
| 231 |
|
| |
| 232 |
|
| obs-qtext = obs-NO-WS-CTL |
| 233 |
|
| |
| 234 |
|
| obs-NO-WS-CTL = %d1-8 / ; US-ASCII control |
| 235 |
|
| %d11 / ; characters that do not |
| 236 |
|
| %d12 / ; include the carriage |
| 237 |
|
| %d14-31 / ; return, line feed, and |
| 238 |
|
| %d127 ; white space characters |
| 239 |
|
| |
| 240 |
|
| So qtext is everything expects '\0', '\t', '\n', '\r', ' ', '"', '\\'. |
| 241 |
|
| */ |
| 242 |
|
| |
| 243 |
|
| /* non-qtext characters */ |
| 244 |
|
| #define CHAR_NEEDS_ESCAPE(c) ((c) == '"' || (c) == '\\' || (c) == '\0' || (c) == '\t' || (c) == '\n' || (c) == '\r') |
| 245 |
|
| |
| 246 |
|
| /* quote with "" and escape all needed characters */ |
| 247 |
251 |
| static void str_append_maybe_escape(string_t *str, const char *data, size_t len, bool quote_dot) |
| 248 |
|
| { |
| 249 |
|
| const char *p; |
| 250 |
|
| const char *end; |
| 251 |
|
| |
| 252 |
251 |
▶ | if (len == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) { |
2 |
249 |
251 |
|
| 253 |
2 |
| str_append(str, "\"\""); |
| 254 |
2 |
| return; |
| 255 |
|
| } |
| 256 |
|
| |
| 257 |
|
| /* leading or trailing dot needs to be always quoted */ |
| 258 |
249 |
▶ | if (data[0] == '.' || data[len-1] == '.') |
Branch: 2 branches
| Branch | True | False | Total |
| if (data[0] == '.' || data[len-1] == '.') |
248 |
1 |
249 |
| if (data[0] == '.' || data[len-1] == '.') |
3 |
245 |
248 |
|
| 259 |
4 |
| quote_dot = true; |
| 260 |
|
| |
| 261 |
249 |
| end = data + len; |
| 262 |
|
| |
| 263 |
|
| /* see if we need to quote it */ |
| 264 |
2296 |
▶ | for (p = data; p != end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = data; p != end; p++) { |
2093 |
203 |
2296 |
|
| 265 |
2093 |
▶ | if (!IS_ATEXT(*p) && (quote_dot || *p != '.')) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!IS_ATEXT(*p) && (quote_dot || *p != '.')) |
115 |
1978 |
2093 |
| if (!IS_ATEXT(*p) && (quote_dot || *p != '.')) |
78 |
37 |
115 |
| if (!IS_ATEXT(*p) && (quote_dot || *p != '.')) |
69 |
9 |
78 |
|
| 266 |
|
| break; |
| 267 |
|
| } |
| 268 |
|
| |
| 269 |
249 |
▶ | if (p == end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == end) { |
203 |
46 |
249 |
|
| 270 |
203 |
| str_append_data(str, data, len); |
| 271 |
203 |
| return; |
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
|
| /* see if we need to escape it */ |
| 275 |
520 |
▶ | for (p = data; p != end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = data; p != end; p++) { |
488 |
32 |
520 |
|
| 276 |
488 |
▶ | if (CHAR_NEEDS_ESCAPE(*p)) |
Branch: 6 branches
| Branch | True | False | Total |
| if (CHAR_NEEDS_ESCAPE(*p)) |
478 |
10 |
488 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
478 |
0 |
478 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
474 |
4 |
478 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
474 |
0 |
474 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
474 |
0 |
474 |
| if (CHAR_NEEDS_ESCAPE(*p)) |
474 |
0 |
474 |
|
| 277 |
|
| break; |
| 278 |
|
| } |
| 279 |
|
| |
| 280 |
46 |
▶ | if (p == end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == end) { |
32 |
14 |
46 |
|
| 281 |
|
| /* only quote */ |
| 282 |
32 |
| str_append_c(str, '"'); |
| 283 |
32 |
| str_append_data(str, data, len); |
| 284 |
32 |
| str_append_c(str, '"'); |
| 285 |
32 |
| return; |
| 286 |
|
| } |
| 287 |
|
| |
| 288 |
|
| /* quote and escape */ |
| 289 |
14 |
| str_append_c(str, '"'); |
| 290 |
14 |
| str_append_data(str, data, (size_t) (p - data)); |
| 291 |
|
| |
| 292 |
180 |
▶ | for (; p != end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; p != end; p++) { |
166 |
14 |
180 |
|
| 293 |
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 |
|
| 294 |
28 |
| str_append_c(str, '\\'); |
| 295 |
166 |
| str_append_c(str, *p); |
| 296 |
|
| } |
| 297 |
|
| |
| 298 |
14 |
| str_append_c(str, '"'); |
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
|
| /* Parse given data using RFC 822 token parser. */ |
| 302 |
140 |
| static void rfc822_parser_init(struct rfc822_parser_context *ctx, |
| 303 |
|
| const unsigned char *data, size_t size, |
| 304 |
|
| string_t *last_comment) |
| 305 |
|
| { |
| 306 |
140 |
| memset(ctx, 0, sizeof(*ctx)); |
| 307 |
140 |
| ctx->data = data; |
| 308 |
140 |
| ctx->end = data + size; |
| 309 |
140 |
| ctx->last_comment = last_comment; |
| 310 |
140 |
| } |
| 311 |
|
| |
| 312 |
|
| /* The functions below return 1 = more data available, 0 = no more data |
| 313 |
|
| available (but a value might have been returned now), -1 = invalid input. |
| 314 |
|
| |
| 315 |
|
| LWSP is automatically skipped after value, but not before it. So typically |
| 316 |
|
| you begin with skipping LWSP and then start using the parse functions. */ |
| 317 |
|
| |
| 318 |
|
| /* Parse comment. Assumes parser's data points to '(' */ |
| 319 |
41 |
| static int rfc822_skip_comment(struct rfc822_parser_context *ctx) |
| 320 |
|
| { |
| 321 |
|
| const unsigned char *start; |
| 322 |
41 |
| int level = 1; |
| 323 |
|
| |
| 324 |
|
| i_assert(*ctx->data == '('); |
| 325 |
|
| |
| 326 |
41 |
▶ | if (ctx->last_comment != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->last_comment != NULL) |
28 |
13 |
41 |
|
| 327 |
28 |
| str_truncate(ctx->last_comment, 0); |
| 328 |
|
| |
| 329 |
41 |
| start = ++ctx->data; |
| 330 |
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 |
|
| 331 |
629 |
| switch (*ctx->data) { |
| 332 |
14 |
| case '(': |
| 333 |
14 |
| level++; |
| 334 |
14 |
| break; |
| 335 |
52 |
| case ')': |
| 336 |
52 |
▶ | if (--level == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (--level == 0) { |
38 |
14 |
52 |
|
| 337 |
38 |
▶ | if (ctx->last_comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->last_comment != NULL) { |
25 |
13 |
38 |
|
| 338 |
25 |
| str_append_data(ctx->last_comment, start, |
| 339 |
25 |
| ctx->data - start); |
| 340 |
|
| } |
| 341 |
38 |
| ctx->data++; |
| 342 |
38 |
| return ctx->data < ctx->end ? 1 : 0; |
| 343 |
|
| } |
| 344 |
14 |
| break; |
| 345 |
0 |
| case '\\': |
| 346 |
0 |
▶ | if (ctx->last_comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->last_comment != NULL) { |
0 |
0 |
0 |
|
| 347 |
0 |
| str_append_data(ctx->last_comment, start, |
| 348 |
0 |
| ctx->data - start); |
| 349 |
|
| } |
| 350 |
0 |
| start = ctx->data + 1; |
| 351 |
|
| |
| 352 |
0 |
| ctx->data++; |
| 353 |
0 |
▶ | if (ctx->data >= ctx->end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data >= ctx->end) |
0 |
0 |
0 |
|
| 354 |
0 |
| return -1; |
| 355 |
0 |
| break; |
| 356 |
|
| } |
| 357 |
|
| } |
| 358 |
|
| |
| 359 |
|
| /* missing ')' */ |
| 360 |
3 |
| return -1; |
| 361 |
|
| } |
| 362 |
|
| |
| 363 |
|
| /* Skip LWSP if there is any */ |
| 364 |
1267 |
| static int rfc822_skip_lwsp(struct rfc822_parser_context *ctx) |
| 365 |
|
| { |
| 366 |
1477 |
▶ | for (; ctx->data < ctx->end;) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; ctx->data < ctx->end;) { |
1432 |
45 |
1477 |
|
| 367 |
1432 |
▶ | if (*ctx->data == ' ' || *ctx->data == '\t' || |
Branch: 2 branches
| Branch | True | False | Total |
| if (*ctx->data == ' ' || *ctx->data == '\t' || |
1261 |
171 |
1432 |
| if (*ctx->data == ' ' || *ctx->data == '\t' || |
1261 |
0 |
1261 |
|
| 368 |
1261 |
▶ | *ctx->data == '\r' || *ctx->data == '\n') { |
Branch: 2 branches
| Branch | True | False | Total |
| *ctx->data == '\r' || *ctx->data == '\n') { |
1261 |
0 |
1261 |
| *ctx->data == '\r' || *ctx->data == '\n') { |
1 |
1260 |
1261 |
|
| 369 |
172 |
| ctx->data++; |
| 370 |
172 |
| continue; |
| 371 |
|
| } |
| 372 |
|
| |
| 373 |
1260 |
▶ | if (*ctx->data != '(') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data != '(') |
1219 |
41 |
1260 |
|
| 374 |
1219 |
| break; |
| 375 |
|
| |
| 376 |
41 |
▶ | if (rfc822_skip_comment(ctx) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_skip_comment(ctx) < 0) |
3 |
38 |
41 |
|
| 377 |
3 |
| return -1; |
| 378 |
|
| } |
| 379 |
1264 |
| return ctx->data < ctx->end ? 1 : 0; |
| 380 |
|
| } |
| 381 |
|
| |
| 382 |
|
| /* Like parse_atom() but don't stop at '.' */ |
| 383 |
296 |
| static int rfc822_parse_dot_atom(struct rfc822_parser_context *ctx, string_t *str) |
| 384 |
|
| { |
| 385 |
|
| const unsigned char *start; |
| 386 |
|
| bool last_is_dot; |
| 387 |
|
| bool dot_problem; |
| 388 |
|
| int ret; |
| 389 |
|
| |
| 390 |
|
| /* |
| 391 |
|
| dot-atom = [CFWS] dot-atom-text [CFWS] |
| 392 |
|
| dot-atom-text = 1*atext *("." 1*atext) |
| 393 |
|
| |
| 394 |
|
| atext = |
| 395 |
|
| ; Any character except controls, SP, and specials. |
| 396 |
|
| |
| 397 |
|
| For RFC-822 compatibility allow LWSP around '.' |
| 398 |
|
| */ |
| 399 |
296 |
▶ | 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)) |
296 |
0 |
296 |
| if (ctx->data >= ctx->end || !IS_ATEXT(*ctx->data)) |
1 |
295 |
296 |
|
| 400 |
1 |
| return -1; |
| 401 |
|
| |
| 402 |
295 |
| last_is_dot = false; |
| 403 |
295 |
| dot_problem = false; |
| 404 |
|
| |
| 405 |
3342 |
▶ | for (start = ctx->data++; ctx->data < ctx->end; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (start = ctx->data++; ctx->data < ctx->end; ) { |
3245 |
97 |
3342 |
|
| 406 |
3245 |
▶ | if (IS_ATEXT(*ctx->data)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (IS_ATEXT(*ctx->data)) { |
2856 |
389 |
3245 |
|
| 407 |
2856 |
| ctx->data++; |
| 408 |
2856 |
| continue; |
| 409 |
|
| } |
| 410 |
|
| |
| 411 |
389 |
| str_append_data(str, start, ctx->data - start); |
| 412 |
|
| |
| 413 |
389 |
▶ | if (ctx->data - start > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data - start > 0) |
387 |
2 |
389 |
|
| 414 |
387 |
| last_is_dot = false; |
| 415 |
|
| |
| 416 |
389 |
▶ | if ((ret = rfc822_skip_lwsp(ctx)) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = rfc822_skip_lwsp(ctx)) <= 0) |
7 |
382 |
389 |
|
| 417 |
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 |
|
| 418 |
|
| |
| 419 |
382 |
▶ | if (*ctx->data != '.') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data != '.') |
191 |
191 |
382 |
|
| 420 |
191 |
▶ | return (last_is_dot || dot_problem) ? -2 : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| return (last_is_dot || dot_problem) ? -2 : 1; |
189 |
2 |
191 |
| return (last_is_dot || dot_problem) ? -2 : 1; |
0 |
189 |
189 |
|
| 421 |
|
| |
| 422 |
191 |
▶ | if (last_is_dot) |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_is_dot) |
0 |
191 |
191 |
|
| 423 |
0 |
| dot_problem = true; |
| 424 |
|
| |
| 425 |
191 |
| ctx->data++; |
| 426 |
191 |
| str_append_c(str, '.'); |
| 427 |
191 |
| last_is_dot = true; |
| 428 |
|
| |
| 429 |
191 |
▶ | if ((ret = rfc822_skip_lwsp(ctx)) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = rfc822_skip_lwsp(ctx)) <= 0) |
0 |
191 |
191 |
|
| 430 |
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 |
|
| 431 |
191 |
| start = ctx->data; |
| 432 |
|
| } |
| 433 |
|
| |
| 434 |
97 |
| str_append_data(str, start, ctx->data - start); |
| 435 |
97 |
▶ | return dot_problem ? -2 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return dot_problem ? -2 : 0; |
0 |
97 |
97 |
|
| 436 |
|
| } |
| 437 |
|
| |
| 438 |
|
| /* "quoted string" */ |
| 439 |
76 |
| static int rfc822_parse_quoted_string(struct rfc822_parser_context *ctx, string_t *str) |
| 440 |
|
| { |
| 441 |
|
| const unsigned char *start; |
| 442 |
|
| size_t len; |
| 443 |
|
| |
| 444 |
|
| i_assert(ctx->data < ctx->end); |
| 445 |
|
| i_assert(*ctx->data == '"'); |
| 446 |
76 |
| ctx->data++; |
| 447 |
|
| |
| 448 |
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 |
|
| 449 |
1447 |
| switch (*ctx->data) { |
| 450 |
76 |
| case '"': |
| 451 |
76 |
| str_append_data(str, start, ctx->data - start); |
| 452 |
76 |
| ctx->data++; |
| 453 |
76 |
| return rfc822_skip_lwsp(ctx); |
| 454 |
0 |
| case '\n': |
| 455 |
|
| /* folding whitespace, remove the (CR)LF */ |
| 456 |
0 |
| len = ctx->data - start; |
| 457 |
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 |
|
| 458 |
0 |
| len--; |
| 459 |
0 |
| str_append_data(str, start, len); |
| 460 |
0 |
| start = ctx->data + 1; |
| 461 |
0 |
| break; |
| 462 |
45 |
| case '\\': |
| 463 |
45 |
| ctx->data++; |
| 464 |
45 |
▶ | if (ctx->data >= ctx->end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data >= ctx->end) |
0 |
45 |
45 |
|
| 465 |
0 |
| return -1; |
| 466 |
|
| |
| 467 |
45 |
| str_append_data(str, start, ctx->data - start - 1); |
| 468 |
45 |
| start = ctx->data; |
| 469 |
45 |
| break; |
| 470 |
|
| } |
| 471 |
|
| } |
| 472 |
|
| |
| 473 |
|
| /* missing '"' */ |
| 474 |
0 |
| return -1; |
| 475 |
|
| } |
| 476 |
|
| |
| 477 |
|
| static int |
| 478 |
76 |
| rfc822_parse_atom_or_dot(struct rfc822_parser_context *ctx, string_t *str) |
| 479 |
|
| { |
| 480 |
|
| const unsigned char *start; |
| 481 |
|
| |
| 482 |
|
| /* |
| 483 |
|
| atom = [CFWS] 1*atext [CFWS] |
| 484 |
|
| atext = |
| 485 |
|
| ; Any character except controls, SP, and specials. |
| 486 |
|
| |
| 487 |
|
| The difference between this function and rfc822_parse_dot_atom() |
| 488 |
|
| is that this doesn't just silently skip over all the whitespace. |
| 489 |
|
| */ |
| 490 |
771 |
▶ | 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++) { |
765 |
6 |
771 |
|
| 491 |
765 |
▶ | if (IS_ATEXT(*ctx->data) || *ctx->data == '.') |
Branch: 2 branches
| Branch | True | False | Total |
| if (IS_ATEXT(*ctx->data) || *ctx->data == '.') |
90 |
675 |
765 |
| if (IS_ATEXT(*ctx->data) || *ctx->data == '.') |
20 |
70 |
90 |
|
| 492 |
695 |
| continue; |
| 493 |
|
| |
| 494 |
70 |
| str_append_data(str, start, ctx->data - start); |
| 495 |
70 |
| return rfc822_skip_lwsp(ctx); |
| 496 |
|
| } |
| 497 |
|
| |
| 498 |
6 |
| str_append_data(str, start, ctx->data - start); |
| 499 |
6 |
| return 0; |
| 500 |
|
| } |
| 501 |
|
| |
| 502 |
|
| /* atom or quoted-string */ |
| 503 |
133 |
| static int rfc822_parse_phrase(struct rfc822_parser_context *ctx, string_t *str) |
| 504 |
|
| { |
| 505 |
|
| int ret; |
| 506 |
|
| |
| 507 |
|
| /* |
| 508 |
|
| phrase = 1*word / obs-phrase |
| 509 |
|
| word = atom / quoted-string |
| 510 |
|
| obs-phrase = word *(word / "." / CFWS) |
| 511 |
|
| */ |
| 512 |
|
| |
| 513 |
133 |
▶ | if (ctx->data >= ctx->end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data >= ctx->end) |
0 |
133 |
133 |
|
| 514 |
0 |
| return 0; |
| 515 |
133 |
▶ | if (*ctx->data == '.') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data == '.') |
133 |
0 |
133 |
|
| 516 |
0 |
| return -1; |
| 517 |
|
| |
| 518 |
|
| for (;;) { |
| 519 |
144 |
▶ | if (*ctx->data == '"') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data == '"') |
68 |
76 |
144 |
|
| 520 |
68 |
| ret = rfc822_parse_quoted_string(ctx, str); |
| 521 |
|
| else |
| 522 |
76 |
| ret = rfc822_parse_atom_or_dot(ctx, str); |
| 523 |
|
| |
| 524 |
144 |
▶ | if (ret <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret <= 0) |
14 |
130 |
144 |
|
| 525 |
14 |
| return ret; |
| 526 |
|
| |
| 527 |
130 |
▶ | if (!IS_ATEXT(*ctx->data) && *ctx->data != '"' |
Branch: 2 branches
| Branch | True | False | Total |
| if (!IS_ATEXT(*ctx->data) && *ctx->data != '"' |
121 |
9 |
130 |
| if (!IS_ATEXT(*ctx->data) && *ctx->data != '"' |
119 |
2 |
121 |
|
| 528 |
119 |
▶ | && *ctx->data != '.') |
Branch: 1 branch
| Branch | True | False | Total |
| && *ctx->data != '.') |
119 |
0 |
119 |
|
| 529 |
119 |
| break; |
| 530 |
11 |
| str_append_c(str, ' '); |
| 531 |
|
| } |
| 532 |
119 |
| return rfc822_skip_lwsp(ctx); |
| 533 |
|
| } |
| 534 |
|
| |
| 535 |
|
| static int |
| 536 |
0 |
| rfc822_parse_domain_literal(struct rfc822_parser_context *ctx, string_t *str) |
| 537 |
|
| { |
| 538 |
|
| const unsigned char *start; |
| 539 |
|
| |
| 540 |
|
| /* |
| 541 |
|
| domain-literal = [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS] |
| 542 |
|
| dcontent = dtext / quoted-pair |
| 543 |
|
| dtext = NO-WS-CTL / ; Non white space controls |
| 544 |
|
| %d33-90 / ; The rest of the US-ASCII |
| 545 |
|
| %d94-126 ; characters not including "[", |
| 546 |
|
| ; "]", or "\" |
| 547 |
|
| */ |
| 548 |
|
| i_assert(ctx->data < ctx->end); |
| 549 |
|
| i_assert(*ctx->data == '['); |
| 550 |
|
| |
| 551 |
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 |
|
| 552 |
0 |
▶ | if (*ctx->data == '\\') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data == '\\') { |
0 |
0 |
0 |
|
| 553 |
0 |
| ctx->data++; |
| 554 |
0 |
▶ | if (ctx->data >= ctx->end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->data >= ctx->end) |
0 |
0 |
0 |
|
| 555 |
0 |
| break; |
| 556 |
0 |
▶ | } else if (*ctx->data == ']') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*ctx->data == ']') { |
0 |
0 |
0 |
|
| 557 |
0 |
| ctx->data++; |
| 558 |
0 |
| str_append_data(str, start, ctx->data - start); |
| 559 |
0 |
| return rfc822_skip_lwsp(ctx); |
| 560 |
|
| } |
| 561 |
|
| } |
| 562 |
|
| |
| 563 |
|
| /* missing ']' */ |
| 564 |
0 |
| return -1; |
| 565 |
|
| } |
| 566 |
|
| |
| 567 |
|
| /* dot-atom / domain-literal */ |
| 568 |
146 |
| static int rfc822_parse_domain(struct rfc822_parser_context *ctx, string_t *str) |
| 569 |
|
| { |
| 570 |
|
| /* |
| 571 |
|
| domain = dot-atom / domain-literal / obs-domain |
| 572 |
|
| domain-literal = [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS] |
| 573 |
|
| obs-domain = atom *("." atom) |
| 574 |
|
| */ |
| 575 |
|
| i_assert(ctx->data < ctx->end); |
| 576 |
|
| i_assert(*ctx->data == '@'); |
| 577 |
146 |
| ctx->data++; |
| 578 |
|
| |
| 579 |
146 |
▶ | if (rfc822_skip_lwsp(ctx) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_skip_lwsp(ctx) <= 0) |
1 |
145 |
146 |
|
| 580 |
1 |
| return -1; |
| 581 |
|
| |
| 582 |
145 |
▶ | if (*ctx->data == '[') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->data == '[') |
0 |
145 |
145 |
|
| 583 |
0 |
| return rfc822_parse_domain_literal(ctx, str); |
| 584 |
|
| else |
| 585 |
145 |
| return rfc822_parse_dot_atom(ctx, str); |
| 586 |
|
| } |
| 587 |
|
| |
| 588 |
85 |
| static void add_address(struct message_address_parser_context *ctx) |
| 589 |
|
| { |
| 590 |
|
| struct message_address *addr; |
| 591 |
|
| |
| 592 |
85 |
| addr = malloc(sizeof(struct message_address)); |
| 593 |
85 |
▶ | if (!addr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!addr) |
0 |
85 |
85 |
|
| 594 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 595 |
|
| |
| 596 |
85 |
| memcpy(addr, &ctx->addr, sizeof(ctx->addr)); |
| 597 |
85 |
| memset(&ctx->addr, 0, sizeof(ctx->addr)); |
| 598 |
|
| |
| 599 |
85 |
▶ | if (ctx->first_addr == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->first_addr == NULL) |
40 |
45 |
85 |
|
| 600 |
40 |
| ctx->first_addr = addr; |
| 601 |
|
| else |
| 602 |
45 |
| ctx->last_addr->next = addr; |
| 603 |
85 |
| ctx->last_addr = addr; |
| 604 |
85 |
| } |
| 605 |
|
| |
| 606 |
159 |
| static int parse_local_part(struct message_address_parser_context *ctx) |
| 607 |
|
| { |
| 608 |
|
| int ret; |
| 609 |
|
| |
| 610 |
|
| /* |
| 611 |
|
| local-part = dot-atom / quoted-string / obs-local-part |
| 612 |
|
| obs-local-part = word *("." word) |
| 613 |
|
| */ |
| 614 |
|
| i_assert(ctx->parser.data < ctx->parser.end); |
| 615 |
|
| |
| 616 |
159 |
| str_truncate(ctx->str, 0); |
| 617 |
159 |
▶ | if (*ctx->parser.data == '"') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data == '"') |
8 |
151 |
159 |
|
| 618 |
8 |
| ret = rfc822_parse_quoted_string(&ctx->parser, ctx->str); |
| 619 |
|
| else |
| 620 |
151 |
| ret = rfc822_parse_dot_atom(&ctx->parser, ctx->str); |
| 621 |
159 |
▶ | if (ret < 0 && ret != -2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ret < 0 && ret != -2) |
4 |
155 |
159 |
| if (ret < 0 && ret != -2) |
2 |
2 |
4 |
|
| 622 |
2 |
| return -1; |
| 623 |
|
| |
| 624 |
157 |
| ctx->addr.mailbox = str_ccopy(ctx->str); |
| 625 |
157 |
| ctx->addr.mailbox_len = str_len(ctx->str); |
| 626 |
157 |
| return ret; |
| 627 |
|
| } |
| 628 |
|
| |
| 629 |
146 |
| static int parse_domain(struct message_address_parser_context *ctx) |
| 630 |
|
| { |
| 631 |
|
| int ret; |
| 632 |
|
| |
| 633 |
146 |
| str_truncate(ctx->str, 0); |
| 634 |
146 |
▶ | if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) < 0 && ret != -2) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) < 0 && ret != -2) |
1 |
145 |
146 |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) < 0 && ret != -2) |
1 |
0 |
1 |
|
| 635 |
1 |
| return -1; |
| 636 |
|
| |
| 637 |
145 |
| ctx->addr.domain = str_ccopy(ctx->str); |
| 638 |
145 |
| ctx->addr.domain_len = str_len(ctx->str); |
| 639 |
145 |
| return ret; |
| 640 |
|
| } |
| 641 |
|
| |
| 642 |
0 |
| static int parse_domain_list(struct message_address_parser_context *ctx) |
| 643 |
|
| { |
| 644 |
|
| int ret; |
| 645 |
|
| bool dot_problem; |
| 646 |
|
| |
| 647 |
|
| /* obs-domain-list = "@" domain *(*(CFWS / "," ) [CFWS] "@" domain) */ |
| 648 |
0 |
| str_truncate(ctx->str, 0); |
| 649 |
0 |
| dot_problem = false; |
| 650 |
|
| for (;;) { |
| 651 |
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 |
|
| 652 |
0 |
▶ | return dot_problem ? -2 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return dot_problem ? -2 : 0; |
0 |
0 |
0 |
|
| 653 |
|
| |
| 654 |
0 |
▶ | if (*ctx->parser.data != '@') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data != '@') |
0 |
0 |
0 |
|
| 655 |
0 |
| break; |
| 656 |
|
| |
| 657 |
0 |
▶ | if (str_len(ctx->str) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_len(ctx->str) > 0) |
0 |
0 |
0 |
|
| 658 |
0 |
| str_append_c(ctx->str, ','); |
| 659 |
|
| |
| 660 |
0 |
| str_append_c(ctx->str, '@'); |
| 661 |
0 |
▶ | if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) <= 0 && ret != -2) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) <= 0 && ret != -2) |
0 |
0 |
0 |
| if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) <= 0 && ret != -2) |
0 |
0 |
0 |
|
| 662 |
0 |
| return ret; |
| 663 |
|
| |
| 664 |
0 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
0 |
0 |
0 |
|
| 665 |
0 |
| dot_problem = true; |
| 666 |
|
| |
| 667 |
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 |
|
| 668 |
0 |
▶ | *ctx->parser.data == ',') |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data == ',') |
0 |
0 |
0 |
|
| 669 |
0 |
| ctx->parser.data++; |
| 670 |
|
| } |
| 671 |
0 |
| ctx->addr.route = str_ccopy(ctx->str); |
| 672 |
0 |
| ctx->addr.route_len = str_len(ctx->str); |
| 673 |
0 |
▶ | return dot_problem ? -2 : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| return dot_problem ? -2 : 1; |
0 |
0 |
0 |
|
| 674 |
|
| } |
| 675 |
|
| |
| 676 |
45 |
| static int parse_angle_addr(struct message_address_parser_context *ctx) |
| 677 |
|
| { |
| 678 |
|
| int ret; |
| 679 |
|
| |
| 680 |
|
| /* "<" [ "@" route ":" ] local-part "@" domain ">" */ |
| 681 |
|
| i_assert(*ctx->parser.data == '<'); |
| 682 |
45 |
| ctx->parser.data++; |
| 683 |
|
| |
| 684 |
45 |
▶ | if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) |
0 |
45 |
45 |
|
| 685 |
0 |
| return ret; |
| 686 |
|
| |
| 687 |
45 |
▶ | if (*ctx->parser.data == '@') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data == '@') { |
0 |
45 |
45 |
|
| 688 |
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 |
|
| 689 |
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 |
|
| 690 |
0 |
| ctx->addr.route = strdup("INVALID_ROUTE"); |
| 691 |
0 |
| ctx->addr.invalid_syntax = true; |
| 692 |
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 |
|
| 693 |
0 |
| return -1; |
| 694 |
0 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
0 |
0 |
0 |
|
| 695 |
0 |
| ctx->parser.data++; |
| 696 |
|
| /* try to continue anyway */ |
| 697 |
|
| } else { |
| 698 |
0 |
| ctx->parser.data++; |
| 699 |
|
| } |
| 700 |
0 |
| ctx->parser.data++; |
| 701 |
0 |
▶ | if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) |
0 |
0 |
0 |
|
| 702 |
0 |
| return ret; |
| 703 |
|
| } |
| 704 |
|
| |
| 705 |
45 |
▶ | if (*ctx->parser.data == '>') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data == '>') { |
45 |
0 |
45 |
|
| 706 |
|
| /* <> address isn't valid */ |
| 707 |
|
| } else { |
| 708 |
45 |
▶ | if ((ret = parse_local_part(ctx)) <= 0 && ret != -2) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((ret = parse_local_part(ctx)) <= 0 && ret != -2) |
1 |
44 |
45 |
| if ((ret = parse_local_part(ctx)) <= 0 && ret != -2) |
0 |
1 |
1 |
|
| 709 |
0 |
| return ret; |
| 710 |
45 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
1 |
44 |
45 |
|
| 711 |
1 |
| ctx->addr.invalid_syntax = true; |
| 712 |
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 |
|
| 713 |
0 |
| return 0; |
| 714 |
45 |
▶ | if (*ctx->parser.data == '@') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data == '@') { |
45 |
0 |
45 |
|
| 715 |
45 |
▶ | if ((ret = parse_domain(ctx)) <= 0 && ret != -2) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((ret = parse_domain(ctx)) <= 0 && ret != -2) |
0 |
45 |
45 |
| if ((ret = parse_domain(ctx)) <= 0 && ret != -2) |
0 |
0 |
0 |
|
| 716 |
0 |
| return ret; |
| 717 |
45 |
▶ | if (ret == -2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -2) |
0 |
45 |
45 |
|
| 718 |
0 |
| ctx->addr.invalid_syntax = true; |
| 719 |
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 |
|
| 720 |
0 |
| return 0; |
| 721 |
|
| } |
| 722 |
|
| } |
| 723 |
|
| |
| 724 |
45 |
▶ | if (*ctx->parser.data != '>') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ctx->parser.data != '>') |
0 |
45 |
45 |
|
| 725 |
0 |
| return -1; |
| 726 |
45 |
| ctx->parser.data++; |
| 727 |
|
| |
| 728 |
45 |
| return rfc822_skip_lwsp(&ctx->parser); |
| 729 |
|
| } |
| 730 |
|
| |
| 731 |
67 |
| static int parse_name_addr(struct message_address_parser_context *ctx) |
| 732 |
|
| { |
| 733 |
|
| /* |
| 734 |
|
| name-addr = [display-name] angle-addr |
| 735 |
|
| display-name = phrase |
| 736 |
|
| */ |
| 737 |
67 |
| str_truncate(ctx->str, 0); |
| 738 |
67 |
▶ | if (rfc822_parse_phrase(&ctx->parser, ctx->str) <= 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_parse_phrase(&ctx->parser, ctx->str) <= 0 || |
60 |
7 |
67 |
|
| 739 |
60 |
▶ | *ctx->parser.data != '<') |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data != '<') |
15 |
45 |
60 |
|
| 740 |
22 |
| return -1; |
| 741 |
|
| |
| 742 |
45 |
▶ | if (str_len(ctx->str) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_len(ctx->str) == 0) { |
1 |
44 |
45 |
|
| 743 |
|
| /* Cope with "<address>" without display name */ |
| 744 |
1 |
| ctx->addr.name = NULL; |
| 745 |
|
| } else { |
| 746 |
44 |
| ctx->addr.name = str_ccopy(ctx->str); |
| 747 |
44 |
| ctx->addr.name_len = str_len(ctx->str); |
| 748 |
|
| } |
| 749 |
|
| |
| 750 |
45 |
▶ | if (ctx->parser.last_comment != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.last_comment != NULL) |
45 |
0 |
45 |
|
| 751 |
45 |
| str_truncate(ctx->parser.last_comment, 0); |
| 752 |
|
| |
| 753 |
45 |
▶ | if (parse_angle_addr(ctx) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_angle_addr(ctx) < 0) { |
0 |
45 |
45 |
|
| 754 |
|
| /* broken */ |
| 755 |
0 |
▶ | if (ctx->fill_missing) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->fill_missing) |
0 |
0 |
0 |
|
| 756 |
0 |
| ctx->addr.domain = strdup("SYNTAX_ERROR"); |
| 757 |
0 |
| ctx->addr.invalid_syntax = true; |
| 758 |
|
| } |
| 759 |
|
| |
| 760 |
45 |
▶ | if (ctx->parser.last_comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.last_comment != NULL) { |
45 |
0 |
45 |
|
| 761 |
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 |
|
| 762 |
7 |
| ctx->addr.comment = |
| 763 |
7 |
| str_ccopy(ctx->parser.last_comment); |
| 764 |
7 |
| ctx->addr.comment_len = |
| 765 |
7 |
| str_len(ctx->parser.last_comment); |
| 766 |
|
| } |
| 767 |
|
| } |
| 768 |
|
| |
| 769 |
45 |
| return ctx->parser.data < ctx->parser.end ? 1 : 0; |
| 770 |
|
| } |
| 771 |
|
| |
| 772 |
114 |
| static int parse_addr_spec(struct message_address_parser_context *ctx) |
| 773 |
|
| { |
| 774 |
|
| /* addr-spec = local-part "@" domain */ |
| 775 |
114 |
| int ret, ret2 = -3; |
| 776 |
|
| |
| 777 |
|
| i_assert(ctx->parser.data < ctx->parser.end); |
| 778 |
|
| |
| 779 |
114 |
▶ | if (ctx->parser.last_comment != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.last_comment != NULL) |
22 |
92 |
114 |
|
| 780 |
22 |
| str_truncate(ctx->parser.last_comment, 0); |
| 781 |
|
| |
| 782 |
|
| #if 0 |
| 783 |
|
| bool quoted_string = *ctx->parser.data == '"'; |
| 784 |
|
| #endif |
| 785 |
114 |
| ret = parse_local_part(ctx); |
| 786 |
114 |
▶ | if (ret <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret <= 0) { |
13 |
101 |
114 |
|
| 787 |
|
| /* end of input or parsing local-part failed */ |
| 788 |
13 |
| ctx->addr.invalid_syntax = true; |
| 789 |
|
| } |
| 790 |
114 |
▶ | 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 && |
104 |
10 |
114 |
| if (ret != 0 && ctx->parser.data < ctx->parser.end && |
103 |
1 |
104 |
|
| 791 |
103 |
▶ | *ctx->parser.data == '@') { |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data == '@') { |
101 |
2 |
103 |
|
| 792 |
101 |
| ret2 = parse_domain(ctx); |
| 793 |
101 |
▶ | if (ret2 <= 0 && ret != -2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ret2 <= 0 && ret != -2) |
97 |
4 |
101 |
| if (ret2 <= 0 && ret != -2) |
96 |
1 |
97 |
|
| 794 |
96 |
| ret = ret2; |
| 795 |
101 |
▶ | if (ret2 == -2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret2 == -2) { |
0 |
101 |
101 |
|
| 796 |
0 |
| ctx->addr.invalid_syntax = true; |
| 797 |
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 |
|
| 798 |
0 |
| ret = 0; |
| 799 |
|
| } |
| 800 |
|
| } |
| 801 |
|
| |
| 802 |
114 |
▶ | 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) { |
22 |
92 |
114 |
| if (ctx->parser.last_comment != NULL && str_len(ctx->parser.last_comment) > 0) { |
3 |
19 |
22 |
|
| 803 |
3 |
| ctx->addr.comment = str_ccopy(ctx->parser.last_comment); |
| 804 |
3 |
| ctx->addr.comment_len = str_len(ctx->parser.last_comment); |
| 805 |
|
| } else if (ret2 == -3) { |
| 806 |
|
| #if 0 |
| 807 |
|
| /* So far we've read user without @domain and without |
| 808 |
|
| (Display Name). We'll assume that a single "user" (already |
| 809 |
|
| read into addr.mailbox) is a mailbox, but if it's followed |
| 810 |
|
| by anything else it's a display-name. */ |
| 811 |
|
| str_append_c(ctx->str, ' '); |
| 812 |
|
| size_t orig_str_len = str_len(ctx->str); |
| 813 |
|
| (void)rfc822_parse_phrase(&ctx->parser, ctx->str); |
| 814 |
|
| if (str_len(ctx->str) != orig_str_len) { |
| 815 |
|
| ctx->addr.mailbox = NULL; |
| 816 |
|
| ctx->addr.name = str_ccopy(ctx->str); |
| 817 |
|
| ctx->addr.name_len = str_len(ctx->str); |
| 818 |
|
| } else { |
| 819 |
|
| if (!quoted_string) |
| 820 |
|
| ctx->addr.domain = strdup(""); |
| 821 |
|
| } |
| 822 |
|
| ctx->addr.invalid_syntax = true; |
| 823 |
|
| ret = -1; |
| 824 |
|
| #endif |
| 825 |
|
| } |
| 826 |
114 |
| return ret; |
| 827 |
|
| } |
| 828 |
|
| |
| 829 |
67 |
| static void add_fixed_address(struct message_address_parser_context *ctx) |
| 830 |
|
| { |
| 831 |
67 |
▶ | if (ctx->addr.mailbox == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.mailbox == NULL) { |
7 |
60 |
67 |
|
| 832 |
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 |
|
| 833 |
7 |
| ctx->addr.invalid_syntax = true; |
| 834 |
|
| } |
| 835 |
67 |
▶ | 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) { |
59 |
8 |
67 |
| if (ctx->addr.domain == NULL || ctx->addr.domain_len == 0) { |
0 |
59 |
59 |
|
| 836 |
8 |
| free(ctx->addr.domain); |
| 837 |
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 |
|
| 838 |
8 |
| ctx->addr.invalid_syntax = true; |
| 839 |
|
| } |
| 840 |
67 |
| add_address(ctx); |
| 841 |
67 |
| } |
| 842 |
|
| |
| 843 |
67 |
| static int parse_mailbox(struct message_address_parser_context *ctx) |
| 844 |
|
| { |
| 845 |
|
| const unsigned char *start; |
| 846 |
|
| size_t len; |
| 847 |
|
| int ret; |
| 848 |
|
| |
| 849 |
|
| /* mailbox = name-addr / addr-spec */ |
| 850 |
67 |
| start = ctx->parser.data; |
| 851 |
67 |
▶ | if ((ret = parse_name_addr(ctx)) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = parse_name_addr(ctx)) < 0) { |
22 |
45 |
67 |
|
| 852 |
|
| /* nope, should be addr-spec */ |
| 853 |
22 |
▶ | if (ctx->addr.name != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.name != NULL) { |
0 |
22 |
22 |
|
| 854 |
0 |
| free(ctx->addr.name); |
| 855 |
0 |
| ctx->addr.name = NULL; |
| 856 |
|
| } |
| 857 |
22 |
▶ | if (ctx->addr.route != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.route != NULL) { |
0 |
22 |
22 |
|
| 858 |
0 |
| free(ctx->addr.route); |
| 859 |
0 |
| ctx->addr.route = NULL; |
| 860 |
|
| } |
| 861 |
22 |
▶ | if (ctx->addr.mailbox != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.mailbox != NULL) { |
0 |
22 |
22 |
|
| 862 |
0 |
| free(ctx->addr.mailbox); |
| 863 |
0 |
| ctx->addr.mailbox = NULL; |
| 864 |
|
| } |
| 865 |
22 |
▶ | if (ctx->addr.domain != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.domain != NULL) { |
0 |
22 |
22 |
|
| 866 |
0 |
| free(ctx->addr.domain); |
| 867 |
0 |
| ctx->addr.domain = NULL; |
| 868 |
|
| } |
| 869 |
22 |
▶ | if (ctx->addr.comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.comment != NULL) { |
0 |
22 |
22 |
|
| 870 |
0 |
| free(ctx->addr.comment); |
| 871 |
0 |
| ctx->addr.comment = NULL; |
| 872 |
|
| } |
| 873 |
22 |
▶ | if (ctx->addr.original != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->addr.original != NULL) { |
0 |
22 |
22 |
|
| 874 |
0 |
| free(ctx->addr.original); |
| 875 |
0 |
| ctx->addr.original = NULL; |
| 876 |
|
| } |
| 877 |
22 |
| ctx->parser.data = start; |
| 878 |
22 |
| ret = parse_addr_spec(ctx); |
| 879 |
22 |
▶ | 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 |
14 |
22 |
| if (ctx->addr.invalid_syntax && ctx->addr.name == NULL && |
8 |
0 |
8 |
|
| 880 |
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 |
|
| 881 |
6 |
| ctx->addr.name = ctx->addr.mailbox; |
| 882 |
6 |
| ctx->addr.name_len = ctx->addr.mailbox_len; |
| 883 |
6 |
| ctx->addr.mailbox = NULL; |
| 884 |
6 |
| ctx->addr.mailbox_len = 0; |
| 885 |
|
| } |
| 886 |
|
| } |
| 887 |
|
| |
| 888 |
67 |
▶ | if (ret < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) |
3 |
64 |
67 |
|
| 889 |
3 |
| ctx->addr.invalid_syntax = true; |
| 890 |
|
| |
| 891 |
67 |
| len = ctx->parser.data - start; |
| 892 |
67 |
| ctx->addr.original = malloc(len + 1); |
| 893 |
67 |
▶ | if (!ctx->addr.original) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx->addr.original) |
0 |
67 |
67 |
|
| 894 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 895 |
|
| |
| 896 |
67 |
| memcpy(ctx->addr.original, start, len); |
| 897 |
67 |
| ctx->addr.original[len] = 0; |
| 898 |
67 |
| ctx->addr.original_len = len; |
| 899 |
|
| |
| 900 |
67 |
| add_fixed_address(ctx); |
| 901 |
|
| |
| 902 |
67 |
| free(ctx->addr.original); |
| 903 |
67 |
| ctx->addr.original = NULL; |
| 904 |
67 |
| return ret; |
| 905 |
|
| } |
| 906 |
|
| |
| 907 |
66 |
| static int parse_group(struct message_address_parser_context *ctx) |
| 908 |
|
| { |
| 909 |
|
| int ret; |
| 910 |
|
| |
| 911 |
|
| /* |
| 912 |
|
| group = display-name ":" [mailbox-list / CFWS] ";" [CFWS] |
| 913 |
|
| display-name = phrase |
| 914 |
|
| */ |
| 915 |
66 |
| str_truncate(ctx->str, 0); |
| 916 |
66 |
▶ | if (rfc822_parse_phrase(&ctx->parser, ctx->str) <= 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_parse_phrase(&ctx->parser, ctx->str) <= 0 || |
59 |
7 |
66 |
|
| 917 |
59 |
▶ | *ctx->parser.data != ':') |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data != ':') |
50 |
9 |
59 |
|
| 918 |
57 |
| return -1; |
| 919 |
|
| |
| 920 |
|
| /* from now on don't return -1 even if there are problems, so that |
| 921 |
|
| the caller knows this is a group */ |
| 922 |
9 |
| ctx->parser.data++; |
| 923 |
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 |
|
| 924 |
0 |
| ctx->addr.invalid_syntax = true; |
| 925 |
|
| |
| 926 |
9 |
| ctx->addr.mailbox = str_ccopy(ctx->str); |
| 927 |
9 |
| ctx->addr.mailbox_len = str_len(ctx->str); |
| 928 |
9 |
| add_address(ctx); |
| 929 |
|
| |
| 930 |
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 |
|
| 931 |
|
| for (;;) { |
| 932 |
|
| /* mailbox-list = |
| 933 |
|
| (mailbox *("," mailbox)) / obs-mbox-list */ |
| 934 |
10 |
| if (parse_mailbox(ctx) <= 0) { |
| 935 |
|
| /* broken mailbox - try to continue anyway. */ |
| 936 |
|
| } |
| 937 |
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 |
|
| 938 |
9 |
▶ | *ctx->parser.data != ',') |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data != ',') |
3 |
6 |
9 |
|
| 939 |
|
| break; |
| 940 |
3 |
| ctx->parser.data++; |
| 941 |
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 |
|
| 942 |
0 |
| ret = -1; |
| 943 |
0 |
| break; |
| 944 |
|
| } |
| 945 |
|
| } |
| 946 |
|
| } |
| 947 |
9 |
▶ | if (ret >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret >= 0) { |
9 |
0 |
9 |
|
| 948 |
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 |
|
| 949 |
8 |
▶ | *ctx->parser.data != ';') |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data != ';') |
0 |
8 |
8 |
|
| 950 |
1 |
| ret = -1; |
| 951 |
|
| else { |
| 952 |
8 |
| ctx->parser.data++; |
| 953 |
8 |
| ret = rfc822_skip_lwsp(&ctx->parser); |
| 954 |
|
| } |
| 955 |
|
| } |
| 956 |
9 |
▶ | if (ret < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) |
1 |
8 |
9 |
|
| 957 |
1 |
| ctx->addr.invalid_syntax = true; |
| 958 |
|
| |
| 959 |
9 |
| add_address(ctx); |
| 960 |
9 |
| return ret == 0 ? 0 : 1; |
| 961 |
|
| } |
| 962 |
|
| |
| 963 |
66 |
| static int parse_address(struct message_address_parser_context *ctx) |
| 964 |
|
| { |
| 965 |
|
| const unsigned char *start; |
| 966 |
|
| int ret; |
| 967 |
|
| |
| 968 |
|
| /* address = mailbox / group */ |
| 969 |
66 |
| start = ctx->parser.data; |
| 970 |
66 |
▶ | if ((ret = parse_group(ctx)) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = parse_group(ctx)) < 0) { |
57 |
9 |
66 |
|
| 971 |
|
| /* not a group, try mailbox */ |
| 972 |
57 |
| ctx->parser.data = start; |
| 973 |
57 |
| ret = parse_mailbox(ctx); |
| 974 |
|
| } |
| 975 |
66 |
| return ret; |
| 976 |
|
| } |
| 977 |
|
| |
| 978 |
40 |
| static int parse_address_list(struct message_address_parser_context *ctx, |
| 979 |
|
| unsigned int max_addresses) |
| 980 |
|
| { |
| 981 |
|
| const unsigned char *start; |
| 982 |
|
| size_t len; |
| 983 |
40 |
| int ret = 0; |
| 984 |
|
| |
| 985 |
|
| /* address-list = (address *("," address)) / obs-addr-list */ |
| 986 |
66 |
▶ | while (max_addresses > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (max_addresses > 0) { |
66 |
0 |
66 |
|
| 987 |
66 |
| max_addresses--; |
| 988 |
66 |
▶ | if ((ret = parse_address(ctx)) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = parse_address(ctx)) == 0) |
36 |
30 |
66 |
|
| 989 |
36 |
| break; |
| 990 |
30 |
▶ | if (ctx->parser.data >= ctx->parser.end || |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->parser.data >= ctx->parser.end || |
26 |
4 |
30 |
|
| 991 |
26 |
▶ | *ctx->parser.data != ',') { |
Branch: 1 branch
| Branch | True | False | Total |
| *ctx->parser.data != ',') { |
0 |
26 |
26 |
|
| 992 |
4 |
| ret = -1; |
| 993 |
4 |
| break; |
| 994 |
|
| } |
| 995 |
26 |
| ctx->parser.data++; |
| 996 |
26 |
| start = ctx->parser.data; |
| 997 |
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 |
|
| 998 |
0 |
▶ | if (ret < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) { |
0 |
0 |
0 |
|
| 999 |
|
| /* ends with some garbage */ |
| 1000 |
0 |
| len = ctx->parser.data - start; |
| 1001 |
0 |
| ctx->addr.original = malloc(len + 1); |
| 1002 |
0 |
▶ | if (!ctx->addr.original) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx->addr.original) |
0 |
0 |
0 |
|
| 1003 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 1004 |
|
| |
| 1005 |
0 |
| memcpy(ctx->addr.original, start, len); |
| 1006 |
0 |
| ctx->addr.original[len] = 0; |
| 1007 |
0 |
| ctx->addr.original_len = len; |
| 1008 |
|
| |
| 1009 |
0 |
| add_fixed_address(ctx); |
| 1010 |
|
| |
| 1011 |
0 |
| free(ctx->addr.original); |
| 1012 |
0 |
| ctx->addr.original = NULL; |
| 1013 |
|
| } |
| 1014 |
0 |
| break; |
| 1015 |
|
| } |
| 1016 |
|
| } |
| 1017 |
40 |
| return ret; |
| 1018 |
|
| } |
| 1019 |
|
| |
| 1020 |
255 |
| static char *mem_copy(const char *mem, size_t len) |
| 1021 |
|
| { |
| 1022 |
|
| char *copy; |
| 1023 |
|
| |
| 1024 |
255 |
| copy = malloc(len+1); |
| 1025 |
255 |
▶ | if (!copy) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!copy) |
0 |
255 |
255 |
|
| 1026 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 1027 |
|
| |
| 1028 |
255 |
| memcpy(copy, mem, len); |
| 1029 |
255 |
| copy[len] = 0; |
| 1030 |
255 |
| return copy; |
| 1031 |
|
| } |
| 1032 |
|
| |
| 1033 |
122 |
| void message_address_add(struct message_address **first, struct message_address **last, |
| 1034 |
|
| const char *name, size_t name_len, const char *route, size_t route_len, |
| 1035 |
|
| const char *mailbox, size_t mailbox_len, const char *domain, size_t domain_len, |
| 1036 |
|
| const char *comment, size_t comment_len) |
| 1037 |
|
| { |
| 1038 |
|
| struct message_address *message; |
| 1039 |
|
| |
| 1040 |
122 |
| message = malloc(sizeof(struct message_address)); |
| 1041 |
122 |
▶ | if (!message) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!message) |
0 |
122 |
122 |
|
| 1042 |
0 |
| i_panic("malloc() failed: %s", strerror(errno)); |
| 1043 |
|
| |
| 1044 |
122 |
▶ | 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; |
53 |
69 |
122 |
|
| 1045 |
122 |
| message->name_len = name_len; |
| 1046 |
122 |
▶ | 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 |
122 |
122 |
|
| 1047 |
122 |
| message->route_len = route_len; |
| 1048 |
122 |
▶ | 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; |
101 |
21 |
122 |
|
| 1049 |
122 |
| message->mailbox_len = mailbox_len; |
| 1050 |
122 |
▶ | 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; |
80 |
42 |
122 |
|
| 1051 |
122 |
| message->domain_len = domain_len; |
| 1052 |
122 |
▶ | 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 |
101 |
122 |
|
| 1053 |
122 |
| message->comment_len = comment_len; |
| 1054 |
122 |
| message->original = NULL; |
| 1055 |
122 |
| message->original_len = 0; |
| 1056 |
122 |
| message->next = NULL; |
| 1057 |
|
| |
| 1058 |
122 |
▶ | if (!*first) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*first) |
58 |
64 |
122 |
|
| 1059 |
58 |
| *first = message; |
| 1060 |
|
| else |
| 1061 |
64 |
| (*last)->next = message; |
| 1062 |
|
| |
| 1063 |
122 |
| *last = message; |
| 1064 |
122 |
| } |
| 1065 |
|
| |
| 1066 |
124 |
| void message_address_free(struct message_address **addr) |
| 1067 |
|
| { |
| 1068 |
|
| struct message_address *current; |
| 1069 |
|
| struct message_address *next; |
| 1070 |
|
| |
| 1071 |
124 |
| current = *addr; |
| 1072 |
|
| |
| 1073 |
331 |
▶ | while (current) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (current) { |
207 |
124 |
331 |
|
| 1074 |
207 |
| next = current->next; |
| 1075 |
207 |
| free(current->name); |
| 1076 |
207 |
| free(current->route); |
| 1077 |
207 |
| free(current->mailbox); |
| 1078 |
207 |
| free(current->domain); |
| 1079 |
207 |
| free(current->comment); |
| 1080 |
207 |
| free(current->original); |
| 1081 |
207 |
| free(current); |
| 1082 |
207 |
| current = next; |
| 1083 |
|
| } |
| 1084 |
|
| |
| 1085 |
124 |
| *addr = NULL; |
| 1086 |
124 |
| } |
| 1087 |
|
| |
| 1088 |
|
| struct message_address * |
| 1089 |
48 |
| message_address_parse(const char *input, size_t input_len, |
| 1090 |
|
| unsigned int max_addresses, bool fill_missing) |
| 1091 |
|
| { |
| 1092 |
|
| string_t *str; |
| 1093 |
|
| struct message_address_parser_context ctx; |
| 1094 |
|
| |
| 1095 |
48 |
| memset(&ctx, 0, sizeof(ctx)); |
| 1096 |
|
| |
| 1097 |
48 |
| str = str_new(128); |
| 1098 |
|
| |
| 1099 |
48 |
| rfc822_parser_init(&ctx.parser, (const unsigned char *)input, input_len, str); |
| 1100 |
|
| |
| 1101 |
48 |
▶ | if (rfc822_skip_lwsp(&ctx.parser) <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rfc822_skip_lwsp(&ctx.parser) <= 0) { |
8 |
40 |
48 |
|
| 1102 |
|
| /* no addresses */ |
| 1103 |
8 |
| str_free(&str); |
| 1104 |
8 |
| return NULL; |
| 1105 |
|
| } |
| 1106 |
|
| |
| 1107 |
40 |
| ctx.str = str_new(128); |
| 1108 |
40 |
| ctx.fill_missing = fill_missing; |
| 1109 |
|
| |
| 1110 |
40 |
| (void)parse_address_list(&ctx, max_addresses); |
| 1111 |
|
| |
| 1112 |
40 |
| str_free(&ctx.str); |
| 1113 |
40 |
| str_free(&str); |
| 1114 |
|
| |
| 1115 |
40 |
| return ctx.first_addr; |
| 1116 |
|
| } |
| 1117 |
|
| |
| 1118 |
73 |
| static bool has_mime_word(const char *str, size_t len) |
| 1119 |
|
| { |
| 1120 |
|
| const char *ptr; |
| 1121 |
|
| const char *end; |
| 1122 |
|
| |
| 1123 |
73 |
| ptr = str; |
| 1124 |
73 |
| end = str+len; |
| 1125 |
|
| |
| 1126 |
73 |
▶ | while ((ptr = memchr(ptr, '=', end - ptr)) != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((ptr = memchr(ptr, '=', end - ptr)) != NULL) { |
3 |
70 |
73 |
|
| 1127 |
3 |
| ptr++; |
| 1128 |
3 |
▶ | if (*ptr == '?') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ptr == '?') |
3 |
0 |
3 |
|
| 1129 |
3 |
| return true; |
| 1130 |
|
| } |
| 1131 |
|
| |
| 1132 |
70 |
| return false; |
| 1133 |
|
| } |
| 1134 |
|
| |
| 1135 |
76 |
| void message_address_write(char **output, size_t *output_len, const struct message_address *addr) |
| 1136 |
|
| { |
| 1137 |
|
| string_t *str; |
| 1138 |
|
| const char *tmp; |
| 1139 |
76 |
| bool first = true, in_group = false; |
| 1140 |
|
| |
| 1141 |
76 |
| str = str_new(128); |
| 1142 |
|
| |
| 1143 |
|
| /* a) mailbox@domain |
| 1144 |
|
| b) name <@route:mailbox@domain> |
| 1145 |
|
| c) group: .. ; */ |
| 1146 |
|
| |
| 1147 |
198 |
▶ | while (addr != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (addr != NULL) { |
122 |
76 |
198 |
|
| 1148 |
122 |
▶ | if (first) |
Branch: 1 branch
| Branch | True | False | Total |
| if (first) |
79 |
43 |
122 |
|
| 1149 |
79 |
| first = false; |
| 1150 |
|
| else |
| 1151 |
43 |
| str_append(str, ", "); |
| 1152 |
|
| |
| 1153 |
122 |
▶ | if (addr->domain == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->domain == NULL) { |
42 |
80 |
122 |
|
| 1154 |
42 |
▶ | if (!in_group) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in_group) { |
21 |
21 |
42 |
|
| 1155 |
|
| /* beginning of group. mailbox is the group |
| 1156 |
|
| name, others are NULL. */ |
| 1157 |
21 |
▶ | 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 |
|
| 1158 |
|
| /* check for MIME encoded-word */ |
| 1159 |
40 |
▶ | if (has_mime_word(addr->mailbox, addr->mailbox_len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_mime_word(addr->mailbox, addr->mailbox_len)) |
1 |
19 |
20 |
|
| 1160 |
|
| /* MIME encoded-word MUST NOT appear within a 'quoted-string' |
| 1161 |
|
| so escaping and quoting of phrase is not possible, instead |
| 1162 |
|
| use obsolete RFC822 phrase syntax which allow spaces */ |
| 1163 |
1 |
| str_append_data(str, addr->mailbox, addr->mailbox_len); |
| 1164 |
|
| else |
| 1165 |
19 |
| str_append_maybe_escape(str, addr->mailbox, addr->mailbox_len, true); |
| 1166 |
|
| } else { |
| 1167 |
|
| /* empty group name needs to be quoted */ |
| 1168 |
1 |
| str_append(str, "\"\""); |
| 1169 |
|
| } |
| 1170 |
21 |
| str_append(str, ": "); |
| 1171 |
21 |
| first = true; |
| 1172 |
|
| } else { |
| 1173 |
|
| /* end of group. all fields should be NULL. */ |
| 1174 |
|
| i_assert(addr->mailbox == NULL); |
| 1175 |
|
| |
| 1176 |
|
| /* cut out the ", " */ |
| 1177 |
21 |
| tmp = str_c(str)+str_len(str)-2; |
| 1178 |
|
| i_assert((tmp[0] == ',' || tmp[0] == ':') && tmp[1] == ' '); |
| 1179 |
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 |
|
| 1180 |
15 |
| str_truncate(str, str_len(str)-2); |
| 1181 |
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 |
|
| 1182 |
6 |
| str_truncate(str, str_len(str)-1); |
| 1183 |
21 |
| str_append_c(str, ';'); |
| 1184 |
|
| } |
| 1185 |
|
| |
| 1186 |
42 |
| in_group = !in_group; |
| 1187 |
80 |
▶ | } 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) && |
53 |
27 |
80 |
| } else if ((addr->name == NULL || addr->name_len == 0) && |
0 |
53 |
53 |
|
| 1188 |
27 |
▶ | addr->route == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| addr->route == NULL) { |
27 |
0 |
27 |
|
| 1189 |
|
| /* no name and no route. use only mailbox@domain */ |
| 1190 |
|
| i_assert(addr->mailbox != NULL); |
| 1191 |
|
| |
| 1192 |
27 |
| str_append_maybe_escape(str, addr->mailbox, addr->mailbox_len, false); |
| 1193 |
27 |
| str_append_c(str, '@'); |
| 1194 |
27 |
| str_append_data(str, addr->domain, addr->domain_len); |
| 1195 |
|
| |
| 1196 |
27 |
▶ | if (addr->comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->comment != NULL) { |
2 |
25 |
27 |
|
| 1197 |
2 |
| str_append(str, " ("); |
| 1198 |
2 |
| str_append_data(str, addr->comment, addr->comment_len); |
| 1199 |
2 |
| str_append_c(str, ')'); |
| 1200 |
|
| } |
| 1201 |
|
| } else { |
| 1202 |
|
| /* name and/or route. use full <mailbox@domain> Name */ |
| 1203 |
|
| i_assert(addr->mailbox != NULL); |
| 1204 |
|
| |
| 1205 |
53 |
▶ | if (addr->name != NULL && addr->name_len != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (addr->name != NULL && addr->name_len != 0) { |
53 |
0 |
53 |
| if (addr->name != NULL && addr->name_len != 0) { |
53 |
0 |
53 |
|
| 1206 |
|
| /* check for MIME encoded-word */ |
| 1207 |
53 |
▶ | if (has_mime_word(addr->name, addr->name_len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_mime_word(addr->name, addr->name_len)) |
2 |
51 |
53 |
|
| 1208 |
|
| /* MIME encoded-word MUST NOT appear within a 'quoted-string' |
| 1209 |
|
| so escaping and quoting of phrase is not possible, instead |
| 1210 |
|
| use obsolete RFC822 phrase syntax which allow spaces */ |
| 1211 |
2 |
| str_append_data(str, addr->name, addr->name_len); |
| 1212 |
|
| else |
| 1213 |
51 |
| str_append_maybe_escape(str, addr->name, addr->name_len, true); |
| 1214 |
|
| } |
| 1215 |
53 |
▶ | if (addr->route != NULL || |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->route != NULL || |
53 |
0 |
53 |
|
| 1216 |
53 |
▶ | addr->mailbox_len != 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| addr->mailbox_len != 0 || |
1 |
52 |
53 |
|
| 1217 |
1 |
▶ | addr->domain_len != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| addr->domain_len != 0) { |
1 |
0 |
1 |
|
| 1218 |
53 |
▶ | if (addr->name != NULL && addr->name_len != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (addr->name != NULL && addr->name_len != 0) |
53 |
0 |
53 |
| if (addr->name != NULL && addr->name_len != 0) |
53 |
0 |
53 |
|
| 1219 |
53 |
| str_append_c(str, ' '); |
| 1220 |
53 |
| str_append_c(str, '<'); |
| 1221 |
53 |
▶ | if (addr->route != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->route != NULL) { |
0 |
53 |
53 |
|
| 1222 |
0 |
| str_append_data(str, addr->route, addr->route_len); |
| 1223 |
0 |
| str_append_c(str, ':'); |
| 1224 |
|
| } |
| 1225 |
53 |
| str_append_maybe_escape(str, addr->mailbox, addr->mailbox_len, false); |
| 1226 |
53 |
▶ | if (addr->domain_len != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->domain_len != 0) { |
53 |
0 |
53 |
|
| 1227 |
53 |
| str_append_c(str, '@'); |
| 1228 |
53 |
| str_append_data(str, addr->domain, addr->domain_len); |
| 1229 |
|
| } |
| 1230 |
53 |
| str_append_c(str, '>'); |
| 1231 |
|
| } |
| 1232 |
53 |
▶ | if (addr->comment != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr->comment != NULL) { |
19 |
34 |
53 |
|
| 1233 |
19 |
| str_append(str, " ("); |
| 1234 |
19 |
| str_append_data(str, addr->comment, addr->comment_len); |
| 1235 |
19 |
| str_append_c(str, ')'); |
| 1236 |
|
| } |
| 1237 |
|
| } |
| 1238 |
|
| |
| 1239 |
122 |
| addr = addr->next; |
| 1240 |
|
| } |
| 1241 |
|
| |
| 1242 |
76 |
| *output = str_ccopy(str); |
| 1243 |
76 |
| *output_len = str_len(str); |
| 1244 |
76 |
| str_free(&str); |
| 1245 |
76 |
| } |
| 1246 |
|
| |
| 1247 |
101 |
| void compose_address(char **output, size_t *output_len, const char *mailbox, size_t mailbox_len, const char *domain, size_t domain_len) |
| 1248 |
|
| { |
| 1249 |
|
| string_t *str; |
| 1250 |
|
| |
| 1251 |
101 |
| str = str_new(128); |
| 1252 |
|
| |
| 1253 |
101 |
| str_append_maybe_escape(str, mailbox, mailbox_len, false); |
| 1254 |
101 |
| str_append_c(str, '@'); |
| 1255 |
101 |
| str_append_data(str, domain, domain_len); |
| 1256 |
|
| |
| 1257 |
101 |
| *output = str_ccopy(str); |
| 1258 |
101 |
| *output_len = str_len(str); |
| 1259 |
101 |
| str_free(&str); |
| 1260 |
101 |
| } |
| 1261 |
|
| |
| 1262 |
93 |
| void split_address(const char *input, size_t input_len, char **mailbox, size_t *mailbox_len, char **domain, size_t *domain_len) |
| 1263 |
|
| { |
| 1264 |
|
| struct message_address_parser_context ctx; |
| 1265 |
|
| int ret; |
| 1266 |
|
| |
| 1267 |
93 |
▶ | if (!input || !input[0]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!input || !input[0]) { |
93 |
0 |
93 |
| if (!input || !input[0]) { |
1 |
92 |
93 |
|
| 1268 |
1 |
| *mailbox = NULL; |
| 1269 |
1 |
| *mailbox_len = 0; |
| 1270 |
1 |
| *domain = NULL; |
| 1271 |
1 |
| *domain_len = 0; |
| 1272 |
1 |
| return; |
| 1273 |
|
| } |
| 1274 |
|
| |
| 1275 |
92 |
| memset(&ctx, 0, sizeof(ctx)); |
| 1276 |
|
| |
| 1277 |
92 |
| rfc822_parser_init(&ctx.parser, (const unsigned char *)input, input_len, NULL); |
| 1278 |
|
| |
| 1279 |
92 |
| ctx.str = str_new(128); |
| 1280 |
92 |
| ctx.fill_missing = false; |
| 1281 |
|
| |
| 1282 |
92 |
| ret = rfc822_skip_lwsp(&ctx.parser); |
| 1283 |
|
| |
| 1284 |
92 |
▶ | if (ret > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret > 0) |
92 |
0 |
92 |
|
| 1285 |
92 |
| ret = parse_addr_spec(&ctx); |
| 1286 |
|
| else |
| 1287 |
0 |
| ret = -1; |
| 1288 |
|
| |
| 1289 |
92 |
▶ | 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) { |
91 |
1 |
92 |
| if (ret < 0 || ctx.parser.data != ctx.parser.end || ctx.addr.invalid_syntax) { |
89 |
2 |
91 |
| if (ret < 0 || ctx.parser.data != ctx.parser.end || ctx.addr.invalid_syntax) { |
4 |
85 |
89 |
|
| 1290 |
7 |
| free(ctx.addr.mailbox); |
| 1291 |
7 |
| free(ctx.addr.domain); |
| 1292 |
7 |
| *mailbox = NULL; |
| 1293 |
7 |
| *mailbox_len = 0; |
| 1294 |
7 |
| *domain = NULL; |
| 1295 |
7 |
| *domain_len = 0; |
| 1296 |
|
| } else { |
| 1297 |
85 |
| *mailbox = ctx.addr.mailbox; |
| 1298 |
85 |
| *mailbox_len = ctx.addr.mailbox_len; |
| 1299 |
85 |
| *domain = ctx.addr.domain; |
| 1300 |
85 |
| *domain_len = ctx.addr.domain_len; |
| 1301 |
|
| } |
| 1302 |
|
| |
| 1303 |
92 |
| free(ctx.addr.comment); |
| 1304 |
92 |
| free(ctx.addr.route); |
| 1305 |
92 |
| free(ctx.addr.name); |
| 1306 |
92 |
| free(ctx.addr.original); |
| 1307 |
|
| |
| 1308 |
92 |
| str_free(&ctx.str); |
| 1309 |
|
| } |
| 1310 |
|
| |
| 1311 |
363 |
| void string_free(char *string) |
| 1312 |
|
| { |
| 1313 |
363 |
| free(string); |
| 1314 |
363 |
| } |