NBT: add write helpers for int32 and int64
parent
6d62d995df
commit
8c7be385ae
|
@ -208,10 +208,58 @@ namespace NBT {
|
||||||
delete value;
|
delete value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//FIXME: endian dependent implementation
|
||||||
void writeInt32(std::vector<uint8_t>* destination, int32_t data) {
|
void writeInt32(std::vector<uint8_t>* destination, int32_t data) {
|
||||||
|
int32_t* value = new int32_t;
|
||||||
|
uint8_t* valueAsBytes = reinterpret_cast<uint8_t*>(value);
|
||||||
|
*value = data;
|
||||||
|
#ifdef FOSSVG_BIG_ENDIAN
|
||||||
|
destination->push_back(*valueAsBytes);
|
||||||
|
destination->push_back(*(valueAsBytes+1));
|
||||||
|
destination->push_back(*(valueAsBytes+2));
|
||||||
|
destination->push_back(*(valueAsBytes+3));
|
||||||
|
#else
|
||||||
|
#ifdef FOSSVG_LITTLE_ENDIAN
|
||||||
|
destination->push_back(*(valueAsBytes+3));
|
||||||
|
destination->push_back(*(valueAsBytes+2));
|
||||||
|
destination->push_back(*(valueAsBytes+1));
|
||||||
|
destination->push_back(*valueAsBytes);
|
||||||
|
#else
|
||||||
|
#error "NBT::helper::writeInt16: An implementation for your endianness is unavailable."
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
delete value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//FIXME: endian dependent implementation
|
||||||
void writeInt64(std::vector<uint8_t>* destination, int64_t data) {
|
void writeInt64(std::vector<uint8_t>* destination, int64_t data) {
|
||||||
|
int64_t* value = new int64_t;
|
||||||
|
uint8_t* valueAsBytes = reinterpret_cast<uint8_t*>(value);
|
||||||
|
*value = data;
|
||||||
|
#ifdef FOSSVG_BIG_ENDIAN
|
||||||
|
destination->push_back(*valueAsBytes);
|
||||||
|
destination->push_back(*(valueAsBytes+1));
|
||||||
|
destination->push_back(*(valueAsBytes+2));
|
||||||
|
destination->push_back(*(valueAsBytes+3));
|
||||||
|
destination->push_back(*(valueAsBytes+4));
|
||||||
|
destination->push_back(*(valueAsBytes+5));
|
||||||
|
destination->push_back(*(valueAsBytes+6));
|
||||||
|
destination->push_back(*(valueAsBytes+7));
|
||||||
|
#else
|
||||||
|
#ifdef FOSSVG_LITTLE_ENDIAN
|
||||||
|
destination->push_back(*(valueAsBytes+7));
|
||||||
|
destination->push_back(*(valueAsBytes+6));
|
||||||
|
destination->push_back(*(valueAsBytes+5));
|
||||||
|
destination->push_back(*(valueAsBytes+4));
|
||||||
|
destination->push_back(*(valueAsBytes+3));
|
||||||
|
destination->push_back(*(valueAsBytes+2));
|
||||||
|
destination->push_back(*(valueAsBytes+1));
|
||||||
|
destination->push_back(*valueAsBytes);
|
||||||
|
#else
|
||||||
|
#error "NBT::helper::writeInt16: An implementation for your endianness is unavailable."
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
delete value;
|
||||||
}
|
}
|
||||||
|
|
||||||
//FIXME: we just assume that float is a single-precision IEEE754
|
//FIXME: we just assume that float is a single-precision IEEE754
|
||||||
|
|
|
@ -86,7 +86,7 @@ int main(){
|
||||||
delete writeInt16TestResult;
|
delete writeInt16TestResult;
|
||||||
ASSERT(dereferencedWriteInt16TestResult[0] == (uint8_t) 0xAB && dereferencedWriteInt16TestResult[1] == (uint8_t) 0xCD);
|
ASSERT(dereferencedWriteInt16TestResult[0] == (uint8_t) 0xAB && dereferencedWriteInt16TestResult[1] == (uint8_t) 0xCD);
|
||||||
|
|
||||||
std::cout << "Passed writeInt8 NBT helper test" << std::endl;
|
std::cout << "Passed writeInt16 NBT helper test" << std::endl;
|
||||||
|
|
||||||
// int32 ###########################################################
|
// int32 ###########################################################
|
||||||
// read successfully
|
// read successfully
|
||||||
|
@ -112,6 +112,19 @@ int main(){
|
||||||
|
|
||||||
std::cout << "Passed readInt32 NBT helper test" << std::endl;
|
std::cout << "Passed readInt32 NBT helper test" << std::endl;
|
||||||
|
|
||||||
|
std::vector<uint8_t>* writeInt32TestResult = new std::vector<uint8_t>();
|
||||||
|
NBT::helper::writeInt32(writeInt32TestResult, (int32_t) 0x12345678);
|
||||||
|
std::vector<uint8_t> dereferencedWriteInt32TestResult = *writeInt32TestResult;
|
||||||
|
delete writeInt32TestResult;
|
||||||
|
ASSERT(
|
||||||
|
dereferencedWriteInt32TestResult[0] == (uint8_t) 0x12 &&
|
||||||
|
dereferencedWriteInt32TestResult[1] == (uint8_t) 0x34 &&
|
||||||
|
dereferencedWriteInt32TestResult[2] == (uint8_t) 0x56 &&
|
||||||
|
dereferencedWriteInt32TestResult[3] == (uint8_t) 0x78
|
||||||
|
);
|
||||||
|
|
||||||
|
std::cout << "Passed writeInt32 NBT helper test" << std::endl;
|
||||||
|
|
||||||
// int64 ###########################################################
|
// int64 ###########################################################
|
||||||
// read successfully
|
// read successfully
|
||||||
currentPosition = 1;
|
currentPosition = 1;
|
||||||
|
@ -134,7 +147,24 @@ int main(){
|
||||||
ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).isError == true);
|
ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).isError == true);
|
||||||
ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR);
|
ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR);
|
||||||
|
|
||||||
std::cout << "Passed int64 NBT helper test" << std::endl;
|
std::cout << "Passed readInt64 NBT helper test" << std::endl;
|
||||||
|
|
||||||
|
std::vector<uint8_t>* writeInt64TestResult = new std::vector<uint8_t>();
|
||||||
|
NBT::helper::writeInt64(writeInt64TestResult, (int64_t) 0x0123456789ABCDEF);
|
||||||
|
std::vector<uint8_t> dereferencedWriteInt64TestResult = *writeInt64TestResult;
|
||||||
|
delete writeInt64TestResult;
|
||||||
|
ASSERT(
|
||||||
|
dereferencedWriteInt64TestResult[0] == (uint8_t) 0x01 &&
|
||||||
|
dereferencedWriteInt64TestResult[1] == (uint8_t) 0x23 &&
|
||||||
|
dereferencedWriteInt64TestResult[2] == (uint8_t) 0x45 &&
|
||||||
|
dereferencedWriteInt64TestResult[3] == (uint8_t) 0x67 &&
|
||||||
|
dereferencedWriteInt64TestResult[4] == (uint8_t) 0x89 &&
|
||||||
|
dereferencedWriteInt64TestResult[5] == (uint8_t) 0xAB &&
|
||||||
|
dereferencedWriteInt64TestResult[6] == (uint8_t) 0xCD &&
|
||||||
|
dereferencedWriteInt64TestResult[7] == (uint8_t) 0xEF
|
||||||
|
);
|
||||||
|
|
||||||
|
std::cout << "Passed writeInt32 NBT helper test" << std::endl;
|
||||||
|
|
||||||
//##################################################################
|
//##################################################################
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue