Fix PNG images being limited to 255px
Some checks failed
.NET Build and Publish to Gitea / build (pull_request) Has been cancelled
Some checks failed
.NET Build and Publish to Gitea / build (pull_request) Has been cancelled
This commit is contained in:
@@ -20,7 +20,7 @@ public class PngHdr
|
|||||||
|
|
||||||
stream.ReadExactly(buffer[..8]);
|
stream.ReadExactly(buffer[..8]);
|
||||||
|
|
||||||
uint ihdrLength = BitConverter.ToUInt32(buffer);
|
uint ihdrLength = ReadBigEndianUInt32(buffer[..4]);
|
||||||
|
|
||||||
// The next four bytes will be the length of the IHDR section (it should be 13 bytes but we only need 8)
|
// The next four bytes will be the length of the IHDR section (it should be 13 bytes but we only need 8)
|
||||||
if (ihdrLength < 8)
|
if (ihdrLength < 8)
|
||||||
@@ -32,8 +32,8 @@ public class PngHdr
|
|||||||
|
|
||||||
stream.ReadExactly(buffer[..8]);
|
stream.ReadExactly(buffer[..8]);
|
||||||
|
|
||||||
uint width = BitConverter.ToUInt32(buffer);
|
uint width = ReadBigEndianUInt32(buffer[..4]);
|
||||||
uint height = BitConverter.ToUInt32(buffer[4..]);
|
uint height = ReadBigEndianUInt32(buffer[4..8]);
|
||||||
|
|
||||||
// Validate the width/height are non-negative and... that's all we care about!
|
// Validate the width/height are non-negative and... that's all we care about!
|
||||||
if (width > int.MaxValue || height > int.MaxValue)
|
if (width > int.MaxValue || height > int.MaxValue)
|
||||||
@@ -46,4 +46,12 @@ public class PngHdr
|
|||||||
return InvalidSize;
|
return InvalidSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
// Minimal helper for big-endian conversion
|
||||||
|
private static uint ReadBigEndianUInt32(ReadOnlySpan<byte> bytes)
|
||||||
|
{
|
||||||
|
return ((uint)bytes[0] << 24) |
|
||||||
|
((uint)bytes[1] << 16) |
|
||||||
|
((uint)bytes[2] << 8) |
|
||||||
|
bytes[3];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user