From 7c344bc2e4b55da3da9a9db0603efa3ef656d3da Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Tue, 14 Aug 2012 21:48:57 +0800 Subject: [PATCH] working on base64 --- src/HTMLRenderer/general.cc | 5 ++++- src/util.h | 29 ++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/HTMLRenderer/general.cc b/src/HTMLRenderer/general.cc index 16c2271..291fb77 100644 --- a/src/HTMLRenderer/general.cc +++ b/src/HTMLRenderer/general.cc @@ -20,6 +20,8 @@ using std::flush; +using boost::filesystem::file_size; + HTMLRenderer::HTMLRenderer(const Param * param) :line_opened(false) ,image_count(0) @@ -134,8 +136,9 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state) const std::string fn = (format("p%|1$x|.png") % pageNum).str(); if(param->single_html) { + auto path = tmp_dir / fn; html_fout << "'data:image/png;base64,"; - copy_base64(html_fout, ifstream(tmp_dir / fn)); + copy_base64(html_fout, ifstream(path), file_size(path)); html_fout << "'"; } else diff --git a/src/util.h b/src/util.h index f524264..90234fb 100644 --- a/src/util.h +++ b/src/util.h @@ -123,17 +123,40 @@ public: double _[6]; }; -// TODO: padding bug of boost -static inline void copy_base64 (ostream & out, istream & in) +static inline void copy_base64 (ostream & out, istream & in, size_t length) { typedef base64_from_binary < transform_width < istream_iterator, 6, 8 > > base64_iter; copy(base64_iter(istream_iterator(in)), base64_iter(istream_iterator()), ostream_iterator(out)); + switch(length % 3) + { + case 1: + out << '='; + // fall through + case 2: + out << '='; + // fall through + case 0: + default: + break; + } } -static inline void copy_base64 (ostream & out, istream && in) +static inline void copy_base64 (ostream & out, istream && in, size_t length) { typedef base64_from_binary < transform_width < istream_iterator, 6, 8 > > base64_iter; copy(base64_iter(istream_iterator(in)), base64_iter(istream_iterator()), ostream_iterator(out)); + switch(length % 3) + { + case 1: + out << '='; + // fall through + case 2: + out << '='; + // fall through + case 0: + default: + break; + } } #endif //UTIL_H__