Replace bswap with proper endian functions
Reviewers: teon.banek Reviewed By: teon.banek Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D2290
This commit is contained in:
parent
134525851b
commit
fb6dc83ef4
@ -6,8 +6,8 @@
|
||||
|
||||
#include "communication/bolt/v1/codes.hpp"
|
||||
#include "communication/bolt/v1/value.hpp"
|
||||
#include "utils/bswap.hpp"
|
||||
#include "utils/cast.hpp"
|
||||
#include "utils/endian.hpp"
|
||||
|
||||
namespace communication::bolt {
|
||||
|
||||
@ -186,18 +186,18 @@ class Decoder {
|
||||
if (!buffer_.Read(reinterpret_cast<uint8_t *>(&tmp), sizeof(tmp))) {
|
||||
return false;
|
||||
}
|
||||
ret = utils::Bswap(tmp);
|
||||
ret = utils::BigEndianToHost(tmp);
|
||||
} else if (marker == Marker::Int32) {
|
||||
int32_t tmp;
|
||||
if (!buffer_.Read(reinterpret_cast<uint8_t *>(&tmp), sizeof(tmp))) {
|
||||
return false;
|
||||
}
|
||||
ret = utils::Bswap(tmp);
|
||||
ret = utils::BigEndianToHost(tmp);
|
||||
} else if (marker == Marker::Int64) {
|
||||
if (!buffer_.Read(reinterpret_cast<uint8_t *>(&ret), sizeof(ret))) {
|
||||
return false;
|
||||
}
|
||||
ret = utils::Bswap(ret);
|
||||
ret = utils::BigEndianToHost(ret);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
@ -212,7 +212,7 @@ class Decoder {
|
||||
if (!buffer_.Read(reinterpret_cast<uint8_t *>(&value), sizeof(value))) {
|
||||
return false;
|
||||
}
|
||||
value = utils::Bswap(value);
|
||||
value = utils::BigEndianToHost(value);
|
||||
ret = utils::MemcpyCast<double>(value);
|
||||
*data = Value(ret);
|
||||
return true;
|
||||
@ -233,14 +233,14 @@ class Decoder {
|
||||
if (!buffer_.Read(reinterpret_cast<uint8_t *>(&tmp), sizeof(tmp))) {
|
||||
return -1;
|
||||
}
|
||||
tmp = utils::Bswap(tmp);
|
||||
tmp = utils::BigEndianToHost(tmp);
|
||||
return tmp;
|
||||
} else if (marker == Marker32[type]) {
|
||||
uint32_t tmp;
|
||||
if (!buffer_.Read(reinterpret_cast<uint8_t *>(&tmp), sizeof(tmp))) {
|
||||
return -1;
|
||||
}
|
||||
tmp = utils::Bswap(tmp);
|
||||
tmp = utils::BigEndianToHost(tmp);
|
||||
return tmp;
|
||||
} else {
|
||||
return -1;
|
||||
|
@ -4,8 +4,8 @@
|
||||
|
||||
#include "communication/bolt/v1/codes.hpp"
|
||||
#include "communication/bolt/v1/value.hpp"
|
||||
#include "utils/bswap.hpp"
|
||||
#include "utils/cast.hpp"
|
||||
#include "utils/endian.hpp"
|
||||
|
||||
static_assert(std::is_same_v<std::uint8_t, char> ||
|
||||
std::is_same_v<std::uint8_t, unsigned char>,
|
||||
@ -216,7 +216,7 @@ class BaseEncoder {
|
||||
private:
|
||||
template <class T>
|
||||
void WritePrimitiveValue(T value) {
|
||||
value = utils::Bswap(value);
|
||||
value = utils::HostToBigEndian(value);
|
||||
WriteRAW(reinterpret_cast<const uint8_t *>(&value), sizeof(value));
|
||||
}
|
||||
};
|
||||
|
@ -8,7 +8,6 @@
|
||||
#include <glog/logging.h>
|
||||
|
||||
#include "communication/bolt/v1/constants.hpp"
|
||||
#include "utils/bswap.hpp"
|
||||
|
||||
namespace communication::bolt {
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include <fstream>
|
||||
|
||||
#include "hasher.hpp"
|
||||
#include "utils/bswap.hpp"
|
||||
#include "utils/endian.hpp"
|
||||
|
||||
/**
|
||||
* Buffer reads data from file and calculates hash of read data. Implements
|
||||
@ -50,8 +50,7 @@ class HashedFileReader {
|
||||
bool ReadType(TValue &val, bool hash = true) {
|
||||
if (!Read(reinterpret_cast<uint8_t *>(&val), sizeof(TValue), hash))
|
||||
return false;
|
||||
// TODO: must be platform specific in the future
|
||||
val = utils::Bswap(val);
|
||||
val = utils::BigEndianToHost(val);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include <fstream>
|
||||
|
||||
#include "hasher.hpp"
|
||||
#include "utils/bswap.hpp"
|
||||
#include "utils/endian.hpp"
|
||||
|
||||
/**
|
||||
* Buffer that writes data to file and calculates hash of written data.
|
||||
@ -54,9 +54,8 @@ class HashedFileWriter {
|
||||
*/
|
||||
template <typename TValue>
|
||||
void WriteValue(const TValue &val, bool hash = true) {
|
||||
TValue val_bswapped = utils::Bswap(val);
|
||||
Write(reinterpret_cast<const uint8_t *>(&val_bswapped), sizeof(TValue),
|
||||
hash);
|
||||
TValue val_big = utils::HostToBigEndian(val);
|
||||
Write(reinterpret_cast<const uint8_t *>(&val_big), sizeof(TValue), hash);
|
||||
}
|
||||
|
||||
// TODO try to remove before diff
|
||||
|
@ -1,43 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <byteswap.h>
|
||||
|
||||
namespace utils {
|
||||
|
||||
template <class T>
|
||||
inline T Bswap(T value);
|
||||
|
||||
template <>
|
||||
inline int16_t Bswap<int16_t>(int16_t value) {
|
||||
return __bswap_16(value);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline uint16_t Bswap<uint16_t>(uint16_t value) {
|
||||
return __bswap_16(value);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline int32_t Bswap<int32_t>(int32_t value) {
|
||||
return __bswap_32(value);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline uint32_t Bswap<uint32_t>(uint32_t value) {
|
||||
return __bswap_32(value);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline int64_t Bswap<int64_t>(int64_t value) {
|
||||
return __bswap_64(value);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline uint64_t Bswap<uint64_t>(uint64_t value) {
|
||||
return __bswap_64(value);
|
||||
}
|
||||
|
||||
} // namespace utils
|
61
src/utils/endian.hpp
Normal file
61
src/utils/endian.hpp
Normal file
@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <endian.h>
|
||||
|
||||
#include "utils/cast.hpp"
|
||||
|
||||
namespace utils {
|
||||
|
||||
inline uint16_t HostToLittleEndian(uint16_t value) { return htole16(value); }
|
||||
inline uint32_t HostToLittleEndian(uint32_t value) { return htole32(value); }
|
||||
inline uint64_t HostToLittleEndian(uint64_t value) { return htole64(value); }
|
||||
inline int16_t HostToLittleEndian(int16_t value) {
|
||||
return MemcpyCast<int16_t>(htole16(MemcpyCast<uint16_t>(value)));
|
||||
}
|
||||
inline int32_t HostToLittleEndian(int32_t value) {
|
||||
return MemcpyCast<int32_t>(htole32(MemcpyCast<uint32_t>(value)));
|
||||
}
|
||||
inline int64_t HostToLittleEndian(int64_t value) {
|
||||
return MemcpyCast<int64_t>(htole64(MemcpyCast<uint64_t>(value)));
|
||||
}
|
||||
|
||||
inline uint16_t LittleEndianToHost(uint16_t value) { return le16toh(value); }
|
||||
inline uint32_t LittleEndianToHost(uint32_t value) { return le32toh(value); }
|
||||
inline uint64_t LittleEndianToHost(uint64_t value) { return le64toh(value); }
|
||||
inline int16_t LittleEndianToHost(int16_t value) {
|
||||
return MemcpyCast<int16_t>(le16toh(MemcpyCast<uint16_t>(value)));
|
||||
}
|
||||
inline int32_t LittleEndianToHost(int32_t value) {
|
||||
return MemcpyCast<int32_t>(le32toh(MemcpyCast<uint32_t>(value)));
|
||||
}
|
||||
inline int64_t LittleEndianToHost(int64_t value) {
|
||||
return MemcpyCast<int64_t>(le64toh(MemcpyCast<uint64_t>(value)));
|
||||
}
|
||||
|
||||
inline uint16_t HostToBigEndian(uint16_t value) { return htobe16(value); }
|
||||
inline uint32_t HostToBigEndian(uint32_t value) { return htobe32(value); }
|
||||
inline uint64_t HostToBigEndian(uint64_t value) { return htobe64(value); }
|
||||
inline int16_t HostToBigEndian(int16_t value) {
|
||||
return MemcpyCast<int16_t>(htobe16(MemcpyCast<uint16_t>(value)));
|
||||
}
|
||||
inline int32_t HostToBigEndian(int32_t value) {
|
||||
return MemcpyCast<int32_t>(htobe32(MemcpyCast<uint32_t>(value)));
|
||||
}
|
||||
inline int64_t HostToBigEndian(int64_t value) {
|
||||
return MemcpyCast<int64_t>(htobe64(MemcpyCast<uint64_t>(value)));
|
||||
}
|
||||
|
||||
inline uint16_t BigEndianToHost(uint16_t value) { return be16toh(value); }
|
||||
inline uint32_t BigEndianToHost(uint32_t value) { return be32toh(value); }
|
||||
inline uint64_t BigEndianToHost(uint64_t value) { return be64toh(value); }
|
||||
inline int16_t BigEndianToHost(int16_t value) {
|
||||
return MemcpyCast<int16_t>(be16toh(MemcpyCast<uint16_t>(value)));
|
||||
}
|
||||
inline int32_t BigEndianToHost(int32_t value) {
|
||||
return MemcpyCast<int32_t>(be32toh(MemcpyCast<uint32_t>(value)));
|
||||
}
|
||||
inline int64_t BigEndianToHost(int64_t value) {
|
||||
return MemcpyCast<int64_t>(be64toh(MemcpyCast<uint64_t>(value)));
|
||||
}
|
||||
|
||||
} // namespace utils
|
@ -91,7 +91,7 @@ const uint8_t ignored_resp[] = {0x00, 0x02, 0xb0, 0x7e, 0x00, 0x00};
|
||||
|
||||
// Write bolt chunk header (length)
|
||||
void WriteChunkHeader(TestInputStream &input_stream, uint16_t len) {
|
||||
len = utils::Bswap(len);
|
||||
len = utils::HostToBigEndian(len);
|
||||
input_stream.Write(reinterpret_cast<uint8_t *>(&len), sizeof(len));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user