Skip to content

Commit ca880df

Browse files
authored
Merge pull request #14106 from rouault/lsb_helpers
port/: add new helpers to read LSB ordered values from memory, as well as VSIVirtualHandle::ReadLSB()/WriteLSB()
2 parents 02cc3e6 + cbefd91 commit ca880df

File tree

9 files changed

+395
-165
lines changed

9 files changed

+395
-165
lines changed

autotest/cpp/test_cpl.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5766,4 +5766,106 @@ TEST_F(test_cpl, cpl_enumerate)
57665766
}
57675767
}
57685768

5769+
TEST_F(test_cpl, CPL_SWAP)
5770+
{
5771+
{
5772+
uint8_t x = 1;
5773+
EXPECT_EQ(CPL_SWAP(x), x);
5774+
EXPECT_EQ(CPL_AS_LSB(x), x);
5775+
}
5776+
{
5777+
int8_t x = 1;
5778+
EXPECT_EQ(CPL_SWAP(x), x);
5779+
EXPECT_EQ(CPL_AS_LSB(x), x);
5780+
}
5781+
{
5782+
uint16_t x = 0x0123;
5783+
auto y = CPL_SWAP(x);
5784+
EXPECT_NE(y, x);
5785+
EXPECT_EQ(CPL_SWAP(y), x);
5786+
#if CPL_IS_LSB
5787+
EXPECT_EQ(CPL_AS_LSB(x), x);
5788+
#else
5789+
EXPECT_EQ(CPL_AS_LSB(x), CPL_SWAP(x));
5790+
#endif
5791+
}
5792+
{
5793+
int16_t x = 0x0123;
5794+
auto y = CPL_SWAP(x);
5795+
EXPECT_NE(y, x);
5796+
EXPECT_EQ(CPL_SWAP(y), x);
5797+
#if CPL_IS_LSB
5798+
EXPECT_EQ(CPL_AS_LSB(x), x);
5799+
#else
5800+
EXPECT_EQ(CPL_AS_LSB(x), CPL_SWAP(x));
5801+
#endif
5802+
}
5803+
{
5804+
uint32_t x = 0x01234567;
5805+
auto y = CPL_SWAP(x);
5806+
EXPECT_NE(y, x);
5807+
EXPECT_EQ(CPL_SWAP(y), x);
5808+
#if CPL_IS_LSB
5809+
EXPECT_EQ(CPL_AS_LSB(x), x);
5810+
#else
5811+
EXPECT_EQ(CPL_AS_LSB(x), CPL_SWAP(x));
5812+
#endif
5813+
}
5814+
{
5815+
int32_t x = 0x01234567;
5816+
auto y = CPL_SWAP(x);
5817+
EXPECT_NE(y, x);
5818+
EXPECT_EQ(CPL_SWAP(y), x);
5819+
#if CPL_IS_LSB
5820+
EXPECT_EQ(CPL_AS_LSB(x), x);
5821+
#else
5822+
EXPECT_EQ(CPL_AS_LSB(x), CPL_SWAP(x));
5823+
#endif
5824+
}
5825+
{
5826+
uint64_t x = (static_cast<uint64_t>(0x01234567) << 32) | 0x89ABCDEF;
5827+
auto y = CPL_SWAP(x);
5828+
EXPECT_NE(y, x);
5829+
EXPECT_EQ(CPL_SWAP(y), x);
5830+
#if CPL_IS_LSB
5831+
EXPECT_EQ(CPL_AS_LSB(x), x);
5832+
#else
5833+
EXPECT_EQ(CPL_AS_LSB(x), CPL_SWAP(x));
5834+
#endif
5835+
}
5836+
{
5837+
int64_t x = (static_cast<int64_t>(0x01234567) << 32) | 0x89ABCDEF;
5838+
auto y = CPL_SWAP(x);
5839+
EXPECT_NE(y, x);
5840+
EXPECT_EQ(CPL_SWAP(y), x);
5841+
#if CPL_IS_LSB
5842+
EXPECT_EQ(CPL_AS_LSB(x), x);
5843+
#else
5844+
EXPECT_EQ(CPL_AS_LSB(x), CPL_SWAP(x));
5845+
#endif
5846+
}
5847+
{
5848+
float x = 1.5;
5849+
auto y = CPL_SWAP(x);
5850+
EXPECT_NE(y, x);
5851+
EXPECT_EQ(CPL_SWAP(y), x);
5852+
#if CPL_IS_LSB
5853+
EXPECT_EQ(CPL_AS_LSB(x), x);
5854+
#else
5855+
EXPECT_EQ(CPL_AS_LSB(x), CPL_SWAP(x));
5856+
#endif
5857+
}
5858+
{
5859+
double x = 1.5;
5860+
auto y = CPL_SWAP(x);
5861+
EXPECT_NE(y, x);
5862+
EXPECT_EQ(CPL_SWAP(y), x);
5863+
#if CPL_IS_LSB
5864+
EXPECT_EQ(CPL_AS_LSB(x), x);
5865+
#else
5866+
EXPECT_EQ(CPL_AS_LSB(x), CPL_SWAP(x));
5867+
#endif
5868+
}
5869+
}
5870+
57695871
} // namespace

doc/source/spelling_wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,7 @@ pathname
23762376
PAux
23772377
pauxdataset
23782378
pbCannotBeExactlyRepresented
2379+
pbError
23792380
pbf
23802381
pbHasNoData
23812382
pbHasNoDataValue

port/cpl_compressor.cpp

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ static bool CPLLZ4Compressor(const void *input_data, size_t input_size,
590590
return false;
591591
}
592592

593-
int32_t sizeLSB = CPL_LSBWORD32(static_cast<int>(input_size));
593+
int32_t sizeLSB = CPL_AS_LSB(static_cast<int32_t>(input_size));
594594
memcpy(*output_data, &sizeLSB, sizeof(sizeLSB));
595595

596596
*output_size = static_cast<size_t>(header_size + ret);
@@ -681,7 +681,7 @@ static bool CPLLZ4Decompressor(const void *input_data, size_t input_size,
681681
{
682682
if (bHeader)
683683
{
684-
int nSize = CPL_LSBSINT32PTR(input_data);
684+
int nSize = CPL_FROM_LSB<int32_t>(input_data);
685685
if (nSize < 0)
686686
{
687687
*output_size = 0;
@@ -704,7 +704,7 @@ static bool CPLLZ4Decompressor(const void *input_data, size_t input_size,
704704
{
705705
if (bHeader)
706706
{
707-
int nSize = CPL_LSBSINT32PTR(input_data);
707+
int nSize = CPL_FROM_LSB<int32_t>(input_data);
708708
if (nSize <= 0)
709709
{
710710
*output_size = 0;
@@ -962,58 +962,6 @@ static bool CPLZlibCompressor(const void *input_data, size_t input_size,
962962
return false;
963963
}
964964

965-
namespace
966-
{
967-
template <class T> inline T swap(T x)
968-
{
969-
return x;
970-
}
971-
972-
template <> inline uint16_t swap<uint16_t>(uint16_t x)
973-
{
974-
return CPL_SWAP16(x);
975-
}
976-
977-
template <> inline int16_t swap<int16_t>(int16_t x)
978-
{
979-
return CPL_SWAP16(x);
980-
}
981-
982-
template <> inline uint32_t swap<uint32_t>(uint32_t x)
983-
{
984-
return CPL_SWAP32(x);
985-
}
986-
987-
template <> inline int32_t swap<int32_t>(int32_t x)
988-
{
989-
return CPL_SWAP32(x);
990-
}
991-
992-
template <> inline uint64_t swap<uint64_t>(uint64_t x)
993-
{
994-
return CPL_SWAP64(x);
995-
}
996-
997-
template <> inline int64_t swap<int64_t>(int64_t x)
998-
{
999-
return CPL_SWAP64(x);
1000-
}
1001-
1002-
template <> inline float swap<float>(float x)
1003-
{
1004-
float ret = x;
1005-
CPL_SWAP32PTR(&ret);
1006-
return ret;
1007-
}
1008-
1009-
template <> inline double swap<double>(double x)
1010-
{
1011-
double ret = x;
1012-
CPL_SWAP64PTR(&ret);
1013-
return ret;
1014-
}
1015-
} // namespace
1016-
1017965
namespace
1018966
{
1019967
// Workaround -ftrapv
@@ -1068,7 +1016,8 @@ static bool DeltaCompressor(const void *input_data, size_t input_size,
10681016
{
10691017
if (bNeedSwap)
10701018
{
1071-
pDst[i] = swap(SubNoOverflow(swap(pSrc[i]), swap(pSrc[i - 1])));
1019+
pDst[i] = CPL_SWAP(
1020+
SubNoOverflow(CPL_SWAP(pSrc[i]), CPL_SWAP(pSrc[i - 1])));
10721021
}
10731022
else
10741023
{
@@ -1565,7 +1514,8 @@ static bool DeltaDecompressor(const void *input_data, size_t input_size,
15651514
{
15661515
if (bNeedSwap)
15671516
{
1568-
pDst[i] = swap(AddNoOverflow(swap(pDst[i - 1]), swap(pSrc[i])));
1517+
pDst[i] = CPL_SWAP(
1518+
AddNoOverflow(CPL_SWAP(pDst[i - 1]), CPL_SWAP(pSrc[i])));
15691519
}
15701520
else
15711521
{

port/cpl_minizip_zip.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <limits>
5252

5353
#include <cassert>
54+
#include <cinttypes>
5455
#include <cstddef>
5556
#include <cstdlib>
5657
#include <cstring>
@@ -1484,9 +1485,8 @@ extern int ZEXPORT cpl_zipCloseFileInZipRaw(zipFile file,
14841485

14851486
if (zi->sozip_index)
14861487
{
1487-
uint64_t nVal =
1488-
static_cast<uint64_t>(zi->ci.totalCompressedData);
1489-
CPL_LSBPTR64(&nVal);
1488+
uint64_t nVal = CPL_AS_LSB(
1489+
static_cast<uint64_t>(zi->ci.totalCompressedData));
14901490
memcpy(zi->sozip_index->data() + 24, &nVal, sizeof(uint64_t));
14911491
}
14921492
}
@@ -2192,7 +2192,7 @@ CPLErr CPLCreateFileInZip(void *hZip, const char *pszFilename,
21922192
abyExtra.push_back(GByte('K'));
21932193
abyExtra.push_back(GByte('V'));
21942194
const uint16_t nDataLengthLE =
2195-
CPL_LSBWORD16(static_cast<uint16_t>(nDataLength));
2195+
CPL_AS_LSB(static_cast<uint16_t>(nDataLength));
21962196
abyExtra.insert(
21972197
abyExtra.end(), reinterpret_cast<const GByte *>(&nDataLengthLE),
21982198
reinterpret_cast<const GByte *>(&nDataLengthLE) + 2);
@@ -2201,20 +2201,20 @@ CPLErr CPLCreateFileInZip(void *hZip, const char *pszFilename,
22012201
reinterpret_cast<const GByte *>("KeyValuePairs") +
22022202
strlen("KeyValuePairs"));
22032203
abyExtra.push_back(1); // number of key/value pairs
2204-
const uint16_t nKeyLen =
2205-
CPL_LSBWORD16(static_cast<uint16_t>(strlen("Content-Type")));
2204+
const uint16_t nKeyLenLSB =
2205+
CPL_AS_LSB(static_cast<uint16_t>(strlen("Content-Type")));
22062206
abyExtra.insert(abyExtra.end(),
2207-
reinterpret_cast<const GByte *>(&nKeyLen),
2208-
reinterpret_cast<const GByte *>(&nKeyLen) + 2);
2207+
reinterpret_cast<const GByte *>(&nKeyLenLSB),
2208+
reinterpret_cast<const GByte *>(&nKeyLenLSB) + 2);
22092209
abyExtra.insert(abyExtra.end(),
22102210
reinterpret_cast<const GByte *>("Content-Type"),
22112211
reinterpret_cast<const GByte *>("Content-Type") +
22122212
strlen("Content-Type"));
2213-
const uint16_t nValLen =
2214-
CPL_LSBWORD16(static_cast<uint16_t>(strlen(pszContentType)));
2213+
const uint16_t nValLenLSB =
2214+
CPL_AS_LSB(static_cast<uint16_t>(strlen(pszContentType)));
22152215
abyExtra.insert(abyExtra.end(),
2216-
reinterpret_cast<const GByte *>(&nValLen),
2217-
reinterpret_cast<const GByte *>(&nValLen) + 2);
2216+
reinterpret_cast<const GByte *>(&nValLenLSB),
2217+
reinterpret_cast<const GByte *>(&nValLenLSB) + 2);
22182218
abyExtra.insert(abyExtra.end(),
22192219
reinterpret_cast<const GByte *>(pszContentType),
22202220
reinterpret_cast<const GByte *>(
@@ -2378,7 +2378,7 @@ CPLErr CPLAddFileInZip(void *hZip, const char *pszArchiveFilename,
23782378
}
23792379

23802380
VSIFSeekL(fpInput, 0, SEEK_END);
2381-
const auto nUncompressedSize = VSIFTellL(fpInput);
2381+
const uint64_t nUncompressedSize = VSIFTellL(fpInput);
23822382
VSIFSeekL(fpInput, 0, SEEK_SET);
23832383

23842384
CPLStringList aosNewsOptions(papszOptions);
@@ -2428,8 +2428,8 @@ CPLErr CPLAddFileInZip(void *hZip, const char *pszArchiveFilename,
24282428

24292429
bSeekOptimized = true;
24302430

2431-
aosNewsOptions.SetNameValue(
2432-
"UNCOMPRESSED_SIZE", CPLSPrintf(CPL_FRMT_GUIB, nUncompressedSize));
2431+
aosNewsOptions.SetNameValue("UNCOMPRESSED_SIZE",
2432+
CPLSPrintf("%" PRIu64, nUncompressedSize));
24332433

24342434
zi->nOffsetSize = nOffsetSize;
24352435
nExpectedIndexSize =
@@ -2454,20 +2454,19 @@ CPLErr CPLAddFileInZip(void *hZip, const char *pszArchiveFilename,
24542454
sozip_index.resize(32);
24552455
uint32_t nVal32;
24562456
// Version
2457-
nVal32 = CPL_LSBWORD32(1);
2457+
nVal32 = CPL_AS_LSB<uint32_t>(1);
24582458
memcpy(sozip_index.data(), &nVal32, sizeof(nVal32));
24592459
// Extra reserved space after 32 bytes of header
2460-
nVal32 = CPL_LSBWORD32(0);
2460+
nVal32 = CPL_AS_LSB<uint32_t>(0);
24612461
memcpy(sozip_index.data() + 4, &nVal32, sizeof(nVal32));
24622462
// Chunksize
2463-
nVal32 = CPL_LSBWORD32(nChunkSize);
2463+
nVal32 = CPL_AS_LSB<uint32_t>(nChunkSize);
24642464
memcpy(sozip_index.data() + 8, &nVal32, sizeof(nVal32));
24652465
// SOZIPIndexEltSize
2466-
nVal32 = CPL_LSBWORD32(static_cast<uint32_t>(nOffsetSize));
2466+
nVal32 = CPL_AS_LSB(static_cast<uint32_t>(nOffsetSize));
24672467
memcpy(sozip_index.data() + 12, &nVal32, sizeof(nVal32));
24682468
// Uncompressed size
2469-
uint64_t nVal64 = nUncompressedSize;
2470-
CPL_LSBPTR64(&nVal64);
2469+
uint64_t nVal64 = CPL_AS_LSB(nUncompressedSize);
24712470
memcpy(sozip_index.data() + 16, &nVal64, sizeof(nVal64));
24722471
zi->sozip_index = &sozip_index;
24732472

0 commit comments

Comments
 (0)