mess822x 1.23
mess822x
Loading...
Searching...
No Matches
822field.c
Go to the documentation of this file.
1#include "buffer.h"
2#include "logmsg.h"
3#include "getln.h"
4#include "mess822.h"
5#include "case.h"
6#include "exit.h"
7#include "str.h"
8
9#define WHO "822field"
10
11static void nomem()
12{
13 logmsg(WHO,111,FATAL,"out of memory");
14}
15
16int flag;
17stralloc value = {0};
18
21 { "subject", &flag, 0, &value, 0, 0, 0 } // initial values
22, { 0, 0, 0, 0, 0, 0, 0 } // occurances
23} ;
24
25stralloc line = {0};
27
28/* The 'header' line may be word-wise encoded;
29 we need to comply with this.
30*/
31
32int header_decode(stralloc *header)
33{
34 unsigned char ch;
35 int seenq = 0;
36 int mimetype = 0;
37 int done = 0;
38
39 stralloc in = {0}; // input for decoding
40 stralloc out = {0}; // output of entire header line
41 stralloc word = {0}; // result of decoding word/line
42 if (!stralloc_copys(&in,"")) nomem();
43 if (!stralloc_copys(&out,"")) nomem();
44
45 for (int i = 0; i < header->len; i++) {
46 ch = header->s[i];
47 if (ch == '\0') goto DONE;
48 if (i == header->len - 2 && ch == '=') goto DONE;
49
50 if (ch == '?') seenq++;
51 if (seenq == 2) {
52 if (!stralloc_copys(&in,"")) nomem();
53 if (ch == 'q' || ch == 'Q') { // new QP word
54 mimetype = 5;
55 } else if (ch == 'b' || ch == 'B') { // new BASE64 word
56 mimetype = 3;
57 }
58 continue;
59 }
60
61 // Get Word or Line
62
63 if (mimetype) {
64 if (seenq == 3 && ch != '?' && ch != ' ') if (!stralloc_append(&in,&ch)) nomem();
65 if (seenq == 4) { done = 1; if (!stralloc_0(&in)) nomem(); }
66 if (ch == ' ' || ch == '\n' || ch == '\t') { if (!stralloc_0(&in)) nomem(); done = 1; }
67 } else if (ch != '=') {
68 if (!stralloc_append(&in,&ch)) nomem();
69 }
70
71 if (i == header->len - 1) done = 1; // nothing to follow
72 if (!done) continue;
73
74 // For each word or line -> decode
75
76 switch (mimetype) {
77 case 0: if (!stralloc_0(&in)) nomem();
78 if (!stralloc_catb(&out,in.s,in.len)) nomem();
79 if (!stralloc_copys(&in,"")) nomem();
80 done = 0;
81 break;
82 case 3: if (!stralloc_copys(&word,"")) nomem();
83 if ((done = mess822_b64decode(&word,in.s,in.len,4648)) > 0) {
84 if (!stralloc_cat(&out,&word)) nomem();
85 if (!stralloc_0(&out)) nomem();
86 if (!stralloc_copys(&in,"")) nomem();
87 } else {
88 logmsg(WHO,0,INFO,B("error decoding header: ",word.s));
89 if (!stralloc_catb(&out,in.s,in.len)) nomem();
90 } done = 0; seenq = 0; mimetype = 0; break;
91 case 5: if (!stralloc_copys(&word,"")) nomem();
92 if ((done = mess822_qpdecode(&word,in.s,in.len,2047)) > 0) {
93 if (!stralloc_cat(&out,&word)) nomem();
94 if (!stralloc_0(&out)) nomem();
95 if (!stralloc_copys(&in,"")) nomem();
96 } else {
97 if (!stralloc_catb(&out,in.s,in.len)) nomem();
98 logmsg(WHO,0,INFO,B("error decoding header: ",word.s));
99 if (!stralloc_catb(&out,in.s,in.len)) nomem();
100 } done = 0; seenq = 0; mimetype = 0; break;
101 }
102 }
103
104
105
106DONE:
107 if (!stralloc_copyb(header,out.s,out.len)) nomem();
108 if (!stralloc_append(header,"\n")) nomem();
109 if (!stralloc_0(header)) nomem();
110
111 return out.len - 1;
112}
113
114int main(int argc,char **argv)
115{
116 if (argv[1]) {
117 if (*argv[1] == '+') {
118 a->verbose = 1;
119 if (str_len(argv[1]) == 1)
120 a[0].name = "subject";
121 else
122 a[0].name = argv[1] + 1;
123 } else
124 a[0].name = argv[1];
125 }
126
127 if (!mess822_begin(&h,a)) nomem();
128
129 for (;;) {
130 if (getln(buffer_0,&line,&match,'\n') == -1)
131 logmsg(WHO,111,FATAL,"unable to read input ");
132
133 if (!mess822_ok(&line)) break;
134 if (!mess822_line(&h,&line)) nomem();
135 if (!match) break;
136 }
137
138 if (!mess822_end(&h)) nomem();
140
141 buffer_putflush(buffer_1,value.s,value.len);
142
143 _exit(flag ? 0 : 100);
144}
int main()
Definition: 822print.c:351
stralloc out
Definition: b64decode.c:12
int mess822_qpdecode(stralloc *, const char *, int, int)
Definition: mess822_qp.c:103
int mess822_line(mess822_header *, stralloc *)
Definition: mess822_line.c:107
int mess822_end(mess822_header *)
Definition: mess822_line.c:26
int mess822_ok(stralloc *)
Definition: mess822_ok.c:4
#define MESS822_HEADER
Definition: mess822.h:48
int mess822_begin(mess822_header *, mess822_action *)
Definition: mess822_line.c:5
int mess822_b64decode(stralloc *, const char *, int, int)
mess822_action a[]
Definition: 822field.c:20
mess822_header h
Definition: 822field.c:19
stralloc value
Definition: 822field.c:17
stralloc line
Definition: 822field.c:25
int header_decode(stralloc *header)
Definition: 822field.c:32
int match
Definition: 822field.c:26
int flag
Definition: 822field.c:16
#define WHO
Definition: 822field.c:9
char * name
Definition: mess822.h:13