OSDN Git Service

Merge branch 'master' of git.sourceforge.jp:/gitroot/heavyosecpu/HeavyOSECPU
[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     //ビットを一つずつ取り出す
12     if (s->bitBufLen == 0) {
13                 s->bitBuf = s->p[0] | s->p[1] << 8;
14                 s->p += 2;
15                 s->bitBufLen = 16;
16         }
17         s->bitBufLen--;
18         return (s->bitBuf >> s->bitBufLen) & 1;
19     /*
20         if (s->bitBufLen == 0) {
21                 s->bitBuf = s->p[0] | s->p[1] << 8;
22                 s->p += 2;
23                 s->bitBufLen |= 16;
24         }
25         s->bitBufLen--;
26         return (s->bitBuf >> s->bitBufLen) & 1;
27      */
28 }
29
30 int ComLib_getTmpBit(struct ComLib_Str *s)
31 {
32     //次のビットをtmpの一番下のビットに押し込んで、その次のビットを返す
33         s->tmp = (s->tmp << 1 | ComLib_getBit(s)) & 0xffff;
34         return ComLib_getBit(s);
35 }
36
37 unsigned char *ComLib_main(const unsigned char *p, unsigned char *q)
38 {
39     //hh4デコーダー?
40         struct ComLib_Str s;
41         int i, dis = 0;
42         dis |= -1;
43         s.p = p;
44         s.bitBufLen &= 0;
45         goto l1;
46 l0:
47         *q++ = *s.p++;
48 l1:
49         i = ComLib_getBit(&s);
50         if (i != 0){
51         //4bit
52         goto l0;
53     }
54         s.tmp = 1;
55         do {
56                 i = ComLib_getTmpBit(&s);
57                 if (s.tmp == 0){
58             goto fin;
59         }
60         } while (i == 0);
61         if (s.tmp >= 3){
62                 dis = ~((s.tmp - 3) << 8 | *s.p++);
63     }
64         s.tmp &= 0;
65         i = ComLib_getTmpBit(&s);
66         s.tmp = s.tmp << 1 | i;
67         if (s.tmp == 0) {
68                 s.tmp |= 1;
69                 do {
70                         i = ComLib_getTmpBit(&s);
71                 } while (i == 0);
72                 s.tmp += 2;
73         }
74         s.tmp++;
75         if (dis < -0xd00){
76         s.tmp++;
77     }
78         for (i = 0; i < s.tmp; i++){
79                 q[i] = q[i + dis];
80     }
81         q += s.tmp;
82         goto l1;
83 fin:
84         return q;
85 }