diff options
| author | Thomas Holder <thomas@thomas-holder.de> | 2018-10-28 11:54:42 +0000 |
|---|---|---|
| committer | Tavmjong Bah <tavmjong@free.fr> | 2018-10-29 09:21:30 +0000 |
| commit | 348271b8b0a8cdf6b57a377d565f278fae4fd3eb (patch) | |
| tree | 1bed11d7c6f5c2128d0e13d8dcc74b19d020b299 /src | |
| parent | cleanup: remove most of uristream.cpp (diff) | |
| download | inkscape-348271b8b0a8cdf6b57a377d565f278fae4fd3eb.tar.gz inkscape-348271b8b0a8cdf6b57a377d565f278fae4fd3eb.zip | |
cleanup: remove Base64OutputStream
It was only used in one place. Use "g_base64_encode" instead, like the
rest of the codebase.
Diffstat (limited to 'src')
| -rw-r--r-- | src/extension/internal/pdfinput/svg-builder.cpp | 28 | ||||
| -rw-r--r-- | src/io/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/io/base64stream.cpp | 336 | ||||
| -rw-r--r-- | src/io/base64stream.h | 124 |
4 files changed, 8 insertions, 482 deletions
diff --git a/src/extension/internal/pdfinput/svg-builder.cpp b/src/extension/internal/pdfinput/svg-builder.cpp index 4d678c3e4..481e1b95d 100644 --- a/src/extension/internal/pdfinput/svg-builder.cpp +++ b/src/extension/internal/pdfinput/svg-builder.cpp @@ -34,8 +34,6 @@ #include "svg/svg-color.h" #include "color.h" #include "util/units.h" -#include "io/stringstream.h" -#include "io/base64stream.h" #include "display/nr-filter-utils.h" #include "libnrtype/font-instance.h" #include "object/sp-defs.h" @@ -1465,22 +1463,14 @@ void SvgBuilder::endTextObject(GfxState * /*state*/) { /** * Helper functions for supporting direct PNG output into a base64 encoded stream */ -void png_write_base64stream(png_structp png_ptr, png_bytep data, png_size_t length) +void png_write_vector(png_structp png_ptr, png_bytep data, png_size_t length) { - Inkscape::IO::Base64OutputStream *stream = - (Inkscape::IO::Base64OutputStream*)png_get_io_ptr(png_ptr); // Get pointer to stream + auto *v_ptr = reinterpret_cast<std::vector<guchar> *>(png_get_io_ptr(png_ptr)); // Get pointer to stream for ( unsigned i = 0 ; i < length ; i++ ) { - stream->put((int)data[i]); + v_ptr->push_back(data[i]); } } -void png_flush_base64stream(png_structp png_ptr) -{ - Inkscape::IO::Base64OutputStream *stream = - (Inkscape::IO::Base64OutputStream*)png_get_io_ptr(png_ptr); // Get pointer to stream - stream->flush(); -} - /** * \brief Creates an <image> element containing the given ImageStream as a PNG * @@ -1511,13 +1501,11 @@ Inkscape::XML::Node *SvgBuilder::_createImage(Stream *str, int width, int height sp_repr_get_int(_preferences, "embedImages", &attr_value); bool embed_image = ( attr_value != 0 ); // Set read/write functions - Inkscape::IO::StringOutputStream base64_string; - Inkscape::IO::Base64OutputStream base64_stream(base64_string); + std::vector<guchar> png_buffer; FILE *fp = nullptr; gchar *file_name = nullptr; if (embed_image) { - base64_stream.setColumnWidth(0); // Disable line breaks - png_set_write_fn(png_ptr, &base64_stream, png_write_base64stream, png_flush_base64stream); + png_set_write_fn(png_ptr, &png_buffer, png_write_vector, nullptr); } else { static int counter = 0; file_name = g_strdup_printf("%s_img%d.png", _docname, counter++); @@ -1652,7 +1640,6 @@ Inkscape::XML::Node *SvgBuilder::_createImage(Stream *str, int width, int height // Close PNG png_write_end(png_ptr, info_ptr); png_destroy_write_struct(&png_ptr, &info_ptr); - base64_stream.close(); // Create repr Inkscape::XML::Node *image_node = _xml_doc->createElement("svg:image"); @@ -1676,8 +1663,9 @@ Inkscape::XML::Node *SvgBuilder::_createImage(Stream *str, int width, int height // Create href if (embed_image) { // Append format specification to the URI - Glib::ustring& png_data = base64_string.getString(); - png_data.insert(0, "data:image/png;base64,"); + auto *base64String = g_base64_encode(png_buffer.data(), png_buffer.size()); + auto png_data = std::string("data:image/png;base64,") + base64String; + g_free(base64String); image_node->setAttribute("xlink:href", png_data.c_str()); } else { fclose(fp); diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt index c816082b0..51e0b2e32 100644 --- a/src/io/CMakeLists.txt +++ b/src/io/CMakeLists.txt @@ -1,6 +1,5 @@ set(io_SRC - base64stream.cpp bufferstream.cpp dir-util.cpp gzipstream.cpp @@ -15,7 +14,6 @@ set(io_SRC # ------- # Headers - base64stream.h bufferstream.h dir-util.h gzipstream.h diff --git a/src/io/base64stream.cpp b/src/io/base64stream.cpp deleted file mode 100644 index c08584e33..000000000 --- a/src/io/base64stream.cpp +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Base64-enabled input and output streams - * - * This class allows easy encoding and decoding - * of Base64 data with a stream interface, hiding - * the implementation from the user. - * - * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html - * - * Authors: - - * Bob Jamison - * - * Copyright (C) 2006 Bob Jamison - * - - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "base64stream.h" - - - - -namespace Inkscape -{ -namespace IO -{ - -//######################################################################### -//# B A S E 6 4 I N P U T S T R E A M -//######################################################################### - -static int base64decode[] = -{ -/*00*/ -1, -1, -1, -1, -1, -1, -1, -1, -/*08*/ -1, -1, -1, -1, -1, -1, -1, -1, -/*10*/ -1, -1, -1, -1, -1, -1, -1, -1, -/*18*/ -1, -1, -1, -1, -1, -1, -1, -1, -/*20*/ -1, -1, -1, -1, -1, -1, -1, -1, -/*28*/ -1, -1, -1, 62, -1, -1, -1, 63, -/*30*/ 52, 53, 54, 55, 56, 57, 58, 59, -/*38*/ 60, 61, -1, -1, -1, -1, -1, -1, -/*40*/ -1, 0, 1, 2, 3, 4, 5, 6, -/*48*/ 7, 8, 9, 10, 11, 12, 13, 14, -/*50*/ 15, 16, 17, 18, 19, 20, 21, 22, -/*58*/ 23, 24, 25, -1, -1, -1, -1, -1, -/*60*/ -1, 26, 27, 28, 29, 30, 31, 32, -/*68*/ 33, 34, 35, 36, 37, 38, 39, 40, -/*70*/ 41, 42, 43, 44, 45, 46, 47, 48, -/*78*/ 49, 50, 51, -1, -1, -1, -1, -1 -}; - - -/** - * - */ -Base64InputStream::Base64InputStream(InputStream &sourceStream) - : BasicInputStream(sourceStream), - outCount(0), - padCount(0), - done(false) -{ - for (int k=0;k<3;k++) - { - outBytes[k]=0; - } -} - -/** - * - */ -Base64InputStream::~Base64InputStream() -{ - close(); -} - -/** - * Returns the number of bytes that can be read (or skipped over) from - * this input stream without blocking by the next caller of a method for - * this input stream. - */ -int Base64InputStream::available() -{ - if (closed ) - return 0; - int len = source.available() * 2 / 3; - return len; -} - - -/** - * Closes this input stream and releases any system resources - * associated with the stream. - */ -void Base64InputStream::close() -{ - if (closed) - return; - source.close(); - closed = true; -} - -/** - * Reads the next byte of data from the input stream. -1 if EOF - */ -int Base64InputStream::get() -{ - if (closed) - return -1; - - if (outCount - padCount > 0) - { - return outBytes[3-(outCount--)]; - } - - if (done) - return -1; - - int inBytes[4]; - int inCount = 0; - while (inCount < 4) - { - int ch = source.get(); - if (ch < 0) - { - while (inCount < 4) //pad if needed - { - inBytes[inCount++] = 0; - padCount++; - } - done = true; - break; - } - if (isspace(ch)) //ascii whitespace - { - //nothing - } - else if (ch == '=') //padding - { - inBytes[inCount++] = 0; - padCount++; - } - else - { - int byteVal = base64decode[ch & 0x7f]; - //printf("char:%c %d\n", ch, byteVal); - if (byteVal < 0) - { - //Bad lookup value - } - inBytes[inCount++] = byteVal; - } - } - - outBytes[0] = ((inBytes[0]<<2) & 0xfc) | ((inBytes[1]>>4) & 0x03); - outBytes[1] = ((inBytes[1]<<4) & 0xf0) | ((inBytes[2]>>2) & 0x0f); - outBytes[2] = ((inBytes[2]<<6) & 0xc0) | ((inBytes[3] ) & 0x3f); - - outCount = 3; - - //try again - if (outCount - padCount > 0) - { - return outBytes[3-(outCount--)]; - } - - return -1; - -} - - -//######################################################################### -//# B A S E 6 4 O U T P U T S T R E A M -//######################################################################### - -static char const *base64encode = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -/** - * - */ -Base64OutputStream::Base64OutputStream(OutputStream &destinationStream) - : BasicOutputStream(destinationStream) -{ - column = 0; - columnWidth = 72; - outBuf = 0L; - bitCount = 0; -} - -/** - * - */ -Base64OutputStream::~Base64OutputStream() -{ - close(); -} - -/** - * Closes this output stream and releases any system resources - * associated with this stream. - */ -void Base64OutputStream::close() -{ - if (closed) - return; - - //get any last bytes (1 or 2) out of the buffer - if (bitCount == 16) - { - outBuf <<= 2; //pad to make 18 bits - - int indx = (int)((outBuf & 0x0003f000L) >> 12); - int obyte = (int)base64encode[indx & 63]; - putCh(obyte); - - indx = (int)((outBuf & 0x00000fc0L) >> 6); - obyte = (int)base64encode[indx & 63]; - putCh(obyte); - - indx = (int)((outBuf & 0x0000003fL) ); - obyte = (int)base64encode[indx & 63]; - putCh(obyte); - - putCh('='); - } - else if (bitCount == 8) - { - outBuf <<= 4; //pad to make 12 bits - - int indx = (int)((outBuf & 0x00000fc0L) >> 6); - int obyte = (int)base64encode[indx & 63]; - putCh(obyte); - - indx = (int)((outBuf & 0x0000003fL) ); - obyte = (int)base64encode[indx & 63]; - putCh(obyte); - - putCh('='); - putCh('='); - } - - if (columnWidth > 0) //if <=0, no newlines - destination.put('\n'); - - destination.close(); - closed = true; -} - -/** - * Flushes this output stream and forces any buffered output - * bytes to be written out. - */ -void Base64OutputStream::flush() -{ - if (closed) - return; - //don't flush here. do it on close() - destination.flush(); -} - -/** - * Private. Put a char to the output stream, checking for line length - */ -void Base64OutputStream::putCh(int ch) -{ - destination.put(ch); - column++; - if (columnWidth > 0 && column >= columnWidth) - { - destination.put('\n'); - column = 0; - } -} - - -/** - * Writes the specified byte to this output stream. - */ -int Base64OutputStream::put(char ch) -{ - if (closed) - { - //probably throw an exception here - return -1; - } - - outBuf <<= 8; - outBuf |= (ch & 0xff); - bitCount += 8; - if (bitCount >= 24) - { - int indx = (int)((outBuf & 0x00fc0000L) >> 18); - int obyte = (int)base64encode[indx & 63]; - putCh(obyte); - - indx = (int)((outBuf & 0x0003f000L) >> 12); - obyte = (int)base64encode[indx & 63]; - putCh(obyte); - - indx = (int)((outBuf & 0x00000fc0L) >> 6); - obyte = (int)base64encode[indx & 63]; - putCh(obyte); - - indx = (int)((outBuf & 0x0000003fL) ); - obyte = (int)base64encode[indx & 63]; - putCh(obyte); - - bitCount = 0; - outBuf = 0L; - } - return 1; -} - - - -} // namespace IO -} // namespace Inkscape - - -//######################################################################### -//# E N D O F F I L E -//######################################################################### diff --git a/src/io/base64stream.h b/src/io/base64stream.h deleted file mode 100644 index 2db5a19e5..000000000 --- a/src/io/base64stream.h +++ /dev/null @@ -1,124 +0,0 @@ -#ifndef SEEN_INKSCAPE_IO_BASE64STREAM_H -#define SEEN_INKSCAPE_IO_BASE64STREAM_H - -/** - * @file - * Base64-enabled input and output streams - * - * This class allows easy encoding and decoding - * of Base64 data with a stream interface, hiding - * the implementation from the user. - */ -/* - * Authors: - * Bob Jamison <rjamison@titan.com> - * - * Copyright (C) 2004 Inkscape.org - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ - - -#include "inkscapestream.h" - - - -namespace Inkscape -{ -namespace IO -{ - -//######################################################################### -//# B A S E 6 4 I N P U T S T R E A M -//######################################################################### - -/** - * This class is for decoding a Base-64 encoded InputStream source - * - */ -class Base64InputStream : public BasicInputStream -{ - -public: - - Base64InputStream(InputStream &sourceStream); - - ~Base64InputStream() override; - - int available() override; - - void close() override; - - int get() override; - -private: - - int outBytes[3]; - - int outCount; - - int padCount; - - bool done; - -}; // class Base64InputStream - - - - -//######################################################################### -//# B A S E 6 4 O U T P U T S T R E A M -//######################################################################### - -/** - * This class is for Base-64 encoding data going to the - * destination OutputStream - * - */ -class Base64OutputStream : public BasicOutputStream -{ - -public: - - Base64OutputStream(OutputStream &destinationStream); - - ~Base64OutputStream() override; - - void close() override; - - void flush() override; - - int put(char ch) override; - - /** - * Sets the maximum line length for base64 output. If - * set to <=0, then there will be no line breaks; - */ - virtual void setColumnWidth(int val) - { columnWidth = val; } - -private: - - void putCh(int ch); - - int column; - - int columnWidth; - - unsigned long outBuf; - - int bitCount; - -}; // class Base64OutputStream - - - - - - - -} // namespace IO -} // namespace Inkscape - - -#endif // SEEN_INKSCAPE_IO_BASE64STREAM_H |
