1
1
// SPDX-License-Identifier: Apache-2.0
2
2
// ----------------------------------------------------------------------------
3
- // Copyright 2011-2023 Arm Limited
3
+ // Copyright 2011-2025 Arm Limited
4
4
//
5
5
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
6
// use this file except in compliance with the License. You may obtain a copy
@@ -1746,6 +1746,21 @@ struct dds_header_dx10
1746
1746
#define DDS_MAGIC 0x20534444
1747
1747
#define DX10_MAGIC 0x30315844
1748
1748
1749
+
1750
+ #if defined(ASTCENC_BIG_ENDIAN)
1751
+ /* *
1752
+ * @brief Reverse the bytes in a uint32_t value.
1753
+ */
1754
+ static uint32_t reverse_bytes_u32 (
1755
+ uint32_t val
1756
+ ) {
1757
+ return ((val >> 24 ) & 0x000000FF ) |
1758
+ ((val >> 8 ) & 0x0000FF00 ) |
1759
+ ((val << 8 ) & 0x00FF0000 ) |
1760
+ ((val << 24 ) & 0xFF000000 );
1761
+ }
1762
+ #endif
1763
+
1749
1764
/* *
1750
1765
* @brief Load an uncompressed DDS image using the local custom loader.
1751
1766
*
@@ -1769,23 +1784,52 @@ static astcenc_image* load_dds_uncompressed_image(
1769
1784
return nullptr ;
1770
1785
}
1771
1786
1772
- uint8_t magic[4 ];
1787
+ // Read and check the DDS magic number
1788
+ uint32_t magic;
1789
+ size_t magic_bytes_read = fread (&magic, 1 , sizeof (uint32_t ), f);
1790
+ if (magic_bytes_read != 4 )
1791
+ {
1792
+ printf (" Failed to read magic number from file %s\n " , filename);
1793
+ fclose (f);
1794
+ return nullptr ;
1795
+ }
1773
1796
1797
+ #if defined(ASTCENC_BIG_ENDIAN)
1798
+ magic = reverse_bytes_u32 (magic);
1799
+ #endif
1800
+
1801
+ if (magic != DDS_MAGIC)
1802
+ {
1803
+ printf (" File %s has incorrect magic number\n " , filename);
1804
+ fclose (f);
1805
+ return nullptr ;
1806
+ }
1807
+
1808
+ // Validate that we can read the DDS header
1774
1809
dds_header hdr;
1775
- size_t magic_bytes_read = fread (magic, 1 , 4 , f);
1776
1810
size_t header_bytes_read = fread (&hdr, 1 , sizeof (hdr), f);
1777
- if (magic_bytes_read != 4 || header_bytes_read != sizeof (hdr))
1811
+ if (header_bytes_read != sizeof (hdr))
1778
1812
{
1779
- printf (" Failed to read header of DDS file %s\n " , filename);
1813
+ printf (" Failed to read header from file %s\n " , filename);
1780
1814
fclose (f);
1781
1815
return nullptr ;
1782
1816
}
1783
1817
1784
- uint32_t magicx = magic[0 ] | (magic[1 ] << 8 ) | (magic[2 ] << 16 ) | (magic[3 ] << 24 );
1818
+ #if defined(ASTCENC_BIG_ENDIAN)
1819
+ // DDS header fields all 32-bit words
1820
+ uint32_t * words = reinterpret_cast <uint32_t *>(&hdr);
1821
+ size_t word_count = sizeof (hdr) / sizeof (uint32_t );
1822
+
1823
+ // Reverse all of them
1824
+ for (size_t i = 0 ; i < word_count; i++)
1825
+ {
1826
+ words[i] = reverse_bytes_u32 (words[i]);
1827
+ }
1828
+ #endif
1785
1829
1786
- if (magicx != DDS_MAGIC || hdr.size != 124 )
1830
+ if (hdr.size != 124 )
1787
1831
{
1788
- printf (" File %s does not have a valid DDS header\n " , filename);
1832
+ printf (" File %s has incorrect header\n " , filename);
1789
1833
fclose (f);
1790
1834
return nullptr ;
1791
1835
}
0 commit comments