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