#6755 Broke up large function for readability.
This commit is contained in:
parent
0a5fbe6e3e
commit
445f616e34
2 changed files with 175 additions and 150 deletions
|
|
@ -111,154 +111,15 @@ ProtocolUtil::vreadf(synergy::IStream* stream, const char* fmt, va_list args)
|
||||||
UInt32 len = eatLength(&fmt);
|
UInt32 len = eatLength(&fmt);
|
||||||
switch (*fmt) {
|
switch (*fmt) {
|
||||||
case 'i': {
|
case 'i': {
|
||||||
// check for valid length
|
readInt(stream, len, args);
|
||||||
if (len == 4 || len == 2 || len == 1) {
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
// convert it
|
|
||||||
void* v = va_arg(args, void*);
|
|
||||||
switch (len) {
|
|
||||||
case 1:
|
|
||||||
// 1 byte integer
|
|
||||||
*static_cast<UInt8*>(v) = buffer[0];
|
|
||||||
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast<UInt8*>(v), *static_cast<UInt8*>(v)));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
// 2 byte integer
|
|
||||||
*static_cast<UInt16*>(v) =
|
|
||||||
static_cast<UInt16>(
|
|
||||||
(static_cast<UInt16>(buffer[0]) << 8) |
|
|
||||||
static_cast<UInt16>(buffer[1]));
|
|
||||||
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast<UInt16*>(v), *static_cast<UInt16*>(v)));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 4:
|
|
||||||
// 4 byte integer
|
|
||||||
*static_cast<UInt32*>(v) =
|
|
||||||
(static_cast<UInt32>(buffer[0]) << 24) |
|
|
||||||
(static_cast<UInt32>(buffer[1]) << 16) |
|
|
||||||
(static_cast<UInt32>(buffer[2]) << 8) |
|
|
||||||
static_cast<UInt32>(buffer[3]);
|
|
||||||
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast<UInt32*>(v), *static_cast<UInt32*>(v)));
|
|
||||||
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
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'I': {
|
case 'I': {
|
||||||
// check for valid length
|
readVectorInt(stream, len, args);
|
||||||
assert(len == 1 || len == 2 || len == 4);
|
|
||||||
|
|
||||||
// read the vector length
|
|
||||||
UInt8 buffer[4];
|
|
||||||
read(stream, buffer, 4);
|
|
||||||
UInt32 n = (static_cast<UInt32>(buffer[0]) << 24) |
|
|
||||||
(static_cast<UInt32>(buffer[1]) << 16) |
|
|
||||||
(static_cast<UInt32>(buffer[2]) << 8) |
|
|
||||||
static_cast<UInt32>(buffer[3]);
|
|
||||||
|
|
||||||
// convert it
|
|
||||||
void* v = va_arg(args, void*);
|
|
||||||
switch (len) {
|
|
||||||
case 1:
|
|
||||||
// 1 byte integer
|
|
||||||
for (UInt32 i = 0; i < n; ++i) {
|
|
||||||
read(stream, buffer, 1);
|
|
||||||
static_cast<std::vector<UInt8>*>(v)->push_back(
|
|
||||||
buffer[0]);
|
|
||||||
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast<std::vector<UInt8>*>(v)->back(), static_cast<std::vector<UInt8>*>(v)->back()));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
// 2 byte integer
|
|
||||||
for (UInt32 i = 0; i < n; ++i) {
|
|
||||||
read(stream, buffer, 2);
|
|
||||||
static_cast<std::vector<UInt16>*>(v)->push_back(
|
|
||||||
static_cast<UInt16>(
|
|
||||||
(static_cast<UInt16>(buffer[0]) << 8) |
|
|
||||||
static_cast<UInt16>(buffer[1])));
|
|
||||||
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast<std::vector<UInt16>*>(v)->back(), static_cast<std::vector<UInt16>*>(v)->back()));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 4:
|
|
||||||
// 4 byte integer
|
|
||||||
for (UInt32 i = 0; i < n; ++i) {
|
|
||||||
read(stream, buffer, 4);
|
|
||||||
static_cast<std::vector<UInt32>*>(v)->push_back(
|
|
||||||
(static_cast<UInt32>(buffer[0]) << 24) |
|
|
||||||
(static_cast<UInt32>(buffer[1]) << 16) |
|
|
||||||
(static_cast<UInt32>(buffer[2]) << 8) |
|
|
||||||
static_cast<UInt32>(buffer[3]));
|
|
||||||
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast<std::vector<UInt32>*>(v)->back(), static_cast<std::vector<UInt32>*>(v)->back()));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 's': {
|
case 's': {
|
||||||
assert(len == 0);
|
readBytes(stream, len, args);
|
||||||
|
|
||||||
// read the string length
|
|
||||||
UInt8 buffer[128];
|
|
||||||
read(stream, buffer, 4);
|
|
||||||
UInt32 len = (static_cast<UInt32>(buffer[0]) << 24) |
|
|
||||||
(static_cast<UInt32>(buffer[1]) << 16) |
|
|
||||||
(static_cast<UInt32>(buffer[2]) << 8) |
|
|
||||||
static_cast<UInt32>(buffer[3]);
|
|
||||||
|
|
||||||
// use a fixed size buffer if its big enough
|
|
||||||
const bool useFixed = (len <= sizeof(buffer));
|
|
||||||
|
|
||||||
// allocate a buffer to read the data
|
|
||||||
UInt8* sBuffer = buffer;
|
|
||||||
if (!useFixed) {
|
|
||||||
try{
|
|
||||||
sBuffer = new UInt8[len];
|
|
||||||
}
|
|
||||||
catch (std::bad_alloc & exception) {
|
|
||||||
// Added try catch due to GHSA-chfm-333q-gfpp
|
|
||||||
LOG((CLOG_ERR "ALLOC: Unable to allocate memory %d bytes", len));
|
|
||||||
LOG((CLOG_DEBUG "bad_alloc detected: Do you have enough free memory?"));
|
|
||||||
throw exception;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// read the data
|
|
||||||
try {
|
|
||||||
read(stream, sBuffer, len);
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
if (!useFixed) {
|
|
||||||
delete[] sBuffer;
|
|
||||||
}
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG((CLOG_DEBUG2 "readf: read %d byte string", len));
|
|
||||||
|
|
||||||
// save the data
|
|
||||||
String* dst = va_arg(args, String*);
|
|
||||||
dst->assign((const char*)sBuffer, len);
|
|
||||||
|
|
||||||
// release the buffer
|
|
||||||
if (!useFixed) {
|
|
||||||
delete[] sBuffer;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -551,6 +412,155 @@ ProtocolUtil::read(synergy::IStream* stream, void* vbuffer, UInt32 count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ProtocolUtil::readInt(synergy::IStream * stream, UInt32 len, va_list args) {
|
||||||
|
// check for valid length
|
||||||
|
if (len == 4 || len == 2 || len == 1) {
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// convert it
|
||||||
|
void* v = va_arg(args, void*);
|
||||||
|
switch (len) {
|
||||||
|
case 1:
|
||||||
|
// 1 byte integer
|
||||||
|
*static_cast<UInt8*>(v) = buffer[0];
|
||||||
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast<UInt8*>(v), *static_cast<UInt8*>(v)));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
// 2 byte integer
|
||||||
|
*static_cast<UInt16*>(v) =
|
||||||
|
static_cast<UInt16>(
|
||||||
|
(static_cast<UInt16>(buffer[0]) << 8) |
|
||||||
|
static_cast<UInt16>(buffer[1]));
|
||||||
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast<UInt16*>(v), *static_cast<UInt16*>(v)));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
// 4 byte integer
|
||||||
|
*static_cast<UInt32*>(v) =
|
||||||
|
(static_cast<UInt32>(buffer[0]) << 24) |
|
||||||
|
(static_cast<UInt32>(buffer[1]) << 16) |
|
||||||
|
(static_cast<UInt32>(buffer[2]) << 8) |
|
||||||
|
static_cast<UInt32>(buffer[3]);
|
||||||
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast<UInt32*>(v), *static_cast<UInt32*>(v)));
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProtocolUtil::readVectorInt(synergy::IStream * stream, UInt32 len, va_list args) {
|
||||||
|
// check for valid length
|
||||||
|
assert(len == 1 || len == 2 || len == 4);
|
||||||
|
|
||||||
|
// read the vector length
|
||||||
|
UInt8 buffer[4];
|
||||||
|
read(stream, buffer, 4);
|
||||||
|
UInt32 n = (static_cast<UInt32>(buffer[0]) << 24) |
|
||||||
|
(static_cast<UInt32>(buffer[1]) << 16) |
|
||||||
|
(static_cast<UInt32>(buffer[2]) << 8) |
|
||||||
|
static_cast<UInt32>(buffer[3]);
|
||||||
|
|
||||||
|
// convert it
|
||||||
|
void* v = va_arg(args, void*);
|
||||||
|
switch (len) {
|
||||||
|
case 1:
|
||||||
|
// 1 byte integer
|
||||||
|
for (UInt32 i = 0; i < n; ++i) {
|
||||||
|
read(stream, buffer, 1);
|
||||||
|
static_cast<std::vector<UInt8>*>(v)->push_back(
|
||||||
|
buffer[0]);
|
||||||
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast<std::vector<UInt8>*>(v)->back(), static_cast<std::vector<UInt8>*>(v)->back()));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
// 2 byte integer
|
||||||
|
for (UInt32 i = 0; i < n; ++i) {
|
||||||
|
read(stream, buffer, 2);
|
||||||
|
static_cast<std::vector<UInt16>*>(v)->push_back(
|
||||||
|
static_cast<UInt16>(
|
||||||
|
(static_cast<UInt16>(buffer[0]) << 8) |
|
||||||
|
static_cast<UInt16>(buffer[1])));
|
||||||
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast<std::vector<UInt16>*>(v)->back(), static_cast<std::vector<UInt16>*>(v)->back()));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
// 4 byte integer
|
||||||
|
for (UInt32 i = 0; i < n; ++i) {
|
||||||
|
read(stream, buffer, 4);
|
||||||
|
static_cast<std::vector<UInt32>*>(v)->push_back(
|
||||||
|
(static_cast<UInt32>(buffer[0]) << 24) |
|
||||||
|
(static_cast<UInt32>(buffer[1]) << 16) |
|
||||||
|
(static_cast<UInt32>(buffer[2]) << 8) |
|
||||||
|
static_cast<UInt32>(buffer[3]));
|
||||||
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast<std::vector<UInt32>*>(v)->back(), static_cast<std::vector<UInt32>*>(v)->back()));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProtocolUtil::readBytes(synergy::IStream * stream, UInt32 len, va_list args) {
|
||||||
|
assert(len == 0);
|
||||||
|
|
||||||
|
// read the string length
|
||||||
|
UInt8 buffer[128];
|
||||||
|
read(stream, buffer, 4);
|
||||||
|
len = (static_cast<UInt32>(buffer[0]) << 24) |
|
||||||
|
(static_cast<UInt32>(buffer[1]) << 16) |
|
||||||
|
(static_cast<UInt32>(buffer[2]) << 8) |
|
||||||
|
static_cast<UInt32>(buffer[3]);
|
||||||
|
|
||||||
|
// use a fixed size buffer if its big enough
|
||||||
|
const bool useFixed = (len <= sizeof(buffer));
|
||||||
|
|
||||||
|
// allocate a buffer to read the data
|
||||||
|
UInt8* sBuffer = buffer;
|
||||||
|
if (!useFixed) {
|
||||||
|
try{
|
||||||
|
sBuffer = new UInt8[len];
|
||||||
|
}
|
||||||
|
catch (std::bad_alloc & exception) {
|
||||||
|
// Added try catch due to GHSA-chfm-333q-gfpp
|
||||||
|
LOG((CLOG_ERR "ALLOC: Unable to allocate memory %d bytes", len));
|
||||||
|
LOG((CLOG_DEBUG "bad_alloc detected: Do you have enough free memory?"));
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// read the data
|
||||||
|
try {
|
||||||
|
read(stream, sBuffer, len);
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
if (!useFixed) {
|
||||||
|
delete[] sBuffer;
|
||||||
|
}
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG((CLOG_DEBUG2 "readf: read %d byte string", len));
|
||||||
|
|
||||||
|
// save the data
|
||||||
|
String* dst = va_arg(args, String*);
|
||||||
|
dst->assign((const char*)sBuffer, len);
|
||||||
|
|
||||||
|
// release the buffer
|
||||||
|
if (!useFixed) {
|
||||||
|
delete[] sBuffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// XIOReadMismatch
|
// XIOReadMismatch
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ public:
|
||||||
- \%s -- converts String* to stream of bytes
|
- \%s -- converts String* to stream of bytes
|
||||||
- \%S -- converts integer N and const UInt8* to stream of N bytes
|
- \%S -- converts integer N and const UInt8* to stream of N bytes
|
||||||
*/
|
*/
|
||||||
static void writef(synergy::IStream*,
|
static void writef(synergy::IStream*,
|
||||||
const char* fmt, ...);
|
const char* fmt, ...);
|
||||||
|
|
||||||
//! Read formatted data
|
//! Read formatted data
|
||||||
|
|
@ -69,19 +69,34 @@ public:
|
||||||
- \%4I -- reads NBO 4 byte integers; arg is std::vector<UInt32>*
|
- \%4I -- reads NBO 4 byte integers; arg is std::vector<UInt32>*
|
||||||
- \%s -- reads bytes; argument must be a String*, \b not a char*
|
- \%s -- reads bytes; argument must be a String*, \b not a char*
|
||||||
*/
|
*/
|
||||||
static bool readf(synergy::IStream*,
|
static bool readf(synergy::IStream*,
|
||||||
const char* fmt, ...);
|
const char* fmt, ...);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void vwritef(synergy::IStream*,
|
static void vwritef(synergy::IStream*,
|
||||||
const char* fmt, UInt32 size, va_list);
|
const char* fmt, UInt32 size, va_list);
|
||||||
static void vreadf(synergy::IStream*,
|
static void vreadf(synergy::IStream*,
|
||||||
const char* fmt, va_list);
|
const char* fmt, va_list);
|
||||||
|
|
||||||
static UInt32 getLength(const char* fmt, va_list);
|
static UInt32 getLength(const char* fmt, va_list);
|
||||||
static void writef(void*, const char* fmt, va_list);
|
static void writef(void*, const char* fmt, va_list);
|
||||||
static UInt32 eatLength(const char** fmt);
|
static UInt32 eatLength(const char** fmt);
|
||||||
static void read(synergy::IStream*, void*, UInt32);
|
static void read(synergy::IStream*, void*, UInt32);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Handles 1,2, or 4 byte Integers
|
||||||
|
*/
|
||||||
|
static void readInt(synergy::IStream*, UInt32, va_list);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Handles a Vector of integers
|
||||||
|
*/
|
||||||
|
static void readVectorInt(synergy::IStream*, UInt32, va_list);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Handles an array of bytes
|
||||||
|
*/
|
||||||
|
static void readBytes(synergy::IStream*, UInt32, va_list);
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Mismatched read exception
|
//! Mismatched read exception
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue