-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuotedPrintable.cpp
More file actions
executable file
·103 lines (89 loc) · 2.19 KB
/
Copy pathQuotedPrintable.cpp
File metadata and controls
executable file
·103 lines (89 loc) · 2.19 KB
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
// QuotedPrintable.cpp: implementation of the QuotedPrintable class.
//
//////////////////////////////////////////////////////////////////////
#include "QuotedPrintable.h"
#include <iostream>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
QuotedPrintable::QuotedPrintable()
{
}
QuotedPrintable::~QuotedPrintable()
{
}
std::string
QuotedPrintable::Encode(const std::string& data)
{
static const std::string Q("?=!\"#$@[\\]^`{|}~");
std::string Result;
int Len=0;
unsigned char b,B;
unsigned char anterior;
const char *S = data.c_str();
while((b = *S++)) {
if(b == '\n'){
anterior = *S-1;
if(anterior != '\r'){
Result+='\r';
Result+=b;
Len++;
Len++;
}
} else {
if(b>127 || (Q.find(b) != std::string::npos)
|| (!*S && (b==' ' || b=='\t'))) {//Preserve trailing spaces
B=b>>4;
b&=0x0F;
B=(B>9) ? B-10+'A' : B+'0';
b=(b>9) ? b-10+'A' : b+'0';
char Buffer[3];
char* ptr=Buffer;
*ptr++=B;
*ptr++=b;
*ptr =0;
Result=Result+'='+ Buffer;
}else{
Result+=b;
Len++;
}
}
if((Len==75) && b!='\r' && b!='\n') {
Len=0;
Result+="=\r\n"; //Soft break for long lines
}
}
return Result; //May append a NL at EOF
}
std::string
QuotedPrintable::Decode(const std::string& S)
{
std::string Result;
unsigned char b,B;
char *DST = new char[S.length()+3]; // Result.GetBufferSetLength(strlen(S)+3);
char *dst = DST;
if(!S.empty()) {
const char* src=S.c_str();
while((b = *src++)) {
if(b=='=') {
if((b = *src++)) {
b|=0x20;
B=(*src++)|0x20;
b=(b>'9') ? b-'a'+10 : b-'0';
B=(B>'9') ? B-'a'+10 : B-'0';
b=(b<<4)|B;
}else{ //Soft Break
*dst=0;
// Result = DST;
return DST; //Result;
}
}
*dst++=b;
}
}
*dst++='\r';
*dst++='\n';
*dst=0;
// Result = DST
return DST; // Result;
}