diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
| commit | 5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch) | |
| tree | 1a81af141708b826e9c61e8a04019994fcca8298 /src/mime/pack.cc | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/mime/pack.cc')
| -rw-r--r-- | src/mime/pack.cc | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/src/mime/pack.cc b/src/mime/pack.cc new file mode 100644 index 0000000..78fde26 --- /dev/null +++ b/src/mime/pack.cc | |||
| @@ -0,0 +1,143 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <mime/pack.h> | ||
| 21 | |||
| 22 | const char hex2char[16]={ | ||
| 23 | '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' | ||
| 24 | }; | ||
| 25 | const unsigned char2hex[256]={ | ||
| 26 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
| 27 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, | ||
| 28 | 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
| 29 | 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
| 30 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
| 31 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
| 32 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
| 33 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
| 34 | }; | ||
| 35 | |||
| 36 | mstream& cashex(mstream& out,unsigned char c) { | ||
| 37 | return out.put(hex2char[c>>4]).put(hex2char[c&15]); | ||
| 38 | } | ||
| 39 | char hexasc(unsigned char c1,unsigned char c2) { | ||
| 40 | return ((char2hex[c1]<<4)|char2hex[c2]); | ||
| 41 | } | ||
| 42 | |||
| 43 | void Pack(mstream& out,const sref& s) | ||
| 44 | { | ||
| 45 | static const charset escaped("&=%+\"'? "); | ||
| 46 | static const charset conv=~cset_printable|escaped; | ||
| 47 | sref q,p=s; | ||
| 48 | while(p.nempty()) { | ||
| 49 | p=p.adv(q=p.left_first_of(conv)); | ||
| 50 | out<<q; | ||
| 51 | while(conv.test(p.front())) { | ||
| 52 | char c=*p; p=p.popf(); | ||
| 53 | if(c==' ') out.put('+'); | ||
| 54 | else { | ||
| 55 | out.put('%'); | ||
| 56 | cashex(out,c); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | void Pack(mstream& out,const rpile& list) | ||
| 63 | { | ||
| 64 | for(rpile::const_iterator p=list.begin();p!=list.end();++p) { | ||
| 65 | Pack(out,*p); | ||
| 66 | out.put('&'); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | void Pack(mstream& out,const rrpile& list) | ||
| 71 | { | ||
| 72 | for(rrpile::const_iterator p=list.begin();p!=list.end();++p) { | ||
| 73 | Pack(out,p->first); | ||
| 74 | out.put('='); | ||
| 75 | Pack(out,p->second); | ||
| 76 | out.put('&'); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | /////////////////////////////////////////////////////////////// | ||
| 81 | |||
| 82 | void Unpack(mstream& out,const sref& in) | ||
| 83 | { | ||
| 84 | static const charset tags("%+"); | ||
| 85 | sref e,p=in; | ||
| 86 | while(p.nempty()) { | ||
| 87 | p=p.adv(e=p.left_first_of(tags)); | ||
| 88 | out<<e; | ||
| 89 | while(tags.test(p.front())) { | ||
| 90 | char c=*p; p=p.popf(); | ||
| 91 | if(c=='+') out.put(' '); | ||
| 92 | else if(c=='%') { | ||
| 93 | unsigned char c1=p[0],c2=p[1]; p=p.adv(2); | ||
| 94 | out.put(hexasc(c1,c2)); | ||
| 95 | } | ||
| 96 | else out.put(c); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | void Unpack(spile& list,const sref& in) | ||
| 102 | { | ||
| 103 | sref e,p=in; | ||
| 104 | while(p.nempty()) { | ||
| 105 | p=p.adv(e=p.left_first('&')).popf(); | ||
| 106 | if(e.nempty()) { | ||
| 107 | list.push_back(mstring()); | ||
| 108 | swrite sw(list.back()); | ||
| 109 | Unpack(sw,e); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | void Unpack(sspile& list,const sref& in) | ||
| 115 | { | ||
| 116 | sref e1,e2,p=in; | ||
| 117 | while(p.nempty()) { | ||
| 118 | p=p.adv(e1=p.left_first('=')).popf(); | ||
| 119 | if(e1.nempty()) { | ||
| 120 | p=p.adv(e2=p.left_first('&')).popf(); | ||
| 121 | list.push_back(sstring()); | ||
| 122 | swrite sw1(list.back().first); | ||
| 123 | Unpack(sw1,e1); | ||
| 124 | swrite sw2(list.back().second); | ||
| 125 | Unpack(sw2,e2); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | /////////////////////////////////////////////////////////////// | ||
| 131 | |||
| 132 | void CRLF2LF(mstream& out,const sref& in) | ||
| 133 | { | ||
| 134 | sref first,rest=in; | ||
| 135 | while(Splitln(first,rest)) out<<first<<"\n"; | ||
| 136 | } | ||
| 137 | |||
| 138 | void LF2CRLF(mstream& out,const sref& in) | ||
| 139 | { | ||
| 140 | sref first,rest=in; | ||
| 141 | while(Splitln(first,rest)) out<<first<<CRLF; | ||
| 142 | } | ||
| 143 | |||
