Skip to content

Commit 7d028ce

Browse files
committed
Guard against big endian
1 parent beb819d commit 7d028ce

7 files changed

Lines changed: 26 additions & 0 deletions

File tree

AssetRipper.TextureDecoder/Atc/AtcDecoder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ private static void DecodeAtcRgba8Block(ReadOnlySpan<byte> input, Span<uint> out
121121
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
122122
private static T ReadAtOffset<T>(this ReadOnlySpan<byte> input, int offset) where T : unmanaged
123123
{
124+
ThrowHelper.ThrowIfNotLittleEndian();
124125
return MemoryMarshal.Read<T>(input.Slice(offset));
125126
}
126127

AssetRipper.TextureDecoder/Dxt/DxtDecoder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static int DecompressDXT1<TOutputColor, TOutputChannelValue>(ReadOnlySpan
6464
where TOutputChannelValue : unmanaged
6565
where TOutputColor : unmanaged, IColor<TOutputChannelValue>
6666
{
67+
ThrowHelper.ThrowIfNotLittleEndian();
6768
return DecompressDXT1<TOutputColor, TOutputChannelValue>(input, width, height, MemoryMarshal.Cast<byte, TOutputColor>(output));
6869
}
6970

@@ -185,6 +186,7 @@ public static int DecompressDXT3<TOutputColor, TOutputChannelValue>(ReadOnlySpan
185186
where TOutputChannelValue : unmanaged
186187
where TOutputColor : unmanaged, IColor<TOutputChannelValue>
187188
{
189+
ThrowHelper.ThrowIfNotLittleEndian();
188190
return DecompressDXT3<TOutputColor, TOutputChannelValue>(input, width, height, MemoryMarshal.Cast<byte, TOutputColor>(output));
189191
}
190192

@@ -316,6 +318,7 @@ public static int DecompressDXT5<TOutputColor, TOutputChannelValue>(ReadOnlySpan
316318
where TOutputChannelValue : unmanaged
317319
where TOutputColor : unmanaged, IColor<TOutputChannelValue>
318320
{
321+
ThrowHelper.ThrowIfNotLittleEndian();
319322
return DecompressDXT5<TOutputColor, TOutputChannelValue>(input, width, height, MemoryMarshal.Cast<byte, TOutputColor>(output));
320323
}
321324

AssetRipper.TextureDecoder/Etc/EtcDecoder.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static int DecompressETC(ReadOnlySpan<byte> input, int width, int height,
1010

1111
public static int DecompressETC(ReadOnlySpan<byte> input, int width, int height, Span<byte> output)
1212
{
13+
ThrowHelper.ThrowIfNotLittleEndian();
1314
int requiredLength = ((width + 3) / 4) * ((height + 3) / 4) * 8;
1415
if (input.Length < requiredLength)
1516
{
@@ -50,6 +51,7 @@ public static int DecompressETC2(ReadOnlySpan<byte> input, int width, int height
5051

5152
public static int DecompressETC2(ReadOnlySpan<byte> input, int width, int height, Span<byte> output)
5253
{
54+
ThrowHelper.ThrowIfNotLittleEndian();
5355
int requiredLength = ((width + 3) / 4) * ((height + 3) / 4) * 8;
5456
if (input.Length < requiredLength)
5557
{
@@ -90,6 +92,7 @@ public static int DecompressETC2A1(ReadOnlySpan<byte> input, int width, int heig
9092

9193
public static int DecompressETC2A1(ReadOnlySpan<byte> input, int width, int height, Span<byte> output)
9294
{
95+
ThrowHelper.ThrowIfNotLittleEndian();
9396
int requiredLength = ((width + 3) / 4) * ((height + 3) / 4) * 8;
9497
if (input.Length < requiredLength)
9598
{
@@ -130,6 +133,7 @@ public static int DecompressETC2A8(ReadOnlySpan<byte> input, int width, int heig
130133

131134
public static int DecompressETC2A8(ReadOnlySpan<byte> input, int width, int height, Span<byte> output)
132135
{
136+
ThrowHelper.ThrowIfNotLittleEndian();
133137
int requiredLength = ((width + 3) / 4) * ((height + 3) / 4) * 16;
134138
if (input.Length < requiredLength)
135139
{
@@ -171,6 +175,7 @@ public static int DecompressEACRUnsigned(ReadOnlySpan<byte> input, int width, in
171175

172176
public static int DecompressEACRUnsigned(ReadOnlySpan<byte> input, int width, int height, Span<byte> output)
173177
{
178+
ThrowHelper.ThrowIfNotLittleEndian();
174179
int requiredLength = ((width + 3) / 4) * ((height + 3) / 4) * 8;
175180
if (input.Length < requiredLength)
176181
{
@@ -215,6 +220,7 @@ public static int DecompressEACRSigned(ReadOnlySpan<byte> input, int width, int
215220

216221
public static int DecompressEACRSigned(ReadOnlySpan<byte> input, int width, int height, Span<byte> output)
217222
{
223+
ThrowHelper.ThrowIfNotLittleEndian();
218224
int requiredLength = ((width + 3) / 4) * ((height + 3) / 4) * 8;
219225
if (input.Length < requiredLength)
220226
{
@@ -259,6 +265,7 @@ public static int DecompressEACRGUnsigned(ReadOnlySpan<byte> input, int width, i
259265

260266
public static int DecompressEACRGUnsigned(ReadOnlySpan<byte> input, int width, int height, Span<byte> output)
261267
{
268+
ThrowHelper.ThrowIfNotLittleEndian();
262269
int bcw = (width + 3) / 4;
263270
int bch = (height + 3) / 4;
264271

@@ -304,6 +311,7 @@ public static int DecompressEACRGSigned(ReadOnlySpan<byte> input, int width, int
304311

305312
public static int DecompressEACRGSigned(ReadOnlySpan<byte> input, int width, int height, Span<byte> output)
306313
{
314+
ThrowHelper.ThrowIfNotLittleEndian();
307315
int requiredLength = ((width + 3) / 4) * ((height + 3) / 4) * 16;
308316
if (input.Length < requiredLength)
309317
{
@@ -741,6 +749,7 @@ private static void DecodeEtc2PunchThrowBlock(ReadOnlySpan<byte> input, Span<uin
741749

742750
private static void DecodeEtc2a8Block(ReadOnlySpan<byte> input, Span<uint> output)
743751
{
752+
ThrowHelper.ThrowIfNotLittleEndian();
744753
int @base = input[0];
745754
int data1 = input[1];
746755
int mul = data1 >> 4;
@@ -761,6 +770,7 @@ private static void DecodeEtc2a8Block(ReadOnlySpan<byte> input, Span<uint> outpu
761770

762771
private static void DecodeEacUnsignedBlock(ReadOnlySpan<byte> input, Span<uint> output, int channel)
763772
{
773+
ThrowHelper.ThrowIfNotLittleEndian();
764774
int @base = input[0];
765775
int data1 = input[1];
766776
int mul = data1 >> 4;
@@ -778,6 +788,7 @@ private static void DecodeEacUnsignedBlock(ReadOnlySpan<byte> input, Span<uint>
778788

779789
private static void DecodeEacSignedBlock(ReadOnlySpan<byte> input, Span<uint> output, int channel)
780790
{
791+
ThrowHelper.ThrowIfNotLittleEndian();
781792
int @base = 127 + unchecked((sbyte)input[0]);
782793
int data1 = input[1];
783794
int mul = data1 >> 4;

AssetRipper.TextureDecoder/Pvrtc/PvrtcDecoder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static int DecompressPVRTC(ReadOnlySpan<byte> input, int xDim, int yDim,
3333
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
3434
public static int DecompressPVRTC(ReadOnlySpan<byte> input, int xDim, int yDim, bool do2bitMode, Span<byte> output)
3535
{
36+
ThrowHelper.ThrowIfNotLittleEndian();
3637
int xBlockSize = do2bitMode ? BlockX2bpp : BlockX4bpp;
3738
// for MBX don't allow the sizes to get too small
3839
int blockXDim = Math.Max(2, xDim / xBlockSize);

AssetRipper.TextureDecoder/Rgb/RgbConverter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ public static int Convert<TSourceColor, TSourceChannel, TDestinationColor, TDest
503503
where TDestinationChannel : unmanaged
504504
where TDestinationColor : unmanaged, IColor<TDestinationChannel>
505505
{
506+
ThrowHelper.ThrowIfNotLittleEndian();
506507
ReadOnlySpan<TSourceColor> sourceSpan = MemoryMarshal.Cast<byte, TSourceColor>(input).Slice(0, width * height);
507508
Span<TDestinationColor> destinationSpan = MemoryMarshal.Cast<byte, TDestinationColor>(output).Slice(0, width * height);
508509
Convert<TSourceColor, TSourceChannel, TDestinationColor, TDestinationChannel>(sourceSpan, destinationSpan);

AssetRipper.TextureDecoder/ThrowHelper.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,13 @@ internal static void ThrowIfNotEnoughSpace(int spaceAvailable, int spaceRequired
1919
throw new InvalidOperationException($"Not enough space. {spaceRequired} is required, but only {spaceAvailable} is available");
2020
}
2121
}
22+
23+
internal static void ThrowIfNotLittleEndian()
24+
{
25+
if (!BitConverter.IsLittleEndian)
26+
{
27+
throw new PlatformNotSupportedException();
28+
}
29+
}
2230
}
2331
}

AssetRipper.TextureDecoder/Yuy2/Yuy2Decoder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public static int DecompressYUY2<TOutputColor, TOutputChannelValue>(ReadOnlySpan
6363
where TOutputChannelValue : unmanaged
6464
where TOutputColor : unmanaged, IColor<TOutputChannelValue>
6565
{
66+
ThrowHelper.ThrowIfNotLittleEndian();
6667
return DecompressYUY2<TOutputColor, TOutputChannelValue>(input, width, height, MemoryMarshal.Cast<byte, TOutputColor>(output));
6768
}
6869

0 commit comments

Comments
 (0)