From ba77c1cc6c331cf0e36453772a51c8d0cc35304c Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Thu, 29 Nov 2012 19:38:57 +0800 Subject: [PATCH] refince code: base64 --- src/util/base64.cc | 40 +++++++++++++++++++++++++++++++++++++++- src/util/base64.h | 37 +++---------------------------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/util/base64.cc b/src/util/base64.cc index 8e462bf..e5da7e3 100644 --- a/src/util/base64.cc +++ b/src/util/base64.cc @@ -2,6 +2,44 @@ namespace pdf2htmlEX { +using std::ostream; + +ostream & base64stream::dumpto(ostream & out) +{ + unsigned char buf[3]; + while(in->read((char*)buf, 3)) + { + out << base64_encoding[(buf[0] & 0xfc)>>2] + << base64_encoding[((buf[0] & 0x03)<<4) | ((buf[1] & 0xf0)>>4)] + << base64_encoding[((buf[1] & 0x0f)<<2) | ((buf[2] & 0xc0)>>6)] + << base64_encoding[(buf[2] & 0x3f)]; + } + auto cnt = in->gcount(); + if(cnt > 0) + { + for(int i = cnt; i < 3; ++i) + buf[i] = 0; + + out << base64_encoding[(buf[0] & 0xfc)>>2] + << base64_encoding[((buf[0] & 0x03)<<4) | ((buf[1] & 0xf0)>>4)]; + + if(cnt > 1) + { + out << base64_encoding[(buf[1] & 0x0f)<<2]; + } + else + { + out << '='; + } + out << '='; + } + + return out; +} + const char * base64stream::base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -} +ostream & operator << (ostream & out, base64stream & bf) { return bf.dumpto(out); } +ostream & operator << (ostream & out, base64stream && bf) { return bf.dumpto(out); } + +} //namespace pdf2htmlEX diff --git a/src/util/base64.h b/src/util/base64.h index 3c53a84..5bdb4fe 100644 --- a/src/util/base64.h +++ b/src/util/base64.h @@ -19,46 +19,15 @@ public: base64stream(std::istream & in) : in(&in) { } base64stream(std::istream && in) : in(&in) { } - std::ostream & dumpto(std::ostream & out) - { - unsigned char buf[3]; - while(in->read((char*)buf, 3)) - { - out << base64_encoding[(buf[0] & 0xfc)>>2] - << base64_encoding[((buf[0] & 0x03)<<4) | ((buf[1] & 0xf0)>>4)] - << base64_encoding[((buf[1] & 0x0f)<<2) | ((buf[2] & 0xc0)>>6)] - << base64_encoding[(buf[2] & 0x3f)]; - } - auto cnt = in->gcount(); - if(cnt > 0) - { - for(int i = cnt; i < 3; ++i) - buf[i] = 0; - - out << base64_encoding[(buf[0] & 0xfc)>>2] - << base64_encoding[((buf[0] & 0x03)<<4) | ((buf[1] & 0xf0)>>4)]; - - if(cnt > 1) - { - out << base64_encoding[(buf[1] & 0x0f)<<2]; - } - else - { - out << '='; - } - out << '='; - } - - return out; - } + std::ostream & dumpto(std::ostream & out); private: std::istream * in; static const char * base64_encoding; }; -static inline std::ostream & operator << (std::ostream & out, base64stream & bf) { return bf.dumpto(out); } -static inline std::ostream & operator << (std::ostream & out, base64stream && bf) { return bf.dumpto(out); } +std::ostream & operator << (std::ostream & out, base64stream & bf); +std::ostream & operator << (std::ostream & out, base64stream && bf); } //namespace pdf2htmlEX #endif //BASE64_H__