Skip to content

Commit

Permalink
* Fix crash when all a sparse codebook's entries are unused.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioctlLR committed Jun 16, 2016
1 parent 9091a9c commit 07b1040
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions NVorbis/VorbisCodebook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,50 +79,53 @@ void InitTree(DataPacket packet)
}
else
{
// mark the entry as unused
Lengths[i] = -1;
}
}
}
MaxBits = Lengths.Max();

int sortedCount = 0;
int[] codewordLengths = null;
if (sparse && total >= Entries >> 2)
// figure out the maximum bit size; if all are unused, don't do anything else
if ((MaxBits = Lengths.Max()) > -1)
{
codewordLengths = new int[Entries];
Array.Copy(Lengths, codewordLengths, Entries);
int sortedCount = 0;
int[] codewordLengths = null;
if (sparse && total >= Entries >> 2)
{
codewordLengths = new int[Entries];
Array.Copy(Lengths, codewordLengths, Entries);

sparse = false;
}
sparse = false;
}

// compute size of sorted tables
if (sparse)
{
sortedCount = total;
}
else
{
sortedCount = 0;
}
// compute size of sorted tables
if (sparse)
{
sortedCount = total;
}
else
{
sortedCount = 0;
}

int sortedEntries = sortedCount;
int sortedEntries = sortedCount;

int[] values = null;
int[] codewords = null;
if (!sparse)
{
codewords = new int[Entries];
}
else if (sortedEntries != 0)
{
codewordLengths = new int[sortedEntries];
codewords = new int[sortedEntries];
values = new int[sortedEntries];
}
int[] values = null;
int[] codewords = null;
if (!sparse)
{
codewords = new int[Entries];
}
else if (sortedEntries != 0)
{
codewordLengths = new int[sortedEntries];
codewords = new int[sortedEntries];
values = new int[sortedEntries];
}

if (!ComputeCodewords(sparse, sortedEntries, codewords, codewordLengths, len: Lengths, n: Entries, values: values)) throw new InvalidDataException();
if (!ComputeCodewords(sparse, sortedEntries, codewords, codewordLengths, len: Lengths, n: Entries, values: values)) throw new InvalidDataException();

PrefixList = Huffman.BuildPrefixedLinkedList(values ?? Enumerable.Range(0, codewords.Length).ToArray(), codewordLengths ?? Lengths, codewords, out PrefixBitLength, out PrefixOverflowTree);
PrefixList = Huffman.BuildPrefixedLinkedList(values ?? Enumerable.Range(0, codewords.Length).ToArray(), codewordLengths ?? Lengths, codewords, out PrefixBitLength, out PrefixOverflowTree);
}
}

bool ComputeCodewords(bool sparse, int sortedEntries, int[] codewords, int[] codewordLengths, int[] len, int n, int[] values)
Expand Down

0 comments on commit 07b1040

Please sign in to comment.