summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Compress')
-rw-r--r--Swiften/Compress/UnitTest/ZLibCompressorTest.cpp38
-rw-r--r--Swiften/Compress/UnitTest/ZLibDecompressorTest.cpp118
-rw-r--r--Swiften/Compress/ZLibCodecompressor.cpp61
-rw-r--r--Swiften/Compress/ZLibCodecompressor.h24
-rw-r--r--Swiften/Compress/ZLibCodecompressor_Private.h12
-rw-r--r--Swiften/Compress/ZLibCompressor.cpp10
-rw-r--r--Swiften/Compress/ZLibCompressor.h16
-rw-r--r--Swiften/Compress/ZLibDecompressor.cpp10
-rw-r--r--Swiften/Compress/ZLibDecompressor.h12
-rw-r--r--Swiften/Compress/ZLibException.h11
10 files changed, 158 insertions, 154 deletions
diff --git a/Swiften/Compress/UnitTest/ZLibCompressorTest.cpp b/Swiften/Compress/UnitTest/ZLibCompressorTest.cpp
index 69ceb56..ca99201 100644
--- a/Swiften/Compress/UnitTest/ZLibCompressorTest.cpp
+++ b/Swiften/Compress/UnitTest/ZLibCompressorTest.cpp
@@ -1,15 +1,15 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
-#include <Swiften/Base/SafeByteArray.h>
#include <QA/Checker/IO.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <Swiften/Base/SafeByteArray.h>
#include <Swiften/Compress/ZLibCompressor.h>
using namespace Swift;
@@ -17,28 +17,28 @@ using namespace Swift;
class ZLibCompressorTest : public CppUnit::TestFixture
{
- CPPUNIT_TEST_SUITE(ZLibCompressorTest);
- CPPUNIT_TEST(testProcess);
- CPPUNIT_TEST(testProcess_Twice);
- CPPUNIT_TEST_SUITE_END();
+ CPPUNIT_TEST_SUITE(ZLibCompressorTest);
+ CPPUNIT_TEST(testProcess);
+ CPPUNIT_TEST(testProcess_Twice);
+ CPPUNIT_TEST_SUITE_END();
- public:
- ZLibCompressorTest() {}
+ public:
+ ZLibCompressorTest() {}
- void testProcess() {
- ZLibCompressor testling;
- SafeByteArray result = testling.process(createSafeByteArray("foo"));
+ void testProcess() {
+ ZLibCompressor testling;
+ SafeByteArray result = testling.process(createSafeByteArray("foo"));
- CPPUNIT_ASSERT_EQUAL(createSafeByteArray("\x78\xda\x4a\xcb\xcf\x07\x00\x00\x00\xff\xff", 11), result);
- }
+ CPPUNIT_ASSERT_EQUAL(createSafeByteArray("\x78\xda\x4a\xcb\xcf\x07\x00\x00\x00\xff\xff", 11), result);
+ }
- void testProcess_Twice() {
- ZLibCompressor testling;
- testling.process(createSafeByteArray("foo"));
- SafeByteArray result = testling.process(createSafeByteArray("bar"));
+ void testProcess_Twice() {
+ ZLibCompressor testling;
+ testling.process(createSafeByteArray("foo"));
+ SafeByteArray result = testling.process(createSafeByteArray("bar"));
- CPPUNIT_ASSERT_EQUAL(createSafeByteArray("\x4a\x4a\x2c\x02\x00\x00\x00\xff\xff",9), result);
- }
+ CPPUNIT_ASSERT_EQUAL(createSafeByteArray("\x4a\x4a\x2c\x02\x00\x00\x00\xff\xff",9), result);
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(ZLibCompressorTest);
diff --git a/Swiften/Compress/UnitTest/ZLibDecompressorTest.cpp b/Swiften/Compress/UnitTest/ZLibDecompressorTest.cpp
index d11ac4a..5873c0e 100644
--- a/Swiften/Compress/UnitTest/ZLibDecompressorTest.cpp
+++ b/Swiften/Compress/UnitTest/ZLibDecompressorTest.cpp
@@ -1,17 +1,17 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
-#include <Swiften/Base/ByteArray.h>
#include <QA/Checker/IO.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
-#include <Swiften/Compress/ZLibDecompressor.h>
+#include <Swiften/Base/ByteArray.h>
#include <Swiften/Compress/ZLibCompressor.h>
+#include <Swiften/Compress/ZLibDecompressor.h>
#include <Swiften/Compress/ZLibException.h>
using namespace Swift;
@@ -19,62 +19,62 @@ using namespace Swift;
class ZLibDecompressorTest : public CppUnit::TestFixture
{
- CPPUNIT_TEST_SUITE(ZLibDecompressorTest);
- CPPUNIT_TEST(testProcess);
- CPPUNIT_TEST(testProcess_Twice);
- CPPUNIT_TEST(testProcess_Invalid);
- CPPUNIT_TEST(testProcess_Huge);
- CPPUNIT_TEST(testProcess_ChunkSize);
- CPPUNIT_TEST_SUITE_END();
-
- public:
- ZLibDecompressorTest() {}
-
- void testProcess() {
- ZLibDecompressor testling;
- SafeByteArray result = testling.process(createSafeByteArray("\x78\xda\x4a\xcb\xcf\x07\x00\x00\x00\xff\xff", 11));
-
- CPPUNIT_ASSERT_EQUAL(createSafeByteArray("foo"), result);
- }
-
- void testProcess_Twice() {
- ZLibDecompressor testling;
- testling.process(createSafeByteArray("\x78\xda\x4a\xcb\xcf\x07\x00\x00\x00\xff\xff", 11));
- SafeByteArray result = testling.process(createSafeByteArray("\x4a\x4a\x2c\x02\x00\x00\x00\xff\xff", 9));
-
- CPPUNIT_ASSERT_EQUAL(createSafeByteArray("bar"), result);
- }
-
- void testProcess_Invalid() {
- ZLibDecompressor testling;
- CPPUNIT_ASSERT_THROW(testling.process(createSafeByteArray("invalid")), ZLibException);
- }
-
- void testProcess_Huge() {
- std::vector<char> data;
- data.reserve(2048);
- for (unsigned int i = 0; i < 2048; ++i) {
- data.push_back(static_cast<char>(i));
- }
- SafeByteArray original(createSafeByteArray(&data[0], data.size()));
- SafeByteArray compressed = ZLibCompressor().process(original);
- SafeByteArray decompressed = ZLibDecompressor().process(compressed);
-
- CPPUNIT_ASSERT_EQUAL(original, decompressed);
- }
-
- void testProcess_ChunkSize() {
- std::vector<char> data;
- data.reserve(1024);
- for (unsigned int i = 0; i < 1024; ++i) {
- data.push_back(static_cast<char>(i));
- }
- SafeByteArray original(createSafeByteArray(&data[0], data.size()));
- SafeByteArray compressed = ZLibCompressor().process(original);
- SafeByteArray decompressed = ZLibDecompressor().process(compressed);
-
- CPPUNIT_ASSERT_EQUAL(original, decompressed);
- }
+ CPPUNIT_TEST_SUITE(ZLibDecompressorTest);
+ CPPUNIT_TEST(testProcess);
+ CPPUNIT_TEST(testProcess_Twice);
+ CPPUNIT_TEST(testProcess_Invalid);
+ CPPUNIT_TEST(testProcess_Huge);
+ CPPUNIT_TEST(testProcess_ChunkSize);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ ZLibDecompressorTest() {}
+
+ void testProcess() {
+ ZLibDecompressor testling;
+ SafeByteArray result = testling.process(createSafeByteArray("\x78\xda\x4a\xcb\xcf\x07\x00\x00\x00\xff\xff", 11));
+
+ CPPUNIT_ASSERT_EQUAL(createSafeByteArray("foo"), result);
+ }
+
+ void testProcess_Twice() {
+ ZLibDecompressor testling;
+ testling.process(createSafeByteArray("\x78\xda\x4a\xcb\xcf\x07\x00\x00\x00\xff\xff", 11));
+ SafeByteArray result = testling.process(createSafeByteArray("\x4a\x4a\x2c\x02\x00\x00\x00\xff\xff", 9));
+
+ CPPUNIT_ASSERT_EQUAL(createSafeByteArray("bar"), result);
+ }
+
+ void testProcess_Invalid() {
+ ZLibDecompressor testling;
+ CPPUNIT_ASSERT_THROW(testling.process(createSafeByteArray("invalid")), ZLibException);
+ }
+
+ void testProcess_Huge() {
+ std::vector<char> data;
+ data.reserve(2048);
+ for (unsigned int i = 0; i < 2048; ++i) {
+ data.push_back(static_cast<char>(i));
+ }
+ SafeByteArray original(createSafeByteArray(&data[0], data.size()));
+ SafeByteArray compressed = ZLibCompressor().process(original);
+ SafeByteArray decompressed = ZLibDecompressor().process(compressed);
+
+ CPPUNIT_ASSERT_EQUAL(original, decompressed);
+ }
+
+ void testProcess_ChunkSize() {
+ std::vector<char> data;
+ data.reserve(1024);
+ for (unsigned int i = 0; i < 1024; ++i) {
+ data.push_back(static_cast<char>(i));
+ }
+ SafeByteArray original(createSafeByteArray(&data[0], data.size()));
+ SafeByteArray compressed = ZLibCompressor().process(original);
+ SafeByteArray decompressed = ZLibDecompressor().process(compressed);
+
+ CPPUNIT_ASSERT_EQUAL(original, decompressed);
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(ZLibDecompressorTest);
diff --git a/Swiften/Compress/ZLibCodecompressor.cpp b/Swiften/Compress/ZLibCodecompressor.cpp
index fdfce78..344e6b7 100644
--- a/Swiften/Compress/ZLibCodecompressor.cpp
+++ b/Swiften/Compress/ZLibCodecompressor.cpp
@@ -1,55 +1,58 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiften/Compress/ZLibCodecompressor.h>
-#include <cassert>
#include <string.h>
-#include <zlib.h>
+
+#include <cassert>
+
#include <boost/numeric/conversion/cast.hpp>
-#include <Swiften/Compress/ZLibException.h>
+#include <zlib.h>
+
#include <Swiften/Compress/ZLibCodecompressor_Private.h>
+#include <Swiften/Compress/ZLibException.h>
namespace Swift {
static const size_t CHUNK_SIZE = 1024; // If you change this, also change the unittest
-ZLibCodecompressor::ZLibCodecompressor() : p(boost::make_shared<Private>()) {
- memset(&p->stream, 0, sizeof(z_stream));
- p->stream.zalloc = Z_NULL;
- p->stream.zfree = Z_NULL;
- p->stream.opaque = Z_NULL;
+ZLibCodecompressor::ZLibCodecompressor() : p(new Private()) {
+ memset(&p->stream, 0, sizeof(z_stream));
+ p->stream.zalloc = Z_NULL;
+ p->stream.zfree = Z_NULL;
+ p->stream.opaque = Z_NULL;
}
ZLibCodecompressor::~ZLibCodecompressor() {
}
SafeByteArray ZLibCodecompressor::process(const SafeByteArray& input) {
- SafeByteArray output;
- p->stream.avail_in = static_cast<unsigned int>(input.size());
- p->stream.next_in = reinterpret_cast<Bytef*>(const_cast<unsigned char*>(vecptr(input)));
- size_t outputPosition = 0;
- do {
- output.resize(outputPosition + CHUNK_SIZE);
- p->stream.avail_out = CHUNK_SIZE;
- p->stream.next_out = reinterpret_cast<Bytef*>(vecptr(output) + outputPosition);
- int result = processZStream();
- if (result != Z_OK && result != Z_BUF_ERROR) {
- throw ZLibException(/* p->stream.msg */);
- }
- outputPosition += CHUNK_SIZE;
- }
- while (p->stream.avail_out == 0);
- if (p->stream.avail_in != 0) {
- throw ZLibException();
- }
- output.resize(outputPosition - p->stream.avail_out);
- return output;
+ SafeByteArray output;
+ p->stream.avail_in = static_cast<unsigned int>(input.size());
+ p->stream.next_in = reinterpret_cast<Bytef*>(const_cast<unsigned char*>(vecptr(input)));
+ size_t outputPosition = 0;
+ do {
+ output.resize(outputPosition + CHUNK_SIZE);
+ p->stream.avail_out = CHUNK_SIZE;
+ p->stream.next_out = reinterpret_cast<Bytef*>(vecptr(output) + outputPosition);
+ int result = processZStream();
+ if (result != Z_OK && result != Z_BUF_ERROR) {
+ throw ZLibException(/* p->stream.msg */);
+ }
+ outputPosition += CHUNK_SIZE;
+ }
+ while (p->stream.avail_out == 0);
+ if (p->stream.avail_in != 0) {
+ throw ZLibException();
+ }
+ output.resize(outputPosition - p->stream.avail_out);
+ return output;
}
}
diff --git a/Swiften/Compress/ZLibCodecompressor.h b/Swiften/Compress/ZLibCodecompressor.h
index 3907258..8bc5d88 100644
--- a/Swiften/Compress/ZLibCodecompressor.h
+++ b/Swiften/Compress/ZLibCodecompressor.h
@@ -1,25 +1,27 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
+#include <memory>
+
#include <Swiften/Base/API.h>
#include <Swiften/Base/SafeByteArray.h>
namespace Swift {
- class SWIFTEN_API ZLibCodecompressor {
- public:
- ZLibCodecompressor();
- virtual ~ZLibCodecompressor();
+ class SWIFTEN_API ZLibCodecompressor {
+ public:
+ ZLibCodecompressor();
+ virtual ~ZLibCodecompressor();
- SafeByteArray process(const SafeByteArray& data);
- virtual int processZStream() = 0;
+ SafeByteArray process(const SafeByteArray& data);
+ virtual int processZStream() = 0;
- protected:
- struct Private;
- boost::shared_ptr<Private> p;
- };
+ protected:
+ struct Private;
+ const std::unique_ptr<Private> p;
+ };
}
diff --git a/Swiften/Compress/ZLibCodecompressor_Private.h b/Swiften/Compress/ZLibCodecompressor_Private.h
index 451bc50..2adc4b6 100644
--- a/Swiften/Compress/ZLibCodecompressor_Private.h
+++ b/Swiften/Compress/ZLibCodecompressor_Private.h
@@ -1,17 +1,17 @@
/*
- * Copyright (c) 2012 Isode Limited.
+ * Copyright (c) 2012-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
-#include <Swiften/Compress/ZLibCodecompressor.h>
-
#include <zlib.h>
+#include <Swiften/Compress/ZLibCodecompressor.h>
+
namespace Swift {
- struct ZLibCodecompressor::Private {
- z_stream stream;
- };
+ struct ZLibCodecompressor::Private {
+ z_stream stream;
+ };
}
diff --git a/Swiften/Compress/ZLibCompressor.cpp b/Swiften/Compress/ZLibCompressor.cpp
index 8887c46..a2fef15 100644
--- a/Swiften/Compress/ZLibCompressor.cpp
+++ b/Swiften/Compress/ZLibCompressor.cpp
@@ -16,17 +16,17 @@
namespace Swift {
ZLibCompressor::ZLibCompressor() {
- int result = deflateInit(&p->stream, COMPRESSION_LEVEL);
- assert(result == Z_OK);
- (void) result;
+ int result = deflateInit(&p->stream, COMPRESSION_LEVEL);
+ assert(result == Z_OK);
+ (void) result;
}
ZLibCompressor::~ZLibCompressor() {
- deflateEnd(&p->stream);
+ deflateEnd(&p->stream);
}
int ZLibCompressor::processZStream() {
- return deflate(&p->stream, Z_SYNC_FLUSH);
+ return deflate(&p->stream, Z_SYNC_FLUSH);
}
}
diff --git a/Swiften/Compress/ZLibCompressor.h b/Swiften/Compress/ZLibCompressor.h
index c92b8c0..59ebbb9 100644
--- a/Swiften/Compress/ZLibCompressor.h
+++ b/Swiften/Compress/ZLibCompressor.h
@@ -10,14 +10,14 @@
#include <Swiften/Compress/ZLibCodecompressor.h>
namespace Swift {
- class SWIFTEN_API ZLibCompressor : public ZLibCodecompressor {
- public:
- ZLibCompressor();
- virtual ~ZLibCompressor();
+ class SWIFTEN_API ZLibCompressor : public ZLibCodecompressor {
+ public:
+ ZLibCompressor();
+ virtual ~ZLibCompressor();
- virtual int processZStream();
+ virtual int processZStream();
- private:
- static const int COMPRESSION_LEVEL = 9;
- };
+ private:
+ static const int COMPRESSION_LEVEL = 9;
+ };
}
diff --git a/Swiften/Compress/ZLibDecompressor.cpp b/Swiften/Compress/ZLibDecompressor.cpp
index 3024150..adc40c5 100644
--- a/Swiften/Compress/ZLibDecompressor.cpp
+++ b/Swiften/Compress/ZLibDecompressor.cpp
@@ -16,17 +16,17 @@
namespace Swift {
ZLibDecompressor::ZLibDecompressor() {
- int result = inflateInit(&p->stream);
- assert(result == Z_OK);
- (void) result;
+ int result = inflateInit(&p->stream);
+ assert(result == Z_OK);
+ (void) result;
}
ZLibDecompressor::~ZLibDecompressor() {
- inflateEnd(&p->stream);
+ inflateEnd(&p->stream);
}
int ZLibDecompressor::processZStream() {
- return inflate(&p->stream, Z_SYNC_FLUSH);
+ return inflate(&p->stream, Z_SYNC_FLUSH);
}
}
diff --git a/Swiften/Compress/ZLibDecompressor.h b/Swiften/Compress/ZLibDecompressor.h
index b758be3..9c2dd4f 100644
--- a/Swiften/Compress/ZLibDecompressor.h
+++ b/Swiften/Compress/ZLibDecompressor.h
@@ -10,11 +10,11 @@
#include <Swiften/Compress/ZLibCodecompressor.h>
namespace Swift {
- class SWIFTEN_API ZLibDecompressor : public ZLibCodecompressor {
- public:
- ZLibDecompressor();
- virtual ~ZLibDecompressor();
+ class SWIFTEN_API ZLibDecompressor : public ZLibCodecompressor {
+ public:
+ ZLibDecompressor();
+ virtual ~ZLibDecompressor();
- virtual int processZStream();
- };
+ virtual int processZStream();
+ };
}
diff --git a/Swiften/Compress/ZLibException.h b/Swiften/Compress/ZLibException.h
index 5fbac6d..2201e62 100644
--- a/Swiften/Compress/ZLibException.h
+++ b/Swiften/Compress/ZLibException.h
@@ -1,15 +1,14 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
-
namespace Swift {
- class ZLibException {
- public:
- ZLibException() {}
- };
+ class ZLibException {
+ public:
+ ZLibException() {}
+ };
}