Include hp version number on all required places. (#265)

* Adding HP version header to binary files and adding hp table to ledger.sqlite.

* Updating hp_version in user challenge message.

* Minor improvement.
This commit is contained in:
Savinda Senevirathne
2021-03-10 20:08:04 +05:30
committed by GitHub
parent 1eabe7db4a
commit b45cdb999d
10 changed files with 196 additions and 15 deletions

View File

@@ -411,6 +411,33 @@ namespace util
return fcntl(fd, F_SETLKW, &lock);
}
/**
* Convert the given uint16_t number to bytes in big endian format.
* @param dest Byte array pointer.
* @param x Number to be converted.
*/
void uint16_to_bytes(uint8_t *dest, const uint16_t x)
{
dest[0] = (uint8_t)((x >> 8) & 0xff);
dest[1] = (uint8_t)((x >> 0) & 0xff);
}
/**
* Read the uint16_t number from the given byte array which is in big endian format.
* @param data Byte array pointer.
* @return The uint16_t number in the given byte array.
*/
uint16_t uint16_from_bytes(const uint8_t *data)
{
return ((uint16_t)data[0] << 8) +
(uint16_t)data[1];
}
/**
* Convert the given uint32_t number to bytes in big endian format.
* @param dest Byte array pointer.
* @param x Number to be converted.
*/
void uint32_to_bytes(uint8_t *dest, const uint32_t x)
{
dest[0] = (uint8_t)((x >> 24) & 0xff);
@@ -419,6 +446,11 @@ namespace util
dest[3] = (uint8_t)((x >> 0) & 0xff);
}
/**
* Read the uint32_t number from the given byte array which is in big endian format.
* @param data Byte array pointer.
* @return The uint32_t number in the given byte array.
*/
uint32_t uint32_from_bytes(const uint8_t *data)
{
return ((uint32_t)data[0] << 24) +
@@ -427,6 +459,11 @@ namespace util
((uint32_t)data[3]);
}
/**
* Convert the given uint64_t number to bytes in big endian format.
* @param dest Byte array pointer.
* @param x Number to be converted.
*/
void uint64_to_bytes(uint8_t *dest, const uint64_t x)
{
dest[0] = (uint8_t)((x >> 56) & 0xff);
@@ -439,6 +476,11 @@ namespace util
dest[7] = (uint8_t)((x >> 0) & 0xff);
}
/**
* Read the uint64_t number from the given byte array which is in big endian format.
* @param data Byte array pointer.
* @return The uint64_t number in the given byte array.
*/
uint64_t uint64_from_bytes(const uint8_t *data)
{
return ((uint64_t)data[0] << 56) +
@@ -451,4 +493,42 @@ namespace util
((uint64_t)data[7]);
}
/**
* Create 16 byte hp version header. First 6 bytes contains the hp version and the
* next 10 bytes are reserved for future use.
* @param header Header byte array to be populated with header data.
* @param hp_version Current hp version string.
* @return Returns -1 on error and 0 on success.
*/
int create_hp_version_header(uint8_t *header, std::string_view hp_version)
{
const std::string delimeter = ".";
size_t start = 0;
size_t end = hp_version.find(delimeter);
if (end == std::string::npos)
return -1;
const uint16_t major = atoi(hp_version.substr(start, end - start).data());
start = end + delimeter.length();
end = hp_version.find(delimeter, start);
if (end == std::string::npos)
return -1;
const uint16_t minor = atoi(hp_version.substr(start, end - start).data());
start = end + delimeter.length();
end = hp_version.find(delimeter, start);
const uint16_t patch = atoi(hp_version.substr(start).data());
uint16_to_bytes(&header[0], major);
uint16_to_bytes(&header[2], minor);
uint16_to_bytes(&header[4], patch);
// Make remaining bytes to zero for future use.
memset(&header[6], 0, 10);
return 0;
}
} // namespace util

View File

@@ -13,10 +13,13 @@
namespace util
{
// Hot Pocket version. Displayed on 'hotpocket version' and written to new configs.
constexpr const char *HP_VERSION = "0.1";
constexpr const char *HP_VERSION = "1.0.0";
// Minimum compatible config version (this will be used to validate configs)
constexpr const char *MIN_CONFIG_VERSION = "0.1";
// Minimum compatible config version (this will be used to validate configs).
constexpr const char *MIN_CONFIG_VERSION = "1.0.0";
// HP version header size.
constexpr const int HP_VERSION_HEADER_SIZE = 16;
/**
* The messaging protocol used in a web socket channel.
@@ -75,11 +78,20 @@ namespace util
int release_lock(const int fd, struct flock &lock);
void uint16_to_bytes(uint8_t *dest, const uint16_t x);
uint16_t uint16_from_bytes(const uint8_t *data);
void uint32_to_bytes(uint8_t *dest, const uint32_t x);
uint32_t uint32_from_bytes(const uint8_t *data);
void uint64_to_bytes(uint8_t *dest, const uint64_t x);
uint64_t uint64_from_bytes(const uint8_t *data);
int create_hp_version_header(uint8_t *header, std::string_view hp_version);
} // namespace util
#endif