1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <unistd.h> |
---|
5 | #include <PT_server.h> |
---|
6 | #include <PT_server_prototypes.h> |
---|
7 | #include "ptpan.h" |
---|
8 | #include "pt_prototypes.h" |
---|
9 | |
---|
10 | /* /// "WriteBits()" */ |
---|
11 | ULONG WriteBits(UBYTE *adr, ULONG bitpos, ULONG code, UWORD len) |
---|
12 | { |
---|
13 | UBYTE *badr = &adr[bitpos >> 3]; |
---|
14 | ULONG newbitpos = bitpos+len; |
---|
15 | UWORD bitfrac = 8 - (bitpos & 7); |
---|
16 | |
---|
17 | /* initial first byte */ |
---|
18 | if(bitfrac == 8) |
---|
19 | { |
---|
20 | *badr = 0; |
---|
21 | } |
---|
22 | if((len >= bitfrac) && (bitfrac < 8)) |
---|
23 | { |
---|
24 | *badr++ |= code >> (len - bitfrac); |
---|
25 | len -= bitfrac; |
---|
26 | bitpos += bitfrac; |
---|
27 | } else { |
---|
28 | if(len < 8) |
---|
29 | { |
---|
30 | *badr |= code << (bitfrac - len); |
---|
31 | return(newbitpos); |
---|
32 | } |
---|
33 | } |
---|
34 | /* whole bytes */ |
---|
35 | while(len > 7) |
---|
36 | { |
---|
37 | len -= 8; |
---|
38 | *badr++ = code >> len; |
---|
39 | } |
---|
40 | /* last fraction */ |
---|
41 | if(len) |
---|
42 | { |
---|
43 | *badr = code << (8 - len); |
---|
44 | } |
---|
45 | return(newbitpos); |
---|
46 | } |
---|
47 | /* \\\ */ |
---|
48 | |
---|
49 | #define ReadBit(adr, bitpos) ((adr[bitpos >> 3] >> (7 - (bitpos & 7))) & 1) |
---|
50 | |
---|
51 | /* /// "ReadBits()" */ |
---|
52 | ULONG ReadBits(UBYTE *adr, ULONG bitpos, UWORD len) |
---|
53 | { |
---|
54 | UBYTE *badr = &adr[bitpos >> 3]; |
---|
55 | UWORD bitfrac = bitpos & 7; |
---|
56 | ULONG res = 0; |
---|
57 | /*printf("BPos: %ld, Len %d, frac %d [%02x %02x %02x %02x %02x]\n", |
---|
58 | bitpos, len, bitfrac, |
---|
59 | badr[0], badr[1], badr[2], badr[3], badr[4]);*/ |
---|
60 | /* initial first byte */ |
---|
61 | if(len + bitfrac < 8) |
---|
62 | { |
---|
63 | return(((*badr << bitfrac) & 0xFF) >> (8 - len)); |
---|
64 | } |
---|
65 | res = *badr++ & (0xFF >> bitfrac); |
---|
66 | //printf("First: %lx ", res); |
---|
67 | len -= (8 - bitfrac); |
---|
68 | while(len > 7) |
---|
69 | { |
---|
70 | res <<= 8; |
---|
71 | res |= *badr++; |
---|
72 | len -= 8; |
---|
73 | } |
---|
74 | //printf("Middle: %lx ", res); |
---|
75 | /* last fraction */ |
---|
76 | if(len) |
---|
77 | { |
---|
78 | res <<= len; |
---|
79 | res |= *badr >> (8 - len); |
---|
80 | } |
---|
81 | //printf("Res: %08lx\n", res); |
---|
82 | return(res); |
---|
83 | } |
---|
84 | /* \\\ */ |
---|
85 | |
---|