#9 make every sign explicit

pull/32/head
Geoffrey Merck 4 years ago
parent 18bb1f365b
commit a54eb90b27

@ -21,7 +21,7 @@
#include "RSMS1AMessageBuilder.h"
#include "StringUtils.h"
std::vector<char> CRSMS1AMessageBuilder::m_charsToEscape = {-17, 0, 17, 19, -2, -25, 26, -3, -1, 36, 13, 44};
std::vector<signed char> CRSMS1AMessageBuilder::m_charsToEscape = {-17, 0, 17, 19, -2, -25, 26, -3, -1, 36, 13, 44};
void CRSMS1AMessageBuilder::buildMessage(std::string& message, const std::string& sender, const std::string& recipient, const std::string body)
{
@ -30,28 +30,28 @@ void CRSMS1AMessageBuilder::buildMessage(std::string& message, const std::string
escapeBody(bodyTmp, body + (char)bodyCrc);
std::string header = CStringUtils::string_format("%s,%s,0011", sender.c_str(), recipient.c_str());
char c1, c2;
signed char c1, c2;
calcMsgIcomCRC(header, c1, c2);
header.push_back(c1);
header.push_back(c2);
message = "$$Msg," + header + bodyTmp + '\r';
}
char CRSMS1AMessageBuilder::calculateBodyCRC(const std::string& body)
signed char CRSMS1AMessageBuilder::calculateBodyCRC(const std::string& body)
{
if(body.length() == 1)
return body[0];
unsigned int num = 0;
for(auto c : body) {
signed int num = 0;
for(signed char c : body) {
num += c;
}
auto res = (num & 255);
signed int res = (num & 255);
if(res >= 128)
res -= 128;
return (char)res;
return (signed char)res;
}
void CRSMS1AMessageBuilder::escapeBody(std::string& output, const std::string& body)
@ -65,18 +65,18 @@ void CRSMS1AMessageBuilder::escapeBody(std::string& output, const std::string& b
}
}
void CRSMS1AMessageBuilder::calcMsgIcomCRC(const std::string& msg, char& c1, char& c2)
void CRSMS1AMessageBuilder::calcMsgIcomCRC(const std::string& msg, signed char& c1, signed char& c2)
{
int num = 0;
for(unsigned int i = 0U; i < msg.length(); i++) {
num += msg[i];
}
c1 = doWhatever((char)((num >> 4) & 15));
c2 = doWhatever((char)(num & 15));
c1 = doWhatever((signed char)((num >> 4) & 15));
c2 = doWhatever((signed char)(num & 15));
}
char CRSMS1AMessageBuilder::doWhatever(char b2) {
signed char CRSMS1AMessageBuilder::doWhatever(signed char b2) {
int i;
int i2 = b2 & 255;
if (i2 >= 0 && i2 <= 9) {
@ -86,7 +86,7 @@ char CRSMS1AMessageBuilder::doWhatever(char b2) {
} else {
i = b2 + 55;
}
return (char) i;
return (signed char) i;
}
RSMS1A_PARSE_STATUS CRSMS1AMessageBuilder::parseMessage(std::string& sender, std::string& recipient, std::string& body, const std::string& message)

@ -34,12 +34,12 @@ public:
static RSMS1A_PARSE_STATUS parseMessage(std::string& sender, std::string& recipient, std::string& body, const std::string& message);
private:
static void calcMsgIcomCRC(const std::string& msg, char& c1, char& c2);
static void calcMsgIcomCRC(const std::string& msg, signed char& c1, signed char& c2);
static void escapeBody(std::string& output, const std::string& body);
static void unescapeBody(std::string& output, const std::string& body);
static void escapeBytes(std::vector<char> output, const std::vector<char> input);
static char calculateBodyCRC(const std::string& body);
static char doWhatever(char b2);
static signed char calculateBodyCRC(const std::string& body);
static signed char doWhatever(signed char b2);
static std::vector<char> m_charsToEscape;
static std::vector<signed char> m_charsToEscape;
};

@ -19,6 +19,8 @@
#include <gtest/gtest.h>
#include "../../Utils.h"
#include "../../StringUtils.h"
#include "../../Log.h"
class Utils_swap_endian_be : public ::testing::Test {
@ -31,4 +33,13 @@ TEST_F(Utils_swap_endian_be, SwapUINT32_be) {
uint32_t res = CUtils::swap_endian_be(test);
EXPECT_EQ(res, 0x12345678U);
}
}
TEST_F(Utils_swap_endian_be, blabla) {
std::vector<char> charsToEscape = {-17, 0, 17, 19, 128-2, -25, 26, 128-3, -1, 36, 13, 44};
for(unsigned int i = 1U; i < charsToEscape.size(); i++) {
CLog::logDebug("%08x - %08x", charsToEscape[i], (unsigned char)charsToEscape[i]);
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.