StyleCop clean Blast

This commit is contained in:
Matthias Mailänder
2013-08-04 08:32:08 +02:00
parent 6436ccd431
commit 47044fc696

View File

@@ -59,24 +59,24 @@ namespace OpenRA.FileFormats
// Decode PKWare Compression Library stream. // Decode PKWare Compression Library stream.
public static byte[] Decompress(byte[] src) public static byte[] Decompress(byte[] src)
{ {
BitReader br = new BitReader(src); var br = new BitReader(src);
// Are literals coded? // Are literals coded?
int coded = br.ReadBits(8); var coded = br.ReadBits(8);
if (coded < 0 || coded > 1) if (coded < 0 || coded > 1)
throw new NotImplementedException("Invalid datastream"); throw new NotImplementedException("Invalid datastream");
bool EncodedLiterals = (coded == 1); var EncodedLiterals = coded == 1;
// log2(dictionary size) - 6 // log2(dictionary size) - 6
int dict = br.ReadBits(8); var dict = br.ReadBits(8);
if (dict < 4 || dict > 6) if (dict < 4 || dict > 6)
throw new InvalidDataException("Invalid dictionary size"); throw new InvalidDataException("Invalid dictionary size");
// output state // output state
ushort next = 0; // index of next write location in out[] ushort next = 0; // index of next write location in out[]
bool first = true; // true to check distances (for first 4K) var first = true; // true to check distances (for first 4K)
byte[] outBuffer = new byte[MAXWIN]; // output buffer and sliding window var outBuffer = new byte[MAXWIN]; // output buffer and sliding window
var ms = new MemoryStream(); var ms = new MemoryStream();
// decode literals and length/distance pairs // decode literals and length/distance pairs
@@ -86,9 +86,11 @@ namespace OpenRA.FileFormats
if (br.ReadBits(1) == 1) if (br.ReadBits(1) == 1)
{ {
// Length // Length
int symbol = Decode(lencode, br); var symbol = Decode(lencode, br);
int len = lengthbase[symbol] + br.ReadBits(extra[symbol]); var len = lengthbase[symbol] + br.ReadBits(extra[symbol]);
if (len == 519) // Magic number for "done"
// Magic number for "done"
if (len == 519)
{ {
for (int i = 0; i < next; i++) for (int i = 0; i < next; i++)
ms.WriteByte(outBuffer[i]); ms.WriteByte(outBuffer[i]);
@@ -97,7 +99,7 @@ namespace OpenRA.FileFormats
// Distance // Distance
symbol = len == 2 ? 2 : dict; symbol = len == 2 ? 2 : dict;
int dist = Decode(distcode, br) << symbol; var dist = Decode(distcode, br) << symbol;
dist += br.ReadBits(symbol); dist += br.ReadBits(symbol);
dist++; dist++;
@@ -107,10 +109,10 @@ namespace OpenRA.FileFormats
// copy length bytes from distance bytes back // copy length bytes from distance bytes back
do do
{ {
int dest = next; var dest = next;
int source = dest - dist; var source = dest - dist;
int copy = MAXWIN; var copy = MAXWIN;
if (next < dist) if (next < dist)
{ {
source += copy; source += copy;
@@ -127,7 +129,7 @@ namespace OpenRA.FileFormats
// copy with old-fashioned memcpy semantics // copy with old-fashioned memcpy semantics
// in case of overlapping ranges. this is NOT // in case of overlapping ranges. this is NOT
// the same as Array.Copy() // the same as Array.Copy()
while( copy-- > 0 ) while (copy-- > 0)
outBuffer[dest++] = outBuffer[source++]; outBuffer[dest++] = outBuffer[source++];
// Flush window to outstream // Flush window to outstream
@@ -140,9 +142,10 @@ namespace OpenRA.FileFormats
} }
} while (len != 0); } while (len != 0);
} }
else // literal value // literal value
else
{ {
int symbol = EncodedLiterals ? Decode(litcode, br) : br.ReadBits(8); var symbol = EncodedLiterals ? Decode(litcode, br) : br.ReadBits(8);
outBuffer[next++] = (byte)symbol; outBuffer[next++] = (byte)symbol;
if (next == MAXWIN) if (next == MAXWIN)
{ {
@@ -160,9 +163,9 @@ namespace OpenRA.FileFormats
// Decode a code using huffman table h. // Decode a code using huffman table h.
static int Decode(Huffman h, BitReader br) static int Decode(Huffman h, BitReader br)
{ {
int code = 0; // len bits being decoded var code = 0; // len bits being decoded
int first = 0; // first code of length len var first = 0; // first code of length len
int index = 0; // index of first code of length len in symbol table var index = 0; // index of first code of length len in symbol table
short next = 1; short next = 1;
while (true) while (true)
{ {
@@ -193,8 +196,8 @@ namespace OpenRA.FileFormats
public int ReadBits(int count) public int ReadBits(int count)
{ {
int ret = 0; var ret = 0;
int filled = 0; var filled = 0;
while (filled < count) while (filled < count)
{ {
if (bitCount == 0) if (bitCount == 0)
@@ -208,6 +211,7 @@ namespace OpenRA.FileFormats
bitCount--; bitCount--;
filled++; filled++;
} }
return ret; return ret;
} }
} }
@@ -226,21 +230,22 @@ namespace OpenRA.FileFormats
public short[] Count; // number of symbols of each length public short[] Count; // number of symbols of each length
public short[] Symbol; // canonically ordered symbols public short[] Symbol; // canonically ordered symbols
public Huffman(byte[] rep, int n, short SymbolCount) public Huffman(byte[] rep, int n, short symbolCount)
{ {
short[] length = new short[256]; // code lengths var length = new short[256]; // code lengths
int s = 0; // current symbol var s = 0; // current symbol
// convert compact repeat counts into symbol bit length list // convert compact repeat counts into symbol bit length list
foreach (byte code in rep) foreach (byte code in rep)
{ {
int num = (code >> 4) + 1; // Number of codes (top four bits plus 1) var num = (code >> 4) + 1; // Number of codes (top four bits plus 1)
byte len = (byte)(code & 15); // Code length (low four bits) var len = (byte)(code & 15); // Code length (low four bits)
do do
{ {
length[s++] = len; length[s++] = len;
} while (--num > 0); } while (--num > 0);
} }
n = s; n = s;
// count number of codes of each length // count number of codes of each length
@@ -253,24 +258,22 @@ namespace OpenRA.FileFormats
return; return;
// check for an over-subscribed or incomplete set of lengths // check for an over-subscribed or incomplete set of lengths
int left = 1; // one possible code of zero length var left = 1; // one possible code of zero length
for (int len = 1; len <= Blast.MAXBITS; len++) for (int len = 1; len <= Blast.MAXBITS; len++)
{ {
left <<= 1; left <<= 1; // one more bit, double codes left
// one more bit, double codes left left -= Count[len]; // deduct count from possible codes
left -= Count[len];
// deduct count from possible codes
if (left < 0) if (left < 0)
throw new InvalidDataException ("over subscribed code set"); throw new InvalidDataException("over subscribed code set");
} }
// generate offsets into symbol table for each length for sorting // generate offsets into symbol table for each length for sorting
short[] offs = new short[Blast.MAXBITS + 1]; var offs = new short[Blast.MAXBITS + 1];
for (int len = 1; len < Blast.MAXBITS; len++) for (int len = 1; len < Blast.MAXBITS; len++)
offs[len + 1] = (short)(offs[len] + Count[len]); offs[len + 1] = (short)(offs[len] + Count[len]);
// put symbols in table sorted by length, by symbol order within each length // put symbols in table sorted by length, by symbol order within each length
Symbol = new short[SymbolCount]; Symbol = new short[symbolCount];
for (short i = 0; i < n; i++) for (short i = 0; i < n; i++)
if (length[i] != 0) if (length[i] != 0)
Symbol[offs[length[i]]++] = i; Symbol[offs[length[i]]++] = i;