summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Tardon <dtardon@redhat.com>2013-06-30 15:30:44 +0200
committerDavid Tardon <dtardon@redhat.com>2013-06-30 16:38:29 +0200
commitc5178caaac468b7f40ac717fcb7bfe8e883ed9a6 (patch)
treede506d8f346054e8119a4646e6d98deea73fb418 /src
parent1957ef21b7acbd505252870415b17b039a56d357 (diff)
add generally usable memory stream impl.
Diffstat (limited to 'src')
-rw-r--r--src/lib/Makefile.am2
-rw-r--r--src/lib/WT602MemoryStream.cpp111
-rw-r--r--src/lib/WT602MemoryStream.h53
-rw-r--r--src/lib/WT602OLEStream.cpp86
4 files changed, 168 insertions, 84 deletions
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 91fe249..62343af 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -19,6 +19,8 @@ libtext602_@TEXT602_MAJOR_VERSION@_@TEXT602_MINOR_VERSION@_la_SOURCES = \
WT602Document.cpp \
WT602Header.cpp \
WT602Header.h \
+ WT602MemoryStream.cpp \
+ WT602MemoryStream.h \
WT602OLEStream.cpp \
WT602OLEStream.h \
WT602Parser.cpp \
diff --git a/src/lib/WT602MemoryStream.cpp b/src/lib/WT602MemoryStream.cpp
new file mode 100644
index 0000000..630fa29
--- /dev/null
+++ b/src/lib/WT602MemoryStream.cpp
@@ -0,0 +1,111 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* libtext602
+ * Version: MPL 2.0 / LGPLv2.1+
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Alternatively, the contents of this file may be used under the terms
+ * of the GNU Lesser General Public License Version 2.1 or later
+ * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
+ * applicable instead of those above.
+ *
+ * For further information visit http://libtext602.sourceforge.net
+ */
+
+#include <algorithm>
+
+#include "WT602MemoryStream.h"
+
+namespace libtext602
+{
+
+WT602MemoryStream::WT602MemoryStream(const unsigned char *data, unsigned length)
+ : m_data(0)
+ , m_length(length)
+ , m_pos(0)
+{
+ unsigned char *buffer = new unsigned char[length];
+ std::copy(data, data + length, buffer);
+ m_data = buffer;
+}
+
+WT602MemoryStream::~WT602MemoryStream()
+{
+ delete[] m_data;
+}
+
+bool WT602MemoryStream::isOLEStream()
+{
+ return false;
+}
+
+WPXInputStream *WT602MemoryStream::getDocumentOLEStream(const char *)
+{
+ return 0;
+}
+
+const unsigned char *WT602MemoryStream::read(unsigned long numBytes, unsigned long &numBytesRead) try
+{
+ numBytesRead = 0;
+
+ if (0 == numBytes)
+ return 0;
+
+ if ((m_pos + numBytes) >= static_cast<unsigned long>(m_length))
+ numBytes = static_cast<unsigned long>(m_length - m_pos);
+
+ const long oldPos = m_pos;
+ m_pos += numBytes;
+
+ numBytesRead = numBytes;
+ return m_data + oldPos;
+}
+catch (...)
+{
+ return 0;
+}
+
+int WT602MemoryStream::seek(const long offset, WPX_SEEK_TYPE seekType) try
+{
+ long pos = 0;
+ switch (seekType)
+ {
+ case WPX_SEEK_SET :
+ pos = offset;
+ break;
+ case WPX_SEEK_CUR :
+ pos = offset + m_pos;
+ break;
+ case WPX_SEEK_END :
+ pos = offset + m_length;
+ break;
+ default :
+ return -1;
+ }
+
+ if ((pos < 0) || (pos > m_length))
+ return 1;
+
+ m_pos = pos;
+ return 0;
+}
+catch (...)
+{
+ return -1;
+}
+
+long WT602MemoryStream::tell()
+{
+ return m_pos;
+}
+
+bool WT602MemoryStream::atEOS()
+{
+ return m_length == m_pos;
+}
+
+}
+
+/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
diff --git a/src/lib/WT602MemoryStream.h b/src/lib/WT602MemoryStream.h
new file mode 100644
index 0000000..b1604fd
--- /dev/null
+++ b/src/lib/WT602MemoryStream.h
@@ -0,0 +1,53 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* libtext602
+ * Version: MPL 2.0 / LGPLv2.1+
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Alternatively, the contents of this file may be used under the terms
+ * of the GNU Lesser General Public License Version 2.1 or later
+ * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
+ * applicable instead of those above.
+ *
+ * For further information visit http://libtext602.sourceforge.net
+ */
+
+#ifndef WT602MEMORYSTREAM_H_INCLUDED
+#define WT602MEMORYSTREAM_H_INCLUDED
+
+#include <libwpd-stream/libwpd-stream.h>
+
+namespace libtext602
+{
+
+class WT602MemoryStream : public WPXInputStream
+{
+// disable copying
+ WT602MemoryStream(const WT602MemoryStream &other);
+ WT602MemoryStream &operator=(const WT602MemoryStream &other);
+
+public:
+ WT602MemoryStream(const unsigned char *data, unsigned length);
+ virtual ~WT602MemoryStream();
+
+ virtual bool isOLEStream();
+ virtual WPXInputStream *getDocumentOLEStream(const char *name);
+
+ virtual const unsigned char *read(unsigned long numBytes, unsigned long &numBytesRead);
+ virtual int seek(long offset, WPX_SEEK_TYPE seekType);
+ virtual long tell();
+ virtual bool atEOS();
+
+private:
+ const unsigned char *m_data;
+ const long m_length;
+ long m_pos;
+};
+
+}
+
+#endif // WT602MEMORYSTREAM_H_INCLUDED
+
+/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
diff --git a/src/lib/WT602OLEStream.cpp b/src/lib/WT602OLEStream.cpp
index bd00848..bf8b47b 100644
--- a/src/lib/WT602OLEStream.cpp
+++ b/src/lib/WT602OLEStream.cpp
@@ -49,6 +49,7 @@
#include <libwpd-stream/libwpd-stream.h>
+#include "WT602MemoryStream.h"
#include "WT602OLEStream.h"
namespace libtext602
@@ -57,89 +58,6 @@ namespace libtext602
namespace
{
-/** an internal class used to return the OLE InputStream */
-class WT602StringStream: public WPXInputStream
-{
-public:
- WT602StringStream(const unsigned char *data, const unsigned int dataSize) :
- buffer(dataSize), offset(0)
- {
- memcpy(&buffer[0], data, dataSize);
- }
- ~WT602StringStream() { }
-
- const unsigned char *read(unsigned long numBytes, unsigned long &numBytesRead);
- long tell()
- {
- return offset;
- }
- int seek(long offset, WPX_SEEK_TYPE seekType);
- bool atEOS()
- {
- return ((long)offset >= (long)buffer.size());
- }
-
- bool isOLEStream()
- {
- return false;
- }
- WPXInputStream *getDocumentOLEStream(const char *)
- {
- return 0;
- };
-
-private:
- std::vector<unsigned char> buffer;
- volatile long offset;
- WT602StringStream(const WT602StringStream &);
- WT602StringStream &operator=(const WT602StringStream &);
-};
-
-int WT602StringStream::seek(long _offset, WPX_SEEK_TYPE seekType)
-{
- if (seekType == WPX_SEEK_CUR)
- offset += _offset;
- else if (seekType == WPX_SEEK_SET)
- offset = _offset;
-
- if (offset < 0)
- {
- offset = 0;
- return 1;
- }
- if ((long)offset > (long)buffer.size())
- {
- offset = long(buffer.size());
- return 1;
- }
- return 0;
-}
-
-const unsigned char *WT602StringStream::read(unsigned long numBytes, unsigned long &numBytesRead)
-{
- numBytesRead = 0;
-
- if (numBytes == 0)
- return 0;
-
- unsigned long numBytesToRead;
-
- if (((unsigned long) offset+numBytes) < buffer.size())
- numBytesToRead = numBytes;
- else
- numBytesToRead = (unsigned long)buffer.size() -(unsigned long)offset;
-
- numBytesRead = numBytesToRead; // about as paranoid as we can be..
-
- if (numBytesToRead == 0)
- return 0;
-
- long oldOffset = offset;
- offset += numBytesToRead;
-
- return &buffer[size_t(oldOffset)];
-}
-
static inline unsigned long readU16( const unsigned char *ptr )
{
return (unsigned long)(ptr[0]+(ptr[1]<<8));
@@ -1072,7 +990,7 @@ shared_ptr<WPXInputStream> libtext602::Storage::getDocumentOLEStream(const std::
TEXT602_DEBUG_MSG(("libtext602::Storage::getDocumentOLEStream: tries to use damaged OLE: %s\n", name.c_str()));
}
- res.reset(new WT602StringStream(buf, (unsigned int) oleLength));
+ res.reset(new WT602MemoryStream(buf, (unsigned int) oleLength));
delete [] buf;
return res;
}