From 5df79c53745fde5d6c3340a2979b1429cd5892c1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 8 Oct 2011 20:30:28 +0200 Subject: Initial import of htcd system 1.0 Signed-off-by: Henrik Rydberg --- src/mime/base64.cc | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/mime/base64.cc (limited to 'src/mime/base64.cc') diff --git a/src/mime/base64.cc b/src/mime/base64.cc new file mode 100644 index 0000000..842bbd3 --- /dev/null +++ b/src/mime/base64.cc @@ -0,0 +1,132 @@ +/************************************************************************* + * + * 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 + +static unsigned char char2six[256]={ + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,62,65,65,65,63, + 52,53,54,55,56,57,58,59, + 60,61,65,65,65,64,65,65, + 65, 0, 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,65,65,65,65,65, + 65,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,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65, + 65,65,65,65,65,65,65,65 +}; +static unsigned char six2char[64]={ + 'A','B','C','D','E','F','G','H', + 'I','J','K','L','M','N','O','P', + 'Q','R','S','T','U','V','W','X', + 'Y','Z','a','b','c','d','e','f', + 'g','h','i','j','k','l','m','n', + 'o','p','q','r','s','t','u','v', + 'w','x','y','z','0','1','2','3', + '4','5','6','7','8','9','+','/' +}; +inline void tri2quad(mstream& out,unsigned code,int m=4) +{ + char buf[4]; + buf[3]=six2char[code&63U]; code>>=6; + buf[2]=six2char[code&63U]; code>>=6; + buf[1]=six2char[code&63U]; code>>=6; + buf[0]=six2char[code&63U]; + out.write(buf,m); +} +inline void quad2tri(mstream& out,unsigned code,int m=3) +{ + char buf[3]; + buf[2]=code&255U; code>>=8; + buf[1]=code&255U; code>>=8; + buf[0]=code&255U; + out.write(buf,m); +} + +void encode64(mstream& out,const sref& in) +{ + unsigned m=0,code=0,wrap=0; + for(const char* p=in.begin();p!=in.end();p++) { + code<<=8; + code|=(unsigned char)*p; + if(++m==3) { + tri2quad(out,code); + m=0; + if(++wrap==15) { out<