1 /* encode a mime message */
8 static char out
[LNSZ
* 2]; /* collected characters */
9 static int out_n
; /* number of characters in out */
10 static int out_mime
; /* set if inside a mime encoded word */
11 static int out_mbuf
; /* buffered characters */
12 static int out_mlen
; /* number of buffered characters */
13 static int out_mhdr
; /* header type; 's' for subject 'a' for address */
15 static char *b64_chr
=
16 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
18 /* encode 3 bytes in base64 */
19 static void b64_word(char *s
, unsigned num
)
21 s
[3] = b64_chr
[num
& 0x3f];
23 s
[2] = b64_chr
[num
& 0x3f];
25 s
[1] = b64_chr
[num
& 0x3f];
27 s
[0] = b64_chr
[num
& 0x3f];
30 static void q_beg(void)
45 static void q_put(int c
)
48 out_mbuf
= (out_mbuf
<< 8) | c
;
50 b64_word(out
+ out_n
, out_mbuf
);
57 static void q_end(void)
60 for (i
= 0; out_mlen
; i
++)
63 out
[out_n
- i
- 1] = '=';
69 static void out_flush(void)
78 static void out_chr(int c
)
81 if (c
== '\r' || c
== '\n') {
83 } else if (out_mhdr
== 'a' && c
!= ' ' && ~c
& 0x80) {
85 } else if (out_n
> 65 && (c
& 0xc0) != 0x80) {
93 if (c
== '\n' || out_n
> 75) {
97 if (out_mhdr
&& (c
& 0x80) && !out_mime
)
106 static int startswith(char *r
, char *s
)
109 if (tolower((unsigned char) *s
++) != tolower((unsigned char) *r
++))
117 while (fgets(ln
, sizeof(ln
), stdin
)) {
119 if (ln
[0] != ' ' && ln
[0] != '\t')
121 if (startswith(ln
, "from:") || startswith(ln
, "to:") ||
122 startswith(ln
, "cc:") || startswith(ln
, "bcc:"))
124 if (startswith(ln
, "subject:"))
127 out_chr((unsigned char) *s
++);
128 if (ln
[0] == '\n' || ln
[0] == '\r')
131 while (fgets(ln
, sizeof(ln
), stdin
))