FreeCalypso > hg > falcon-mail-tools
annotate f-demime/b2q_out.c @ 2:1857d0d5a7bd
f-demime: fix parsing of Content-* headers
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 06 May 2023 06:33:26 +0000 |
parents | 7e0d08176f32 |
children |
rev | line source |
---|---|
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements the output side of base64-to-QP conversion. |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <stdio.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdlib.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <string.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <strings.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include "defs.h" |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 extern FILE *tempfile; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 static char linebuf[QP_MAX_LINE]; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 static unsigned line_len, trailing_newline; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 static void |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 add_plain_char(ch) |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 linebuf[line_len++] = ch; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 linebuf[line_len] = '\0'; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 if (!strcmp(linebuf, "From ")) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 strcpy(linebuf, "=46rom "); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 line_len = 7; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 } else if (!strcmp(linebuf, "--")) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 strcpy(linebuf, "=2D-"); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 line_len = 4; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 } else if (!strcmp(linebuf, ".")) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 strcpy(linebuf, "=2E"); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 line_len = 3; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 static void |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 add_octet_char(ch) |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 sprintf(linebuf + line_len, "=%02X", ch); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 line_len += 3; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 static void |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 process_newline() |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 if (line_len) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 if (linebuf[line_len-1] == ' ') |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 strcpy(linebuf + line_len - 1, "=20"); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 fputs(linebuf, stdout); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 line_len = 0; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 putchar('\n'); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 static void |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 process_char(ch) |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (ch == '\n') { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 process_newline(); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 trailing_newline++; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 if (trailing_newline > 2) |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 trailing_newline = 2; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 return; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (line_len >= QP_MAX_LINE - 2) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 printf("%s=\n", linebuf); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 line_len = 0; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 if (ch == '=') |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 add_octet_char(ch); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 else if (ch >= ' ' && ch <= '~') |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 add_plain_char(ch); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 else |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 add_octet_char(ch); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 trailing_newline = 0; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 void |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 b2q_emit_output() |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 int c; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 rewind(tempfile); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 line_len = 0; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 trailing_newline = 1; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 while ((c = getc(tempfile)) != EOF) |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 process_char(c); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 fclose(tempfile); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 while (trailing_newline < 2) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 process_newline(); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 trailing_newline++; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 } |