From e8dd7b431ccded6dd4373fe8a42a941a2d8d4d06 Mon Sep 17 00:00:00 2001 From: cxl Date: Mon, 28 Apr 2014 15:46:52 +0000 Subject: [PATCH] Core: QPEncode git-svn-id: svn://ultimatepp.org/upp/trunk@7332 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Inet.h | 1 + uppsrc/Core/InetUtil.cpp | 45 +++++++++++++++++++++++++++++- uppsrc/Core/src.tpp/Inet$en-us.tpp | 6 ++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/uppsrc/Core/Inet.h b/uppsrc/Core/Inet.h index e9357cbf9..49a38b339 100644 --- a/uppsrc/Core/Inet.h +++ b/uppsrc/Core/Inet.h @@ -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); diff --git a/uppsrc/Core/InetUtil.cpp b/uppsrc/Core/InetUtil.cpp index 73a80f523..4902c33d0 100644 --- a/uppsrc/Core/InetUtil.cpp +++ b/uppsrc/Core/InetUtil.cpp @@ -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; diff --git a/uppsrc/Core/src.tpp/Inet$en-us.tpp b/uppsrc/Core/src.tpp/Inet$en-us.tpp index 4d7109305..fdb6773dc 100644 --- a/uppsrc/Core/src.tpp/Inet$en-us.tpp +++ b/uppsrc/Core/src.tpp/Inet$en-us.tpp @@ -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