Skip to content

Commit c1c60eb

Browse files
authored
Use fixed width types (#1258)
Use fixed width types in `byteSwap` functions
1 parent 3bc0ea0 commit c1c60eb

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

taglib/toolkit/tbytevector.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -282,47 +282,47 @@ ByteVector ByteVector::fromCString(const char *s, unsigned int length)
282282

283283
ByteVector ByteVector::fromUInt(unsigned int value, bool mostSignificantByteFirst)
284284
{
285-
return fromNumber<unsigned int>(value, mostSignificantByteFirst);
285+
return fromNumber<uint32_t>(value, mostSignificantByteFirst);
286286
}
287287

288288
ByteVector ByteVector::fromShort(short value, bool mostSignificantByteFirst)
289289
{
290-
return fromNumber<unsigned short>(value, mostSignificantByteFirst);
290+
return fromNumber<uint16_t>(value, mostSignificantByteFirst);
291291
}
292292

293293
ByteVector ByteVector::fromUShort(unsigned short value, bool mostSignificantByteFirst)
294294
{
295-
return fromNumber<unsigned short>(value, mostSignificantByteFirst);
295+
return fromNumber<uint16_t>(value, mostSignificantByteFirst);
296296
}
297297

298298
ByteVector ByteVector::fromLongLong(long long value, bool mostSignificantByteFirst)
299299
{
300-
return fromNumber<unsigned long long>(value, mostSignificantByteFirst);
300+
return fromNumber<uint64_t>(value, mostSignificantByteFirst);
301301
}
302302

303303
ByteVector ByteVector::fromULongLong(unsigned long long value, bool mostSignificantByteFirst)
304304
{
305-
return fromNumber<unsigned long long>(value, mostSignificantByteFirst);
305+
return fromNumber<uint64_t>(value, mostSignificantByteFirst);
306306
}
307307

308308
ByteVector ByteVector::fromFloat32LE(float value)
309309
{
310-
return fromFloat<float, unsigned int, Utils::LittleEndian>(value);
310+
return fromFloat<float, uint32_t, Utils::LittleEndian>(value);
311311
}
312312

313313
ByteVector ByteVector::fromFloat32BE(float value)
314314
{
315-
return fromFloat<float, unsigned int, Utils::BigEndian>(value);
315+
return fromFloat<float, uint32_t, Utils::BigEndian>(value);
316316
}
317317

318318
ByteVector ByteVector::fromFloat64LE(double value)
319319
{
320-
return fromFloat<double, unsigned long long, Utils::LittleEndian>(value);
320+
return fromFloat<double, uint64_t, Utils::LittleEndian>(value);
321321
}
322322

323323
ByteVector ByteVector::fromFloat64BE(double value)
324324
{
325-
return fromFloat<double, unsigned long long, Utils::BigEndian>(value);
325+
return fromFloat<double, uint64_t, Utils::BigEndian>(value);
326326
}
327327

328328
////////////////////////////////////////////////////////////////////////////////
@@ -656,79 +656,86 @@ bool ByteVector::isEmpty() const
656656
return d->length == 0;
657657
}
658658

659+
// Sanity checks
660+
static_assert(sizeof(unsigned short) == sizeof(uint16_t), "unsigned short and uint16_t are different sizes");
661+
static_assert(sizeof(unsigned int) == sizeof(uint32_t), "unsigned int and uint32_t are different sizes");
662+
static_assert(sizeof(unsigned long long) == sizeof(uint64_t), "unsigned long long and uint64_t are different sizes");
663+
static_assert(sizeof(float) == sizeof(uint32_t), "float and uint32_t are different sizes");
664+
static_assert(sizeof(double) == sizeof(uint64_t), "double and uint64_t are different sizes");
665+
659666
unsigned int ByteVector::toUInt(bool mostSignificantByteFirst) const
660667
{
661-
return toNumber<unsigned int>(*this, 0, mostSignificantByteFirst);
668+
return toNumber<uint32_t>(*this, 0, mostSignificantByteFirst);
662669
}
663670

664671
unsigned int ByteVector::toUInt(unsigned int offset, bool mostSignificantByteFirst) const
665672
{
666-
return toNumber<unsigned int>(*this, offset, mostSignificantByteFirst);
673+
return toNumber<uint32_t>(*this, offset, mostSignificantByteFirst);
667674
}
668675

669676
unsigned int ByteVector::toUInt(unsigned int offset, unsigned int length, bool mostSignificantByteFirst) const
670677
{
671-
return toNumber<unsigned int>(*this, offset, length, mostSignificantByteFirst);
678+
return toNumber<uint32_t>(*this, offset, length, mostSignificantByteFirst);
672679
}
673680

674681
short ByteVector::toShort(bool mostSignificantByteFirst) const
675682
{
676-
return toNumber<unsigned short>(*this, 0, mostSignificantByteFirst);
683+
return toNumber<uint16_t>(*this, 0, mostSignificantByteFirst);
677684
}
678685

679686
short ByteVector::toShort(unsigned int offset, bool mostSignificantByteFirst) const
680687
{
681-
return toNumber<unsigned short>(*this, offset, mostSignificantByteFirst);
688+
return toNumber<uint16_t>(*this, offset, mostSignificantByteFirst);
682689
}
683690

684691
unsigned short ByteVector::toUShort(bool mostSignificantByteFirst) const
685692
{
686-
return toNumber<unsigned short>(*this, 0, mostSignificantByteFirst);
693+
return toNumber<uint16_t>(*this, 0, mostSignificantByteFirst);
687694
}
688695

689696
unsigned short ByteVector::toUShort(unsigned int offset, bool mostSignificantByteFirst) const
690697
{
691-
return toNumber<unsigned short>(*this, offset, mostSignificantByteFirst);
698+
return toNumber<uint16_t>(*this, offset, mostSignificantByteFirst);
692699
}
693700

694701
long long ByteVector::toLongLong(bool mostSignificantByteFirst) const
695702
{
696-
return toNumber<unsigned long long>(*this, 0, mostSignificantByteFirst);
703+
return toNumber<uint64_t>(*this, 0, mostSignificantByteFirst);
697704
}
698705

699706
long long ByteVector::toLongLong(unsigned int offset, bool mostSignificantByteFirst) const
700707
{
701-
return toNumber<unsigned long long>(*this, offset, mostSignificantByteFirst);
708+
return toNumber<uint64_t>(*this, offset, mostSignificantByteFirst);
702709
}
703710

704711
unsigned long long ByteVector::toULongLong(bool mostSignificantByteFirst) const
705712
{
706-
return toNumber<unsigned long long>(*this, 0, mostSignificantByteFirst);
713+
return toNumber<uint64_t>(*this, 0, mostSignificantByteFirst);
707714
}
708715

709716
unsigned long long ByteVector::toULongLong(unsigned int offset, bool mostSignificantByteFirst) const
710717
{
711-
return toNumber<unsigned long long>(*this, offset, mostSignificantByteFirst);
718+
return toNumber<uint64_t>(*this, offset, mostSignificantByteFirst);
712719
}
713720

714721
float ByteVector::toFloat32LE(size_t offset) const
715722
{
716-
return toFloat<float, unsigned int, Utils::LittleEndian>(*this, offset);
723+
return toFloat<float, uint32_t, Utils::LittleEndian>(*this, offset);
717724
}
718725

719726
float ByteVector::toFloat32BE(size_t offset) const
720727
{
721-
return toFloat<float, unsigned int, Utils::BigEndian>(*this, offset);
728+
return toFloat<float, uint32_t, Utils::BigEndian>(*this, offset);
722729
}
723730

724731
double ByteVector::toFloat64LE(size_t offset) const
725732
{
726-
return toFloat<double, unsigned long long, Utils::LittleEndian>(*this, offset);
733+
return toFloat<double, uint64_t, Utils::LittleEndian>(*this, offset);
727734
}
728735

729736
double ByteVector::toFloat64BE(size_t offset) const
730737
{
731-
return toFloat<double, unsigned long long, Utils::BigEndian>(*this, offset);
738+
return toFloat<double, uint64_t, Utils::BigEndian>(*this, offset);
732739
}
733740

734741
long double ByteVector::toFloat80LE(size_t offset) const

taglib/toolkit/tutils.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#ifndef DO_NOT_DOCUMENT // tell Doxygen not to document this header
3232

33+
#include <cstdint>
3334
#include <cstdio>
3435
#include <cstdarg>
3536
#include <cstring>
@@ -60,7 +61,7 @@ namespace TagLib
6061
/*!
6162
* Reverses the order of bytes in a 16-bit integer.
6263
*/
63-
inline unsigned short byteSwap(unsigned short x)
64+
inline uint16_t byteSwap(uint16_t x)
6465
{
6566
#if defined(HAVE_GCC_BYTESWAP)
6667

@@ -92,7 +93,7 @@ namespace TagLib
9293
/*!
9394
* Reverses the order of bytes in a 32-bit integer.
9495
*/
95-
inline unsigned int byteSwap(unsigned int x)
96+
inline uint32_t byteSwap(uint32_t x)
9697
{
9798
#if defined(HAVE_GCC_BYTESWAP)
9899

@@ -127,7 +128,7 @@ namespace TagLib
127128
/*!
128129
* Reverses the order of bytes in a 64-bit integer.
129130
*/
130-
inline unsigned long long byteSwap(unsigned long long x)
131+
inline uint64_t byteSwap(uint64_t x)
131132
{
132133
#if defined(HAVE_GCC_BYTESWAP)
133134

0 commit comments

Comments
 (0)