Code restyling.

This commit is contained in:
2020-02-29 18:03:35 +00:00
parent 4ea327f0c6
commit f7e173710e
855 changed files with 43605 additions and 38045 deletions

View File

@@ -66,10 +66,10 @@ namespace Aaru.Compression
/* Huffman coding parameters */
const int N_CHAR = 256 - THRESHOLD + F;
const int N_CHAR = (256 - THRESHOLD) + F;
/* character code (= 0..N_CHAR-1) */
const int T = N_CHAR * 2 - 1; /* Size of table */
const int ROOT = T - 1; /* root position */
const int T = (N_CHAR * 2) - 1; /* Size of table */
const int ROOT = T - 1; /* root position */
const int MAX_FREQ = 0x8000;
/*
@@ -80,61 +80,59 @@ namespace Aaru.Compression
/* decoder table */
readonly byte[] d_code =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D, 0x0E,
0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F, 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11, 0x12, 0x12,
0x12, 0x12, 0x13, 0x13, 0x13, 0x13, 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, 0x16, 0x16, 0x16,
0x16, 0x17, 0x17, 0x17, 0x17, 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B, 0x1C, 0x1C, 0x1D, 0x1D,
0x1E, 0x1E, 0x1F, 0x1F, 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23, 0x24, 0x24, 0x25, 0x25, 0x26,
0x26, 0x27, 0x27, 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B, 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E,
0x2F, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,
0x3F
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
0x09, 0x09, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D, 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F, 0x10, 0x10,
0x10, 0x10, 0x11, 0x11, 0x11, 0x11, 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13, 0x14, 0x14, 0x14, 0x14,
0x15, 0x15, 0x15, 0x15, 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17, 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A,
0x1B, 0x1B, 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F, 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27, 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B, 0x2C, 0x2C,
0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
0x3C, 0x3D, 0x3E, 0x3F
};
readonly byte[] d_len =
{
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08
};
readonly ushort[] freq = new ushort[T + 1]; /* cumulative freq table */
readonly Stream inStream;
ushort[] freq = new ushort[T + 1]; /* cumulative freq table */
ushort getbuf;
byte getlen;
/*
* pointing parent nodes.
* area [T..(T + N_CHAR - 1)] are pointers for leaves
*/
short[] prnt = new short[T + N_CHAR];
readonly short[] prnt = new short[T + N_CHAR];
/* pointing children nodes (son[], son[] + 1)*/
short[] son = new short[T];
readonly short[] son = new short[T];
readonly byte[] text_buf = new byte[(N + F) - 1];
ushort getbuf;
byte getlen;
Tdlzhuf tdctl;
byte[] text_buf = new byte[N + F - 1];
public TeleDiskLzh(Stream dataStream)
{
@@ -145,7 +143,9 @@ namespace Aaru.Compression
tdctl.Ibufcnt = tdctl.Ibufndx = 0; // input buffer is empty
tdctl.Bufcnt = 0;
StartHuff();
for(i = 0; i < N - F; i++) text_buf[i] = 0x20;
for(i = 0; i < N - F; i++)
text_buf[i] = 0x20;
tdctl.R = N - F;
inStream = dataStream;
@@ -162,10 +162,12 @@ namespace Aaru.Compression
short c;
buf = new byte[len];
int count; // was an unsigned long, seems unnecessary
for(count = 0; count < len;)
if(tdctl.Bufcnt == 0)
{
if((c = DecodeChar()) < 0) return count; // fatal error
if((c = DecodeChar()) < 0)
return count; // fatal error
if(c < 256)
{
@@ -177,17 +179,20 @@ namespace Aaru.Compression
else
{
short pos;
if((pos = DecodePosition()) < 0) return count; // fatal error
if((pos = DecodePosition()) < 0)
return count; // fatal error
tdctl.Bufpos = (ushort)((tdctl.R - pos - 1) & (N - 1));
tdctl.Bufcnt = (ushort)(c - 255 + THRESHOLD);
tdctl.Bufcnt = (ushort)((c - 255) + THRESHOLD);
tdctl.Bufndx = 0;
}
}
else
{
// still chars from last string
while(tdctl.Bufndx < tdctl.Bufcnt && count < len)
while(tdctl.Bufndx < tdctl.Bufcnt &&
count < len)
{
c = text_buf[(tdctl.Bufpos + tdctl.Bufndx) & (N - 1)];
buf[count] = (byte)c;
@@ -198,7 +203,8 @@ namespace Aaru.Compression
}
// reset bufcnt after copy string from text_buf[]
if(tdctl.Bufndx >= tdctl.Bufcnt) tdctl.Bufndx = tdctl.Bufcnt = 0;
if(tdctl.Bufndx >= tdctl.Bufcnt)
tdctl.Bufndx = tdctl.Bufcnt = 0;
}
return count; // count == len, success
@@ -206,10 +212,12 @@ namespace Aaru.Compression
long DataRead(out byte[] buf, long size)
{
if(size > inStream.Length - inStream.Position) size = inStream.Length - inStream.Position;
if(size > inStream.Length - inStream.Position)
size = inStream.Length - inStream.Position;
buf = new byte[size];
inStream.Read(buf, 0, (int)size);
return size;
}
@@ -219,7 +227,9 @@ namespace Aaru.Compression
{
tdctl.Ibufndx = 0;
tdctl.Ibufcnt = (ushort)DataRead(out tdctl.Inbuf, BUFSZ);
if(tdctl.Ibufcnt <= 0) return -1;
if(tdctl.Ibufcnt <= 0)
return -1;
}
while(getlen <= 8)
@@ -234,22 +244,26 @@ namespace Aaru.Compression
int GetBit() /* get one bit */
{
if(NextWord() < 0) return -1;
if(NextWord() < 0)
return -1;
short i = (short)getbuf;
getbuf <<= 1;
getlen--;
return i < 0 ? 1 : 0;
}
int GetByte() /* get a byte */
{
if(NextWord() != 0) return -1;
if(NextWord() != 0)
return -1;
ushort i = getbuf;
getbuf <<= 8;
getlen -= 8;
i = (ushort)(i >> 8);
return i;
}
@@ -268,6 +282,7 @@ namespace Aaru.Compression
i = 0;
int j = N_CHAR;
while(j <= ROOT)
{
freq[j] = (ushort)(freq[i] + freq[i + 1]);
@@ -289,6 +304,7 @@ namespace Aaru.Compression
/* halven cumulative freq for leaf nodes */
short j = 0;
for(i = 0; i < T; i++)
if(son[i] >= T)
{
@@ -302,7 +318,8 @@ namespace Aaru.Compression
{
k = (short)(i + 1);
ushort f = freq[j] = (ushort)(freq[i] + freq[k]);
for(k = (short)(j - 1); f < freq[k]; k--) { }
for(k = (short)(j - 1); f < freq[k]; k--) {}
k++;
ushort l = (ushort)((j - k) * 2);
@@ -325,17 +342,22 @@ namespace Aaru.Compression
void Update(int c)
{
if(freq[ROOT] == MAX_FREQ) Reconst();
if(freq[ROOT] == MAX_FREQ)
Reconst();
c = prnt[c + T];
do
{
int k = ++freq[c];
/* swap nodes to keep the tree freq-ordered */
int l;
if(k <= freq[l = c + 1]) continue;
while(k > freq[++l]) { }
if(k <= freq[l = c + 1])
continue;
while(k > freq[++l]) {}
l--;
freq[c] = freq[l];
@@ -343,18 +365,22 @@ namespace Aaru.Compression
int i = son[c];
prnt[i] = (short)l;
if(i < T) prnt[i + 1] = (short)l;
if(i < T)
prnt[i + 1] = (short)l;
int j = son[l];
son[l] = (short)i;
prnt[j] = (short)c;
if(j < T) prnt[j + 1] = (short)c;
if(j < T)
prnt[j + 1] = (short)c;
son[c] = (short)j;
c = l;
}
while((c = prnt[c]) != 0); /* do it until reaching the root */
} while((c = prnt[c]) != 0); /* do it until reaching the root */
}
short DecodeChar()
@@ -369,7 +395,9 @@ namespace Aaru.Compression
while(c < T)
{
int ret;
if((ret = GetBit()) < 0) return -1;
if((ret = GetBit()) < 0)
return -1;
c += (ushort)ret;
c = (ushort)son[c];
@@ -377,6 +405,7 @@ namespace Aaru.Compression
c -= T;
Update(c);
return (short)c;
}
@@ -385,7 +414,8 @@ namespace Aaru.Compression
short bit;
/* decode upper 6 bits from given table */
if((bit = (short)GetByte()) < 0) return -1;
if((bit = (short)GetByte()) < 0)
return -1;
ushort i = (ushort)bit;
ushort c = (ushort)(d_code[i] << 6);
@@ -393,9 +423,11 @@ namespace Aaru.Compression
/* input lower 6 bits directly */
j -= 2;
while(j-- > 0)
{
if((bit = (short)GetBit()) < 0) return -1;
if((bit = (short)GetBit()) < 0)
return -1;
i = (ushort)((i << 1) + bit);
}
@@ -407,14 +439,10 @@ namespace Aaru.Compression
struct Tdlzhuf
{
public ushort R,
Bufcnt,
Bufndx,
Bufpos, // string buffer
public ushort R, Bufcnt, Bufndx, Bufpos, // string buffer
// the following to allow block reads from input in next_word()
Ibufcnt,
Ibufndx; // input buffer counters
public byte[] Inbuf; // input buffer
Ibufcnt, Ibufndx; // input buffer counters
public byte[] Inbuf; // input buffer
}
}
}