1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/*************************************************************************
*
* HTCd - Copyright (C) 1998-2006 Henrik Rydberg
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <mime/enc.h>
#include <mime/pack.h>
#include <algorithm>
const charset special("=\"#$\\^`{|}~");
//const charset special("=!\"#$@[\\]^`{|}~");
const charset headspecial("_?");
const charset printable=cset_printable&~special;
const charset printhead=printable&~headspecial;
const mstring beg_tag("=?"),end_tag("?=");
const mstring enc_qp_tag("Q"),enc_64_tag("B");
const int WRAPAT=76;
const int HEADWRAP=WRAPAT-7;
void decodeQP(mstream& out,const sref& in)
{
const char* p=in.begin();
while(p<in.end()) {
unsigned char c=*p++;
if(c=='=') {
if(cset_ws.test(*p)) p=in(p).past_nl().data();
else {
unsigned char c1=*p++,c2=*p++;
out.put(hexasc(c1,c2));
}
}
else out.put(c);
}
}
void encodeQP(mstream& out,const sref& in)
{
sref first,rest=in;
while(Splitln(first,rest)) {
int n=0;
const char* p=first.begin();
while(p<first.end()) {
unsigned char c=*p++;
if(printable.test(c)) {
if(first(p).empty()&&cset_spaces.test(c)) { cashex(out.put('='),c); n+=3; }
else { out.put(c); n++; }
}
else { cashex(out.put('='),c); n+=3; }
if(first(p).nempty()&&n>=WRAPAT) { out.put('=')<<CRLF; n=0; }
}
out<<CRLF;
}
}
static void decodeHQ(mstream& out,const sref& in)
{
const char* p=in.begin();
while(p<in.end()) {
unsigned char c=*p++;
if(c=='=') {
unsigned char c1=*p++,c2=*p++;
out.put(hexasc(c1,c2));
}
else if(c=='_') out.put(' ');
else out.put(c);
}
}
static int encodeHQ(mstream& out,const sref& in)
{
int enc=0;
const char* p=in.begin();
while(p<in.end()) {
unsigned char c=*p++;
if(!printable.test(c)) enc=1;
if(printhead.test(c)) { if(c==32) out.put('_'); else out.put(c); }
else cashex(out.put('='),c);
}
return enc;
}
void decodeHeader(mstream& out,mstring& mime,const sref& in)
{
sref e,p=in;
while(p.nempty()) {
p=p.adv(e=p.left_first(beg_tag));
out<<e;
if(p.nempty()) {
p=p.adv(beg_tag.size());
mime=p.left_first('?');
p=p.adv(mime.size());
if(p.empty()) { out<<beg_tag<<mime; p.clear(); }
else {
p=p.popf();
sref enc=p.left_first('?');
p=p.adv(enc);
if(p.empty()) { out<<beg_tag<<mime<<"?"<<enc; p.clear(); }
else {
p=p.adv(e=p.popf().left_first(end_tag));
if(p.empty()) { out<<beg_tag<<mime<<"?"<<enc<<"?"<<e; p.clear(); }
else {
p=p.adv(end_tag.size());
if(eqn(enc,enc_qp_tag)) decodeHQ(out,e);
else if(eqn(enc,enc_64_tag)) decode64(out,e);
else out<<"BAD: ["<<mime<<"]["<<enc<<"]["<<e<<"]";
}
}
}
}
}
}
void encodeHeader(mstream& out,const sref& mime,const sref& in)
{
mstring tmp;
swrite sw(tmp);
if(encodeHQ(sw,in)) {
int wrapat=HEADWRAP-mime.size();
sref e,p=tmp;
p=p.adv(e=p.left(std::min(wrapat,p.size())));
out<<beg_tag<<mime<<"?Q?"<<e<<end_tag;
while(p.nempty()) {
p=p.adv(e=p.left(std::min(wrapat,p.size())));
out<<"\r\n\t"<<beg_tag<<mime<<"?Q?"<<e<<end_tag;
}
}
else {
sref e,p=in;
p=p.adv(e=p.left(std::min(WRAPAT,p.size())));
out<<e;
while(p.nempty()) {
p=p.adv(e=p.left(std::min(WRAPAT,p.size())));
out<<"\r\n\t"<<e;
}
}
}
|