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