Skip to content

Commit e707719

Browse files
committed
use more byteSwap
Signed-off-by: Rosen Penev <[email protected]>
1 parent f22d23b commit e707719

File tree

1 file changed

+16
-52
lines changed

1 file changed

+16
-52
lines changed

src/types.cpp

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "enforce.hpp"
77
#include "futils.hpp"
88
#include "i18n.h" // for _exvGettext
9+
#include "image.hpp"
910
#include "utils.hpp"
1011

1112
// + standard includes
@@ -333,44 +334,21 @@ double getDouble(const byte* buf, ByteOrder byteOrder) {
333334
}
334335

335336
size_t us2Data(byte* buf, uint16_t s, ByteOrder byteOrder) {
336-
if (byteOrder == littleEndian) {
337-
buf[0] = static_cast<byte>(s & 0x00ffU);
338-
buf[1] = static_cast<byte>((s & 0xff00U) >> 8);
339-
} else {
340-
buf[0] = static_cast<byte>((s & 0xff00U) >> 8);
341-
buf[1] = static_cast<byte>(s & 0x00ffU);
342-
}
343-
return 2;
337+
s = Image::byteSwap(s, byteOrder == bigEndian);
338+
std::memcpy(buf, &s, sizeof(s));
339+
return sizeof(s);
344340
}
345341

346342
size_t ul2Data(byte* buf, uint32_t l, ByteOrder byteOrder) {
347-
if (byteOrder == littleEndian) {
348-
buf[0] = static_cast<byte>(l & 0x000000ffU);
349-
buf[1] = static_cast<byte>((l & 0x0000ff00U) >> 8);
350-
buf[2] = static_cast<byte>((l & 0x00ff0000U) >> 16);
351-
buf[3] = static_cast<byte>((l & 0xff000000U) >> 24);
352-
} else {
353-
buf[0] = static_cast<byte>((l & 0xff000000U) >> 24);
354-
buf[1] = static_cast<byte>((l & 0x00ff0000U) >> 16);
355-
buf[2] = static_cast<byte>((l & 0x0000ff00U) >> 8);
356-
buf[3] = static_cast<byte>(l & 0x000000ffU);
357-
}
358-
return 4;
343+
l = Image::byteSwap(l, byteOrder == bigEndian);
344+
std::memcpy(buf, &l, sizeof(l));
345+
return sizeof(l);
359346
}
360347

361348
size_t ull2Data(byte* buf, uint64_t l, ByteOrder byteOrder) {
362-
if (byteOrder == littleEndian) {
363-
for (size_t i = 0; i < 8; i++) {
364-
buf[i] = static_cast<byte>(l & 0xff);
365-
l >>= 8;
366-
}
367-
} else {
368-
for (size_t i = 0; i < 8; i++) {
369-
buf[8 - i - 1] = static_cast<byte>(l & 0xff);
370-
l >>= 8;
371-
}
372-
}
373-
return 8;
349+
l = Image::byteSwap(l, byteOrder == bigEndian);
350+
std::memcpy(buf, &l, sizeof(l));
351+
return sizeof(l);
374352
}
375353

376354
size_t ur2Data(byte* buf, URational l, ByteOrder byteOrder) {
@@ -380,29 +358,15 @@ size_t ur2Data(byte* buf, URational l, ByteOrder byteOrder) {
380358
}
381359

382360
size_t s2Data(byte* buf, int16_t s, ByteOrder byteOrder) {
383-
if (byteOrder == littleEndian) {
384-
buf[0] = static_cast<byte>(s & 0x00ffU);
385-
buf[1] = static_cast<byte>((s & 0xff00U) >> 8);
386-
} else {
387-
buf[0] = static_cast<byte>((s & 0xff00U) >> 8);
388-
buf[1] = static_cast<byte>(s & 0x00ffU);
389-
}
390-
return 2;
361+
s = Image::byteSwap(static_cast<uint16_t>(s), byteOrder == bigEndian);
362+
std::memcpy(buf, &s, sizeof(s));
363+
return sizeof(s);
391364
}
392365

393366
size_t l2Data(byte* buf, int32_t l, ByteOrder byteOrder) {
394-
if (byteOrder == littleEndian) {
395-
buf[0] = static_cast<byte>(l & 0x000000ffU);
396-
buf[1] = static_cast<byte>((l & 0x0000ff00U) >> 8);
397-
buf[2] = static_cast<byte>((l & 0x00ff0000U) >> 16);
398-
buf[3] = static_cast<byte>((l & 0xff000000U) >> 24);
399-
} else {
400-
buf[0] = static_cast<byte>((l & 0xff000000U) >> 24);
401-
buf[1] = static_cast<byte>((l & 0x00ff0000U) >> 16);
402-
buf[2] = static_cast<byte>((l & 0x0000ff00U) >> 8);
403-
buf[3] = static_cast<byte>(l & 0x000000ffU);
404-
}
405-
return 4;
367+
l = Image::byteSwap(static_cast<uint32_t>(l), byteOrder == bigEndian);
368+
std::memcpy(buf, &l, sizeof(l));
369+
return sizeof(l);
406370
}
407371

408372
size_t r2Data(byte* buf, Rational l, ByteOrder byteOrder) {

0 commit comments

Comments
 (0)