diff --git a/src/lib/synergy/ProtocolUtil.cpp b/src/lib/synergy/ProtocolUtil.cpp
index 65c09bce8..1d28ab0f6 100644
--- a/src/lib/synergy/ProtocolUtil.cpp
+++ b/src/lib/synergy/ProtocolUtil.cpp
@@ -16,6 +16,7 @@
* along with this program. If not, see .
*/
+#include
#include "synergy/ProtocolUtil.h"
#include "io/IStream.h"
#include "base/Log.h"
@@ -60,7 +61,7 @@ ProtocolUtil::readf(synergy::IStream* stream, const char* fmt, ...)
catch (XIO&) {
result = false;
}
- catch (std::bad_alloc & exception) {
+ catch (std::bad_alloc&) {
result = false;
}
va_end(args);
@@ -113,14 +114,52 @@ ProtocolUtil::vreadf(synergy::IStream* stream, const char* fmt, va_list args)
switch (*fmt) {
case 'i': {
void* destination = va_arg(args, void*);
- readInt(stream, len, destination);
+ switch (len) {
+ case 1:
+ // 1 byte integer
+ *static_cast(destination) = read1ByteInt(stream);
+ break;
+ case 2:
+ // 2 byte integer
+ *static_cast(destination) = read2BytesInt(stream);
+ break;
+ case 4:
+ // 4 byte integer
+ *static_cast(destination) = read4BytesInt(stream);
+ break;
+ default:
+ //the length is wrong
+ LOG((CLOG_ERR "read: length to be read is wrong: '%d' should be 1,2, or 4", len));
+ assert(false); //assert for debugging
+ break;
+ }
break;
}
+
case 'I': {
void* destination = va_arg(args, void*);
- readVectorInt(stream, len, destination);
+ switch (len) {
+ case 1:
+ // 1 byte integer
+ readVector1ByteInt(stream, *static_cast*>(destination));
+ break;
+ case 2:
+ // 2 byte integer
+ readVector2BytesInt(stream, *static_cast*>(destination));
+ break;
+ case 4:
+ // 4 byte integer
+ readVector4BytesInt(stream, *static_cast*>(destination));
+ break;
+ default:
+ //the length is wrong
+ LOG((CLOG_ERR "read: length to be read is wrong: '%d' should be 1,2, or 4", len));
+ assert(false); //assert for debugging
+ break;
+ }
break;
}
+
case 's': {
String* destination = va_arg(args, String*);
readBytes(stream, len, destination);
@@ -416,115 +455,67 @@ ProtocolUtil::read(synergy::IStream* stream, void* vbuffer, UInt32 count)
}
}
-void ProtocolUtil::readInt(synergy::IStream * stream, UInt32 len, void* destination) {
- // check for valid length
- if (len == 4 || len == 2 || len == 1) {
+UInt8 ProtocolUtil::read1ByteInt(synergy::IStream * stream)
+{
+ const UInt32 BufferSize = 1;
+ std::array buffer = {};
+ read(stream, buffer.data(), BufferSize);
- static const int buffer_size = 4;
- // read the data
- UInt8 buffer[buffer_size];
- //Read the buffer till the len or buffers_size, which ever is smaller
- read(stream, buffer, len > buffer_size ? buffer_size : len);
+ UInt8 Result = buffer[0];
+ LOG((CLOG_DEBUG2 "readf: read 1 byte integer: %d (0x%x)", Result, Result));
- switch (len) {
- case 1:
- // 1 byte integer
- *static_cast(destination) = buffer[0];
- LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)",
- len,
- *static_cast(destination),
- *static_cast(destination)));
- break;
+ return Result;
+}
- case 2:
- // 2 byte integer
- *static_cast(destination) =
- static_cast(
- (static_cast(buffer[0]) << 8) |
- static_cast(buffer[1]));
- LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)",
- len,
- *static_cast(destination),
- *static_cast(destination)));
- break;
+UInt16 ProtocolUtil::read2BytesInt(synergy::IStream * stream)
+{
+ const UInt32 BufferSize = 2;
+ std::array buffer = {};
+ read(stream, buffer.data(), BufferSize);
- case 4:
- // 4 byte integer
- *static_cast(destination) =
- (static_cast(buffer[0]) << 24) |
- (static_cast(buffer[1]) << 16) |
- (static_cast(buffer[2]) << 8) |
- static_cast(buffer[3]);
- LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)",
- len,
- *static_cast(destination),
- *static_cast(destination)));
- break;
- }
- }
- else {
- //the length is wrong
- LOG((CLOG_ERR "read: length to be read is wrong: '%d' should be 1,2, or 4", len));
- assert(false); //assert for debugging
+ UInt16 Result = (static_cast(buffer[0]) << 8) | static_cast(buffer[1]);
+ LOG((CLOG_DEBUG2 "readf: read 2 byte integer: %d (0x%x)", Result, Result));
+
+ return Result;
+}
+
+UInt32 ProtocolUtil::read4BytesInt(synergy::IStream * stream)
+{
+ const int BufferSize = 4;
+ std::array buffer = {};
+ read(stream, buffer.data(), BufferSize);
+
+ UInt32 Result = (static_cast(buffer[0]) << 24) |
+ (static_cast(buffer[1]) << 16) |
+ (static_cast(buffer[2]) << 8) |
+ (static_cast(buffer[3]));
+
+ LOG((CLOG_DEBUG2 "readf: read 4 byte integer: %d (0x%x)", Result, Result));
+
+ return Result;
+}
+
+void ProtocolUtil::readVector1ByteInt(synergy::IStream* stream, std::vector& destination)
+{
+ UInt32 size = read4BytesInt(stream);
+ for (UInt32 i = 0; i < size; ++i) {
+ destination.push_back(read1ByteInt(stream));
}
}
-void ProtocolUtil::readVectorInt(synergy::IStream * stream, UInt32 len, void* destination) {
- // check for valid length
- assert(len == 1 || len == 2 || len == 4);
+void ProtocolUtil::readVector2BytesInt(synergy::IStream* stream, std::vector& destination)
+{
+ UInt32 size = read4BytesInt(stream);
+ for (UInt32 i = 0; i < size; ++i) {
+ destination.push_back(read2BytesInt(stream));
+ }
+}
- // read the vector length
- UInt8 buffer[4];
- read(stream, buffer, 4);
- UInt32 n = (static_cast(buffer[0]) << 24) |
- (static_cast(buffer[1]) << 16) |
- (static_cast(buffer[2]) << 8) |
- static_cast(buffer[3]);
-
- // convert it
- switch (len) {
- case 1:
- // 1 byte integer
- for (UInt32 i = 0; i < n; ++i) {
- read(stream, buffer, 1);
- static_cast*>(destination)->push_back(buffer[0]);
- LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)",
- len, i,
- static_cast*>(destination)->back(),
- static_cast*>(destination)->back()));
- }
- break;
-
- case 2:
- // 2 byte integer
- for (UInt32 i = 0; i < n; ++i) {
- read(stream, buffer, 2);
- static_cast*>(destination)->push_back(
- static_cast(
- (static_cast(buffer[0]) << 8) |
- static_cast(buffer[1])));
- LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)",
- len, i,
- static_cast*>(destination)->back(),
- static_cast*>(destination)->back()));
- }
- break;
-
- case 4:
- // 4 byte integer
- for (UInt32 i = 0; i < n; ++i) {
- read(stream, buffer, 4);
- static_cast*>(destination)->push_back(
- (static_cast(buffer[0]) << 24) |
- (static_cast(buffer[1]) << 16) |
- (static_cast(buffer[2]) << 8) |
- static_cast(buffer[3]));
- LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)",
- len, i,
- static_cast*>(destination)->back(),
- static_cast*>(destination)->back()));
- }
- break;
+void ProtocolUtil::readVector4BytesInt(synergy::IStream* stream, std::vector& destination)
+{
+ UInt32 size = read4BytesInt(stream);
+ for (UInt32 i = 0; i < size; ++i) {
+ destination.push_back(read4BytesInt(stream));
}
}
@@ -533,12 +524,7 @@ void ProtocolUtil::readBytes(synergy::IStream * stream, UInt32 len, String* dest
// read the string length
UInt8 buffer[128];
- read(stream, buffer, 4);
- len = (static_cast(buffer[0]) << 24) |
- (static_cast(buffer[1]) << 16) |
- (static_cast(buffer[2]) << 8) |
- static_cast(buffer[3]);
-
+ len = read4BytesInt(stream);
// use a fixed size buffer if its big enough
const bool useFixed = (len <= sizeof(buffer));
diff --git a/src/lib/synergy/ProtocolUtil.h b/src/lib/synergy/ProtocolUtil.h
index 877a36e2b..a49da3096 100644
--- a/src/lib/synergy/ProtocolUtil.h
+++ b/src/lib/synergy/ProtocolUtil.h
@@ -86,12 +86,16 @@ private:
/**
* @brief Handles 1,2, or 4 byte Integers
*/
- static void readInt(synergy::IStream*, UInt32, void*);
+ static UInt8 read1ByteInt(synergy::IStream * stream);
+ static UInt16 read2BytesInt(synergy::IStream * stream);
+ static UInt32 read4BytesInt(synergy::IStream * stream);
/**
* @brief Handles a Vector of integers
*/
- static void readVectorInt(synergy::IStream*, UInt32, void*);
+ static void readVector1ByteInt(synergy::IStream*, std::vector&);
+ static void readVector2BytesInt(synergy::IStream*, std::vector&);
+ static void readVector4BytesInt(synergy::IStream*, std::vector&);
/**
* @brief Handles an array of bytes
diff --git a/src/test/unittests/synergy/ProtocolUtilTests.cpp b/src/test/unittests/synergy/ProtocolUtilTests.cpp
index 7ea60f325..3a502d9a4 100644
--- a/src/test/unittests/synergy/ProtocolUtilTests.cpp
+++ b/src/test/unittests/synergy/ProtocolUtilTests.cpp
@@ -124,15 +124,12 @@ TEST_F(ProtocolUtilTests, readf_string)
class ReadfIntTestFixture : public ::testing::TestWithParam< std::tuple >
{
-private:
-
+public:
+ MockStream stream;
UInt8 StreamData1Byte = 10;
std::array StreamData2Bytes = {0, 10};
std::array StreamData4Bytes = {0, 0, 0, 10};
-public:
- MockStream stream;
-
UInt8* getStreamData(int size)
{
UInt8* StreamData = nullptr;
@@ -240,8 +237,8 @@ class ReadfIntAndStringTest : public ReadfIntTestFixture
{
public:
UInt8 ActualInt8 = 0;
- UInt8 ActualInt16 = 0;
- UInt8 ActualInt32 = 32;
+ UInt16 ActualInt16 = 0;
+ UInt32 ActualInt32 = 32;
std::string ActualString;
};
@@ -307,20 +304,20 @@ TEST_F(ProtocolUtilTests, readf_string_and_int4bytes)
const UInt8 ExpectedInt = 10;
std::array StreamIntData = {0,0,0,ExpectedInt};
- const std::string ExpectedString(32768, 'x');
- std::array StringSize = {0,0,128,0};
+ const std::string ExpectedStr(32768, 'x');
+ std::array Size = {0,0,128,0};
EXPECT_CALL(stream, read(_, _))
.WillOnce(
DoAll(
- SetValueToVoidPointerArg0(StringSize.data(), StringSize.size()),
- Return(StringSize.size())
+ SetValueToVoidPointerArg0(Size.data(), Size.size()),
+ Return(Size.size())
)
)
.WillOnce(
DoAll(
- SetValueToVoidPointerArg0(ExpectedString.c_str(), ExpectedString.length()),
- Return(ExpectedString.length())
+ SetValueToVoidPointerArg0(ExpectedStr.c_str(), ExpectedStr.length()),
+ Return(ExpectedStr.length())
)
)
.WillOnce(
@@ -331,31 +328,31 @@ TEST_F(ProtocolUtilTests, readf_string_and_int4bytes)
);
EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4i", &ActualString, &ActualInt32));
- EXPECT_EQ(ExpectedString, ActualString);
+ EXPECT_EQ(ExpectedStr, ActualString);
EXPECT_EQ(ExpectedInt, ActualInt32);
}
TEST_F(ProtocolUtilTests, readf_string_and_vector_int4bytes)
{
- std::vector Actual4Bytes = {};
+ std::vector Actual = {};
const std::vector Expected4Bytes = {10,10};
std::array StreamVectorSize = {0,0,0,2};
std::array StreamData4Bytes = {0, 0, 0, 10};
- const std::string ExpectedString(32768, 'x');
- std::array StringSize = {0,0,128,0};
+ const std::string ExpString(32768, 'x');
+ std::array SizeString = {0,0,128,0};
EXPECT_CALL(stream, read(_, _))
.WillOnce(
DoAll(
- SetValueToVoidPointerArg0(StringSize.data(), StringSize.size()),
- Return(StringSize.size())
+ SetValueToVoidPointerArg0(SizeString.data(), SizeString.size()),
+ Return(SizeString.size())
)
)
.WillOnce(
DoAll(
- SetValueToVoidPointerArg0(ExpectedString.c_str(), ExpectedString.length()),
- Return(ExpectedString.length())
+ SetValueToVoidPointerArg0(ExpString.c_str(), ExpString.length()),
+ Return(ExpString.length())
)
)
.WillOnce(
@@ -371,9 +368,9 @@ TEST_F(ProtocolUtilTests, readf_string_and_vector_int4bytes)
)
);
- EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4I", &ActualString, &Actual4Bytes));
- EXPECT_EQ(ExpectedString, ActualString);
- EXPECT_EQ(Expected4Bytes, Actual4Bytes);
+ EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4I", &ActualString, &Actual));
+ EXPECT_EQ(ExpString, ActualString);
+ EXPECT_EQ(Expected4Bytes, Actual);
}
TEST_F(ProtocolUtilTests, readf_vector_int4bytes_and_string)