1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * bitio_m_random.h -- Macros for bitio to a file (random access)
|
---|
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 | **************************************************************************
|
---|
21 | *
|
---|
22 | * This file contains macros for doing bitwise input and output on an array
|
---|
23 | * of chars. These routines are slower than the ones in "mem" files. but
|
---|
24 | * with these routines you can mix reads and writes, or multiple writes, on
|
---|
25 | * the array of chars at the same time and guarantee them to work, also you
|
---|
26 | * can seek to a point and do a write. The decode and encode macros cannot
|
---|
27 | * detect when the end off the character is reached and just continue
|
---|
28 | * processing.
|
---|
29 | *
|
---|
30 | *
|
---|
31 | **************************************************************************/
|
---|
32 |
|
---|
33 | #ifndef H_BITIO_M_RANDOM
|
---|
34 | #define H_BITIO_M_RANDOM
|
---|
35 |
|
---|
36 | typedef struct random_bitio_state
|
---|
37 | {
|
---|
38 | FILE *File;
|
---|
39 | unsigned char *Buf;
|
---|
40 | unsigned long Base;
|
---|
41 | unsigned long Used;
|
---|
42 | unsigned long pos;
|
---|
43 | unsigned long len;
|
---|
44 | unsigned long sft;
|
---|
45 | }
|
---|
46 | random_bitio_state;
|
---|
47 |
|
---|
48 |
|
---|
49 | #define ENCODE_START(f,l) \
|
---|
50 | { \
|
---|
51 | register FILE *__file = f; \
|
---|
52 | register unsigned char *__buf; \
|
---|
53 | register unsigned long __pos = 0; \
|
---|
54 | register unsigned long __base = 0; \
|
---|
55 | register unsigned long __is_used = 0; \
|
---|
56 | register unsigned long __len = (l)-1; \
|
---|
57 | register unsigned long __sft = 0; \
|
---|
58 | while (__len) { ++__sft; __len >>=1; } \
|
---|
59 | __len = 1<<__sft; \
|
---|
60 | __buf = Xmalloc(__len); \
|
---|
61 | fseek(__file, 0, 0); \
|
---|
62 | fread(__buf, 1, __len, __file);
|
---|
63 |
|
---|
64 | #define ENCODE_CONTINUE(b) \
|
---|
65 | { \
|
---|
66 | register FILE *__file = (b).File; \
|
---|
67 | register unsigned char *__buf = (b).Buf; \
|
---|
68 | register unsigned long __pos = (b).pos; \
|
---|
69 | register unsigned long __base = (b).Base; \
|
---|
70 | register unsigned long __is_used = (b).Used; \
|
---|
71 | register unsigned long __len = (b).len; \
|
---|
72 | register unsigned long __sft = (b).sft;
|
---|
73 |
|
---|
74 | #define SEEK fprintf(stderr, "Seek to %d\n",__base)
|
---|
75 | #define READ fprintf(stderr, "Read of %d\n",__len)
|
---|
76 | #define WRITE fprintf(stderr, "Write of %d\n",__is_used)
|
---|
77 |
|
---|
78 | #define WRITE_READ \
|
---|
79 | (__is_used ? (fseek(__file, __base, 0), \
|
---|
80 | fwrite(__buf, 1, __len, __file)) : 0, \
|
---|
81 | __base += __len, \
|
---|
82 | fseek(__file, __base, 0), \
|
---|
83 | fread(__buf, 1, __len, __file), \
|
---|
84 | __pos = 0, __is_used = 0)
|
---|
85 |
|
---|
86 | #define ENCODE_BIT(b) \
|
---|
87 | do { \
|
---|
88 | if (b) \
|
---|
89 | __buf[__pos>>3] |= 0x80 >> (__pos&7); \
|
---|
90 | else \
|
---|
91 | __buf[__pos>>3] &= 0xff7f >> (__pos&7); \
|
---|
92 | __is_used = 1; \
|
---|
93 | ++__pos; \
|
---|
94 | if ((__pos>>3) >= __len) \
|
---|
95 | (void)WRITE_READ; \
|
---|
96 | } while(0)
|
---|
97 |
|
---|
98 | #define ENCODE_PAUSE(b) \
|
---|
99 | (b).File = __file; \
|
---|
100 | (b).Buf = __buf; \
|
---|
101 | (b).pos = __pos; \
|
---|
102 | (b).Base = __base; \
|
---|
103 | (b).Used = __is_used; \
|
---|
104 | (b).len = __len; \
|
---|
105 | (b).sft = __sft; \
|
---|
106 | }
|
---|
107 |
|
---|
108 | #define ENCODE_FLUSH \
|
---|
109 | if (__is_used) \
|
---|
110 | { \
|
---|
111 | fseek(__file, __base, 0); \
|
---|
112 | fwrite(__buf, 1, __len, __file); \
|
---|
113 | __is_used = 0; \
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 |
|
---|
118 | #define ENCODE_DONE \
|
---|
119 | ENCODE_FLUSH; \
|
---|
120 | free(__buf); \
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | #define DECODE_START(f,l) ENCODE_START(f,l)
|
---|
125 |
|
---|
126 | #define DECODE_CONTINUE(b) ENCODE_CONTINUE(b)
|
---|
127 |
|
---|
128 | #define DECODE_BIT \
|
---|
129 | (__pos++, (((__pos-1)>>3) >= __len ? WRITE_READ, __pos=1 : 0), \
|
---|
130 | ((__buf[(__pos-1)>>3] & (0x80 >> ((__pos-1)&7))) != 0))
|
---|
131 |
|
---|
132 | #define DECODE_ADD_FF(b) ((b) += (b) + DECODE_BIT)
|
---|
133 |
|
---|
134 | #define DECODE_ADD_00(b) DECODE_ADD_FF(b)
|
---|
135 |
|
---|
136 | #define DECODE_DONE ENCODE_DONE
|
---|
137 |
|
---|
138 | #define DECODE_PAUSE(b) ENCODE_PAUSE(b)
|
---|
139 |
|
---|
140 | #define ENCODE_SEEK(pos) \
|
---|
141 | do { \
|
---|
142 | if ((((pos)>>3) >= __base) && ((((pos)+7)>>3) < (__base+__len))) \
|
---|
143 | __pos = (pos)-(__base << 3); \
|
---|
144 | else \
|
---|
145 | { \
|
---|
146 | ENCODE_FLUSH; \
|
---|
147 | __base = (pos) >> 3; \
|
---|
148 | __base = ((pos) >> (__sft+3)) << __sft; \
|
---|
149 | fseek(__file, __base, 0); \
|
---|
150 | fread(__buf, 1, __len, __file); \
|
---|
151 | __pos = (pos)&7; \
|
---|
152 | __pos = (pos)&((8 << __sft)-1); \
|
---|
153 | } \
|
---|
154 | } while(0)
|
---|
155 |
|
---|
156 |
|
---|
157 | #define DECODE_SEEK(pos) ENCODE_SEEK(pos)
|
---|
158 |
|
---|
159 | #define ENCODE_TELL ((__base << 3) + __pos)
|
---|
160 |
|
---|
161 | #define DECODE_TELL ENCODE_TELL
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 | #endif
|
---|