Skip to content

Commit e435a29

Browse files
authored
fix swap functions correctness (#3)
1 parent a2b9f13 commit e435a29

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

include/nbytes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ void ForceAscii(const char *src, char *dst, size_t len);
9393

9494
// Swaps bytes in place. nbytes is the number of bytes to swap and must be a
9595
// multiple of the word size (checked by function).
96-
bool SwapBytes16(void *data, size_t nbytes);
97-
bool SwapBytes32(void *data, size_t nbytes);
98-
bool SwapBytes64(void *data, size_t nbytes);
96+
bool SwapBytes16(char *data, size_t nbytes);
97+
bool SwapBytes32(char *data, size_t nbytes);
98+
bool SwapBytes64(char *data, size_t nbytes);
9999

100100
// ============================================================================
101101
// Base64 (legacy)

src/nbytes.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace {
4343
#endif
4444
} // namespace
4545

46-
bool SwapBytes16(void *data, size_t nbytes) {
46+
bool SwapBytes16(char *data, size_t nbytes) {
4747
if (nbytes % sizeof(uint16_t) != 0) return false;
4848

4949
#if defined(_MSC_VER)
@@ -59,17 +59,16 @@ bool SwapBytes16(void *data, size_t nbytes) {
5959
#endif
6060

6161
uint16_t temp;
62-
uint8_t *ptr = reinterpret_cast<uint8_t *>(data);
63-
for (size_t i = 0; i < nbytes; i += sizeof(uint16_t)) {
64-
memcpy(&temp, &ptr[i], sizeof(uint16_t));
62+
for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
63+
memcpy(&temp, &data[i], sizeof(temp));
6564
temp = BSWAP_2(temp);
66-
memcpy(&ptr[i], &temp, sizeof(uint16_t));
65+
memcpy(&data[i], &temp, sizeof(temp));
6766
}
6867

6968
return true;
7069
}
7170

72-
bool SwapBytes32(void *data, size_t nbytes) {
71+
bool SwapBytes32(char *data, size_t nbytes) {
7372
if (nbytes % sizeof(uint32_t) != 0) return false;
7473

7574
#if defined(_MSC_VER)
@@ -84,18 +83,17 @@ bool SwapBytes32(void *data, size_t nbytes) {
8483
}
8584
#endif
8685

87-
uint32_t temp = 0;
88-
uint8_t *ptr = reinterpret_cast<uint8_t *>(data);
89-
for (size_t i = 0; i < nbytes; i += sizeof(uint32_t)) {
90-
memcpy(&temp, &ptr[i], sizeof(uint32_t));
86+
uint32_t temp;
87+
for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
88+
memcpy(&temp, &data[i], sizeof(temp));
9189
temp = BSWAP_4(temp);
92-
memcpy(&ptr[i], &temp, sizeof(uint32_t));
90+
memcpy(&data[i], &temp, sizeof(temp));
9391
}
9492

9593
return true;
9694
}
9795

98-
bool SwapBytes64(void *data, size_t nbytes) {
96+
bool SwapBytes64(char *data, size_t nbytes) {
9997
if (nbytes % sizeof(uint64_t) != 0) return false;
10098

10199
#if defined(_MSC_VER)
@@ -110,12 +108,11 @@ bool SwapBytes64(void *data, size_t nbytes) {
110108
}
111109
#endif
112110

113-
uint64_t temp = 0;
114-
uint8_t *ptr = reinterpret_cast<uint8_t *>(data);
115-
for (size_t i = 0; i < nbytes; i += sizeof(uint64_t)) {
116-
memcpy(&temp, &ptr[i], sizeof(uint64_t));
111+
uint64_t temp;
112+
for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
113+
memcpy(&temp, &data[i], sizeof(temp));
117114
temp = BSWAP_8(temp);
118-
memcpy(&ptr[i], &temp, sizeof(uint64_t));
115+
memcpy(&data[i], &temp, sizeof(temp));
119116
}
120117

121118
return true;

tests/basic.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,26 @@
44

55
#include <gtest/gtest.h>
66

7-
TEST(basic, it_works) { SUCCEED(); }
7+
TEST(basic, swap_bytes16) {
8+
std::vector<char> input = {1, 2, 3, 4, 5, 6, 7, 8};
9+
nbytes::SwapBytes16(input.data(), input.size());
10+
std::vector<char> expected = {2, 1, 4, 3, 6, 5, 8, 7};
11+
EXPECT_EQ(input, expected);
12+
SUCCEED();
13+
}
14+
15+
TEST(basic, swap_bytes32) {
16+
std::vector<char> input = {1, 2, 3, 4, 5, 6, 7, 8};
17+
nbytes::SwapBytes32(input.data(), input.size());
18+
std::vector<char> expected = {4, 3, 2, 1, 8, 7, 6, 5};
19+
EXPECT_EQ(input, expected);
20+
SUCCEED();
21+
}
22+
23+
TEST(basic, swap_bytes64) {
24+
std::vector<char> input = {1, 2, 3, 4, 5, 6, 7, 8};
25+
nbytes::SwapBytes64(input.data(), input.size());
26+
std::vector<char> expected = {8, 7, 6, 5, 4, 3, 2, 1};
27+
EXPECT_EQ(input, expected);
28+
SUCCEED();
29+
}

0 commit comments

Comments
 (0)