Skip to content

Commit

Permalink
Merge pull request #1473 from dpogue/netcli-endian
Browse files Browse the repository at this point in the history
pnNcCli should consistently convert to little endian
  • Loading branch information
dpogue authored Sep 9, 2023
2 parents 7e73665 + cb844f9 commit ad85620
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 97 deletions.
86 changes: 59 additions & 27 deletions Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,54 +300,64 @@ static void BufferedSendData (
case kNetMsgFieldInteger: {
const unsigned count = cmd->count ? cmd->count : 1;
const unsigned bytes = cmd->size * count;
void * temp = malloc(bytes);

if (count == 1)
{
auto temp = std::make_unique<uint8_t[]>(bytes);

if (count == 1) {
// Single values are passed by value
if (cmd->size == sizeof(uint8_t)) {
*(uint8_t*)temp = *(uint8_t*)msg;
*(uint8_t*)&temp[0] = *(uint8_t*)msg;
} else if (cmd->size == sizeof(uint16_t)) {
*(uint16_t*)temp = hsToLE16(*(uint16_t*)msg);
*(uint16_t*)&temp[0] = hsToLE16(*(uint16_t*)msg);
} else if (cmd->size == sizeof(uint32_t)) {
*(uint32_t*)temp = hsToLE32(*(uint32_t*)msg);
*(uint32_t*)&temp[0] = hsToLE32(*(uint32_t*)msg);
} else if (cmd->size == sizeof(uint64_t)) {
*(uint64_t*)temp = hsToLE64(*(uint64_t*)msg);
*(uint64_t*)&temp[0] = hsToLE64(*(uint64_t*)msg);
}
}
else
{
} else {
// Value arrays are passed in by ptr
for (size_t i = 0; i < count; i++) {
if (cmd->size == sizeof(uint8_t)) {
((uint8_t*)temp)[i] = ((uint8_t*)*msg)[i];
*(uint8_t*)&temp[i] = ((uint8_t*)*msg)[i];
} else if (cmd->size == sizeof(uint16_t)) {
((uint16_t*)temp)[i] = hsToLE16(((uint16_t*)*msg)[i]);
*(uint16_t*)&temp[i] = hsToLE16(((uint16_t*)*msg)[i]);
} else if (cmd->size == sizeof(uint32_t)) {
((uint32_t*)temp)[i] = hsToLE32(((uint32_t*)*msg)[i]);
*(uint32_t*)&temp[i] = hsToLE32(((uint32_t*)*msg)[i]);
} else if (cmd->size == sizeof(uint64_t)) {
((uint64_t*)temp)[i] = hsToLE64(((uint64_t*)*msg)[i]);
*(uint64_t*)&temp[i] = hsToLE64(((uint64_t*)*msg)[i]);
}
}
}

// Write values to send buffer
AddToSendBuffer(cli, bytes, temp);

free(temp);
// Write values to send buffer
AddToSendBuffer(cli, bytes, temp.get());
}
break;

case kNetMsgFieldReal: {
const unsigned count = cmd->count ? cmd->count : 1;
const unsigned bytes = cmd->size * count;

if (count == 1)
auto temp = std::make_unique<uint8_t[]>(bytes);

if (count == 1) {
// Single values are passed in by value
AddToSendBuffer(cli, bytes, (const void *) msg);
else
if (cmd->size == sizeof(float)) {
*(float*)&temp[0] = hsToLEFloat(*(float*)msg);
} else if (cmd->size == sizeof(double)) {
*(double*)&temp[0] = hsToLEFloat(*(double*)msg);
}
} else {
// Value arrays are passed in by ptr
AddToSendBuffer(cli, bytes, (const void *) *msg);
for (size_t i = 0; i < count; i++) {
if (cmd->size == sizeof(float)) {
*(float*)&temp[i] = hsToLEFloat(((float*)*msg)[i]);
} else if (cmd->size == sizeof(double)) {
*(double*)&temp[i] = hsToLEFloat(((double*)*msg)[i]);
}
}
}

// Write values to send buffer
AddToSendBuffer(cli, bytes, temp.get());
}
break;

Expand All @@ -356,11 +366,18 @@ static void BufferedSendData (
// we reserve one space for the NULL terminator
const uint16_t length = (uint16_t) std::char_traits<char16_t>::length((const char16_t *) *msg);
ASSERT_MSG_VALID(length < cmd->count);

// Write actual string length
uint16_t size = hsToLE16(length);
AddToSendBuffer(cli, sizeof(uint16_t), (const void*)&size);

auto temp = std::make_unique<char16_t[]>(length);
for (size_t i = 0; i < length; i++) {
temp[i] = hsToLE16(((char16_t*)*msg)[i]);
}

// Write string data
AddToSendBuffer(cli, length * sizeof(char16_t), (const void *) *msg);
AddToSendBuffer(cli, length * sizeof(char16_t), temp.get());
}
break;

Expand Down Expand Up @@ -461,8 +478,7 @@ static bool DispatchData (NetCli * cli, void * param) {
goto NEED_MORE_DATA;
}

// byte-swap integers
// This is so screwed up >.<
// Convert to platform endianness
for (size_t i = 0; i < count; i++) {
if (cli->recvField->size == sizeof(uint16_t)) {
((uint16_t*)data)[i] = hsToLE16(((uint16_t*)data)[i]);
Expand Down Expand Up @@ -493,6 +509,15 @@ static bool DispatchData (NetCli * cli, void * param) {
goto NEED_MORE_DATA;
}

// Convert to platform endianness
for (size_t i = 0; i < count; i++) {
if (cli->recvField->size == sizeof(float)) {
((float*)data)[i] = hsToLEFloat(((float*)data)[i]);
} else if (cli->recvField->size == sizeof(double)) {
((double*)data)[i] = hsToLEDouble(((double*)data)[i]);
}
}

// Field complete
}
break;
Expand Down Expand Up @@ -574,6 +599,11 @@ static bool DispatchData (NetCli * cli, void * param) {
goto NEED_MORE_DATA;
}

// Convert to platform endianness
for (size_t i = 0; i < cli->recvField->count; i++) {
((char16_t*)data)[i] = hsToLE16(((char16_t*)data)[i]);
}

// Insert NULL terminator
* (char16_t *)(data + cli->recvFieldBytes) = 0;

Expand All @@ -583,6 +613,8 @@ static bool DispatchData (NetCli * cli, void * param) {
cli->recvFieldBytes = 0;
}
break;

default: break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static const unsigned kFileSrvBuildId = 0;
struct Cli2File_ConnData {
uint32_t dataBytes;
uint32_t buildId;
unsigned serverType;
uint32_t serverType;
};
struct Cli2File_Connect {
AsyncSocketConnectPacket hdr;
Expand Down
10 changes: 5 additions & 5 deletions Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglAuth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1536,13 +1536,13 @@ static void Connect (

Cli2Auth_Connect connect;
connect.hdr.connType = kConnTypeCliToAuth;
connect.hdr.hdrBytes = sizeof(connect.hdr);
connect.hdr.buildId = plProduct::BuildId();
connect.hdr.buildType = plProduct::BuildType();
connect.hdr.branchId = plProduct::BranchId();
connect.hdr.hdrBytes = hsToLE16(sizeof(connect.hdr));
connect.hdr.buildId = hsToLE32(plProduct::BuildId());
connect.hdr.buildType = hsToLE32(plProduct::BuildType());
connect.hdr.branchId = hsToLE32(plProduct::BranchId());
connect.hdr.productId = plProduct::UUID();
connect.data.token = conn->token;
connect.data.dataBytes = sizeof(connect.data);
connect.data.dataBytes = hsToLE32(sizeof(connect.data));

AsyncSocketConnect(
&conn->cancelId,
Expand Down
Loading

0 comments on commit ad85620

Please sign in to comment.