/src/dovecot/src/lib-mail/quoted-printable.c
Line | Count | Source |
1 | | /* Copyright (c) Dovecot authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "buffer.h" |
5 | | #include "hex-binary.h" |
6 | | #include "quoted-printable.h" |
7 | | |
8 | | int quoted_printable_q_decode(const unsigned char *src, size_t src_size, |
9 | | buffer_t *dest) |
10 | 2.66k | { |
11 | 2.66k | char hexbuf[3]; |
12 | 2.66k | size_t src_pos, next; |
13 | 2.66k | bool errors = FALSE; |
14 | | |
15 | 2.66k | hexbuf[2] = '\0'; |
16 | | |
17 | 2.66k | next = 0; |
18 | 6.01M | for (src_pos = 0; src_pos < src_size; src_pos++) { |
19 | 6.00M | if (src[src_pos] != '_' && src[src_pos] != '=') |
20 | 5.99M | continue; |
21 | | |
22 | 17.0k | buffer_append(dest, src + next, src_pos - next); |
23 | 17.0k | next = src_pos; |
24 | | |
25 | 17.0k | if (src[src_pos] == '_') { |
26 | 433 | buffer_append_c(dest, ' '); |
27 | 433 | next++; |
28 | 433 | continue; |
29 | 433 | } |
30 | | |
31 | 16.6k | if (src_pos+2 >= src_size) |
32 | 576 | break; |
33 | | |
34 | | /* =<hex> */ |
35 | 16.0k | hexbuf[0] = (char) src[src_pos+1]; |
36 | 16.0k | hexbuf[1] = (char) src[src_pos+2]; |
37 | | |
38 | 16.0k | if (hex_to_binary(hexbuf, dest) == 0) { |
39 | 1.50k | src_pos += 2; |
40 | 1.50k | next = src_pos+1; |
41 | 14.5k | } else { |
42 | | /* non-hex data, show as-is */ |
43 | 14.5k | errors = TRUE; |
44 | 14.5k | next = src_pos; |
45 | 14.5k | } |
46 | 16.0k | } |
47 | 2.66k | buffer_append(dest, src + next, src_size - next); |
48 | 2.66k | return errors ? -1 : 0; |
49 | 2.66k | } |