From 13f69779c298173bd21b83dd6cc538814f2b9155 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Sun, 12 Oct 2008 13:59:09 +0000 Subject: New namespace for encoders. --- SConstruct | 21 +- src/base.cpp | 10 +- src/encoder.cpp | 73 ----- src/encoder7bit.cpp | 36 --- src/encoder8bit.cpp | 36 --- src/encoderB64.cpp | 305 ------------------- src/encoderBinary.cpp | 36 --- src/encoderDefault.cpp | 70 ----- src/encoderFactory.cpp | 110 ------- src/encoderQP.cpp | 469 ----------------------------- src/encoderUUE.cpp | 328 --------------------- src/encoding.cpp | 7 +- src/net/smtp/SMTPTransport.cpp | 1 - src/security/sasl/SASLContext.cpp | 9 +- src/streamContentHandler.cpp | 8 +- src/stringContentHandler.cpp | 8 +- src/utility/encoder/b64Encoder.cpp | 308 +++++++++++++++++++ src/utility/encoder/binaryEncoder.cpp | 39 +++ src/utility/encoder/defaultEncoder.cpp | 73 +++++ src/utility/encoder/eightBitEncoder.cpp | 39 +++ src/utility/encoder/encoder.cpp | 76 +++++ src/utility/encoder/encoderFactory.cpp | 113 +++++++ src/utility/encoder/qpEncoder.cpp | 472 ++++++++++++++++++++++++++++++ src/utility/encoder/sevenBitEncoder.cpp | 39 +++ src/utility/encoder/uuEncoder.cpp | 331 +++++++++++++++++++++ src/word.cpp | 12 +- src/wordEncoder.cpp | 9 +- tests/parser/encoderTest.cpp | 288 ------------------ tests/utility/encoderTest.cpp | 290 ++++++++++++++++++ vmime/encoder.hpp | 107 ------- vmime/encoder7bit.hpp | 49 ---- vmime/encoder8bit.hpp | 49 ---- vmime/encoderB64.hpp | 59 ---- vmime/encoderBinary.hpp | 49 ---- vmime/encoderDefault.hpp | 52 ---- vmime/encoderFactory.hpp | 149 ---------- vmime/encoderQP.hpp | 59 ---- vmime/encoderUUE.hpp | 54 ---- vmime/encoding.hpp | 5 +- vmime/net/imap/IMAPParser.hpp | 10 +- vmime/utility/encoder/b64Encoder.hpp | 62 ++++ vmime/utility/encoder/binaryEncoder.hpp | 52 ++++ vmime/utility/encoder/defaultEncoder.hpp | 55 ++++ vmime/utility/encoder/eightBitEncoder.hpp | 52 ++++ vmime/utility/encoder/encoder.hpp | 110 +++++++ vmime/utility/encoder/encoderFactory.hpp | 152 ++++++++++ vmime/utility/encoder/qpEncoder.hpp | 62 ++++ vmime/utility/encoder/sevenBitEncoder.hpp | 52 ++++ vmime/utility/encoder/uuEncoder.hpp | 57 ++++ vmime/vmime.hpp | 2 +- vmime/wordEncoder.hpp | 8 +- 51 files changed, 2495 insertions(+), 2427 deletions(-) delete mode 100644 src/encoder.cpp delete mode 100644 src/encoder7bit.cpp delete mode 100644 src/encoder8bit.cpp delete mode 100644 src/encoderB64.cpp delete mode 100644 src/encoderBinary.cpp delete mode 100644 src/encoderDefault.cpp delete mode 100644 src/encoderFactory.cpp delete mode 100644 src/encoderQP.cpp delete mode 100644 src/encoderUUE.cpp create mode 100644 src/utility/encoder/b64Encoder.cpp create mode 100644 src/utility/encoder/binaryEncoder.cpp create mode 100644 src/utility/encoder/defaultEncoder.cpp create mode 100644 src/utility/encoder/eightBitEncoder.cpp create mode 100644 src/utility/encoder/encoder.cpp create mode 100644 src/utility/encoder/encoderFactory.cpp create mode 100644 src/utility/encoder/qpEncoder.cpp create mode 100644 src/utility/encoder/sevenBitEncoder.cpp create mode 100644 src/utility/encoder/uuEncoder.cpp delete mode 100644 tests/parser/encoderTest.cpp create mode 100644 tests/utility/encoderTest.cpp delete mode 100644 vmime/encoder.hpp delete mode 100644 vmime/encoder7bit.hpp delete mode 100644 vmime/encoder8bit.hpp delete mode 100644 vmime/encoderB64.hpp delete mode 100644 vmime/encoderBinary.hpp delete mode 100644 vmime/encoderDefault.hpp delete mode 100644 vmime/encoderFactory.hpp delete mode 100644 vmime/encoderQP.hpp delete mode 100644 vmime/encoderUUE.hpp create mode 100644 vmime/utility/encoder/b64Encoder.hpp create mode 100644 vmime/utility/encoder/binaryEncoder.hpp create mode 100644 vmime/utility/encoder/defaultEncoder.hpp create mode 100644 vmime/utility/encoder/eightBitEncoder.hpp create mode 100644 vmime/utility/encoder/encoder.hpp create mode 100644 vmime/utility/encoder/encoderFactory.hpp create mode 100644 vmime/utility/encoder/qpEncoder.hpp create mode 100644 vmime/utility/encoder/sevenBitEncoder.hpp create mode 100644 vmime/utility/encoder/uuEncoder.hpp diff --git a/SConstruct b/SConstruct index 2d10a124..6622b128 100644 --- a/SConstruct +++ b/SConstruct @@ -93,15 +93,6 @@ libvmime_sources = [ 'defaultAttachment.cpp', 'defaultAttachment.hpp', 'disposition.cpp', 'disposition.hpp', 'emptyContentHandler.cpp', 'emptyContentHandler.hpp', - 'encoder.cpp', 'encoder.hpp', - 'encoder7bit.cpp', 'encoder7bit.hpp', - 'encoder8bit.cpp', 'encoder8bit.hpp', - 'encoderB64.cpp', 'encoderB64.hpp', - 'encoderBinary.cpp', 'encoderBinary.hpp', - 'encoderDefault.cpp', 'encoderDefault.hpp', - 'encoderFactory.cpp', 'encoderFactory.hpp', - 'encoderQP.cpp', 'encoderQP.hpp', - 'encoderUUE.cpp', 'encoderUUE.hpp', 'encoding.cpp', 'encoding.hpp', 'exception.cpp', 'exception.hpp', 'fileAttachment.cpp', 'fileAttachment.hpp', @@ -157,6 +148,16 @@ libvmime_sources = [ 'utility/stringUtils.cpp', 'utility/stringUtils.hpp', 'utility/url.cpp', 'utility/url.hpp', 'utility/urlUtils.cpp', 'utility/urlUtils.hpp', + # -- encoder + 'utility/encoder/encoder.cpp', 'utility/encoder/encoder.hpp', + 'utility/encoder/sevenBitEncoder.cpp', 'utility/encoder/sevenBitEncoder.hpp', + 'utility/encoder/eightBitEncoder.cpp', 'utility/encoder/eightBitEncoder.hpp', + 'utility/encoder/b64Encoder.cpp', 'utility/encoder/b64Encoder.hpp', + 'utility/encoder/binaryEncoder.cpp', 'utility/encoder/binaryEncoder.hpp', + 'utility/encoder/defaultEncoder.cpp', 'utility/encoder/defaultEncoder.hpp', + 'utility/encoder/encoderFactory.cpp', 'utility/encoder/encoderFactory.hpp', + 'utility/encoder/qpEncoder.cpp', 'utility/encoder/qpEncoder.hpp', + 'utility/encoder/uuEncoder.cpp', 'utility/encoder/uuEncoder.hpp', # =============================== MDN ================================ 'mdn/MDNHelper.cpp', 'mdn/MDNHelper.hpp', 'mdn/MDNInfos.cpp', 'mdn/MDNInfos.hpp', @@ -345,7 +346,6 @@ libvmimetest_sources = [ 'tests/parser/charsetTest.cpp', 'tests/parser/datetimeTest.cpp', 'tests/parser/dispositionTest.cpp', - 'tests/parser/encoderTest.cpp', 'tests/parser/headerTest.cpp', 'tests/parser/htmlTextPartTest.cpp', 'tests/parser/mailboxTest.cpp', @@ -363,6 +363,7 @@ libvmimetest_sources = [ 'tests/utility/pathTest.cpp', 'tests/utility/urlTest.cpp', 'tests/utility/smartPtrTest.cpp', + 'tests/utility/encoderTest.cpp', # =============================== Misc =============================== 'tests/misc/importanceHelperTest.cpp', # ============================= Security ============================= diff --git a/src/base.cpp b/src/base.cpp index afba23d9..71c63cdc 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -26,9 +26,9 @@ #include "vmime/charset.hpp" #include "vmime/base.hpp" -#include "vmime/encoder.hpp" -#include "vmime/encoderB64.hpp" -#include "vmime/encoderQP.hpp" +#include "vmime/utility/encoder/encoder.hpp" +#include "vmime/utility/encoder/b64Encoder.hpp" +#include "vmime/utility/encoder/qpEncoder.hpp" #include "vmime/text.hpp" @@ -37,7 +37,7 @@ #include "vmime/utility/stringUtils.hpp" // For initializing -#include "vmime/encoderFactory.hpp" +#include "vmime/utility/encoder/encoderFactory.hpp" #include "vmime/headerFieldFactory.hpp" #include "vmime/textPartFactory.hpp" #include "vmime/options.hpp" @@ -140,7 +140,7 @@ public: { options::getInstance(); - encoderFactory::getInstance(); + utility::encoder::encoderFactory::getInstance(); headerFieldFactory::getInstance(); textPartFactory::getInstance(); diff --git a/src/encoder.cpp b/src/encoder.cpp deleted file mode 100644 index 38c7764a..00000000 --- a/src/encoder.cpp +++ /dev/null @@ -1,73 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#include "vmime/encoder.hpp" -#include "vmime/exception.hpp" - - -namespace vmime -{ - - -encoder::encoder() -{ -} - - -encoder::~encoder() -{ -} - - -const propertySet& encoder::getProperties() const -{ - return (m_props); -} - - -propertySet& encoder::getProperties() -{ - return (m_props); -} - - -const propertySet& encoder::getResults() const -{ - return (m_results); -} - - -propertySet& encoder::getResults() -{ - return (m_results); -} - - -const std::vector encoder::getAvailableProperties() const -{ - std::vector list; - return (list); -} - - -} // vmime diff --git a/src/encoder7bit.cpp b/src/encoder7bit.cpp deleted file mode 100644 index 4a60950f..00000000 --- a/src/encoder7bit.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#include "vmime/encoder7bit.hpp" - - -namespace vmime -{ - - -encoder7bit::encoder7bit() -{ -} - - -} // vmime diff --git a/src/encoder8bit.cpp b/src/encoder8bit.cpp deleted file mode 100644 index c85a3804..00000000 --- a/src/encoder8bit.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#include "vmime/encoder8bit.hpp" - - -namespace vmime -{ - - -encoder8bit::encoder8bit() -{ -} - - -} // vmime diff --git a/src/encoderB64.cpp b/src/encoderB64.cpp deleted file mode 100644 index 2c16ee75..00000000 --- a/src/encoderB64.cpp +++ /dev/null @@ -1,305 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#include "vmime/encoderB64.hpp" -#include "vmime/parserHelpers.hpp" - - -namespace vmime -{ - - -encoderB64::encoderB64() -{ -} - - -const std::vector encoderB64::getAvailableProperties() const -{ - std::vector list(encoder::getAvailableProperties()); - - list.push_back("maxlinelength"); - - return (list); -} - - -// 7-bits alphabet used to encode binary data -const unsigned char encoderB64::sm_alphabet[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - -const unsigned char encoderB64::sm_decodeMap[256] = -{ - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0x00 - 0x0f - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0x10 - 0x1f - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3e,0xff,0xff,0xff,0x3f, // 0x20 - 0x2f - 0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0xff,0xff,0xff,0x3d,0xff,0xff, // 0x30 - 0x3f - 0xff,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e, // 0x40 - 0x4f - 0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0xff,0xff,0xff,0xff,0xff, // 0x50 - 0x5f - 0xff,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28, // 0x60 - 0x6f - 0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0xff,0xff,0xff,0xff, // 0x70 - 0x7f - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0x80 - 0x8f - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0x90 - 0x9f - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xa0 - 0xaf - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xb0 - 0xbf - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xc0 - 0xcf - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xd0 - 0xdf - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xe0 - 0xef - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xf0 - 0xff -}; - -#ifndef VMIME_BUILDING_DOC - #define B64_WRITE(s, x, l) s.write(reinterpret_cast (x), l) -#endif // VMIME_BUILDING_DOC - - - -utility::stream::size_type encoderB64::encode(utility::inputStream& in, - utility::outputStream& out, utility::progressListener* progress) -{ - in.reset(); // may not work... - - const int propMaxLineLength = getProperties().getProperty ("maxlinelength", -1); - - const bool cutLines = (propMaxLineLength != -1); - const int maxLineLength = std::min(propMaxLineLength, 76); - - // Process data - utility::stream::value_type buffer[65536]; - utility::stream::size_type bufferLength = 0; - utility::stream::size_type bufferPos = 0; - - unsigned char bytes[3]; - unsigned char output[4]; - - utility::stream::size_type total = 0; - utility::stream::size_type inTotal = 0; - - int curCol = 0; - - if (progress) - progress->start(0); - - while (bufferPos < bufferLength || !in.eof()) - { - if (bufferPos >= bufferLength) - { - bufferLength = in.read(buffer, sizeof(buffer)); - bufferPos = 0; - - if (bufferLength == 0) - break; - } - - // Get 3 bytes of data - int count = 0; - - while (count < 3 && bufferPos < bufferLength) - bytes[count++] = buffer[bufferPos++]; - - while (count < 3) - { - // There may be more data in the next chunk... - if (bufferPos >= bufferLength) - { - bufferLength = in.read(buffer, sizeof(buffer)); - bufferPos = 0; - - if (bufferLength == 0) - break; - } - - while (count < 3 && bufferPos < bufferLength) - bytes[count++] = buffer[bufferPos++]; - } - - // Encode data - switch (count) - { - case 1: - - output[0] = sm_alphabet[(bytes[0] & 0xFC) >> 2]; - output[1] = sm_alphabet[(bytes[0] & 0x03) << 4]; - output[2] = sm_alphabet[64]; // padding - output[3] = sm_alphabet[64]; // padding - - break; - - case 2: - - output[0] = sm_alphabet[(bytes[0] & 0xFC) >> 2]; - output[1] = sm_alphabet[((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xF0) >> 4)]; - output[2] = sm_alphabet[(bytes[1] & 0x0F) << 2]; - output[3] = sm_alphabet[64]; // padding - - break; - - default: - case 3: - - output[0] = sm_alphabet[(bytes[0] & 0xFC) >> 2]; - output[1] = sm_alphabet[((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xF0) >> 4)]; - output[2] = sm_alphabet[((bytes[1] & 0x0F) << 2) | ((bytes[2] & 0xC0) >> 6)]; - output[3] = sm_alphabet[(bytes[2] & 0x3F)]; - - break; - } - - // Write encoded data to output stream - B64_WRITE(out, output, 4); - - inTotal += count; - total += 4; - curCol += 4; - - if (cutLines && curCol >= maxLineLength - 2 /* \r\n */ - 4 /* next bytes */) - { - out.write("\r\n", 2); - curCol = 0; - } - - if (progress) - progress->progress(inTotal, inTotal); - } - - if (progress) - progress->stop(inTotal); - - return (total); -} - - -utility::stream::size_type encoderB64::decode(utility::inputStream& in, - utility::outputStream& out, utility::progressListener* progress) -{ - in.reset(); // may not work... - - // Process the data - char buffer[16384]; - int bufferLength = 0; - int bufferPos = 0; - - utility::stream::size_type total = 0; - utility::stream::size_type inTotal = 0; - - unsigned char bytes[4]; - unsigned char output[3]; - - if (progress) - progress->start(0); - - while (bufferPos < bufferLength || !in.eof()) - { - bytes[0] = '='; - bytes[1] = '='; - bytes[2] = '='; - bytes[3] = '='; - - // Need to get more data? - if (bufferPos >= bufferLength) - { - bufferLength = in.read(buffer, sizeof(buffer)); - bufferPos = 0; - - // No more data - if (bufferLength == 0) - break; - } - - // 4 bytes of input provide 3 bytes of output, so - // get the next 4 bytes from the input stream. - int count = 0; - - while (count < 4 && bufferPos < bufferLength) - { - const unsigned char c = buffer[bufferPos++]; - - if (!parserHelpers::isSpace(c)) - bytes[count++] = c; - } - - if (count != 4) - { - while (count < 4 && !in.eof()) - { - // Data continues on the next chunk - bufferLength = in.read(buffer, sizeof(buffer)); - bufferPos = 0; - - while (count < 4 && bufferPos < bufferLength) - { - const unsigned char c = buffer[bufferPos++]; - - if (!parserHelpers::isSpace(c)) - bytes[count++] = c; - } - } - } - - // Decode the bytes - unsigned char c1 = bytes[0]; - unsigned char c2 = bytes[1]; - - if (c1 == '=' || c2 == '=') // end - break; - - output[0] = static_cast ((sm_decodeMap[c1] << 2) | ((sm_decodeMap[c2] & 0x30) >> 4)); - - c1 = bytes[2]; - - if (c1 == '=') // end - { - B64_WRITE(out, output, 1); - total += 1; - break; - } - - output[1] = static_cast (((sm_decodeMap[c2] & 0xf) << 4) | ((sm_decodeMap[c1] & 0x3c) >> 2)); - - c2 = bytes[3]; - - if (c2 == '=') // end - { - B64_WRITE(out, output, 2); - total += 2; - break; - } - - output[2] = static_cast (((sm_decodeMap[c1] & 0x03) << 6) | sm_decodeMap[c2]); - - B64_WRITE(out, output, 3); - total += 3; - inTotal += count; - - if (progress) - progress->progress(inTotal, inTotal); - } - - if (progress) - progress->stop(inTotal); - - return (total); -} - - -} // vmime diff --git a/src/encoderBinary.cpp b/src/encoderBinary.cpp deleted file mode 100644 index dd77b072..00000000 --- a/src/encoderBinary.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#include "vmime/encoderBinary.hpp" - - -namespace vmime -{ - - -encoderBinary::encoderBinary() -{ -} - - -} // vmime diff --git a/src/encoderDefault.cpp b/src/encoderDefault.cpp deleted file mode 100644 index 1f2ba859..00000000 --- a/src/encoderDefault.cpp +++ /dev/null @@ -1,70 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#include "vmime/encoderDefault.hpp" - - -namespace vmime -{ - - -encoderDefault::encoderDefault() -{ -} - - -utility::stream::size_type encoderDefault::encode(utility::inputStream& in, - utility::outputStream& out, utility::progressListener* progress) -{ - in.reset(); // may not work... - - // No encoding performed - utility::stream::size_type res = 0; - - if (progress) - res = utility::bufferedStreamCopy(in, out, 0, progress); - else - res = utility::bufferedStreamCopy(in, out); - - return res; -} - - -utility::stream::size_type encoderDefault::decode(utility::inputStream& in, - utility::outputStream& out, utility::progressListener* progress) -{ - in.reset(); // may not work... - - // No decoding performed - utility::stream::size_type res = 0; - - if (progress) - res = utility::bufferedStreamCopy(in, out, 0, progress); - else - res = utility::bufferedStreamCopy(in, out); - - return res; -} - - -} // vmime diff --git a/src/encoderFactory.cpp b/src/encoderFactory.cpp deleted file mode 100644 index 605286be..00000000 --- a/src/encoderFactory.cpp +++ /dev/null @@ -1,110 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#include "vmime/encoderFactory.hpp" -#include "vmime/exception.hpp" - -#include "vmime/encoderB64.hpp" -#include "vmime/encoderQP.hpp" -#include "vmime/encoderUUE.hpp" -#include "vmime/encoderBinary.hpp" -#include "vmime/encoder7bit.hpp" -#include "vmime/encoder8bit.hpp" - - -namespace vmime -{ - - -encoderFactory::encoderFactory() -{ - // Register some default encoders - registerName ("base64"); - registerName ("quoted-printable"); - registerName ("uuencode"); - registerName ("7bit"); - registerName ("8bit"); - registerName ("binary"); -} - - -encoderFactory::~encoderFactory() -{ -} - - -encoderFactory* encoderFactory::getInstance() -{ - static encoderFactory instance; - return (&instance); -} - - -ref encoderFactory::create(const string& name) -{ - return (getEncoderByName(name)->create()); -} - - -const ref encoderFactory::getEncoderByName(const string& name) const -{ - const string lcName(utility::stringUtils::toLower(name)); - - for (std::vector >::const_iterator it = m_encoders.begin() ; - it != m_encoders.end() ; ++it) - { - if ((*it)->getName() == lcName) - return (*it); - } - - throw exceptions::no_encoder_available(name); -} - - -int encoderFactory::getEncoderCount() const -{ - return (m_encoders.size()); -} - - -const ref encoderFactory::getEncoderAt(const int pos) const -{ - return (m_encoders[pos]); -} - - -const std::vector > encoderFactory::getEncoderList() const -{ - std::vector > res; - - for (std::vector >::const_iterator it = m_encoders.begin() ; - it != m_encoders.end() ; ++it) - { - res.push_back(*it); - } - - return (res); -} - - -} // vmime diff --git a/src/encoderQP.cpp b/src/encoderQP.cpp deleted file mode 100644 index 95ca331c..00000000 --- a/src/encoderQP.cpp +++ /dev/null @@ -1,469 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#include "vmime/encoderQP.hpp" -#include "vmime/parserHelpers.hpp" - - -namespace vmime -{ - - -encoderQP::encoderQP() -{ -} - - -const std::vector encoderQP::getAvailableProperties() const -{ - std::vector list(encoder::getAvailableProperties()); - - list.push_back("maxlinelength"); - - list.push_back("text"); // if set, '\r' and '\n' are not hex-encoded. - // WARNING! You should not use this for binary data! - - list.push_back("rfc2047"); // for header fields encoding (RFC #2047) - - return (list); -} - - - -// Encoding table -const unsigned char encoderQP::sm_hexDigits[] = "0123456789ABCDEF"; - -// Decoding table -const unsigned char encoderQP::sm_hexDecodeTable[256] = -{ - 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, - 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, - 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, - 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, - 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, - 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, - 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, - 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 -}; - - -#ifndef VMIME_BUILDING_DOC - -#define QP_ENCODE_HEX(x) \ - outBuffer[outBufferPos] = '='; \ - outBuffer[outBufferPos + 1] = sm_hexDigits[x >> 4]; \ - outBuffer[outBufferPos + 2] = sm_hexDigits[x & 0xF]; \ - outBufferPos += 3; \ - curCol += 3; - -#define QP_WRITE(s, x, l) s.write(reinterpret_cast (x), l) - -#endif // VMIME_BUILDING_DOC - - -utility::stream::size_type encoderQP::encode(utility::inputStream& in, - utility::outputStream& out, utility::progressListener* progress) -{ - in.reset(); // may not work... - - const string::size_type propMaxLineLength = - getProperties().getProperty ("maxlinelength", static_cast (-1)); - - const bool rfc2047 = getProperties().getProperty ("rfc2047", false); - const bool text = getProperties().getProperty ("text", false); // binary mode by default - - const bool cutLines = (propMaxLineLength != static_cast (-1)); - const string::size_type maxLineLength = std::min(propMaxLineLength, static_cast (74)); - - // Process the data - char buffer[16384]; - int bufferLength = 0; - int bufferPos = 0; - - string::size_type curCol = 0; - - unsigned char outBuffer[16384]; - int outBufferPos = 0; - - utility::stream::size_type total = 0; - utility::stream::size_type inTotal = 0; - - if (progress) - progress->start(0); - - while (bufferPos < bufferLength || !in.eof()) - { - // Flush current output buffer - if (outBufferPos + 6 >= static_cast (sizeof(outBuffer))) - { - QP_WRITE(out, outBuffer, outBufferPos); - - total += outBufferPos; - outBufferPos = 0; - } - - // Need to get more data? - if (bufferPos >= bufferLength) - { - bufferLength = in.read(buffer, sizeof(buffer)); - bufferPos = 0; - - // No more data - if (bufferLength == 0) - break; - } - - // Get the next char and encode it - const unsigned char c = static_cast (buffer[bufferPos++]); - - switch (c) - { - case '.': - { - if (!rfc2047 && curCol == 0) - { - // If a '.' appears at the beginning of a line, we encode it to - // to avoid problems with SMTP servers... ("\r\n.\r\n" means the - // end of data transmission). - QP_ENCODE_HEX('.') - continue; - } - - outBuffer[outBufferPos++] = '.'; - ++curCol; - break; - } - case ' ': - { - // RFC-2047, Page 5, 4.2. The "Q" encoding: - // << The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be - // represented as "_" (underscore, ASCII 95.). >> - if (rfc2047) - { - outBuffer[outBufferPos++] = '_'; - ++curCol; - } - else - { - // Need to get more data? - if (bufferPos >= bufferLength) - { - bufferLength = in.read(buffer, sizeof(buffer)); - bufferPos = 0; - } - - // Spaces cannot appear at the end of a line. So, encode the space. - if (bufferPos >= bufferLength || - (buffer[bufferPos] == '\r' || buffer[bufferPos] == '\n')) - { - QP_ENCODE_HEX(' '); - } - else - { - outBuffer[outBufferPos++] = ' '; - ++curCol; - } - } - - break; - } - case '\t': - { - QP_ENCODE_HEX(c) - break; - } - case '\r': - case '\n': - { - // Text mode (where using CRLF or LF or ... does not - // care for a new line...) - if (text) - { - outBuffer[outBufferPos++] = c; - ++curCol; - } - // Binary mode (where CR and LF bytes are important!) - else - { - QP_ENCODE_HEX(c) - } - - break; - } - case '=': - { - QP_ENCODE_HEX('=') - break; - } - // RFC-2047 'especials' characters - case ',': - case ';': - case ':': - case '_': - case '@': - case '(': - case ')': - case '<': - case '>': - case '[': - case ']': - { - if (rfc2047) - { - QP_ENCODE_HEX(c) - } - else - { - outBuffer[outBufferPos++] = c; - ++curCol; - } - - break; - } - /* - Rule #2: (Literal representation) Octets with decimal values of 33 - through 60 inclusive, and 62 through 126, inclusive, MAY be - represented as the ASCII characters which correspond to those - octets (EXCLAMATION POINT through LESS THAN, and GREATER THAN - through TILDE, respectively). - */ - default: - { - //if ((c >= 33 && c <= 60) || (c >= 62 && c <= 126)) - if (c >= 33 && c <= 126 && c != 61 && c != 63) - { - outBuffer[outBufferPos++] = c; - ++curCol; - } - // Other characters: '=' + hexadecimal encoding - else - { - QP_ENCODE_HEX(c) - } - - break; - } - - } - - // Soft line break : "=\r\n" - if (cutLines && curCol >= maxLineLength - 1) - { - outBuffer[outBufferPos] = '='; - outBuffer[outBufferPos + 1] = '\r'; - outBuffer[outBufferPos + 2] = '\n'; - - outBufferPos += 3; - curCol = 0; - } - - ++inTotal; - - if (progress) - progress->progress(inTotal, inTotal); - } - - // Flush remaining output buffer - if (outBufferPos != 0) - { - QP_WRITE(out, outBuffer, outBufferPos); - total += outBufferPos; - } - - if (progress) - progress->stop(inTotal); - - return (total); -} - - -utility::stream::size_type encoderQP::decode(utility::inputStream& in, - utility::outputStream& out, utility::progressListener* progress) -{ - in.reset(); // may not work... - - // Process the data - const bool rfc2047 = getProperties().getProperty ("rfc2047", false); - - char buffer[16384]; - int bufferLength = 0; - int bufferPos = 0; - - unsigned char outBuffer[16384]; - int outBufferPos = 0; - - utility::stream::size_type total = 0; - utility::stream::size_type inTotal = 0; - - while (bufferPos < bufferLength || !in.eof()) - { - // Flush current output buffer - if (outBufferPos >= static_cast (sizeof(outBuffer))) - { - QP_WRITE(out, outBuffer, outBufferPos); - - total += outBufferPos; - outBufferPos = 0; - } - - // Need to get more data? - if (bufferPos >= bufferLength) - { - bufferLength = in.read(buffer, sizeof(buffer)); - bufferPos = 0; - - // No more data - if (bufferLength == 0) - break; - } - - // Decode the next sequence (hex-encoded byte or printable character) - unsigned char c = static_cast (buffer[bufferPos++]); - - ++inTotal; - - switch (c) - { - case '=': - { - if (bufferPos >= bufferLength) - { - bufferLength = in.read(buffer, sizeof(buffer)); - bufferPos = 0; - } - - if (bufferPos < bufferLength) - { - c = static_cast (buffer[bufferPos++]); - - ++inTotal; - - switch (c) - { - // Ignore soft line break ("=\r\n" or "=\n") - case '\r': - - // Read one byte more - if (bufferPos >= bufferLength) - { - bufferLength = in.read(buffer, sizeof(buffer)); - bufferPos = 0; - } - - if (bufferPos < bufferLength) - { - ++bufferPos; - ++inTotal; - } - - break; - - case '\n': - - break; - - // Hex-encoded char - default: - { - // We need another byte... - if (bufferPos >= bufferLength) - { - bufferLength = in.read(buffer, sizeof(buffer)); - bufferPos = 0; - } - - if (bufferPos < bufferLength) - { - const unsigned char next = static_cast (buffer[bufferPos++]); - - ++inTotal; - - const unsigned char value = static_cast - (sm_hexDecodeTable[c] * 16 + sm_hexDecodeTable[next]); - - outBuffer[outBufferPos++] = value; - } - else - { - // Premature end-of-data - } - - break; - } - - } - } - else - { - // Premature end-of-data - } - - break; - } - case '_': - { - if (rfc2047) - { - // RFC-2047, Page 5, 4.2. The "Q" encoding: - // << Note that the "_" always represents hexadecimal 20, even if the SPACE - // character occupies a different code position in the character set in use. >> - outBuffer[outBufferPos++] = 0x20; - break; - } - - // no break here... - } - default: - { - outBuffer[outBufferPos++] = c; - } - - } - - if (progress) - progress->progress(inTotal, inTotal); - } - - // Flush remaining output buffer - if (outBufferPos != 0) - { - QP_WRITE(out, outBuffer, outBufferPos); - total += outBufferPos; - } - - if (progress) - progress->stop(inTotal); - - return (total); -} - - -} // vmime diff --git a/src/encoderUUE.cpp b/src/encoderUUE.cpp deleted file mode 100644 index ed54ffa7..00000000 --- a/src/encoderUUE.cpp +++ /dev/null @@ -1,328 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#include "vmime/encoderUUE.hpp" -#include "vmime/parserHelpers.hpp" - - -namespace vmime -{ - - -encoderUUE::encoderUUE() -{ - getProperties()["mode"] = 644; - getProperties()["filename"] = "no_name"; - getProperties()["maxlinelength"] = 46; -} - - -const std::vector encoderUUE::getAvailableProperties() const -{ - std::vector list(encoder::getAvailableProperties()); - - list.push_back("maxlinelength"); - - list.push_back("mode"); - list.push_back("filename"); - - return (list); -} - - -// This is the character encoding function to make a character printable -static inline unsigned char UUENCODE(const unsigned char c) -{ - return ((c & 077) + ' '); -} - -// Single character decoding -static inline unsigned char UUDECODE(const unsigned char c) -{ - return ((c - ' ') & 077); -} - - -utility::stream::size_type encoderUUE::encode(utility::inputStream& in, - utility::outputStream& out, utility::progressListener* progress) -{ - in.reset(); // may not work... - - const string propFilename = getProperties().getProperty ("filename", ""); - const string propMode = getProperties().getProperty ("mode", "644"); - - const string::size_type maxLineLength = - std::min(getProperties().getProperty ("maxlinelength", 46), - static_cast (46)); - - utility::stream::size_type total = 0; - utility::stream::size_type inTotal = 0; - - // Output the prelude text ("begin [mode] [filename]") - out << "begin"; - - if (!propFilename.empty()) - { - out << " " << propMode << " " << propFilename; - total += 2 + propMode.length() + propFilename.length(); - } - - out << "\r\n"; - total += 7; - - // Process the data - utility::stream::value_type inBuffer[64]; - utility::stream::value_type outBuffer[64]; - - if (progress) - progress->start(0); - - while (!in.eof()) - { - // Process up to 45 characters per line - std::fill(inBuffer, inBuffer + sizeof(inBuffer), 0); - - const utility::stream::size_type inLength = in.read(inBuffer, maxLineLength - 1); - - outBuffer[0] = UUENCODE(inLength); // Line length - - utility::stream::size_type j = 1; - - for (utility::stream::size_type i = 0 ; i < inLength ; i += 3, j += 4) - { - const unsigned char c1 = static_cast (inBuffer[i]); - const unsigned char c2 = static_cast (inBuffer[i + 1]); - const unsigned char c3 = static_cast (inBuffer[i + 2]); - - outBuffer[j] = UUENCODE(c1 >> 2); - outBuffer[j + 1] = UUENCODE((c1 << 4) & 060 | (c2 >> 4) & 017); - outBuffer[j + 2] = UUENCODE((c2 << 2) & 074 | (c3 >> 6) & 03); - outBuffer[j + 3] = UUENCODE(c3 & 077); - } - - outBuffer[j] = '\r'; - outBuffer[j + 1] = '\n'; - - out.write(outBuffer, j + 2); - - total += j + 2; - inTotal += inLength; - - if (progress) - progress->progress(inTotal, inTotal); - } - - out << "end\r\n"; - total += 5; - - if (progress) - progress->stop(inTotal); - - return (total); -} - - -utility::stream::size_type encoderUUE::decode(utility::inputStream& in, - utility::outputStream& out, utility::progressListener* progress) -{ - in.reset(); // may not work... - - // Process the data - utility::stream::value_type inBuffer[64]; - utility::stream::value_type outBuffer[64]; - - utility::stream::size_type total = 0; - utility::stream::size_type inTotal = 0; - - bool stop = false; - - std::fill(inBuffer, inBuffer + sizeof(inBuffer), 0); - - if (progress) - progress->start(0); - - while (!stop && !in.eof()) - { - // Get the line length - utility::stream::value_type lengthChar; - - if (in.read(&lengthChar, 1) == 0) - break; - - const utility::stream::size_type outLength = UUDECODE(lengthChar); - const utility::stream::size_type inLength = - std::min((outLength * 4) / 3, static_cast (64)); - utility::stream::value_type inPos = 0; - - switch (lengthChar) - { - case ' ': - case '\t': - case '\r': - case '\n': - { - // Ignore - continue; - } - case 'b': - { - // Read 5 characters more to check for begin ("begin ...\r\n" or "begin ...\n") - inPos = in.read(inBuffer, 5); - - if (inPos == 5 && - inBuffer[0] == 'e' && - inBuffer[1] == 'g' && - inBuffer[2] == 'i' && - inBuffer[3] == 'n' && - parserHelpers::isSpace(inBuffer[4])) - { - inTotal += 5; - - utility::stream::value_type c = 0; - - utility::stream::size_type count = 0; - utility::stream::value_type buffer[512]; - - while (count < sizeof(buffer) - 1 && in.read(&c, 1) == 1) - { - if (c == '\n') - break; - - buffer[count++] = c; - } - - inTotal += count; - - if (c != '\n') - { - // OOPS! Weird line. Don't try to decode more... - - if (progress) - progress->stop(inTotal); - - return (total); - } - - // Parse filename and mode - if (count > 0) - { - buffer[count] = '\0'; - - utility::stream::value_type* p = buffer; - - while (*p && parserHelpers::isSpace(*p)) ++p; - - utility::stream::value_type* modeStart = buffer; - - while (*p && !parserHelpers::isSpace(*p)) ++p; - - getResults()["mode"] = string(modeStart, p); - - while (*p && parserHelpers::isSpace(*p)) ++p; - - utility::stream::value_type* filenameStart = buffer; - - while (*p && !(*p == '\r' || *p == '\n')) ++p; - - getResults()["filename"] = string(filenameStart, p); - } - // No filename or mode specified - else - { - getResults()["filename"] = "untitled"; - getResults()["mode"] = 644; - } - - continue; - } - - break; - } - case 'e': - { - // Read 3 characters more to check for end ("end\r\n" or "end\n") - inPos = in.read(inBuffer, 3); - - if (inPos == 3 && - inBuffer[0] == 'n' && - inBuffer[1] == 'd' && - (inBuffer[2] == '\r' || inBuffer[2] == '\n')) - { - stop = true; - inTotal += 3; - continue; - } - - break; - } - - } - - // Read encoded data - if (in.read(inBuffer + inPos, inLength - inPos) != inLength - inPos) - { - // Premature end of data - break; - } - - inTotal += (inLength - inPos); - - // Decode data - for (utility::stream::size_type i = 0, j = 0 ; i < inLength ; i += 4, j += 3) - { - const unsigned char c1 = static_cast (inBuffer[i]); - const unsigned char c2 = static_cast (inBuffer[i + 1]); - const unsigned char c3 = static_cast (inBuffer[i + 2]); - const unsigned char c4 = static_cast (inBuffer[i + 3]); - - const utility::stream::size_type n = - std::min(inLength - i, static_cast (3)); - - switch (n) - { - default: - case 3: outBuffer[j + 2] = UUDECODE(c3) << 6 | UUDECODE(c4); - case 2: outBuffer[j + 1] = UUDECODE(c2) << 4 | UUDECODE(c3) >> 2; - case 1: outBuffer[j] = UUDECODE(c1) << 2 | UUDECODE(c2) >> 4; - case 0: break; - } - - total += n; - } - - out.write(outBuffer, outLength); - - std::fill(inBuffer, inBuffer + sizeof(inBuffer), 0); - - if (progress) - progress->progress(inTotal, inTotal); - } - - if (progress) - progress->stop(inTotal); - - return (total); -} - - -} // vmime diff --git a/src/encoding.cpp b/src/encoding.cpp index 202dfe90..a45573de 100644 --- a/src/encoding.cpp +++ b/src/encoding.cpp @@ -22,9 +22,10 @@ // #include "vmime/encoding.hpp" -#include "vmime/encoderFactory.hpp" #include "vmime/contentHandler.hpp" +#include "vmime/utility/encoder/encoderFactory.hpp" + #include @@ -77,9 +78,9 @@ void encoding::generate(utility::outputStream& os, const string::size_type /* ma } -ref encoding::getEncoder() const +ref encoding::getEncoder() const { - return (encoderFactory::getInstance()->create(generate())); + return (utility::encoder::encoderFactory::getInstance()->create(generate())); } diff --git a/src/net/smtp/SMTPTransport.cpp b/src/net/smtp/SMTPTransport.cpp index 0a2e5b9f..3ce8d217 100644 --- a/src/net/smtp/SMTPTransport.cpp +++ b/src/net/smtp/SMTPTransport.cpp @@ -26,7 +26,6 @@ #include "vmime/exception.hpp" #include "vmime/platform.hpp" -#include "vmime/encoderB64.hpp" #include "vmime/mailboxList.hpp" #include "vmime/utility/filteredStream.hpp" diff --git a/src/security/sasl/SASLContext.cpp b/src/security/sasl/SASLContext.cpp index 6de2f25c..fca95c72 100644 --- a/src/security/sasl/SASLContext.cpp +++ b/src/security/sasl/SASLContext.cpp @@ -29,7 +29,8 @@ #include "vmime/security/sasl/SASLMechanism.hpp" #include "vmime/base.hpp" -#include "vmime/encoderFactory.hpp" + +#include "vmime/utility/encoder/encoderFactory.hpp" #include "vmime/utility/stream.hpp" @@ -103,7 +104,8 @@ void SASLContext::decodeB64(const string& input, byte_t** output, int* outputLen utility::inputStreamStringAdapter is(input); utility::outputStreamStringAdapter os(res); - ref dec = encoderFactory::getInstance()->create("base64"); + ref dec = + utility::encoder::encoderFactory::getInstance()->create("base64"); dec->decode(is, os); @@ -123,7 +125,8 @@ const string SASLContext::encodeB64(const byte_t* input, const int inputLen) utility::inputStreamByteBufferAdapter is(input, inputLen); utility::outputStreamStringAdapter os(res); - ref enc = encoderFactory::getInstance()->create("base64"); + ref enc = + utility::encoder::encoderFactory::getInstance()->create("base64"); enc->encode(is, os); diff --git a/src/streamContentHandler.cpp b/src/streamContentHandler.cpp index 386b0e17..b5f02bf0 100644 --- a/src/streamContentHandler.cpp +++ b/src/streamContentHandler.cpp @@ -94,8 +94,8 @@ void streamContentHandler::generate(utility::outputStream& os, const vmime::enco // buffer, and then re-encode to output stream... if (m_encoding != enc) { - ref theDecoder = m_encoding.getEncoder(); - ref theEncoder = enc.getEncoder(); + ref theDecoder = m_encoding.getEncoder(); + ref theEncoder = enc.getEncoder(); theEncoder->getProperties()["maxlinelength"] = maxLineLength; @@ -122,7 +122,7 @@ void streamContentHandler::generate(utility::outputStream& os, const vmime::enco // Need to encode data before else { - ref theEncoder = enc.getEncoder(); + ref theEncoder = enc.getEncoder(); theEncoder->getProperties()["maxlinelength"] = maxLineLength; m_stream->reset(); // may not work... @@ -151,7 +151,7 @@ void streamContentHandler::extract(utility::outputStream& os, // Need to decode data else { - ref theDecoder = m_encoding.getEncoder(); + ref theDecoder = m_encoding.getEncoder(); m_stream->reset(); // may not work... diff --git a/src/stringContentHandler.cpp b/src/stringContentHandler.cpp index 6dd2719b..84cada5f 100644 --- a/src/stringContentHandler.cpp +++ b/src/stringContentHandler.cpp @@ -114,8 +114,8 @@ void stringContentHandler::generate(utility::outputStream& os, // buffer, and then re-encode to output stream... if (m_encoding != enc) { - ref theDecoder = m_encoding.getEncoder(); - ref theEncoder = enc.getEncoder(); + ref theDecoder = m_encoding.getEncoder(); + ref theEncoder = enc.getEncoder(); theEncoder->getProperties()["maxlinelength"] = maxLineLength; @@ -140,7 +140,7 @@ void stringContentHandler::generate(utility::outputStream& os, // Need to encode data before else { - ref theEncoder = enc.getEncoder(); + ref theEncoder = enc.getEncoder(); theEncoder->getProperties()["maxlinelength"] = maxLineLength; utility::inputStreamStringProxyAdapter in(m_string); @@ -161,7 +161,7 @@ void stringContentHandler::extract(utility::outputStream& os, // Need to decode data else { - ref theDecoder = m_encoding.getEncoder(); + ref theDecoder = m_encoding.getEncoder(); utility::inputStreamStringProxyAdapter in(m_string); utility::progressListenerSizeAdapter plsa(progress, getLength()); diff --git a/src/utility/encoder/b64Encoder.cpp b/src/utility/encoder/b64Encoder.cpp new file mode 100644 index 00000000..c4ba2b39 --- /dev/null +++ b/src/utility/encoder/b64Encoder.cpp @@ -0,0 +1,308 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/encoder/b64Encoder.hpp" +#include "vmime/parserHelpers.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +b64Encoder::b64Encoder() +{ +} + + +const std::vector b64Encoder::getAvailableProperties() const +{ + std::vector list(encoder::getAvailableProperties()); + + list.push_back("maxlinelength"); + + return (list); +} + + +// 7-bits alphabet used to encode binary data +const unsigned char b64Encoder::sm_alphabet[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + +const unsigned char b64Encoder::sm_decodeMap[256] = +{ + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0x00 - 0x0f + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0x10 - 0x1f + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3e,0xff,0xff,0xff,0x3f, // 0x20 - 0x2f + 0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0xff,0xff,0xff,0x3d,0xff,0xff, // 0x30 - 0x3f + 0xff,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e, // 0x40 - 0x4f + 0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0xff,0xff,0xff,0xff,0xff, // 0x50 - 0x5f + 0xff,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28, // 0x60 - 0x6f + 0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0xff,0xff,0xff,0xff, // 0x70 - 0x7f + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0x80 - 0x8f + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0x90 - 0x9f + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xa0 - 0xaf + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xb0 - 0xbf + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xc0 - 0xcf + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xd0 - 0xdf + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xe0 - 0xef + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // 0xf0 - 0xff +}; + +#ifndef VMIME_BUILDING_DOC + #define B64_WRITE(s, x, l) s.write(reinterpret_cast (x), l) +#endif // VMIME_BUILDING_DOC + + + +utility::stream::size_type b64Encoder::encode(utility::inputStream& in, + utility::outputStream& out, utility::progressListener* progress) +{ + in.reset(); // may not work... + + const int propMaxLineLength = getProperties().getProperty ("maxlinelength", -1); + + const bool cutLines = (propMaxLineLength != -1); + const int maxLineLength = std::min(propMaxLineLength, 76); + + // Process data + utility::stream::value_type buffer[65536]; + utility::stream::size_type bufferLength = 0; + utility::stream::size_type bufferPos = 0; + + unsigned char bytes[3]; + unsigned char output[4]; + + utility::stream::size_type total = 0; + utility::stream::size_type inTotal = 0; + + int curCol = 0; + + if (progress) + progress->start(0); + + while (bufferPos < bufferLength || !in.eof()) + { + if (bufferPos >= bufferLength) + { + bufferLength = in.read(buffer, sizeof(buffer)); + bufferPos = 0; + + if (bufferLength == 0) + break; + } + + // Get 3 bytes of data + int count = 0; + + while (count < 3 && bufferPos < bufferLength) + bytes[count++] = buffer[bufferPos++]; + + while (count < 3) + { + // There may be more data in the next chunk... + if (bufferPos >= bufferLength) + { + bufferLength = in.read(buffer, sizeof(buffer)); + bufferPos = 0; + + if (bufferLength == 0) + break; + } + + while (count < 3 && bufferPos < bufferLength) + bytes[count++] = buffer[bufferPos++]; + } + + // Encode data + switch (count) + { + case 1: + + output[0] = sm_alphabet[(bytes[0] & 0xFC) >> 2]; + output[1] = sm_alphabet[(bytes[0] & 0x03) << 4]; + output[2] = sm_alphabet[64]; // padding + output[3] = sm_alphabet[64]; // padding + + break; + + case 2: + + output[0] = sm_alphabet[(bytes[0] & 0xFC) >> 2]; + output[1] = sm_alphabet[((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xF0) >> 4)]; + output[2] = sm_alphabet[(bytes[1] & 0x0F) << 2]; + output[3] = sm_alphabet[64]; // padding + + break; + + default: + case 3: + + output[0] = sm_alphabet[(bytes[0] & 0xFC) >> 2]; + output[1] = sm_alphabet[((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xF0) >> 4)]; + output[2] = sm_alphabet[((bytes[1] & 0x0F) << 2) | ((bytes[2] & 0xC0) >> 6)]; + output[3] = sm_alphabet[(bytes[2] & 0x3F)]; + + break; + } + + // Write encoded data to output stream + B64_WRITE(out, output, 4); + + inTotal += count; + total += 4; + curCol += 4; + + if (cutLines && curCol >= maxLineLength - 2 /* \r\n */ - 4 /* next bytes */) + { + out.write("\r\n", 2); + curCol = 0; + } + + if (progress) + progress->progress(inTotal, inTotal); + } + + if (progress) + progress->stop(inTotal); + + return (total); +} + + +utility::stream::size_type b64Encoder::decode(utility::inputStream& in, + utility::outputStream& out, utility::progressListener* progress) +{ + in.reset(); // may not work... + + // Process the data + char buffer[16384]; + int bufferLength = 0; + int bufferPos = 0; + + utility::stream::size_type total = 0; + utility::stream::size_type inTotal = 0; + + unsigned char bytes[4]; + unsigned char output[3]; + + if (progress) + progress->start(0); + + while (bufferPos < bufferLength || !in.eof()) + { + bytes[0] = '='; + bytes[1] = '='; + bytes[2] = '='; + bytes[3] = '='; + + // Need to get more data? + if (bufferPos >= bufferLength) + { + bufferLength = in.read(buffer, sizeof(buffer)); + bufferPos = 0; + + // No more data + if (bufferLength == 0) + break; + } + + // 4 bytes of input provide 3 bytes of output, so + // get the next 4 bytes from the input stream. + int count = 0; + + while (count < 4 && bufferPos < bufferLength) + { + const unsigned char c = buffer[bufferPos++]; + + if (!parserHelpers::isSpace(c)) + bytes[count++] = c; + } + + if (count != 4) + { + while (count < 4 && !in.eof()) + { + // Data continues on the next chunk + bufferLength = in.read(buffer, sizeof(buffer)); + bufferPos = 0; + + while (count < 4 && bufferPos < bufferLength) + { + const unsigned char c = buffer[bufferPos++]; + + if (!parserHelpers::isSpace(c)) + bytes[count++] = c; + } + } + } + + // Decode the bytes + unsigned char c1 = bytes[0]; + unsigned char c2 = bytes[1]; + + if (c1 == '=' || c2 == '=') // end + break; + + output[0] = static_cast ((sm_decodeMap[c1] << 2) | ((sm_decodeMap[c2] & 0x30) >> 4)); + + c1 = bytes[2]; + + if (c1 == '=') // end + { + B64_WRITE(out, output, 1); + total += 1; + break; + } + + output[1] = static_cast (((sm_decodeMap[c2] & 0xf) << 4) | ((sm_decodeMap[c1] & 0x3c) >> 2)); + + c2 = bytes[3]; + + if (c2 == '=') // end + { + B64_WRITE(out, output, 2); + total += 2; + break; + } + + output[2] = static_cast (((sm_decodeMap[c1] & 0x03) << 6) | sm_decodeMap[c2]); + + B64_WRITE(out, output, 3); + total += 3; + inTotal += count; + + if (progress) + progress->progress(inTotal, inTotal); + } + + if (progress) + progress->stop(inTotal); + + return (total); +} + + +} // encoder +} // utility +} // vmime diff --git a/src/utility/encoder/binaryEncoder.cpp b/src/utility/encoder/binaryEncoder.cpp new file mode 100644 index 00000000..2bc77d27 --- /dev/null +++ b/src/utility/encoder/binaryEncoder.cpp @@ -0,0 +1,39 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/encoder/binaryEncoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +binaryEncoder::binaryEncoder() +{ +} + + +} // encoder +} // utility +} // vmime diff --git a/src/utility/encoder/defaultEncoder.cpp b/src/utility/encoder/defaultEncoder.cpp new file mode 100644 index 00000000..db833c85 --- /dev/null +++ b/src/utility/encoder/defaultEncoder.cpp @@ -0,0 +1,73 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/encoder/defaultEncoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +defaultEncoder::defaultEncoder() +{ +} + + +utility::stream::size_type defaultEncoder::encode(utility::inputStream& in, + utility::outputStream& out, utility::progressListener* progress) +{ + in.reset(); // may not work... + + // No encoding performed + utility::stream::size_type res = 0; + + if (progress) + res = utility::bufferedStreamCopy(in, out, 0, progress); + else + res = utility::bufferedStreamCopy(in, out); + + return res; +} + + +utility::stream::size_type defaultEncoder::decode(utility::inputStream& in, + utility::outputStream& out, utility::progressListener* progress) +{ + in.reset(); // may not work... + + // No decoding performed + utility::stream::size_type res = 0; + + if (progress) + res = utility::bufferedStreamCopy(in, out, 0, progress); + else + res = utility::bufferedStreamCopy(in, out); + + return res; +} + + +} // encoder +} // utility +} // vmime diff --git a/src/utility/encoder/eightBitEncoder.cpp b/src/utility/encoder/eightBitEncoder.cpp new file mode 100644 index 00000000..88f07bc4 --- /dev/null +++ b/src/utility/encoder/eightBitEncoder.cpp @@ -0,0 +1,39 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/encoder/eightBitEncoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +eightBitEncoder::eightBitEncoder() +{ +} + + +} // encoder +} // utility +} // vmime diff --git a/src/utility/encoder/encoder.cpp b/src/utility/encoder/encoder.cpp new file mode 100644 index 00000000..893ed4fc --- /dev/null +++ b/src/utility/encoder/encoder.cpp @@ -0,0 +1,76 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/encoder/encoder.hpp" +#include "vmime/exception.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +encoder::encoder() +{ +} + + +encoder::~encoder() +{ +} + + +const propertySet& encoder::getProperties() const +{ + return (m_props); +} + + +propertySet& encoder::getProperties() +{ + return (m_props); +} + + +const propertySet& encoder::getResults() const +{ + return (m_results); +} + + +propertySet& encoder::getResults() +{ + return (m_results); +} + + +const std::vector encoder::getAvailableProperties() const +{ + std::vector list; + return (list); +} + + +} // encoder +} // utility +} // vmime diff --git a/src/utility/encoder/encoderFactory.cpp b/src/utility/encoder/encoderFactory.cpp new file mode 100644 index 00000000..9d7c1070 --- /dev/null +++ b/src/utility/encoder/encoderFactory.cpp @@ -0,0 +1,113 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/encoder/encoderFactory.hpp" +#include "vmime/exception.hpp" + +#include "vmime/utility/encoder/b64Encoder.hpp" +#include "vmime/utility/encoder/qpEncoder.hpp" +#include "vmime/utility/encoder/uuEncoder.hpp" +#include "vmime/utility/encoder/binaryEncoder.hpp" +#include "vmime/utility/encoder/sevenBitEncoder.hpp" +#include "vmime/utility/encoder/eightBitEncoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +encoderFactory::encoderFactory() +{ + // Register some default encoders + registerName ("base64"); + registerName ("quoted-printable"); + registerName ("uuencode"); + registerName ("7bit"); + registerName ("8bit"); + registerName ("binary"); +} + + +encoderFactory::~encoderFactory() +{ +} + + +encoderFactory* encoderFactory::getInstance() +{ + static encoderFactory instance; + return (&instance); +} + + +ref encoderFactory::create(const string& name) +{ + return (getEncoderByName(name)->create()); +} + + +const ref encoderFactory::getEncoderByName(const string& name) const +{ + const string lcName(utility::stringUtils::toLower(name)); + + for (std::vector >::const_iterator it = m_encoders.begin() ; + it != m_encoders.end() ; ++it) + { + if ((*it)->getName() == lcName) + return (*it); + } + + throw exceptions::no_encoder_available(name); +} + + +int encoderFactory::getEncoderCount() const +{ + return (m_encoders.size()); +} + + +const ref encoderFactory::getEncoderAt(const int pos) const +{ + return (m_encoders[pos]); +} + + +const std::vector > encoderFactory::getEncoderList() const +{ + std::vector > res; + + for (std::vector >::const_iterator it = m_encoders.begin() ; + it != m_encoders.end() ; ++it) + { + res.push_back(*it); + } + + return (res); +} + + +} // encoder +} // utility +} // vmime diff --git a/src/utility/encoder/qpEncoder.cpp b/src/utility/encoder/qpEncoder.cpp new file mode 100644 index 00000000..737d488a --- /dev/null +++ b/src/utility/encoder/qpEncoder.cpp @@ -0,0 +1,472 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/encoder/qpEncoder.hpp" +#include "vmime/parserHelpers.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +qpEncoder::qpEncoder() +{ +} + + +const std::vector qpEncoder::getAvailableProperties() const +{ + std::vector list(encoder::getAvailableProperties()); + + list.push_back("maxlinelength"); + + list.push_back("text"); // if set, '\r' and '\n' are not hex-encoded. + // WARNING! You should not use this for binary data! + + list.push_back("rfc2047"); // for header fields encoding (RFC #2047) + + return (list); +} + + + +// Encoding table +const unsigned char qpEncoder::sm_hexDigits[] = "0123456789ABCDEF"; + +// Decoding table +const unsigned char qpEncoder::sm_hexDecodeTable[256] = +{ + 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, + 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, + 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, + 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, + 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, + 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, + 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, + 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 +}; + + +#ifndef VMIME_BUILDING_DOC + +#define QP_ENCODE_HEX(x) \ + outBuffer[outBufferPos] = '='; \ + outBuffer[outBufferPos + 1] = sm_hexDigits[x >> 4]; \ + outBuffer[outBufferPos + 2] = sm_hexDigits[x & 0xF]; \ + outBufferPos += 3; \ + curCol += 3; + +#define QP_WRITE(s, x, l) s.write(reinterpret_cast (x), l) + +#endif // VMIME_BUILDING_DOC + + +utility::stream::size_type qpEncoder::encode(utility::inputStream& in, + utility::outputStream& out, utility::progressListener* progress) +{ + in.reset(); // may not work... + + const string::size_type propMaxLineLength = + getProperties().getProperty ("maxlinelength", static_cast (-1)); + + const bool rfc2047 = getProperties().getProperty ("rfc2047", false); + const bool text = getProperties().getProperty ("text", false); // binary mode by default + + const bool cutLines = (propMaxLineLength != static_cast (-1)); + const string::size_type maxLineLength = std::min(propMaxLineLength, static_cast (74)); + + // Process the data + char buffer[16384]; + int bufferLength = 0; + int bufferPos = 0; + + string::size_type curCol = 0; + + unsigned char outBuffer[16384]; + int outBufferPos = 0; + + utility::stream::size_type total = 0; + utility::stream::size_type inTotal = 0; + + if (progress) + progress->start(0); + + while (bufferPos < bufferLength || !in.eof()) + { + // Flush current output buffer + if (outBufferPos + 6 >= static_cast (sizeof(outBuffer))) + { + QP_WRITE(out, outBuffer, outBufferPos); + + total += outBufferPos; + outBufferPos = 0; + } + + // Need to get more data? + if (bufferPos >= bufferLength) + { + bufferLength = in.read(buffer, sizeof(buffer)); + bufferPos = 0; + + // No more data + if (bufferLength == 0) + break; + } + + // Get the next char and encode it + const unsigned char c = static_cast (buffer[bufferPos++]); + + switch (c) + { + case '.': + { + if (!rfc2047 && curCol == 0) + { + // If a '.' appears at the beginning of a line, we encode it to + // to avoid problems with SMTP servers... ("\r\n.\r\n" means the + // end of data transmission). + QP_ENCODE_HEX('.') + continue; + } + + outBuffer[outBufferPos++] = '.'; + ++curCol; + break; + } + case ' ': + { + // RFC-2047, Page 5, 4.2. The "Q" encoding: + // << The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be + // represented as "_" (underscore, ASCII 95.). >> + if (rfc2047) + { + outBuffer[outBufferPos++] = '_'; + ++curCol; + } + else + { + // Need to get more data? + if (bufferPos >= bufferLength) + { + bufferLength = in.read(buffer, sizeof(buffer)); + bufferPos = 0; + } + + // Spaces cannot appear at the end of a line. So, encode the space. + if (bufferPos >= bufferLength || + (buffer[bufferPos] == '\r' || buffer[bufferPos] == '\n')) + { + QP_ENCODE_HEX(' '); + } + else + { + outBuffer[outBufferPos++] = ' '; + ++curCol; + } + } + + break; + } + case '\t': + { + QP_ENCODE_HEX(c) + break; + } + case '\r': + case '\n': + { + // Text mode (where using CRLF or LF or ... does not + // care for a new line...) + if (text) + { + outBuffer[outBufferPos++] = c; + ++curCol; + } + // Binary mode (where CR and LF bytes are important!) + else + { + QP_ENCODE_HEX(c) + } + + break; + } + case '=': + { + QP_ENCODE_HEX('=') + break; + } + // RFC-2047 'especials' characters + case ',': + case ';': + case ':': + case '_': + case '@': + case '(': + case ')': + case '<': + case '>': + case '[': + case ']': + { + if (rfc2047) + { + QP_ENCODE_HEX(c) + } + else + { + outBuffer[outBufferPos++] = c; + ++curCol; + } + + break; + } + /* + Rule #2: (Literal representation) Octets with decimal values of 33 + through 60 inclusive, and 62 through 126, inclusive, MAY be + represented as the ASCII characters which correspond to those + octets (EXCLAMATION POINT through LESS THAN, and GREATER THAN + through TILDE, respectively). + */ + default: + { + //if ((c >= 33 && c <= 60) || (c >= 62 && c <= 126)) + if (c >= 33 && c <= 126 && c != 61 && c != 63) + { + outBuffer[outBufferPos++] = c; + ++curCol; + } + // Other characters: '=' + hexadecimal encoding + else + { + QP_ENCODE_HEX(c) + } + + break; + } + + } + + // Soft line break : "=\r\n" + if (cutLines && curCol >= maxLineLength - 1) + { + outBuffer[outBufferPos] = '='; + outBuffer[outBufferPos + 1] = '\r'; + outBuffer[outBufferPos + 2] = '\n'; + + outBufferPos += 3; + curCol = 0; + } + + ++inTotal; + + if (progress) + progress->progress(inTotal, inTotal); + } + + // Flush remaining output buffer + if (outBufferPos != 0) + { + QP_WRITE(out, outBuffer, outBufferPos); + total += outBufferPos; + } + + if (progress) + progress->stop(inTotal); + + return (total); +} + + +utility::stream::size_type qpEncoder::decode(utility::inputStream& in, + utility::outputStream& out, utility::progressListener* progress) +{ + in.reset(); // may not work... + + // Process the data + const bool rfc2047 = getProperties().getProperty ("rfc2047", false); + + char buffer[16384]; + int bufferLength = 0; + int bufferPos = 0; + + unsigned char outBuffer[16384]; + int outBufferPos = 0; + + utility::stream::size_type total = 0; + utility::stream::size_type inTotal = 0; + + while (bufferPos < bufferLength || !in.eof()) + { + // Flush current output buffer + if (outBufferPos >= static_cast (sizeof(outBuffer))) + { + QP_WRITE(out, outBuffer, outBufferPos); + + total += outBufferPos; + outBufferPos = 0; + } + + // Need to get more data? + if (bufferPos >= bufferLength) + { + bufferLength = in.read(buffer, sizeof(buffer)); + bufferPos = 0; + + // No more data + if (bufferLength == 0) + break; + } + + // Decode the next sequence (hex-encoded byte or printable character) + unsigned char c = static_cast (buffer[bufferPos++]); + + ++inTotal; + + switch (c) + { + case '=': + { + if (bufferPos >= bufferLength) + { + bufferLength = in.read(buffer, sizeof(buffer)); + bufferPos = 0; + } + + if (bufferPos < bufferLength) + { + c = static_cast (buffer[bufferPos++]); + + ++inTotal; + + switch (c) + { + // Ignore soft line break ("=\r\n" or "=\n") + case '\r': + + // Read one byte more + if (bufferPos >= bufferLength) + { + bufferLength = in.read(buffer, sizeof(buffer)); + bufferPos = 0; + } + + if (bufferPos < bufferLength) + { + ++bufferPos; + ++inTotal; + } + + break; + + case '\n': + + break; + + // Hex-encoded char + default: + { + // We need another byte... + if (bufferPos >= bufferLength) + { + bufferLength = in.read(buffer, sizeof(buffer)); + bufferPos = 0; + } + + if (bufferPos < bufferLength) + { + const unsigned char next = static_cast (buffer[bufferPos++]); + + ++inTotal; + + const unsigned char value = static_cast + (sm_hexDecodeTable[c] * 16 + sm_hexDecodeTable[next]); + + outBuffer[outBufferPos++] = value; + } + else + { + // Premature end-of-data + } + + break; + } + + } + } + else + { + // Premature end-of-data + } + + break; + } + case '_': + { + if (rfc2047) + { + // RFC-2047, Page 5, 4.2. The "Q" encoding: + // << Note that the "_" always represents hexadecimal 20, even if the SPACE + // character occupies a different code position in the character set in use. >> + outBuffer[outBufferPos++] = 0x20; + break; + } + + // no break here... + } + default: + { + outBuffer[outBufferPos++] = c; + } + + } + + if (progress) + progress->progress(inTotal, inTotal); + } + + // Flush remaining output buffer + if (outBufferPos != 0) + { + QP_WRITE(out, outBuffer, outBufferPos); + total += outBufferPos; + } + + if (progress) + progress->stop(inTotal); + + return (total); +} + + +} // encoder +} // utility +} // vmime diff --git a/src/utility/encoder/sevenBitEncoder.cpp b/src/utility/encoder/sevenBitEncoder.cpp new file mode 100644 index 00000000..812afa90 --- /dev/null +++ b/src/utility/encoder/sevenBitEncoder.cpp @@ -0,0 +1,39 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/encoder/sevenBitEncoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +sevenBitEncoder::sevenBitEncoder() +{ +} + + +} // encoder +} // utility +} // vmime diff --git a/src/utility/encoder/uuEncoder.cpp b/src/utility/encoder/uuEncoder.cpp new file mode 100644 index 00000000..0e72599b --- /dev/null +++ b/src/utility/encoder/uuEncoder.cpp @@ -0,0 +1,331 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/encoder/uuEncoder.hpp" +#include "vmime/parserHelpers.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +uuEncoder::uuEncoder() +{ + getProperties()["mode"] = 644; + getProperties()["filename"] = "no_name"; + getProperties()["maxlinelength"] = 46; +} + + +const std::vector uuEncoder::getAvailableProperties() const +{ + std::vector list(encoder::getAvailableProperties()); + + list.push_back("maxlinelength"); + + list.push_back("mode"); + list.push_back("filename"); + + return (list); +} + + +// This is the character encoding function to make a character printable +static inline unsigned char UUENCODE(const unsigned char c) +{ + return ((c & 077) + ' '); +} + +// Single character decoding +static inline unsigned char UUDECODE(const unsigned char c) +{ + return ((c - ' ') & 077); +} + + +utility::stream::size_type uuEncoder::encode(utility::inputStream& in, + utility::outputStream& out, utility::progressListener* progress) +{ + in.reset(); // may not work... + + const string propFilename = getProperties().getProperty ("filename", ""); + const string propMode = getProperties().getProperty ("mode", "644"); + + const string::size_type maxLineLength = + std::min(getProperties().getProperty ("maxlinelength", 46), + static_cast (46)); + + utility::stream::size_type total = 0; + utility::stream::size_type inTotal = 0; + + // Output the prelude text ("begin [mode] [filename]") + out << "begin"; + + if (!propFilename.empty()) + { + out << " " << propMode << " " << propFilename; + total += 2 + propMode.length() + propFilename.length(); + } + + out << "\r\n"; + total += 7; + + // Process the data + utility::stream::value_type inBuffer[64]; + utility::stream::value_type outBuffer[64]; + + if (progress) + progress->start(0); + + while (!in.eof()) + { + // Process up to 45 characters per line + std::fill(inBuffer, inBuffer + sizeof(inBuffer), 0); + + const utility::stream::size_type inLength = in.read(inBuffer, maxLineLength - 1); + + outBuffer[0] = UUENCODE(inLength); // Line length + + utility::stream::size_type j = 1; + + for (utility::stream::size_type i = 0 ; i < inLength ; i += 3, j += 4) + { + const unsigned char c1 = static_cast (inBuffer[i]); + const unsigned char c2 = static_cast (inBuffer[i + 1]); + const unsigned char c3 = static_cast (inBuffer[i + 2]); + + outBuffer[j] = UUENCODE(c1 >> 2); + outBuffer[j + 1] = UUENCODE((c1 << 4) & 060 | (c2 >> 4) & 017); + outBuffer[j + 2] = UUENCODE((c2 << 2) & 074 | (c3 >> 6) & 03); + outBuffer[j + 3] = UUENCODE(c3 & 077); + } + + outBuffer[j] = '\r'; + outBuffer[j + 1] = '\n'; + + out.write(outBuffer, j + 2); + + total += j + 2; + inTotal += inLength; + + if (progress) + progress->progress(inTotal, inTotal); + } + + out << "end\r\n"; + total += 5; + + if (progress) + progress->stop(inTotal); + + return (total); +} + + +utility::stream::size_type uuEncoder::decode(utility::inputStream& in, + utility::outputStream& out, utility::progressListener* progress) +{ + in.reset(); // may not work... + + // Process the data + utility::stream::value_type inBuffer[64]; + utility::stream::value_type outBuffer[64]; + + utility::stream::size_type total = 0; + utility::stream::size_type inTotal = 0; + + bool stop = false; + + std::fill(inBuffer, inBuffer + sizeof(inBuffer), 0); + + if (progress) + progress->start(0); + + while (!stop && !in.eof()) + { + // Get the line length + utility::stream::value_type lengthChar; + + if (in.read(&lengthChar, 1) == 0) + break; + + const utility::stream::size_type outLength = UUDECODE(lengthChar); + const utility::stream::size_type inLength = + std::min((outLength * 4) / 3, static_cast (64)); + utility::stream::value_type inPos = 0; + + switch (lengthChar) + { + case ' ': + case '\t': + case '\r': + case '\n': + { + // Ignore + continue; + } + case 'b': + { + // Read 5 characters more to check for begin ("begin ...\r\n" or "begin ...\n") + inPos = in.read(inBuffer, 5); + + if (inPos == 5 && + inBuffer[0] == 'e' && + inBuffer[1] == 'g' && + inBuffer[2] == 'i' && + inBuffer[3] == 'n' && + parserHelpers::isSpace(inBuffer[4])) + { + inTotal += 5; + + utility::stream::value_type c = 0; + + utility::stream::size_type count = 0; + utility::stream::value_type buffer[512]; + + while (count < sizeof(buffer) - 1 && in.read(&c, 1) == 1) + { + if (c == '\n') + break; + + buffer[count++] = c; + } + + inTotal += count; + + if (c != '\n') + { + // OOPS! Weird line. Don't try to decode more... + + if (progress) + progress->stop(inTotal); + + return (total); + } + + // Parse filename and mode + if (count > 0) + { + buffer[count] = '\0'; + + utility::stream::value_type* p = buffer; + + while (*p && parserHelpers::isSpace(*p)) ++p; + + utility::stream::value_type* modeStart = buffer; + + while (*p && !parserHelpers::isSpace(*p)) ++p; + + getResults()["mode"] = string(modeStart, p); + + while (*p && parserHelpers::isSpace(*p)) ++p; + + utility::stream::value_type* filenameStart = buffer; + + while (*p && !(*p == '\r' || *p == '\n')) ++p; + + getResults()["filename"] = string(filenameStart, p); + } + // No filename or mode specified + else + { + getResults()["filename"] = "untitled"; + getResults()["mode"] = 644; + } + + continue; + } + + break; + } + case 'e': + { + // Read 3 characters more to check for end ("end\r\n" or "end\n") + inPos = in.read(inBuffer, 3); + + if (inPos == 3 && + inBuffer[0] == 'n' && + inBuffer[1] == 'd' && + (inBuffer[2] == '\r' || inBuffer[2] == '\n')) + { + stop = true; + inTotal += 3; + continue; + } + + break; + } + + } + + // Read encoded data + if (in.read(inBuffer + inPos, inLength - inPos) != inLength - inPos) + { + // Premature end of data + break; + } + + inTotal += (inLength - inPos); + + // Decode data + for (utility::stream::size_type i = 0, j = 0 ; i < inLength ; i += 4, j += 3) + { + const unsigned char c1 = static_cast (inBuffer[i]); + const unsigned char c2 = static_cast (inBuffer[i + 1]); + const unsigned char c3 = static_cast (inBuffer[i + 2]); + const unsigned char c4 = static_cast (inBuffer[i + 3]); + + const utility::stream::size_type n = + std::min(inLength - i, static_cast (3)); + + switch (n) + { + default: + case 3: outBuffer[j + 2] = UUDECODE(c3) << 6 | UUDECODE(c4); + case 2: outBuffer[j + 1] = UUDECODE(c2) << 4 | UUDECODE(c3) >> 2; + case 1: outBuffer[j] = UUDECODE(c1) << 2 | UUDECODE(c2) >> 4; + case 0: break; + } + + total += n; + } + + out.write(outBuffer, outLength); + + std::fill(inBuffer, inBuffer + sizeof(inBuffer), 0); + + if (progress) + progress->progress(inTotal, inTotal); + } + + if (progress) + progress->stop(inTotal); + + return (total); +} + + +} // encoder +} // utility +} // vmime diff --git a/src/word.cpp b/src/word.cpp index b129965d..95adc5c6 100644 --- a/src/word.cpp +++ b/src/word.cpp @@ -28,9 +28,9 @@ #include "vmime/utility/smartPtr.hpp" #include "vmime/parserHelpers.hpp" -#include "vmime/encoder.hpp" -#include "vmime/encoderB64.hpp" -#include "vmime/encoderQP.hpp" +#include "vmime/utility/encoder/encoder.hpp" +#include "vmime/utility/encoder/b64Encoder.hpp" +#include "vmime/utility/encoder/qpEncoder.hpp" #include "vmime/wordEncoder.hpp" @@ -267,17 +267,17 @@ void word::parse(const string& buffer, const string::size_type position, const string::const_iterator dataEnd = p; p += 2; // skip '?=' - encoder* theEncoder = NULL; + utility::encoder::encoder* theEncoder = NULL; // Base-64 encoding if (*encPos == 'B' || *encPos == 'b') { - theEncoder = new encoderB64; + theEncoder = new utility::encoder::b64Encoder(); } // Quoted-Printable encoding else if (*encPos == 'Q' || *encPos == 'q') { - theEncoder = new encoderQP; + theEncoder = new utility::encoder::qpEncoder(); theEncoder->getProperties()["rfc2047"] = true; } diff --git a/src/wordEncoder.cpp b/src/wordEncoder.cpp index 9888c169..ab7c004c 100644 --- a/src/wordEncoder.cpp +++ b/src/wordEncoder.cpp @@ -25,8 +25,9 @@ #include "vmime/exception.hpp" #include "vmime/charsetConverter.hpp" -#include "vmime/encoderB64.hpp" -#include "vmime/encoderQP.hpp" + +#include "vmime/utility/encoder/b64Encoder.hpp" +#include "vmime/utility/encoder/qpEncoder.hpp" #include "vmime/utility/stringUtils.hpp" @@ -62,11 +63,11 @@ wordEncoder::wordEncoder(const string& buffer, const charset& charset, const Enc if (m_encoding == ENCODING_B64) { - m_encoder = vmime::create (); + m_encoder = vmime::create (); } else // ENCODING_QP { - m_encoder = vmime::create (); + m_encoder = vmime::create (); m_encoder->getProperties()["rfc2047"] = true; } } diff --git a/tests/parser/encoderTest.cpp b/tests/parser/encoderTest.cpp deleted file mode 100644 index 180d1296..00000000 --- a/tests/parser/encoderTest.cpp +++ /dev/null @@ -1,288 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#include "tests/testUtils.hpp" - - -#define VMIME_TEST_SUITE encoderTest -#define VMIME_TEST_SUITE_MODULE "Parser" - - -VMIME_TEST_SUITE_BEGIN - - VMIME_TEST_LIST_BEGIN - VMIME_TEST(testBase64) - VMIME_TEST(testQuotedPrintable) - VMIME_TEST_LIST_END - - - // Encoding helper function - static const vmime::string encode(const vmime::string& name, const vmime::string& in, int maxLineLength = 0) - { - vmime::ref enc = vmime::encoderFactory::getInstance()->create(name); - - if (maxLineLength != 0) - enc->getProperties()["maxlinelength"] = maxLineLength; - - vmime::utility::inputStreamStringAdapter vin(in); - - std::ostringstream out; - vmime::utility::outputStreamAdapter vout(out); - - enc->encode(vin, vout); - - return (out.str()); - } - - // Decoding helper function - static const vmime::string decode(const vmime::string& name, const vmime::string& in, int maxLineLength = 0) - { - vmime::ref enc = vmime::encoderFactory::getInstance()->create(name); - - if (maxLineLength != 0) - enc->getProperties()["maxlinelength"] = maxLineLength; - - vmime::utility::inputStreamStringAdapter vin(in); - - std::ostringstream out; - vmime::utility::outputStreamAdapter vout(out); - - enc->decode(vin, vout); - - return (out.str()); - } - - - void testBase64() - { - static const vmime::string testSuites[] = - { - // Test 1 - "", - - "", - - // Test 2 - "A", - - "QQ==", - - // Test 3 - "AB", - - "QUI=", - - // Test 4 - "ABC", - - "QUJD", - - // Test 5 - "foo", - - "Zm9v", - - // Test 6 - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", - - "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAx" - "MjM0NTY3ODk=", - - // Test 7 - vmime::string( - "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" - "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" - "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" - "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" - "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" - "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" - "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" - "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" - "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" - "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" - "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" - "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" - "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" - "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" - "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" - "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", - 256), - - "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1" - "Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr" - "bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6Ch" - "oqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX" - "2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==" - }; - - - for (unsigned int i = 0 ; i < sizeof(testSuites) / sizeof(testSuites[0]) / 2 ; ++i) - { - const vmime::string decoded = testSuites[i * 2]; - const vmime::string encoded = testSuites[i * 2 + 1]; - - std::ostringstream oss; - oss << "[Base64] Test " << (i + 1) << ": "; - - // Encoding - VASSERT_EQ(oss.str() + "encoding", encoded, encode("base64", decoded)); - - // Decoding - VASSERT_EQ(oss.str() + "decoding", decoded, decode("base64", encoded)); - - // Multiple and successive encoding/decoding - VASSERT_EQ(oss.str() + "multiple1", decoded, - decode("base64", - encode("base64", decoded))); - - VASSERT_EQ(oss.str() + "multiple2", decoded, - decode("base64", - decode("base64", - encode("base64", - encode("base64", decoded))))); - - VASSERT_EQ(oss.str() + "multiple3", decoded, - decode("base64", - decode("base64", - decode("base64", - encode("base64", - encode("base64", - encode("base64", decoded))))))); - - VASSERT_EQ(oss.str() + "multiple4", decoded, - decode("base64", - decode("base64", - decode("base64", - decode("base64", - encode("base64", - encode("base64", - encode("base64", - encode("base64", decoded))))))))); - } - } - - void testQuotedPrintable() - { - static const vmime::string testSuites[] = - { - // Test 1 - "", - - "", - - // Test 2 - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", - - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", - - // Test 3 - "0123456789012345678901234567890123456789012345678901234567890123456789012" - "3456789012345678901234567890123456789012345678901234567890123456789012345" - "6789", - - "0123456789012345678901234567890123456789012345678901234567890123456789012=\r\n" - "3456789012345678901234567890123456789012345678901234567890123456789012345=\r\n" - "6789", - - // Test 4 - vmime::string( - "\x89\xe8\x24\x04\x2f\xe8\xff\xfb\xeb\xff\x90\xd7\x74\x8d\x00\x26\x89\x55" - "\x83\xe5\x08\xec\x04\xc7\xa0\x24\x05\xa2\xe8\x08\x43\xee\x00\x00\xc0\x85" - "\x0a\x74\xec\x89\xc3\x5d\xb6\x8d\x00\x00\x00\x00\x04\xc7\xa8\x24\x05\xa2" - "\xe8\x08\x43\xd4\x00\x00\xe8\xeb\xf6\x89\x89\x55\x81\xe5\xa8\xec\x00\x00" - "\x89\x00\xfc\x75\x75\x8b\x89\x08\xf8\x5d\xb9\xe8\xff\xff\x83\xff\x14\xfe" - "\x47\x74\xc0\x31\x9d\x8d\xff\x68\xff\xff\x85\x89\xff\x68\xff\xff\x85\x8d" - "\xff\x6c\xff\xff\x04\x89\xe8\x24\xfa\x50\xff\xff\x45\xc7\x00\xec\x00\x00" - "\x31\x00\x89\xc0\x24\x44\x89\x08\x24\x5c\x89\x04\x24\x34\x87\xe8\xff\xf6" - "\x89\xff\x24\x34\x2f\xe8\xff\xf9\x8b\xff\xf8\x5d\x75\x8b\x89\xfc\x5d\xec" - "\xbe\xc3\x00\x13\x00\x00\xe7\xeb\xb6\x8d\x00\x00\x00\x00\x89\x55\x57\xe5" - "\x53\x56\xec\x81\x01\xdc\x00\x00\x45\xbb\x05\x5c\x8b\x08\x0c\x55\xe4\x83" - "\x8b\xf0\x89\x02\x24\x5c\xc7\x04\x24\x04\x00\x06\x00\x00\xec\xa3\x05\xa9" - "\xe8\x08\xf7\x2a\xff\xff\x04\xc7\x46\x24\x05\x5c\xb9\x08\x5c\x50\x08\x05" - "\x4c\x89\x04\x24\xf5\xe8\xff\xf7\xc7\xff\x24\x04\x5c\x46\x08\x05\xe9\xe8" - "\xff\xf8\xc7\xff\x24\x04\x1d\x70\x08\x05\x55\xe8\x00\xbb\xb8\x00\x00\x01" - "\x00\x00\xd2\x31\x08\xa3\x05\xa7\xb8\x08\x00\x01\x00\x00\x0c\xa3\x05\xa7", - 18 * 16), - - "=89=E8$=04/=E8=FF=FB=EB=FF=90=D7t=8D=00&=89U=83=E5=08=EC=04=C7=A0$=05=A2=E8=\r\n" - "=08C=EE=00=00=C0=85=0At=EC=89=C3]=B6=8D=00=00=00=00=04=C7=A8$=05=A2=E8=08=\r\n" - "C=D4=00=00=E8=EB=F6=89=89U=81=E5=A8=EC=00=00=89=00=FCuu=8B=89=08=F8]=B9=E8=\r\n" - "=FF=FF=83=FF=14=FEGt=C01=9D=8D=FFh=FF=FF=85=89=FFh=FF=FF=85=8D=FFl=FF=FF=04=\r\n" - "=89=E8$=FAP=FF=FFE=C7=00=EC=00=001=00=89=C0$D=89=08$\\=89=04$4=87=E8=FF=F6=\r\n" - "=89=FF$4/=E8=FF=F9=8B=FF=F8]u=8B=89=FC]=EC=BE=C3=00=13=00=00=E7=EB=B6=8D=00=\r\n" - "=00=00=00=89UW=E5SV=EC=81=01=DC=00=00E=BB=05\\=8B=08=0CU=E4=83=8B=F0=89=02=\r\n" - "$\\=C7=04$=04=00=06=00=00=EC=A3=05=A9=E8=08=F7*=FF=FF=04=C7F$=05\\=B9=08\\P=08=\r\n" - "=05L=89=04$=F5=E8=FF=F7=C7=FF$=04\\F=08=05=E9=E8=FF=F8=C7=FF$=04=1Dp=08=05=\r\n" - "U=E8=00=BB=B8=00=00=01=00=00=D21=08=A3=05=A7=B8=08=00=01=00=00=0C=A3=05=A7=\r\n" - }; - - - for (unsigned int i = 0 ; i < sizeof(testSuites) / sizeof(testSuites[0]) / 2 ; ++i) - { - const vmime::string decoded = testSuites[i * 2]; - const vmime::string encoded = testSuites[i * 2 + 1]; - - std::ostringstream oss; - oss << "[QP] Test " << (i + 1) << ": "; - - // Encoding - VASSERT_EQ(oss.str() + "encoding", encoded, encode("quoted-printable", decoded, 74)); - - // Decoding - VASSERT_EQ(oss.str() + "decoding", decoded, decode("quoted-printable", encoded, 74)); - - // Multiple and successive encoding/decoding - VASSERT_EQ(oss.str() + "multiple1", decoded, - decode("quoted-printable", - encode("quoted-printable", decoded))); - - VASSERT_EQ(oss.str() + "multiple2", decoded, - decode("quoted-printable", - decode("quoted-printable", - encode("quoted-printable", - encode("quoted-printable", decoded))))); - - VASSERT_EQ(oss.str() + "multiple3", decoded, - decode("quoted-printable", - decode("quoted-printable", - decode("quoted-printable", - encode("quoted-printable", - encode("quoted-printable", - encode("quoted-printable", decoded))))))); - - VASSERT_EQ(oss.str() + "multiple4", decoded, - decode("quoted-printable", - decode("quoted-printable", - decode("quoted-printable", - decode("quoted-printable", - encode("quoted-printable", - encode("quoted-printable", - encode("quoted-printable", - encode("quoted-printable", decoded))))))))); - } - } - - // TODO: UUEncode - -VMIME_TEST_SUITE_END - diff --git a/tests/utility/encoderTest.cpp b/tests/utility/encoderTest.cpp new file mode 100644 index 00000000..648af78b --- /dev/null +++ b/tests/utility/encoderTest.cpp @@ -0,0 +1,290 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + + +#define VMIME_TEST_SUITE encoderTest +#define VMIME_TEST_SUITE_MODULE "Parser" + + +VMIME_TEST_SUITE_BEGIN + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testBase64) + VMIME_TEST(testQuotedPrintable) + VMIME_TEST_LIST_END + + + // Encoding helper function + static const vmime::string encode(const vmime::string& name, const vmime::string& in, int maxLineLength = 0) + { + vmime::ref enc = + vmime::utility::encoder::encoderFactory::getInstance()->create(name); + + if (maxLineLength != 0) + enc->getProperties()["maxlinelength"] = maxLineLength; + + vmime::utility::inputStreamStringAdapter vin(in); + + std::ostringstream out; + vmime::utility::outputStreamAdapter vout(out); + + enc->encode(vin, vout); + + return (out.str()); + } + + // Decoding helper function + static const vmime::string decode(const vmime::string& name, const vmime::string& in, int maxLineLength = 0) + { + vmime::ref enc = + vmime::utility::encoder::encoderFactory::getInstance()->create(name); + + if (maxLineLength != 0) + enc->getProperties()["maxlinelength"] = maxLineLength; + + vmime::utility::inputStreamStringAdapter vin(in); + + std::ostringstream out; + vmime::utility::outputStreamAdapter vout(out); + + enc->decode(vin, vout); + + return (out.str()); + } + + + void testBase64() + { + static const vmime::string testSuites[] = + { + // Test 1 + "", + + "", + + // Test 2 + "A", + + "QQ==", + + // Test 3 + "AB", + + "QUI=", + + // Test 4 + "ABC", + + "QUJD", + + // Test 5 + "foo", + + "Zm9v", + + // Test 6 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + + "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAx" + "MjM0NTY3ODk=", + + // Test 7 + vmime::string( + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" + "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" + "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" + "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" + "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" + "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" + "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" + "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" + "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" + "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" + "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" + "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" + "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" + "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" + "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", + 256), + + "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1" + "Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr" + "bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6Ch" + "oqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX" + "2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==" + }; + + + for (unsigned int i = 0 ; i < sizeof(testSuites) / sizeof(testSuites[0]) / 2 ; ++i) + { + const vmime::string decoded = testSuites[i * 2]; + const vmime::string encoded = testSuites[i * 2 + 1]; + + std::ostringstream oss; + oss << "[Base64] Test " << (i + 1) << ": "; + + // Encoding + VASSERT_EQ(oss.str() + "encoding", encoded, encode("base64", decoded)); + + // Decoding + VASSERT_EQ(oss.str() + "decoding", decoded, decode("base64", encoded)); + + // Multiple and successive encoding/decoding + VASSERT_EQ(oss.str() + "multiple1", decoded, + decode("base64", + encode("base64", decoded))); + + VASSERT_EQ(oss.str() + "multiple2", decoded, + decode("base64", + decode("base64", + encode("base64", + encode("base64", decoded))))); + + VASSERT_EQ(oss.str() + "multiple3", decoded, + decode("base64", + decode("base64", + decode("base64", + encode("base64", + encode("base64", + encode("base64", decoded))))))); + + VASSERT_EQ(oss.str() + "multiple4", decoded, + decode("base64", + decode("base64", + decode("base64", + decode("base64", + encode("base64", + encode("base64", + encode("base64", + encode("base64", decoded))))))))); + } + } + + void testQuotedPrintable() + { + static const vmime::string testSuites[] = + { + // Test 1 + "", + + "", + + // Test 2 + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", + + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", + + // Test 3 + "0123456789012345678901234567890123456789012345678901234567890123456789012" + "3456789012345678901234567890123456789012345678901234567890123456789012345" + "6789", + + "0123456789012345678901234567890123456789012345678901234567890123456789012=\r\n" + "3456789012345678901234567890123456789012345678901234567890123456789012345=\r\n" + "6789", + + // Test 4 + vmime::string( + "\x89\xe8\x24\x04\x2f\xe8\xff\xfb\xeb\xff\x90\xd7\x74\x8d\x00\x26\x89\x55" + "\x83\xe5\x08\xec\x04\xc7\xa0\x24\x05\xa2\xe8\x08\x43\xee\x00\x00\xc0\x85" + "\x0a\x74\xec\x89\xc3\x5d\xb6\x8d\x00\x00\x00\x00\x04\xc7\xa8\x24\x05\xa2" + "\xe8\x08\x43\xd4\x00\x00\xe8\xeb\xf6\x89\x89\x55\x81\xe5\xa8\xec\x00\x00" + "\x89\x00\xfc\x75\x75\x8b\x89\x08\xf8\x5d\xb9\xe8\xff\xff\x83\xff\x14\xfe" + "\x47\x74\xc0\x31\x9d\x8d\xff\x68\xff\xff\x85\x89\xff\x68\xff\xff\x85\x8d" + "\xff\x6c\xff\xff\x04\x89\xe8\x24\xfa\x50\xff\xff\x45\xc7\x00\xec\x00\x00" + "\x31\x00\x89\xc0\x24\x44\x89\x08\x24\x5c\x89\x04\x24\x34\x87\xe8\xff\xf6" + "\x89\xff\x24\x34\x2f\xe8\xff\xf9\x8b\xff\xf8\x5d\x75\x8b\x89\xfc\x5d\xec" + "\xbe\xc3\x00\x13\x00\x00\xe7\xeb\xb6\x8d\x00\x00\x00\x00\x89\x55\x57\xe5" + "\x53\x56\xec\x81\x01\xdc\x00\x00\x45\xbb\x05\x5c\x8b\x08\x0c\x55\xe4\x83" + "\x8b\xf0\x89\x02\x24\x5c\xc7\x04\x24\x04\x00\x06\x00\x00\xec\xa3\x05\xa9" + "\xe8\x08\xf7\x2a\xff\xff\x04\xc7\x46\x24\x05\x5c\xb9\x08\x5c\x50\x08\x05" + "\x4c\x89\x04\x24\xf5\xe8\xff\xf7\xc7\xff\x24\x04\x5c\x46\x08\x05\xe9\xe8" + "\xff\xf8\xc7\xff\x24\x04\x1d\x70\x08\x05\x55\xe8\x00\xbb\xb8\x00\x00\x01" + "\x00\x00\xd2\x31\x08\xa3\x05\xa7\xb8\x08\x00\x01\x00\x00\x0c\xa3\x05\xa7", + 18 * 16), + + "=89=E8$=04/=E8=FF=FB=EB=FF=90=D7t=8D=00&=89U=83=E5=08=EC=04=C7=A0$=05=A2=E8=\r\n" + "=08C=EE=00=00=C0=85=0At=EC=89=C3]=B6=8D=00=00=00=00=04=C7=A8$=05=A2=E8=08=\r\n" + "C=D4=00=00=E8=EB=F6=89=89U=81=E5=A8=EC=00=00=89=00=FCuu=8B=89=08=F8]=B9=E8=\r\n" + "=FF=FF=83=FF=14=FEGt=C01=9D=8D=FFh=FF=FF=85=89=FFh=FF=FF=85=8D=FFl=FF=FF=04=\r\n" + "=89=E8$=FAP=FF=FFE=C7=00=EC=00=001=00=89=C0$D=89=08$\\=89=04$4=87=E8=FF=F6=\r\n" + "=89=FF$4/=E8=FF=F9=8B=FF=F8]u=8B=89=FC]=EC=BE=C3=00=13=00=00=E7=EB=B6=8D=00=\r\n" + "=00=00=00=89UW=E5SV=EC=81=01=DC=00=00E=BB=05\\=8B=08=0CU=E4=83=8B=F0=89=02=\r\n" + "$\\=C7=04$=04=00=06=00=00=EC=A3=05=A9=E8=08=F7*=FF=FF=04=C7F$=05\\=B9=08\\P=08=\r\n" + "=05L=89=04$=F5=E8=FF=F7=C7=FF$=04\\F=08=05=E9=E8=FF=F8=C7=FF$=04=1Dp=08=05=\r\n" + "U=E8=00=BB=B8=00=00=01=00=00=D21=08=A3=05=A7=B8=08=00=01=00=00=0C=A3=05=A7=\r\n" + }; + + + for (unsigned int i = 0 ; i < sizeof(testSuites) / sizeof(testSuites[0]) / 2 ; ++i) + { + const vmime::string decoded = testSuites[i * 2]; + const vmime::string encoded = testSuites[i * 2 + 1]; + + std::ostringstream oss; + oss << "[QP] Test " << (i + 1) << ": "; + + // Encoding + VASSERT_EQ(oss.str() + "encoding", encoded, encode("quoted-printable", decoded, 74)); + + // Decoding + VASSERT_EQ(oss.str() + "decoding", decoded, decode("quoted-printable", encoded, 74)); + + // Multiple and successive encoding/decoding + VASSERT_EQ(oss.str() + "multiple1", decoded, + decode("quoted-printable", + encode("quoted-printable", decoded))); + + VASSERT_EQ(oss.str() + "multiple2", decoded, + decode("quoted-printable", + decode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", decoded))))); + + VASSERT_EQ(oss.str() + "multiple3", decoded, + decode("quoted-printable", + decode("quoted-printable", + decode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", decoded))))))); + + VASSERT_EQ(oss.str() + "multiple4", decoded, + decode("quoted-printable", + decode("quoted-printable", + decode("quoted-printable", + decode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", decoded))))))))); + } + } + + // TODO: UUEncode + +VMIME_TEST_SUITE_END + diff --git a/vmime/encoder.hpp b/vmime/encoder.hpp deleted file mode 100644 index cb5de063..00000000 --- a/vmime/encoder.hpp +++ /dev/null @@ -1,107 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#ifndef VMIME_ENCODER_HPP_INCLUDED -#define VMIME_ENCODER_HPP_INCLUDED - - -#include "vmime/base.hpp" -#include "vmime/propertySet.hpp" -#include "vmime/exception.hpp" -#include "vmime/utility/progressListener.hpp" - - -namespace vmime -{ - - -/** Encode/decode data in different encodings. - */ - -class encoder : public object -{ -public: - - encoder(); - virtual ~encoder(); - - /** Encode data. - * - * @param in input data (decoded) - * @param out output stream for encoded data - * @param progress progress listener, or NULL if you do not - * want to receive progress notifications - * @return number of bytes written into output stream - */ - virtual utility::stream::size_type encode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL) = 0; - - /** Decode data. - * - * @param in input data (encoded) - * @param out output stream for decoded data - * @param progress progress listener, or NULL if you do not - * want to receive progress notifications - * @return number of bytes written into output stream - */ - virtual utility::stream::size_type decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL) = 0; - - /** Return the properties of the encoder. - * - * @return properties of the encoder - */ - const propertySet& getProperties() const; - - /** Return the properties of the encoder. - * - * @return properties of the encoder - */ - propertySet& getProperties(); - - /** Return a list of property names that can be set for - * this encoder. - * - * @return list of property names - */ - virtual const std::vector getAvailableProperties() const; - - /** Return the results returned by this encoder. - * - * @return results returned by the encoder - */ - const propertySet& getResults() const; - -protected: - - propertySet& getResults(); - -private: - - propertySet m_props; - propertySet m_results; -}; - - -} // vmime - - -#endif // VMIME_ENCODER_HPP_INCLUDED diff --git a/vmime/encoder7bit.hpp b/vmime/encoder7bit.hpp deleted file mode 100644 index 0b8fd177..00000000 --- a/vmime/encoder7bit.hpp +++ /dev/null @@ -1,49 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#ifndef VMIME_ENCODER7BIT_HPP_INCLUDED -#define VMIME_ENCODER7BIT_HPP_INCLUDED - - -#include "vmime/encoderDefault.hpp" - - -namespace vmime -{ - - -/** 7-bit encoder. - */ - -class encoder7bit : public encoderDefault -{ -public: - - encoder7bit(); -}; - - -} // vmime - - -#endif // VMIME_ENCODER7BIT_HPP_INCLUDED diff --git a/vmime/encoder8bit.hpp b/vmime/encoder8bit.hpp deleted file mode 100644 index 67722b06..00000000 --- a/vmime/encoder8bit.hpp +++ /dev/null @@ -1,49 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#ifndef VMIME_ENCODER8BIT_HPP_INCLUDED -#define VMIME_ENCODER8BIT_HPP_INCLUDED - - -#include "vmime/encoderDefault.hpp" - - -namespace vmime -{ - - -/** 8-bit encoder. - */ - -class encoder8bit : public encoderDefault -{ -public: - - encoder8bit(); -}; - - -} // vmime - - -#endif // VMIME_ENCODER8BIT_HPP_INCLUDED diff --git a/vmime/encoderB64.hpp b/vmime/encoderB64.hpp deleted file mode 100644 index 46444a53..00000000 --- a/vmime/encoderB64.hpp +++ /dev/null @@ -1,59 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#ifndef VMIME_ENCODERB64_HPP_INCLUDED -#define VMIME_ENCODERB64_HPP_INCLUDED - - -#include "vmime/encoder.hpp" - - -namespace vmime -{ - - -/** Base64 encoder. - */ - -class encoderB64 : public encoder -{ -public: - - encoderB64(); - - utility::stream::size_type encode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); - utility::stream::size_type decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); - - const std::vector getAvailableProperties() const; - -protected: - - static const unsigned char sm_alphabet[]; - static const unsigned char sm_decodeMap[256]; -}; - - -} // vmime - - -#endif // VMIME_ENCODERB64_HPP_INCLUDED diff --git a/vmime/encoderBinary.hpp b/vmime/encoderBinary.hpp deleted file mode 100644 index 4708c791..00000000 --- a/vmime/encoderBinary.hpp +++ /dev/null @@ -1,49 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#ifndef VMIME_ENCODERBINARY_HPP_INCLUDED -#define VMIME_ENCODERBINARY_HPP_INCLUDED - - -#include "vmime/encoderDefault.hpp" - - -namespace vmime -{ - - -/** Binary encoder. - */ - -class encoderBinary : public encoderDefault -{ -public: - - encoderBinary(); -}; - - -} // vmime - - -#endif // VMIME_ENCODERBINARY_HPP_INCLUDED diff --git a/vmime/encoderDefault.hpp b/vmime/encoderDefault.hpp deleted file mode 100644 index 71dc9e9f..00000000 --- a/vmime/encoderDefault.hpp +++ /dev/null @@ -1,52 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#ifndef VMIME_ENCODERDEFAULT_HPP_INCLUDED -#define VMIME_ENCODERDEFAULT_HPP_INCLUDED - - -#include "vmime/encoder.hpp" - - -namespace vmime -{ - - -/** Default encoder (simple copy, no encoding/decoding is performed). - */ - -class encoderDefault : public encoder -{ -public: - - encoderDefault(); - - utility::stream::size_type encode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); - utility::stream::size_type decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); -}; - - -} // vmime - - -#endif // VMIME_ENCODERDEFAUL_HPP_INCLUDED diff --git a/vmime/encoderFactory.hpp b/vmime/encoderFactory.hpp deleted file mode 100644 index 5358ce76..00000000 --- a/vmime/encoderFactory.hpp +++ /dev/null @@ -1,149 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#ifndef VMIME_ENCODERFACTORY_HPP_INCLUDED -#define VMIME_ENCODERFACTORY_HPP_INCLUDED - - -#include "vmime/encoder.hpp" -#include "vmime/utility/stringUtils.hpp" - - -namespace vmime -{ - - -/** A factory to create 'encoder' objects for the specified encoding. - */ - -class encoderFactory -{ -private: - - encoderFactory(); - ~encoderFactory(); - -public: - - static encoderFactory* getInstance(); - - /** Information about a registered encoder. */ - class registeredEncoder : public object - { - protected: - - virtual ~registeredEncoder() { } - - public: - - virtual ref create() const = 0; - - virtual const string& getName() const = 0; - }; - -private: - - template - class registeredEncoderImpl : public registeredEncoder - { - friend class vmime::creator; - - protected: - - registeredEncoderImpl(const string& name) : m_name(name) { } - - public: - - ref create() const - { - return vmime::create (); - } - - const string& getName() const - { - return (m_name); - } - - private: - - const string m_name; - }; - - - std::vector > m_encoders; - -public: - - /** Register a new encoder by its encoding name. - * - * @param name encoding name - */ - template - void registerName(const string& name) - { - m_encoders.push_back(vmime::create >(utility::stringUtils::toLower(name))); - } - - /** Create a new encoder instance from an encoding name. - * - * @param name encoding name (eg. "base64") - * @return a new encoder instance for the specified encoding - * @throw exceptions::no_encoder_available if no encoder is registered - * for this encoding - */ - ref create(const string& name); - - /** Return information about a registered encoder. - * - * @param name encoding name - * @return information about this encoder - * @throw exceptions::no_encoder_available if no encoder is registered - * for this encoding - */ - const ref getEncoderByName(const string& name) const; - - /** Return the number of registered encoders. - * - * @return number of registered encoders - */ - int getEncoderCount() const; - - /** Return the registered encoder at the specified position. - * - * @param pos position of the registered encoder to return - * @return registered encoder at the specified position - */ - const ref getEncoderAt(const int pos) const; - - /** Return a list of all registered encoders. - * - * @return list of registered encoders - */ - const std::vector > getEncoderList() const; -}; - - -} // vmime - - -#endif // VMIME_ENCODERFACTORY_HPP_INCLUDED diff --git a/vmime/encoderQP.hpp b/vmime/encoderQP.hpp deleted file mode 100644 index bd604abc..00000000 --- a/vmime/encoderQP.hpp +++ /dev/null @@ -1,59 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#ifndef VMIME_ENCODERQP_HPP_INCLUDED -#define VMIME_ENCODERQP_HPP_INCLUDED - - -#include "vmime/encoder.hpp" - - -namespace vmime -{ - - -/** Quoted-printable encoder. - */ - -class encoderQP : public encoder -{ -public: - - encoderQP(); - - utility::stream::size_type encode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); - utility::stream::size_type decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); - - const std::vector getAvailableProperties() const; - -protected: - - static const unsigned char sm_hexDigits[17]; - static const unsigned char sm_hexDecodeTable[256]; -}; - - -} // vmime - - -#endif // VMIME_ENCODERQP_HPP_INCLUDED diff --git a/vmime/encoderUUE.hpp b/vmime/encoderUUE.hpp deleted file mode 100644 index bdffb101..00000000 --- a/vmime/encoderUUE.hpp +++ /dev/null @@ -1,54 +0,0 @@ -// -// VMime library (http://www.vmime.org) -// Copyright (C) 2002-2008 Vincent Richard -// -// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// Linking this library statically or dynamically with other modules is making -// a combined work based on this library. Thus, the terms and conditions of -// the GNU General Public License cover the whole combination. -// - -#ifndef VMIME_ENCODERUUE_HPP_INCLUDED -#define VMIME_ENCODERUUE_HPP_INCLUDED - - -#include "vmime/encoder.hpp" - - -namespace vmime -{ - - -/** UUEncode encoder. - */ - -class encoderUUE : public encoder -{ -public: - - encoderUUE(); - - utility::stream::size_type encode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); - utility::stream::size_type decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); - - const std::vector getAvailableProperties() const; -}; - - -} // vmime - - -#endif // VMIME_ENCODERUUE_HPP_INCLUDED diff --git a/vmime/encoding.hpp b/vmime/encoding.hpp index fa5cc183..aa998539 100644 --- a/vmime/encoding.hpp +++ b/vmime/encoding.hpp @@ -27,7 +27,8 @@ #include "vmime/base.hpp" #include "vmime/headerFieldValue.hpp" -#include "vmime/encoder.hpp" + +#include "vmime/utility/encoder/encoder.hpp" namespace vmime @@ -99,7 +100,7 @@ public: * is registered for the encoding * @return a new encoder object for the encoding type */ - ref getEncoder() const; + ref getEncoder() const; private: diff --git a/vmime/net/imap/IMAPParser.hpp b/vmime/net/imap/IMAPParser.hpp index c6e1831d..b3079570 100644 --- a/vmime/net/imap/IMAPParser.hpp +++ b/vmime/net/imap/IMAPParser.hpp @@ -34,8 +34,8 @@ #include "vmime/utility/stringUtils.hpp" #include "vmime/utility/progressListener.hpp" -#include "vmime/encoderB64.hpp" -#include "vmime/encoderQP.hpp" +#include "vmime/utility/encoder/b64Encoder.hpp" +#include "vmime/utility/encoder/qpEncoder.hpp" #include "vmime/platform.hpp" @@ -1233,18 +1233,18 @@ public: delete (theCharset); // Decode text - encoder* theEncoder = NULL; + utility::encoder::encoder* theEncoder = NULL; if (theEncoding->value()[0] == 'q' || theEncoding->value()[0] == 'Q') { // Quoted-printable - theEncoder = new encoderQP; + theEncoder = new utility::encoder::qpEncoder(); theEncoder->getProperties()["rfc2047"] = true; } else if (theEncoding->value()[0] == 'b' || theEncoding->value()[0] == 'B') { // Base64 - theEncoder = new encoderB64; + theEncoder = new utility::encoder::b64Encoder(); } if (theEncoder) diff --git a/vmime/utility/encoder/b64Encoder.hpp b/vmime/utility/encoder/b64Encoder.hpp new file mode 100644 index 00000000..831e9f6f --- /dev/null +++ b/vmime/utility/encoder/b64Encoder.hpp @@ -0,0 +1,62 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#ifndef VMIME_UTILITY_ENCODER_B64ENCODER_HPP_INCLUDED +#define VMIME_UTILITY_ENCODER_B64ENCODER_HPP_INCLUDED + + +#include "vmime/utility/encoder/encoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +/** Base64 encoder. + */ + +class b64Encoder : public encoder +{ +public: + + b64Encoder(); + + utility::stream::size_type encode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); + utility::stream::size_type decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); + + const std::vector getAvailableProperties() const; + +protected: + + static const unsigned char sm_alphabet[]; + static const unsigned char sm_decodeMap[256]; +}; + + +} // encoder +} // utility +} // vmime + + +#endif // VMIME_UTILITY_ENCODER_B64ENCODER_HPP_INCLUDED diff --git a/vmime/utility/encoder/binaryEncoder.hpp b/vmime/utility/encoder/binaryEncoder.hpp new file mode 100644 index 00000000..783f8312 --- /dev/null +++ b/vmime/utility/encoder/binaryEncoder.hpp @@ -0,0 +1,52 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#ifndef VMIME_UTILITY_ENCODER_BINARYENCODER_HPP_INCLUDED +#define VMIME_UTILITY_ENCODER_BINARYENCODER_HPP_INCLUDED + + +#include "vmime/utility/encoder/defaultEncoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +/** Binary encoder. + */ + +class binaryEncoder : public defaultEncoder +{ +public: + + binaryEncoder(); +}; + + +} // encoder +} // utility +} // vmime + + +#endif // VMIME_UTILITY_ENCODER_BINARYENCODER_HPP_INCLUDED diff --git a/vmime/utility/encoder/defaultEncoder.hpp b/vmime/utility/encoder/defaultEncoder.hpp new file mode 100644 index 00000000..3ddad1fb --- /dev/null +++ b/vmime/utility/encoder/defaultEncoder.hpp @@ -0,0 +1,55 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#ifndef VMIME_UTILITY_ENCODER_DEFAULTENCODER_HPP_INCLUDED +#define VMIME_UTILITY_ENCODER_DEFAULTENCODER_HPP_INCLUDED + + +#include "vmime/utility/encoder/encoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +/** Default encoder (simple copy, no encoding/decoding is performed). + */ + +class defaultEncoder : public encoder +{ +public: + + defaultEncoder(); + + utility::stream::size_type encode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); + utility::stream::size_type decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); +}; + + +} // encoder +} // utility +} // vmime + + +#endif // VMIME_UTILITY_ENCODER_DEFAULTENCODER_HPP_INCLUDED diff --git a/vmime/utility/encoder/eightBitEncoder.hpp b/vmime/utility/encoder/eightBitEncoder.hpp new file mode 100644 index 00000000..2e9b1381 --- /dev/null +++ b/vmime/utility/encoder/eightBitEncoder.hpp @@ -0,0 +1,52 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#ifndef VMIME_UTILITY_ENCODER_EIGHTBITENCODER_HPP_INCLUDED +#define VMIME_UTILITY_ENCODER_EIGHTBITENCODER_HPP_INCLUDED + + +#include "vmime/utility/encoder/defaultEncoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +/** 8-bit encoder. + */ + +class eightBitEncoder : public defaultEncoder +{ +public: + + eightBitEncoder(); +}; + + +} // encoder +} // utility +} // vmime + + +#endif // VMIME_UTILITY_ENCODER_EIGHTBITENCODER_HPP_INCLUDED diff --git a/vmime/utility/encoder/encoder.hpp b/vmime/utility/encoder/encoder.hpp new file mode 100644 index 00000000..eec00035 --- /dev/null +++ b/vmime/utility/encoder/encoder.hpp @@ -0,0 +1,110 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#ifndef VMIME_UTILITY_ENCODER_ENCODER_HPP_INCLUDED +#define VMIME_UTILITY_ENCODER_ENCODER_HPP_INCLUDED + + +#include "vmime/base.hpp" +#include "vmime/propertySet.hpp" +#include "vmime/exception.hpp" +#include "vmime/utility/progressListener.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +/** Encode/decode data in different encodings. + */ + +class encoder : public object +{ +public: + + encoder(); + virtual ~encoder(); + + /** Encode data. + * + * @param in input data (decoded) + * @param out output stream for encoded data + * @param progress progress listener, or NULL if you do not + * want to receive progress notifications + * @return number of bytes written into output stream + */ + virtual utility::stream::size_type encode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL) = 0; + + /** Decode data. + * + * @param in input data (encoded) + * @param out output stream for decoded data + * @param progress progress listener, or NULL if you do not + * want to receive progress notifications + * @return number of bytes written into output stream + */ + virtual utility::stream::size_type decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL) = 0; + + /** Return the properties of the encoder. + * + * @return properties of the encoder + */ + const propertySet& getProperties() const; + + /** Return the properties of the encoder. + * + * @return properties of the encoder + */ + propertySet& getProperties(); + + /** Return a list of property names that can be set for + * this encoder. + * + * @return list of property names + */ + virtual const std::vector getAvailableProperties() const; + + /** Return the results returned by this encoder. + * + * @return results returned by the encoder + */ + const propertySet& getResults() const; + +protected: + + propertySet& getResults(); + +private: + + propertySet m_props; + propertySet m_results; +}; + + +} // encoder +} // utility +} // vmime + + +#endif // VMIME_UTILITY_ENCODER_ENCODER_HPP_INCLUDED diff --git a/vmime/utility/encoder/encoderFactory.hpp b/vmime/utility/encoder/encoderFactory.hpp new file mode 100644 index 00000000..22f28f4b --- /dev/null +++ b/vmime/utility/encoder/encoderFactory.hpp @@ -0,0 +1,152 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#ifndef VMIME_UTILITY_ENCODER_ENCODERFACTORY_HPP_INCLUDED +#define VMIME_UTILITY_ENCODER_ENCODERFACTORY_HPP_INCLUDED + + +#include "vmime/utility/encoder/encoder.hpp" +#include "vmime/utility/stringUtils.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +/** A factory to create 'encoder' objects for the specified encoding. + */ + +class encoderFactory +{ +private: + + encoderFactory(); + ~encoderFactory(); + +public: + + static encoderFactory* getInstance(); + + /** Information about a registered encoder. */ + class registeredEncoder : public object + { + protected: + + virtual ~registeredEncoder() { } + + public: + + virtual ref create() const = 0; + + virtual const string& getName() const = 0; + }; + +private: + + template + class registeredEncoderImpl : public registeredEncoder + { + friend class vmime::creator; + + protected: + + registeredEncoderImpl(const string& name) : m_name(name) { } + + public: + + ref create() const + { + return vmime::create (); + } + + const string& getName() const + { + return (m_name); + } + + private: + + const string m_name; + }; + + + std::vector > m_encoders; + +public: + + /** Register a new encoder by its encoding name. + * + * @param name encoding name + */ + template + void registerName(const string& name) + { + m_encoders.push_back(vmime::create >(utility::stringUtils::toLower(name))); + } + + /** Create a new encoder instance from an encoding name. + * + * @param name encoding name (eg. "base64") + * @return a new encoder instance for the specified encoding + * @throw exceptions::no_encoder_available if no encoder is registered + * for this encoding + */ + ref create(const string& name); + + /** Return information about a registered encoder. + * + * @param name encoding name + * @return information about this encoder + * @throw exceptions::no_encoder_available if no encoder is registered + * for this encoding + */ + const ref getEncoderByName(const string& name) const; + + /** Return the number of registered encoders. + * + * @return number of registered encoders + */ + int getEncoderCount() const; + + /** Return the registered encoder at the specified position. + * + * @param pos position of the registered encoder to return + * @return registered encoder at the specified position + */ + const ref getEncoderAt(const int pos) const; + + /** Return a list of all registered encoders. + * + * @return list of registered encoders + */ + const std::vector > getEncoderList() const; +}; + + +} // encoder +} // utility +} // vmime + + +#endif // VMIME_UTILITY_ENCODER_ENCODERFACTORY_HPP_INCLUDED diff --git a/vmime/utility/encoder/qpEncoder.hpp b/vmime/utility/encoder/qpEncoder.hpp new file mode 100644 index 00000000..6af3c901 --- /dev/null +++ b/vmime/utility/encoder/qpEncoder.hpp @@ -0,0 +1,62 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#ifndef VMIME_UTILITY_ENCODER_QPENCODER_HPP_INCLUDED +#define VMIME_UTILITY_ENCODER_QPENCODER_HPP_INCLUDED + + +#include "vmime/utility/encoder/encoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +/** Quoted-printable encoder. + */ + +class qpEncoder : public encoder +{ +public: + + qpEncoder(); + + utility::stream::size_type encode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); + utility::stream::size_type decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); + + const std::vector getAvailableProperties() const; + +protected: + + static const unsigned char sm_hexDigits[17]; + static const unsigned char sm_hexDecodeTable[256]; +}; + + +} // encoder +} // utility +} // vmime + + +#endif // VMIME_UTILITY_ENCODER_QPENCODER_HPP_INCLUDED diff --git a/vmime/utility/encoder/sevenBitEncoder.hpp b/vmime/utility/encoder/sevenBitEncoder.hpp new file mode 100644 index 00000000..98a999aa --- /dev/null +++ b/vmime/utility/encoder/sevenBitEncoder.hpp @@ -0,0 +1,52 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#ifndef VMIME_UTILITY_ENCODER_SEVENBITENCODER_HPP_INCLUDED +#define VMIME_UTILITY_ENCODER_SEVENBITENCODER_HPP_INCLUDED + + +#include "vmime/utility/encoder/defaultEncoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +/** 7-bit encoder. + */ + +class sevenBitEncoder : public defaultEncoder +{ +public: + + sevenBitEncoder(); +}; + + +} // encoder +} // utility +} // vmime + + +#endif // VMIME_UTILITY_ENCODER_SEVENBITENCODER_HPP_INCLUDED diff --git a/vmime/utility/encoder/uuEncoder.hpp b/vmime/utility/encoder/uuEncoder.hpp new file mode 100644 index 00000000..6cb5fa1e --- /dev/null +++ b/vmime/utility/encoder/uuEncoder.hpp @@ -0,0 +1,57 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2008 Vincent Richard +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#ifndef VMIME_UTILITY_ENCODER_UUENCODER_HPP_INCLUDED +#define VMIME_UTILITY_ENCODER_UUENCODER_HPP_INCLUDED + + +#include "vmime/utility/encoder/encoder.hpp" + + +namespace vmime { +namespace utility { +namespace encoder { + + +/** UUEncode encoder. + */ + +class uuEncoder : public encoder +{ +public: + + uuEncoder(); + + utility::stream::size_type encode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); + utility::stream::size_type decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress = NULL); + + const std::vector getAvailableProperties() const; +}; + + +} // encoder +} // utility +} // vmime + + +#endif // VMIME_UTILITY_ENCODER_UUENCODER_HPP_INCLUDED diff --git a/vmime/vmime.hpp b/vmime/vmime.hpp index f62e53db..0e54d7b7 100644 --- a/vmime/vmime.hpp +++ b/vmime/vmime.hpp @@ -66,7 +66,7 @@ #include "vmime/parameterizedHeaderField.hpp" // Encoders -#include "vmime/encoderFactory.hpp" +#include "vmime/utility/encoder/encoderFactory.hpp" // Message builder/parser #include "vmime/messageBuilder.hpp" diff --git a/vmime/wordEncoder.hpp b/vmime/wordEncoder.hpp index 98294fa6..09c141d0 100644 --- a/vmime/wordEncoder.hpp +++ b/vmime/wordEncoder.hpp @@ -32,8 +32,14 @@ namespace vmime { +namespace utility { +namespace encoder { + class encoder; +} // encoder +} // utility + /** Encodes words following RFC-2047. */ @@ -83,7 +89,7 @@ private: charset m_charset; Encoding m_encoding; - ref m_encoder; + ref m_encoder; }; -- cgit v1.2.3