static unsigned short dicsiz1;
#define offset (0x100 - 2)
-void decode_start_pm2(void)
+void
+decode_start_pm2(void)
{
dicsiz1 = (1 << dicbit) - 1;
init_getbits();
hist_init();
nextcount = 0;
lastupdate = 0;
- getbits(1); /* discard bit */
+ getbits(1); /* discard bit */
}
static int repeatBits[6] = { 3, 3, 5, 6, 7, 0 };
static int repeatBase[6] = { 17, 25, 33, 65, 129, 256 };
-unsigned short decode_c_pm2(void)
+unsigned short
+decode_c_pm2(void)
{
/* various admin: */
- while (lastupdate != loc)
- {
+ while (lastupdate != loc) {
hist_update(dtext[lastupdate]);
lastupdate = (lastupdate + 1) & dicsiz1;
}
while (decode_count >= nextcount)
- /* Actually it will never loop, because count doesn't grow that fast.
- However, this is the way LHA does it.
- Probably other encoding methods can have repeats larger than 256 bytes.
- Note: LHA puts this code in decode_p...
- */
+ /* Actually it will never loop, because count doesn't grow that fast.
+ However, this is the way LHA does it.
+ Probably other encoding methods can have repeats larger than 256 bytes.
+ Note: LHA puts this code in decode_p...
+ */
{
- if (nextcount == 0x0000)
- {
+ if (nextcount == 0x0000) {
maketree1();
maketree2(5);
nextcount = 0x0400;
}
- else if (nextcount == 0x0400)
- {
+ else if (nextcount == 0x0400) {
maketree2(6);
nextcount = 0x0800;
}
- else if (nextcount == 0x0800)
- {
+ else if (nextcount == 0x0800) {
maketree2(7);
nextcount = 0x1000;
}
- else if (nextcount == 0x1000)
- {
- if (getbits(1) != 0) maketree1();
+ else if (nextcount == 0x1000) {
+ if (getbits(1) != 0)
+ maketree1();
maketree2(8);
nextcount = 0x2000;
}
- else
- { /* 0x2000, 0x3000, 0x4000, ... */
- if (getbits(1) != 0)
- {
+ else { /* 0x2000, 0x3000, 0x4000, ... */
+ if (getbits(1) != 0) {
maketree1();
maketree2(8);
}
nextcount += 0x1000;
}
}
- gettree1 = tree_get(&tree1); /* value preserved for decode_p */
-
+ gettree1 = tree_get(&tree1); /* value preserved for decode_p */
+
/* direct value (ret <= UCHAR_MAX) */
- if (gettree1 < 8) return hist_lookup(
- historyBase[gettree1] + getbits(historyBits[gettree1]) );
+ if (gettree1 < 8)
+ return hist_lookup(historyBase[gettree1] +
+ getbits(historyBits[gettree1]));
/* repeats: (ret > UCHAR_MAX) */
- if (gettree1 < 23) return offset + 2 + (gettree1 - 8);
+ if (gettree1 < 23)
+ return offset + 2 + (gettree1 - 8);
+
return offset + repeatBase[gettree1 - 23]
+ getbits(repeatBits[gettree1 - 23]);
}
-unsigned short decode_p_pm2(void)
+unsigned short
+decode_p_pm2(void)
{
/* gettree1 value preserved from decode_c */
int nbits, delta, gettree2;
- if (gettree1 == 8)
- { /* 2-byte repeat with offset 0..63 */
- nbits = 6; delta = 0;
+ if (gettree1 == 8) { /* 2-byte repeat with offset 0..63 */
+ nbits = 6;
+ delta = 0;
}
- else if (gettree1 < 28)
- { /* n-byte repeat with offset 0..8191 */
+ else if (gettree1 < 28) { /* n-byte repeat with offset 0..8191 */
gettree2 = tree_get(&tree2);
- if (gettree2 == 0)
- {
- nbits = 6; delta = 0;
- }
- else
- { /* 1..7 */
- nbits = 5 + gettree2; delta = 1 << nbits;
- }
+ if (gettree2 == 0) {
+ nbits = 6;
+ delta = 0;
+ }
+ else { /* 1..7 */
+ nbits = 5 + gettree2;
+ delta = 1 << nbits;
+ }
}
- else
- { /* 256 bytes repeat with offset 0 */
- nbits = 0; delta = 0;
+ else { /* 256 bytes repeat with offset 0 */
+ nbits = 0;
+ delta = 0;
}
+
return delta + getbits(nbits);
-
}
static unsigned char next[0x100];
static unsigned char lastbyte;
-void hist_init()
+void
+hist_init()
{
int i;
- for (i = 0; i < 0x100; i++)
- {
+ for (i = 0; i < 0x100; i++) {
prev[(0xFF + i) & 0xFF] = i;
- next[(0x01 + i) & 0xFF] = i;
+ next[(0x01 + i) & 0xFF] = i;
}
prev[0x7F] = 0x00; next[0x00] = 0x7F;
prev[0xDF] = 0x80; next[0x80] = 0xDF;
lastbyte = 0x20;
}
-unsigned char hist_lookup(int n)
+unsigned char
+hist_lookup(int n)
{
int i;
unsigned char *direction = prev;
- if (n >= 0x80)
- {
+ if (n >= 0x80) {
/* Speedup: If you have to process more than half the ring,
- it's faster to walk the other way around.
- */
+ it's faster to walk the other way around.
+ */
direction = next;
n = 0x100 - n;
}
- for (i = lastbyte; n != 0; n--) i = direction[i];
+ for (i = lastbyte; n != 0; n--)
+ i = direction[i];
+
return i;
-}
-
-void hist_update(unsigned char data)
+}
+
+void
+hist_update(unsigned char data)
{
unsigned char oldNext, oldPrev, newNext;
- if (data == lastbyte) return;
-
+
+ if (data == lastbyte)
+ return;
+
/* detach from old position */
oldNext = next[data];
oldPrev = prev[data];
prev[oldNext] = oldPrev;
next[oldPrev] = oldNext;
-
+
/* attach to new next */
newNext = next[lastbyte];
prev[newNext] = data;
next[data] = newNext;
-
+
/* attach to new prev */
prev[data] = lastbyte;
next[lastbyte] = data;
-
+
lastbyte = data;
}
-
static unsigned char tree1bound;
static unsigned char mindepth;
-void maketree1()
+void
+maketree1()
{
int i, nbits, x;
tree1bound = getbits(5);
mindepth = getbits(3);
- if (mindepth == 0)
- {
+ if (mindepth == 0) {
tree_setsingle(&tree1, tree1bound - 1);
}
- else
- {
- for (i = 0; i<32; i++) table1[i] = 0;
- nbits = getbits(3);
- for (i = 0; i < tree1bound; i++) {
+ else {
+ for (i = 0; i < 32; i++)
+ table1[i] = 0;
+ nbits = getbits(3);
+ for (i = 0; i < tree1bound; i++) {
x = getbits(nbits);
- table1[i] = ( x == 0 ? 0 : x - 1 + mindepth );
+ table1[i] = (x == 0 ? 0 : x - 1 + mindepth);
}
- tree_rebuild(&tree1, tree1bound, mindepth, table1);
+ tree_rebuild(&tree1, tree1bound, mindepth, table1);
}
}
-void maketree2(int par_b) /* in use: 5 <= par_b <= 8 */
+void
+maketree2(int par_b) /* in use: 5 <= par_b <= 8 */
{
int i, count, index;
- if (tree1bound < 10) return;
- if (tree1bound == 29 && mindepth == 0) return;
-
- for (i = 0; i < 8; i++) table2[i] = 0;
- for (i = 0; i < par_b; i++) table2[i] = getbits(3);
+
+ if (tree1bound < 10)
+ return;
+
+ if (tree1bound == 29 && mindepth == 0)
+ return;
+
+ for (i = 0; i < 8; i++)
+ table2[i] = 0;
+ for (i = 0; i < par_b; i++)
+ table2[i] = getbits(3);
+
index = 0;
count = 0;
- for (i = 0; i < 8; i++)
- {
- if (table2[i] != 0)
- {
- index = i;
- count++;
+ for (i = 0; i < 8; i++) {
+ if (table2[i] != 0) {
+ index = i;
+ count++;
}
}
-
- if (count == 1)
- {
+
+ if (count == 1) {
tree_setsingle(&tree2, index);
}
- else if (count > 1)
- {
- mindepth = 1;
- tree_rebuild(&tree2, 8, mindepth, table2);
+ else if (count > 1) {
+ mindepth = 1;
+ tree_rebuild(&tree2, 8, mindepth, table2);
}
// Note: count == 0 is possible!
// Excluding that possibility was a bug in version 1.
}
-int tree_get(struct tree *t)
+int
+tree_get(struct tree *t)
{
int i;
i = t->root;
- while (i < 0x80)
- {
- i = ( getbits(1) == 0 ? t->leftarr[i] : t->rightarr[i] );
+ while (i < 0x80) {
+ i = (getbits(1) == 0 ? t->leftarr[i] : t->rightarr[i]);
}
return i & 0x7F;
}
-void tree_setsingle(struct tree *t, unsigned char value)
+void
+tree_setsingle(struct tree *t, unsigned char value)
{
t->root = 128 | value;
}
-void tree_rebuild(t, bound, mindepth, table)
-struct tree *t;
-unsigned char bound;
-unsigned char mindepth;
-unsigned char *table;
+void
+tree_rebuild(struct tree *t,
+ unsigned char bound,
+ unsigned char mindepth,
+ unsigned char *table)
{
unsigned char *parentarr, d;
int i, curr, empty, n;
+
parentarr = (unsigned char *)malloc(bound * sizeof(unsigned char));
t->root = 0;
- for (i = 0; i < bound; i++)
- {
- t->leftarr[i] = 0;
- t->rightarr[i] = 0;
+ for (i = 0; i < bound; i++) {
+ t->leftarr[i] = 0;
+ t->rightarr[i] = 0;
parentarr[i] = 0;
}
-
- for (i = 0; i < mindepth - 1; i++)
- {
- t->leftarr[i] = i + 1;
- parentarr[i+1] = i;
+
+ for (i = 0; i < mindepth - 1; i++) {
+ t->leftarr[i] = i + 1;
+ parentarr[i + 1] = i;
}
curr = mindepth - 1;
empty = mindepth;
- for (d = mindepth; TRUE; d++)
- {
- for (i = 0; i < bound; i++)
- {
- if (table[i] == d)
- {
- if (t->leftarr[curr] == 0) t->leftarr[curr] = i | 128;
- else
- {
+ for (d = mindepth; TRUE; d++) {
+ for (i = 0; i < bound; i++) {
+ if (table[i] == d) {
+ if (t->leftarr[curr] == 0)
+ t->leftarr[curr] = i | 128;
+ else {
t->rightarr[curr] = i | 128;
n = 0;
- while (t->rightarr[curr] != 0)
- {
- if (curr == 0) /* root? -> done */
- {
- free(parentarr);
- return;
- }
- curr = parentarr[curr];
- n++;
+ while (t->rightarr[curr] != 0) {
+ if (curr == 0) { /* root? -> done */
+ free(parentarr);
+ return;
+ }
+ curr = parentarr[curr];
+ n++;
}
t->rightarr[curr] = empty;
- for (;;)
- {
- parentarr[empty] = curr;
- curr = empty;
- empty++;
-
- n--; if (n == 0) break;
- t->leftarr[curr] = empty;
+ for (;;) {
+ parentarr[empty] = curr;
+ curr = empty;
+ empty++;
+
+ n--;
+ if (n == 0)
+ break;
+ t->leftarr[curr] = empty;
}
}
}
}
- if (t->leftarr[curr] == 0) t->leftarr[curr] = empty; else t->rightarr[curr] = empty;
-
+ if (t->leftarr[curr] == 0)
+ t->leftarr[curr] = empty;
+ else
+ t->rightarr[curr] = empty;
+
parentarr[empty] = curr;
curr = empty;
empty++;
}
}
-
pm2tree.h -- tree for pmext2 decoding
***********************************************************/
-struct tree { unsigned char root, *leftarr, *rightarr; };
+struct tree {
+ unsigned char root, *leftarr, *rightarr;
+};
extern struct tree tree1, tree2;
int tree_get(struct tree *t);
void tree_setsingle(struct tree *t, unsigned char value);
void tree_rebuild(struct tree *t, unsigned char bound,
- unsigned char mindepth, unsigned char *table);
+ unsigned char mindepth, unsigned char *table);