Skip to content

Commit

Permalink
fix byteutils
Browse files Browse the repository at this point in the history
  • Loading branch information
omaraflak committed Aug 29, 2023
1 parent 9bc2eb3 commit fe91169
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 56 deletions.
74 changes: 24 additions & 50 deletions src/lib/byteutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ void byteutils::push_long(std::vector<uint8_t>& stack, const int64_t& value) {
stack.push_back(BYTE_7(value));
}

void byteutils::push_ushort(std::vector<uint8_t>& stack, const uint16_t& value) {
stack.push_back(BYTE_0(value));
stack.push_back(BYTE_1(value));
}

void byteutils::push_uint(std::vector<uint8_t>& stack, const uint32_t& value) {
stack.push_back(BYTE_0(value));
stack.push_back(BYTE_1(value));
stack.push_back(BYTE_2(value));
stack.push_back(BYTE_3(value));
}

void byteutils::push_ulong(std::vector<uint8_t>& stack, const uint64_t& value) {
stack.push_back(BYTE_0(value));
stack.push_back(BYTE_1(value));
Expand All @@ -46,52 +34,38 @@ void byteutils::push_ulong(std::vector<uint8_t>& stack, const uint64_t& value) {
stack.push_back(BYTE_7(value));
}

namespace byteutils {
int16_t read_short(const uint8_t* stack, const uint64_t& index) {
return ((int16_t) stack[index + 1] << 8) | stack[index];
}

int32_t read_int(const uint8_t* stack, const uint64_t& index) {
return ((int32_t) read_short(stack, index + 2) << 16) | read_short(stack, index);
}

int64_t read_long(const uint8_t* stack, const uint64_t& index) {
return ((int64_t) read_int(stack, index + 4) << 32) | read_int(stack, index);
}

uint16_t read_ushort(const uint8_t* stack, const uint64_t& index) {
return ((uint16_t) stack[index + 1] << 8) | stack[index];
}

uint32_t read_uint(const uint8_t* stack, const uint64_t& index) {
return ((uint32_t) read_ushort(stack, index + 2) << 16) | read_ushort(stack, index);
}

uint64_t read_ulong(const uint8_t* stack, const uint64_t& index) {
return ((uint64_t) read_uint(stack, index + 4) << 32) | read_uint(stack, index);
}
}

int16_t byteutils::read_short(const std::vector<uint8_t>& stack, const uint64_t& index) {
return byteutils::read_short(stack.data(), index);
return ((int16_t) stack[index + 1] << 8) | stack[index + 0];
}

int32_t byteutils::read_int(const std::vector<uint8_t>& stack, const uint64_t& index) {
return byteutils::read_int(stack.data(), index);
return
((int32_t) stack[index + 3] << 24) |
((int32_t) stack[index + 2] << 16) |
((int32_t) stack[index + 1] << 8) |
stack[index + 0];
}

int64_t byteutils::read_long(const std::vector<uint8_t>& stack, const uint64_t& index) {
return byteutils::read_long(stack.data(), index);
}

uint16_t read_ushort(const std::vector<uint8_t>& stack, const uint64_t& index) {
return byteutils::read_ushort(stack.data(), index);
}

uint32_t read_uint(const std::vector<uint8_t>& stack, const uint64_t& index) {
return byteutils::read_uint(stack.data(), index);
return
((int64_t) stack[index + 7] << 56) |
((int64_t) stack[index + 6] << 48) |
((int64_t) stack[index + 5] << 40) |
((int64_t) stack[index + 4] << 32) |
((int64_t) stack[index + 3] << 24) |
((int64_t) stack[index + 2] << 16) |
((int64_t) stack[index + 1] << 8) |
stack[index + 0];
}

uint64_t byteutils::read_ulong(const std::vector<uint8_t>& stack, const uint64_t& index) {
return byteutils::read_ulong(stack.data(), index);
return
((uint64_t) stack[index + 7] << 56) |
((uint64_t) stack[index + 6] << 48) |
((uint64_t) stack[index + 5] << 40) |
((uint64_t) stack[index + 4] << 32) |
((uint64_t) stack[index + 3] << 24) |
((uint64_t) stack[index + 2] << 16) |
((uint64_t) stack[index + 1] << 8) |
stack[index + 0];
}
6 changes: 0 additions & 6 deletions src/lib/byteutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,11 @@ namespace byteutils {
void push_short(std::vector<uint8_t>& stack, const int16_t& value);
void push_int(std::vector<uint8_t>& stack, const int32_t& value);
void push_long(std::vector<uint8_t>& stack, const int64_t& value);

void push_ushort(std::vector<uint8_t>& stack, const uint16_t& value);
void push_uint(std::vector<uint8_t>& stack, const uint32_t& value);
void push_ulong(std::vector<uint8_t>& stack, const uint64_t& value);

int16_t read_short(const std::vector<uint8_t>& stack, const uint64_t& index);
int32_t read_int(const std::vector<uint8_t>& stack, const uint64_t& index);
int64_t read_long(const std::vector<uint8_t>& stack, const uint64_t& index);

uint16_t read_ushort(const std::vector<uint8_t>& stack, const uint64_t& index);
uint32_t read_uint(const std::vector<uint8_t>& stack, const uint64_t& index);
uint64_t read_ulong(const std::vector<uint8_t>& stack, const uint64_t& index);
}

Expand Down

0 comments on commit fe91169

Please sign in to comment.