6
6
#include " enforce.hpp"
7
7
#include " futils.hpp"
8
8
#include " i18n.h" // for _exvGettext
9
+ #include " image.hpp"
9
10
#include " utils.hpp"
10
11
11
12
// + standard includes
@@ -333,44 +334,21 @@ double getDouble(const byte* buf, ByteOrder byteOrder) {
333
334
}
334
335
335
336
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);
344
340
}
345
341
346
342
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);
359
346
}
360
347
361
348
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);
374
352
}
375
353
376
354
size_t ur2Data (byte* buf, URational l, ByteOrder byteOrder) {
@@ -380,29 +358,15 @@ size_t ur2Data(byte* buf, URational l, ByteOrder byteOrder) {
380
358
}
381
359
382
360
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);
391
364
}
392
365
393
366
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);
406
370
}
407
371
408
372
size_t r2Data (byte* buf, Rational l, ByteOrder byteOrder) {
0 commit comments