OSDN Git Service

removed .gitxxx files from index
[heavyosecpu/HeavyOSECPU.git] / comlib.c
1 #include "osecpu.h"
2
3 struct ComLib_Str {
4         const unsigned char *p;
5         int bitBuf, bitBufLen;
6         int tmp;
7 };
8
9 int ComLib_getBit(struct ComLib_Str *s)
10 {
11         if (s->bitBufLen == 0) {
12                 s->bitBuf = s->p[0] | s->p[1] << 8;
13                 s->p += 2;
14                 s->bitBufLen |= 16;
15         }
16         s->bitBufLen--;
17         return (s->bitBuf >> s->bitBufLen) & 1;
18 }
19
20 int ComLib_getTmpBit(struct ComLib_Str *s)
21 {
22         s->tmp = (s->tmp << 1 | ComLib_getBit(s)) & 0xffff;
23         return ComLib_getBit(s);
24 }
25
26 unsigned char *ComLib_main(const unsigned char *p, unsigned char *q)
27 {
28         struct ComLib_Str s;
29         int i, dis;
30         dis |= -1;
31         s.p = p;
32         s.bitBufLen &= 0;
33         goto l1;
34 l0:
35         *q++ = *s.p++;
36 l1:
37         i = ComLib_getBit(&s);
38         if (i != 0) goto l0;
39         s.tmp = 1;
40         do {
41                 i = ComLib_getTmpBit(&s);
42                 if (s.tmp == 0) goto fin;
43         } while (i == 0);
44         if (s.tmp >= 3)
45                 dis = ~((s.tmp - 3) << 8 | *s.p++);
46         s.tmp &= 0;
47         i = ComLib_getTmpBit(&s);
48         s.tmp = s.tmp << 1 | i;
49         if (s.tmp == 0) {
50                 s.tmp |= 1;
51                 do {
52                         i = ComLib_getTmpBit(&s);
53                 } while (i == 0);
54                 s.tmp += 2;
55         }
56         s.tmp++;
57         if (dis < -0xd00) s.tmp++;
58         for (i = 0; i < s.tmp; i++)
59                 q[i] = q[i + dis];
60         q += s.tmp;
61         goto l1;
62 fin:
63         return q;
64 }