Skip to content

Commit 1e38243

Browse files
Revert unneeded bounds check introduced in #804
1 parent caacfc3 commit 1e38243

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanTable.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ internal unsafe struct HuffmanTable
5050
/// <param name="values">The huffman values</param>
5151
public HuffmanTable(MemoryAllocator memoryAllocator, ReadOnlySpan<byte> codeLengths, ReadOnlySpan<byte> values)
5252
{
53-
// We do some bounds checks in the code here to protect against AccessViolationExceptions
54-
const int HuffCodeLength = 257;
55-
const int MaxSizeLength = HuffCodeLength - 1;
56-
using (IMemoryOwner<short> huffcode = memoryAllocator.Allocate<short>(HuffCodeLength))
53+
const int Length = 257;
54+
using (IMemoryOwner<short> huffcode = memoryAllocator.Allocate<short>(Length))
5755
{
5856
ref short huffcodeRef = ref MemoryMarshal.GetReference(huffcode.GetSpan());
5957
ref byte codeLengthsRef = ref MemoryMarshal.GetReference(codeLengths);
@@ -65,7 +63,7 @@ public HuffmanTable(MemoryAllocator memoryAllocator, ReadOnlySpan<byte> codeLeng
6563
for (short i = 1; i < 17; i++)
6664
{
6765
byte length = Unsafe.Add(ref codeLengthsRef, i);
68-
for (short j = 0; j < length && x < MaxSizeLength; j++)
66+
for (short j = 0; j < length; j++)
6967
{
7068
Unsafe.Add(ref sizesRef, x++) = i;
7169
}
@@ -86,7 +84,7 @@ public HuffmanTable(MemoryAllocator memoryAllocator, ReadOnlySpan<byte> codeLeng
8684
Unsafe.Add(ref valOffsetRef, k) = (int)(si - code);
8785
if (Unsafe.Add(ref sizesRef, si) == k)
8886
{
89-
while (Unsafe.Add(ref sizesRef, si) == k && si < HuffCodeLength)
87+
while (Unsafe.Add(ref sizesRef, si) == k)
9088
{
9189
Unsafe.Add(ref huffcodeRef, si++) = (short)code++;
9290
}
@@ -103,19 +101,19 @@ public HuffmanTable(MemoryAllocator memoryAllocator, ReadOnlySpan<byte> codeLeng
103101
// Generate non-spec lookup tables to speed up decoding.
104102
const int FastBits = ScanDecoder.FastBits;
105103
ref byte lookaheadRef = ref this.Lookahead[0];
106-
const uint MaxFastLength = 1 << FastBits;
107-
Unsafe.InitBlockUnaligned(ref lookaheadRef, 0xFF, MaxFastLength); // Flag for non-accelerated
104+
Unsafe.InitBlockUnaligned(ref lookaheadRef, 0xFF, 1 << FastBits); // Flag for non-accelerated
108105

109106
for (int i = 0; i < si; i++)
110107
{
111108
int size = Unsafe.Add(ref sizesRef, i);
112109
if (size <= FastBits)
113110
{
114-
int huffCode = Unsafe.Add(ref huffcodeRef, i) << (FastBits - size);
115-
int max = 1 << (FastBits - size);
116-
for (int left = 0; left < max; left++)
111+
int fastOffset = FastBits - size;
112+
int fastCode = Unsafe.Add(ref huffcodeRef, i) << fastOffset;
113+
int fastMax = 1 << fastOffset;
114+
for (int left = 0; left < fastMax; left++)
117115
{
118-
Unsafe.Add(ref lookaheadRef, huffCode + left) = (byte)i;
116+
Unsafe.Add(ref lookaheadRef, fastCode + left) = (byte)i;
119117
}
120118
}
121119
}

0 commit comments

Comments
 (0)