Core: QPEncode

git-svn-id: svn://ultimatepp.org/upp/trunk@7332 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2014-04-28 15:46:52 +00:00
parent b41861c5b0
commit e8dd7b431c
3 changed files with 51 additions and 1 deletions

View file

@ -11,6 +11,7 @@ String UrlDecode(const char *s, const char *end);
String UrlDecode(const char *s, int len);
String UrlDecode(const String& s);
String QPEncode(const char* s);
String QPDecode(const char *s);
String Base64Encode(const char *s, const char *end);

View file

@ -168,7 +168,50 @@ String UrlDecode(const String& s)
{
return UrlDecode(~s, s.GetLength());
}
String QPEncode(const char* s)
{
StringBuffer r;
int len = 0;
const int limit = 70;
while(*s) {
if(s[0] >= 33 && s[0] <= 126 && s[0] != '=') {
r.Cat(s[0]);
len++;
}
else
if(s[0] == '\r')
;
else
if(s[0] == '\n') {
r.Cat("\r\n");
len = 0;
}
else // Encode HT or SP only if they are at the end of line (before CRLF or EOF)
if((s[0] == ' ' || s[0] == '\t') &&
(s[1] && s[1] != '\n') &&
(s[1] != '\r' || (s[1] == '\r' && s[2] && s[2] != '\n'))) {
r.Cat(s[0]);
len++;
}
else {
static const char hex[] = "0123456789ABCDEF";
r.Cat('=');
r.Cat(hex[(s[0] >> 4) & 15]);
r.Cat(hex[s[0] & 15]);
len += 3;
}
if(len > limit) {
// Soft-break.
r.Cat('=');
r.Cat("\r\n");
len = 0;
}
s++;
}
return r;
}
String QPDecode(const char *s)
{
StringBuffer r;

View file

@ -56,6 +56,12 @@ tring][@(0.0.255) `&]_[*@3 s])&]
IME type application/x`-www`-form`-urlencoded] (see UrlEncode).&]
[s3;%% &]
[s4; &]
[s5;:QPEncode`(const char`*`): [_^String^ String]_[* QPEncode]([@(0.0.255) const]_[@(0.0.255) c
har`*]_[*@3 s])&]
[s2;%% Encodes string to [^http`:`/`/en`.wikipedia`.org`/wiki`/Quoted`-printable^ quote
d`-printable] format .&]
[s3;%% &]
[s4; &]
[s5;:QPDecode`(const char`*`): [_^String^ String]_[* QPDecode]([@(0.0.255) const]_[@(0.0.255) c
har]_`*[*@3 s])&]
[s2;%% Decodes string from [^http`:`/`/en`.wikipedia`.org`/wiki`/Quoted`-printable^ quo