Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit 990a176

Browse files
committed
Common: Fix read/write 64bits
Signed-off-by: Quentin Guidée <[email protected]>
1 parent eb3180d commit 990a176

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/common/stream.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ class InputStream
7070
T read(uint8_t n_bytes)
7171
{
7272
T value = 0;
73-
for (int8_t offset = (n_bytes - 1) * BYTE; offset >= 0; offset -= BYTE)
74-
value |= read_u8() << offset;
73+
for (int16_t offset = (n_bytes - 1) * BYTE; offset >= 0; offset -= BYTE)
74+
value |= (T)read_u8() << offset;
7575
return value;
7676
}
7777

7878
template <typename T>
7979
T read_le(uint8_t n_bytes)
8080
{
8181
T value = 0;
82-
for (uint8_t offset = 0; offset <= (n_bytes - 1) * BYTE; offset += BYTE)
83-
value |= read_u8() << offset;
82+
for (uint16_t offset = 0; offset <= (n_bytes - 1) * BYTE; offset += BYTE)
83+
value |= (T)read_u8() << offset;
8484
return value;
8585
}
8686
};
@@ -111,14 +111,14 @@ class OutputStream
111111
template <typename T>
112112
void write(uint8_t n_bytes, T value)
113113
{
114-
for (int8_t offset = (n_bytes - 1) * BYTE; offset >= 0; offset -= BYTE)
114+
for (int16_t offset = (n_bytes - 1) * BYTE; offset >= 0; offset -= BYTE)
115115
write_u8(value >> offset);
116116
}
117117

118118
template <typename T>
119119
void write_le(uint8_t n_bytes, T value)
120120
{
121-
for (uint8_t offset = 0; offset <= (n_bytes - 1) * BYTE; offset += BYTE)
121+
for (uint16_t offset = 0; offset <= (n_bytes - 1) * BYTE; offset += BYTE)
122122
write_u8(value >> offset);
123123
}
124124
};

tests/common_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ static void test_write(OutputStream& out)
2222
out.write_i24(0xABCDEF);
2323
out.write_u32(0x12345678);
2424
out.write_u32_le(0x12345678);
25+
out.write<uint64_t>(8, 0xFEDCBA9876543210);
2526
}
2627
static void test_read(InputStream& in)
2728
{
28-
EXPECT_EQ(in.size(), 36);
29+
EXPECT_EQ(in.size(), 44);
2930

3031
EXPECT_EQ(in.peek_u8(), 0);
3132
EXPECT_EQ(in.read_u8(), 0);
@@ -57,6 +58,7 @@ static void test_read(InputStream& in)
5758
EXPECT_EQ(in.read_i32(), 0x12345678);
5859
EXPECT_EQ(in.peek_u32(), 0x78563412);
5960
EXPECT_EQ(in.read_u32(), 0x78563412);
61+
EXPECT_EQ(in.read<uint64_t>(8), 0xFEDCBA9876543210);
6062
}
6163

6264
TEST(CommonTest, BufferStream)

0 commit comments

Comments
 (0)