2004-10-05 10:28:21 +00:00
|
|
|
//
|
2005-03-18 21:33:11 +00:00
|
|
|
// VMime library (http://www.vmime.org)
|
2009-09-06 12:02:10 +00:00
|
|
|
// Copyright (C) 2002-2009 Vincent Richard <vincent@vincent-richard.net>
|
2004-10-05 10:28:21 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
2009-09-06 12:02:10 +00:00
|
|
|
// published by the Free Software Foundation; either version 3 of
|
2004-10-05 10:28:21 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2005-09-17 10:10:29 +00:00
|
|
|
// 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.
|
2004-10-05 10:28:21 +00:00
|
|
|
//
|
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/charset.hpp"
|
|
|
|
#include "vmime/exception.hpp"
|
2006-10-11 14:52:41 +00:00
|
|
|
#include "vmime/platform.hpp"
|
2010-05-23 16:18:00 +00:00
|
|
|
#include "vmime/encoding.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/utility/stringUtils.hpp"
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-10-19 11:25:57 +00:00
|
|
|
#include "vmime/charsetConverter.hpp"
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace vmime
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
charset::charset()
|
|
|
|
: m_name(charsets::US_ASCII)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
charset::charset(const string& name)
|
|
|
|
: m_name(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-20 20:32:48 +00:00
|
|
|
charset::charset(const char* name)
|
|
|
|
: m_name(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
void charset::parse(const string& buffer, const string::size_type position,
|
|
|
|
const string::size_type end, string::size_type* newPosition)
|
|
|
|
{
|
2006-08-25 14:36:44 +00:00
|
|
|
m_name = utility::stringUtils::trim
|
|
|
|
(string(buffer.begin() + position, buffer.begin() + end));
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-12-15 20:28:09 +00:00
|
|
|
setParsedBounds(position, end);
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
if (newPosition)
|
|
|
|
*newPosition = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void charset::generate(utility::outputStream& os, const string::size_type /* maxLineLength */,
|
|
|
|
const string::size_type curLinePos, string::size_type* newLinePos) const
|
|
|
|
{
|
|
|
|
os << m_name;
|
|
|
|
|
|
|
|
if (newLinePos)
|
|
|
|
*newLinePos = curLinePos + m_name.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void charset::convert(utility::inputStream& in, utility::outputStream& out,
|
|
|
|
const charset& source, const charset& dest)
|
|
|
|
{
|
2005-10-19 11:25:57 +00:00
|
|
|
charsetConverter conv(source, dest);
|
|
|
|
conv.convert(in, out);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void charset::convert(const string& in, string& out, const charset& source, const charset& dest)
|
|
|
|
{
|
2005-10-19 11:25:57 +00:00
|
|
|
charsetConverter conv(source, dest);
|
|
|
|
conv.convert(in, out);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const charset charset::getLocaleCharset()
|
|
|
|
{
|
2006-10-11 14:52:41 +00:00
|
|
|
return (platform::getHandler()->getLocaleCharset());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
charset& charset::operator=(const charset& other)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
copyFrom(other);
|
2004-10-05 10:28:21 +00:00
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool charset::operator==(const charset& value) const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-01-02 17:17:12 +00:00
|
|
|
return (utility::stringUtils::isStringEqualNoCase(m_name, value.m_name));
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool charset::operator!=(const charset& value) const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
return !(*this == value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <component> charset::clone() const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
return vmime::create <charset>(m_name);
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const string& charset::getName() const
|
|
|
|
{
|
|
|
|
return (m_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void charset::copyFrom(const component& other)
|
|
|
|
{
|
|
|
|
m_name = dynamic_cast <const charset&>(other).m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <const component> > charset::getChildComponents() const
|
2004-12-20 12:33:55 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
return std::vector <ref <const component> >();
|
2004-12-20 12:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-23 16:18:00 +00:00
|
|
|
|
|
|
|
// Explicitly force encoding for some charsets
|
|
|
|
struct CharsetEncodingEntry
|
|
|
|
{
|
|
|
|
CharsetEncodingEntry(const string& charset_, const string& encoding_)
|
|
|
|
: charset(charset_), encoding(encoding_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const string charset;
|
|
|
|
const string encoding;
|
|
|
|
};
|
|
|
|
|
|
|
|
CharsetEncodingEntry g_charsetEncodingMap[] =
|
|
|
|
{
|
|
|
|
// Use QP encoding for ISO-8859-x charsets
|
|
|
|
CharsetEncodingEntry("iso-8859", encodingTypes::QUOTED_PRINTABLE),
|
|
|
|
CharsetEncodingEntry("iso8859", encodingTypes::QUOTED_PRINTABLE),
|
|
|
|
|
|
|
|
// RFC-1468 states:
|
|
|
|
// " ISO-2022-JP may also be used in MIME Part 2 headers. The "B"
|
|
|
|
// encoding should be used with ISO-2022-JP text. "
|
|
|
|
// Use Base64 encoding for all ISO-2022 charsets.
|
|
|
|
CharsetEncodingEntry("iso-2022", encodingTypes::BASE64),
|
|
|
|
CharsetEncodingEntry("iso2022", encodingTypes::BASE64),
|
|
|
|
|
|
|
|
// Last entry is not used
|
|
|
|
CharsetEncodingEntry("", "")
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool charset::getRecommendedEncoding(encoding& enc) const
|
|
|
|
{
|
|
|
|
// Special treatment for some charsets
|
|
|
|
const string cset = utility::stringUtils::toLower(getName());
|
|
|
|
|
|
|
|
for (unsigned int i = 0 ; i < (sizeof(g_charsetEncodingMap) / sizeof(g_charsetEncodingMap[0])) - 1 ; ++i)
|
|
|
|
{
|
|
|
|
if (cset.find(g_charsetEncodingMap[i].charset) != string::npos)
|
|
|
|
{
|
|
|
|
enc = g_charsetEncodingMap[i].encoding;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
} // vmime
|