1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * bitio_m_mem.h -- Macros for bitio to memory
|
---|
4 | * Copyright (C) 1994 Neil Sharman
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: bitio_m_mem.h 3745 2003-02-20 21:20:24Z mdewsnip $
|
---|
21 | *
|
---|
22 | **************************************************************************
|
---|
23 | *
|
---|
24 | * This file contains macros for doing bitwise input and output on an array
|
---|
25 | * of chars. These routines are faster than the ones in "mems" files. But
|
---|
26 | * with these routines you cannot mix reads and writes on the array of chars,
|
---|
27 | * or multiple write, at the same time and guarantee them to work, also you
|
---|
28 | * cannot seek to a point and do a write. The decode routine can detect when
|
---|
29 | * you run off the end of the array and will produce an approate error
|
---|
30 | * message, and the encode routine will stop when it gets to the end of the
|
---|
31 | * character array.
|
---|
32 | *
|
---|
33 | **************************************************************************/
|
---|
34 |
|
---|
35 | #ifndef H_BITIO_M_MEM
|
---|
36 | #define H_BITIO_M_MEM
|
---|
37 |
|
---|
38 | typedef struct mem_bitio_state
|
---|
39 | {
|
---|
40 | unsigned char *Base;
|
---|
41 | unsigned char *Pos;
|
---|
42 | int Remaining;
|
---|
43 | unsigned char Buff;
|
---|
44 | unsigned char Btg;
|
---|
45 | }
|
---|
46 | mem_bitio_state;
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 | #ifndef DECODE_ERROR
|
---|
52 | #define DECODE_ERROR (fprintf(stderr,"Unexpected EOF in \"%s\" on line %d\n",\
|
---|
53 | __FILE__, __LINE__), exit(1))
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | #define ENCODE_START(p,r) \
|
---|
59 | { \
|
---|
60 | register unsigned char *__pos = p; \
|
---|
61 | register unsigned char *__base = p; \
|
---|
62 | register int __remaining = r; \
|
---|
63 | register unsigned char __buff = 0; \
|
---|
64 | register unsigned char __btg = sizeof(__buff)*8;
|
---|
65 |
|
---|
66 | #define ENCODE_CONTINUE(b) \
|
---|
67 | { \
|
---|
68 | register unsigned char *__pos = (b).Pos; \
|
---|
69 | register unsigned char *__base = (b).Base; \
|
---|
70 | register int __remaining = (b).Remaining; \
|
---|
71 | register unsigned char __buff = (b).Buff; \
|
---|
72 | register unsigned char __btg = (b).Btg;
|
---|
73 |
|
---|
74 | #define ENCODE_BIT(b) \
|
---|
75 | do { \
|
---|
76 | __btg--; \
|
---|
77 | if (b) __buff |= (1 << __btg); \
|
---|
78 | if (!__btg) \
|
---|
79 | { \
|
---|
80 | if (__remaining) \
|
---|
81 | { \
|
---|
82 | *__pos++ = __buff; \
|
---|
83 | __remaining--; \
|
---|
84 | } \
|
---|
85 | __buff = 0; \
|
---|
86 | __btg = sizeof(__buff)*8; \
|
---|
87 | } \
|
---|
88 | } while(0)
|
---|
89 |
|
---|
90 | #define ENCODE_PAUSE(b) \
|
---|
91 | (b).Pos = __pos; \
|
---|
92 | (b).Base = __base; \
|
---|
93 | (b).Remaining = __remaining; \
|
---|
94 | (b).Buff = __buff; \
|
---|
95 | (b).Btg = __btg; \
|
---|
96 | }
|
---|
97 |
|
---|
98 | #define ENCODE_FLUSH \
|
---|
99 | do { \
|
---|
100 | if (__btg != sizeof(__buff)*8) \
|
---|
101 | if (__remaining) \
|
---|
102 | { \
|
---|
103 | *__pos++ = __buff; \
|
---|
104 | __remaining--; \
|
---|
105 | } \
|
---|
106 | __btg = sizeof(__buff)*8; \
|
---|
107 | } while (0)
|
---|
108 |
|
---|
109 | #define ENCODE_DONE \
|
---|
110 | ENCODE_FLUSH; \
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | #define DECODE_START(p,r) \
|
---|
115 | { \
|
---|
116 | register unsigned char *__pos = p; \
|
---|
117 | register unsigned char *__base = p; \
|
---|
118 | register int __remaining = r; \
|
---|
119 | register unsigned char __buff = 0; \
|
---|
120 | register unsigned char __btg = 0;
|
---|
121 |
|
---|
122 | #define DECODE_CONTINUE(b) \
|
---|
123 | { \
|
---|
124 | register unsigned char *__pos = (b).Pos; \
|
---|
125 | register unsigned char *__base = (b).Base; \
|
---|
126 | register int __remaining = (b).Remaining; \
|
---|
127 | register unsigned char __buff = (b).Buff; \
|
---|
128 | register unsigned char __btg = (b).Btg;
|
---|
129 |
|
---|
130 | #define DECODE_ADD_FF(b) \
|
---|
131 | do { \
|
---|
132 | if (!__btg) \
|
---|
133 | { \
|
---|
134 | if (__remaining) \
|
---|
135 | { \
|
---|
136 | __buff = *__pos++; \
|
---|
137 | __remaining--; \
|
---|
138 | } \
|
---|
139 | else \
|
---|
140 | __buff = 0xff; \
|
---|
141 | __btg = sizeof(__buff)*8; \
|
---|
142 | } \
|
---|
143 | (b) += (b) + ((__buff >> --__btg) & 1); \
|
---|
144 | } while(0)
|
---|
145 |
|
---|
146 | #define DECODE_ADD_00(b) \
|
---|
147 | do { \
|
---|
148 | if (!__btg) \
|
---|
149 | { \
|
---|
150 | if (__remaining) \
|
---|
151 | { \
|
---|
152 | __buff = *__pos++; \
|
---|
153 | __remaining--; \
|
---|
154 | } \
|
---|
155 | else \
|
---|
156 | __buff = 0x00; \
|
---|
157 | __btg = sizeof(__buff)*8; \
|
---|
158 | } \
|
---|
159 | (b) += (b) + ((__buff >> --__btg) & 1); \
|
---|
160 | } while(0)
|
---|
161 |
|
---|
162 | #define DECODE_BIT (__btg ? (__buff >> --__btg) & 1 : \
|
---|
163 | (!__remaining ? \
|
---|
164 | (DECODE_ERROR, 0) : \
|
---|
165 | (__buff = *__pos++, __remaining--, __btg = sizeof(__buff)*8, \
|
---|
166 | (__buff >> --__btg) & 1)))
|
---|
167 |
|
---|
168 | #define DECODE_DONE ; \
|
---|
169 | }
|
---|
170 |
|
---|
171 | #define DECODE_PAUSE(b) \
|
---|
172 | (b).Pos = __pos; \
|
---|
173 | (b).Base = __base; \
|
---|
174 | (b).Remaining = __remaining; \
|
---|
175 | (b).Buff = __buff; \
|
---|
176 | (b).Btg = __btg; \
|
---|
177 | }
|
---|
178 |
|
---|
179 | #define DECODE_SEEK(pos) \
|
---|
180 | do { \
|
---|
181 | register long __new_pos = (pos); \
|
---|
182 | __pos = __base + (__new_pos >> 3); \
|
---|
183 | __buff = *__pos++; \
|
---|
184 | __btg = 8 - (__new_pos&7); \
|
---|
185 | } \
|
---|
186 | while(0)
|
---|
187 |
|
---|
188 |
|
---|
189 |
|
---|
190 |
|
---|
191 | #endif
|
---|