2005-04-28 10:57:41 +00:00
|
|
|
//
|
|
|
|
// VMime library (http://www.vmime.org)
|
|
|
|
// Copyright (C) 2002-2005 Vincent Richard <vincent@vincent-richard.net>
|
|
|
|
//
|
|
|
|
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "vmime/utility/filteredStream.hpp"
|
|
|
|
|
2005-04-28 20:44:28 +00:00
|
|
|
#include <algorithm>
|
2005-04-28 10:57:41 +00:00
|
|
|
|
2005-06-15 22:22:01 +00:00
|
|
|
|
2005-04-28 10:57:41 +00:00
|
|
|
namespace vmime {
|
|
|
|
namespace utility {
|
|
|
|
|
|
|
|
|
2005-06-15 22:22:01 +00:00
|
|
|
// dotFilteredInputStream
|
|
|
|
|
|
|
|
dotFilteredInputStream::dotFilteredInputStream(inputStream& is)
|
|
|
|
: m_stream(is)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inputStream& dotFilteredInputStream::getPreviousInputStream()
|
|
|
|
{
|
|
|
|
return (m_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const bool dotFilteredInputStream::eof() const
|
|
|
|
{
|
|
|
|
return (m_stream.eof());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void dotFilteredInputStream::reset()
|
|
|
|
{
|
|
|
|
m_previousChar2 = '\0';
|
|
|
|
m_previousChar1 = '\0';
|
|
|
|
|
|
|
|
m_stream.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const stream::size_type dotFilteredInputStream::read(value_type* const data, const size_type count)
|
|
|
|
{
|
|
|
|
const stream::size_type read = m_stream.read(data, count);
|
|
|
|
|
|
|
|
const value_type* readPtr = data;
|
|
|
|
value_type* writePtr = data;
|
|
|
|
|
|
|
|
const value_type* end = data + read;
|
|
|
|
|
|
|
|
stream::size_type written = 0;
|
|
|
|
|
|
|
|
// Replace "\n.." with "\n."
|
|
|
|
while (readPtr < end)
|
|
|
|
{
|
|
|
|
if (*readPtr == '.')
|
|
|
|
{
|
|
|
|
const value_type prevChar2 =
|
|
|
|
(readPtr == data + 1 ? m_previousChar1 :
|
|
|
|
readPtr == data ? m_previousChar2 : *(readPtr - 2));
|
|
|
|
const value_type prevChar1 =
|
|
|
|
(readPtr == data ? m_previousChar1 : *(readPtr - 1));
|
|
|
|
|
|
|
|
if (prevChar2 == '\n' && prevChar1 == '.')
|
|
|
|
{
|
|
|
|
// Ignore last dot
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*writePtr = *readPtr;
|
|
|
|
|
|
|
|
++writePtr;
|
|
|
|
++written;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*writePtr = *readPtr;
|
|
|
|
|
|
|
|
++writePtr;
|
|
|
|
++written;
|
|
|
|
}
|
|
|
|
|
|
|
|
++readPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_previousChar2 = (read >= 2 ? data[read - 2] : m_previousChar1);
|
|
|
|
m_previousChar1 = (read >= 1 ? data[read - 1] : '\0');
|
|
|
|
|
|
|
|
return (written);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const stream::size_type dotFilteredInputStream::skip(const size_type /* count */)
|
|
|
|
{
|
|
|
|
// Skipping bytes is not supported
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-28 10:57:41 +00:00
|
|
|
// dotFilteredOutputStream
|
|
|
|
|
|
|
|
dotFilteredOutputStream::dotFilteredOutputStream(outputStream& os)
|
|
|
|
: m_stream(os)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
outputStream& dotFilteredOutputStream::getNextOutputStream()
|
|
|
|
{
|
|
|
|
return (m_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void dotFilteredOutputStream::write
|
|
|
|
(const value_type* const data, const size_type count)
|
|
|
|
{
|
2005-06-15 22:22:01 +00:00
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
2005-04-28 10:57:41 +00:00
|
|
|
const value_type* pos = data;
|
|
|
|
const value_type* end = data + count;
|
|
|
|
const value_type* start = data;
|
|
|
|
|
|
|
|
// Replace "\n." with "\n.."
|
|
|
|
while ((pos = std::find(pos, end, '.')) != end)
|
|
|
|
{
|
|
|
|
const value_type previousChar =
|
|
|
|
(pos == data ? m_previousChar : *(pos - 1));
|
|
|
|
|
|
|
|
if (previousChar == '\n')
|
|
|
|
{
|
|
|
|
m_stream.write(start, pos - data);
|
|
|
|
m_stream.write("..", 2);
|
|
|
|
|
|
|
|
start = pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_stream.write(start, end - start);
|
|
|
|
m_previousChar = data[count - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CRLFToLFFilteredOutputStream
|
|
|
|
|
|
|
|
CRLFToLFFilteredOutputStream::CRLFToLFFilteredOutputStream(outputStream& os)
|
|
|
|
: m_stream(os)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
outputStream& CRLFToLFFilteredOutputStream::getNextOutputStream()
|
|
|
|
{
|
|
|
|
return (m_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CRLFToLFFilteredOutputStream::write
|
|
|
|
(const value_type* const data, const size_type count)
|
|
|
|
{
|
2005-06-15 22:22:01 +00:00
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
2005-04-28 10:57:41 +00:00
|
|
|
const value_type* pos = data;
|
|
|
|
const value_type* end = data + count;
|
|
|
|
const value_type* start = data;
|
|
|
|
|
2005-06-15 22:22:01 +00:00
|
|
|
// Warning: if the whole buffer finishes with '\r', this
|
|
|
|
// last character will not be written back...
|
|
|
|
// TODO: add a finalize() method?
|
|
|
|
if (m_previousChar == '\r')
|
|
|
|
{
|
|
|
|
if (*pos != '\n')
|
|
|
|
{
|
|
|
|
m_stream.write("\r", 1); // write back \r
|
|
|
|
m_previousChar = *pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-28 10:57:41 +00:00
|
|
|
// Replace "\r\n" (CRLF) with "\n" (LF)
|
|
|
|
while ((pos = std::find(pos, end, '\n')) != end)
|
|
|
|
{
|
|
|
|
const value_type previousChar =
|
|
|
|
(pos == data ? m_previousChar : *(pos - 1));
|
|
|
|
|
|
|
|
if (previousChar == '\r')
|
|
|
|
{
|
2005-06-15 22:22:01 +00:00
|
|
|
if (pos != data)
|
|
|
|
m_stream.write(start, pos - 1 - data); // do not write \r
|
|
|
|
|
2005-04-28 10:57:41 +00:00
|
|
|
m_stream.write("\n", 1);
|
|
|
|
|
|
|
|
start = pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
2005-06-15 22:22:01 +00:00
|
|
|
if (data[count - 1] == '\r')
|
|
|
|
{
|
|
|
|
m_stream.write(start, end - start - 1);
|
|
|
|
m_previousChar = '\r';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_stream.write(start, end - start);
|
|
|
|
m_previousChar = data[count - 1];
|
|
|
|
}
|
2005-04-28 10:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // utility
|
|
|
|
} // vmime
|
|
|
|
|